####################################################### # # display-notes related to Intro to Perl, Class 1 # ####################################################### # last modified: 4-6-03 #------------------------------------------------------------- # what version of Perl are you running? # on a UNIX system, you should be able to find out by typing: perl --version # note that redwood is running 5.005 (!!), and sorrel is # running 5.8.0 --- we'll be using sorrel for this course! #--------------------------------------------------------- # under what kind of name should you save a Perl program? # under any name you wish; # in text's opinion --- including NO extension/suffix is preferable; # (BUT --- note that some non-UNIX versions require .plx as an extension/suffix) #---------------------------------------------------------- # how do you make a file executable? # varies --- but, in UNIX: # chmod 700 filename # IF you do not anyone else to read/write/execute it; # chmod 755 filename # IF you'd like others to be able to read and execute it, also; #------------------------------------------------------------ # what do you put IN this file? #-------------------------------------------------------------- # a TYPICAL Perl program should start with: #!/usr/bin/perl # ...on a UNIX system, where the perl executable is stored (or has a symbolic link to) /usr/bin/perl, as it OFTEN does; # (as it must on redwood and sorrel, for this DOES work in both!) # non-UNIX-note: text suggests that it is traditional/useful to use: #!perl ...in a Perl script on a non-UNIX system, if at all possible; this clues in a future maintenance programmer about what's IN there...! #------------------------------------------------------------------- # see hello1 #------------------------------------------------------------------- # by the way, Perl statements should end with semicolons.... #------------------------------------------------------------------- # a COMMENT # everything from # to the end of the line is ignored # (there are no multi-line comments in Perl...) #--------------------------------------------------------------------- # scalar - a single value #-------------------------------------------------------------------- # numbers # all are stored underneath as floating point numbers # (as C double values) # large integers: CAN use underscores to make 'em more readable! # 1_000_000 for 1 million # integers and floats: are just what you think they are... # see hello2 #------------------------------------------------------------------- # operations # the usual! # also % - remainder/modulus # also ** - exponentiation # also ++, -- # also +=, -=, everything = #-------------------------------------------------------------------- # strings # single-quoted string literals # pretty much wysiwyg... (what you see is what you get) # EXCEPT: type \' to get a single quote # type \\ to get a single backspace 'hello' 'Hello world!' 'Jane\'s Airplances' 'c:\\personal' # double-quoted string literals # now backslash has its usual power (\n is newline, \t is tab, # see p. 24 for more) # AND: when you put a variable inside, its VALUE appears int the string! # (more on that in a minute...) # string concatenation: the character . # "hello" . 'world' # see hello3 # string repetition: lowecase x "fred" x 3 # see hello3 #--------------------------------------------------------------- # Perl is not picky about many things.... # ...converts between numbers and strings as needed! "12" * 6 "12fred34" * " 3" evaluates to 36 "fred" * 3 #--------------------------------------------------------------- # warnings: perl -w name_of_program OR inside script: #!/usr/bin/perl -w #---------------------------------------------------------------- # scalar variables # a name starting with a $ (dollar sign) $fred =5; # see hello4 # remember: to see variable's VALUE inside a string: use DOUBLE quotes # to use the variable name itself in a string: use SINGLE quotes # but, yes, ++, --, +=, etc. do work with variables. # what if you want a variable's value to be "surrounded" by non-blank # characters?? print "I want $freds right now!\n"; # above will NOT work --- Perl thinks it is an undefined variable # $freds # put {} around the variable name to let Perl know THAT's the name: print "I want ${fred}s right now!\n"; #----------------------------------------------------------- # operator precedence in Perl - see p. 32 #----------------------------------------------------------- # comparison # for numbers: just what you think # note: == for equal, # != for not equal # for strings: NOT the above!!!!! # lt le eq ge gt ne $fred = "apple"; $ann = "pear"; $fred lt $ann # boolean operators: && (and) || (or) #---------------------------------------------------------- # boolean values # the following are all considered to be false undef 0 '' (the empty string) '0' # just about eveything else is considered to be true #----------------------------------------------------------- # if if (cond) { action1; } else { action2; } # there is an elsif that you can put in here... #------------------------------------------------------- # while while (cond) { action; } # for is there, too and DOES behave like you expect. #-------------------------------------------------------- # user input $fred = ; ...reads the next line of input into variable $fred ... NOTE: this NORMALLY includes a newline character!!! if you don't WANT it --- you chomp it off! chomp($fred); # the newline is chopped off, IF it exists. you COULD say: print "type something"; $fred = ; chomp($fred); print "type something"; chomp($fred= ); print $fred x 3;