CommentsThe pound sign '#' (musicians call it a 'sharp') is the comment character. All characters from a sharp to the end of the line will be ignored.To comment out whole sections of code surround it with two special lines:# this is a comment print "hello"; # another comment 13 28-29 24-29,161=comment the line above begins a multi-line comment and it goes on and on until it finds a line like the one below =cut
StatementsMultiple statements are separated by a semicolon ';' and are executed one after the other in sequence:15 29 79print "hello\n"; print "goodbye\n"; One can give multiple arguments to print with commas separating the arguments: Perl will simply print the values out one directly after the other with no space between the values. Note the extra space after 'hi' and 'there' above. Without the extra spaces the output would be 'hithereyes'.print "hi ", "there ", "yes"; 28 76 68 Perl is a free form language - use white space (blanks, tabs and newlines) as you see fit to make it look prettier and cleaner:
14 32 20
Remember that a lined up program is a happy program because your EYES can help your MIND understand. Double quoted strings are a form of string constants. A backslash inside such a string constant is treated specially. In addition to \n there is \t and \a. \t is tab - tabstops are usually set to 8 on most printers and terminal emulators. \a rings the bell - Alert. 22 32 62
VariablesSimple variable names are preceded by a dollar sign '$'. To set them use '=' for assignment:Variable names begin with a letter and can contain any number of letters, digits and underscores. They are case sensitive.$x = 4; $frac = 0.414; $name = "Charlie"; $y = $x; Variables are referenced in the same way - with a preceding dollar sign '$': Note that these names are not 'declared' and that one can assign an integer, floating point number or string to any variable name.print "hello ", $name, "\n"; 26-27 60-61 63-64
Numeric ConstantsThere are many ways to specify a number:
Do not worry about integer overflow; just do whatever arithmetic you
need to do and Perl will generally take care of all the details.
There is a way to do arbitrary precision arithmetic with the
use of a special Number::BigInt module.
18-20 38-40 61
Arithmetic OperatorsPerl has all of the standard operations:Parentheses are important in case it is not clear what the order of evaluation will be. With liberal use of parentheses you can tell Perl exactly what to compute first. For example:+ - * / % # modulo - remainder when divided by ** # exponentiation ( ) Is the answer 26 or 44? Is the multiplication done first or the addition? With parentheses you can make it clear to yourself and to others:4*5+6 20-21 46-48 654*(5+6) (4*5)+6 |