Back  Index  Next

Comments

The 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.

# this is a comment
print "hello";    # another comment
To comment out whole sections of code surround it with two special lines:

=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
13  28-29  24-29,161

Statements

Multiple statements are separated by a semicolon ';' and are executed one after the other in sequence:

print "hello\n";
print "goodbye\n";
15  29  79

One can give multiple arguments to print with commas separating the arguments:


print "hi ", "there ", "yes";
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'.

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:


print "hi ",
      "there ",
      "yes";
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

Variables

Simple variable names are preceded by a dollar sign '$'. To set them use '=' for assignment:

$x = 4;
$frac = 0.414;
$name = "Charlie";
$y = $x;
Variable names begin with a letter and can contain any number of letters, digits and underscores. They are case sensitive.

Variables are referenced in the same way - with a preceding dollar sign '$':


print "hello ", $name, "\n";
Note that these names are not 'declared' and that one can assign an integer, floating point number or string to any variable name.

26-27  60-61  63-64

Numeric Constants

There are many ways to specify a number:

1   3   143         # decimal
073  013            # octal
0x4E  0xFF 0x5C463f # hex
4.512               # floating point
5.62E-21            # scientific notation
123_456_198.123     # _ for a comma like syntax
                    # _ is basically ignored within
                    # numeric constants...
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 Operators

Perl has all of the standard operations:

+
-
*
/
%           # modulo - remainder when divided by
**          # exponentiation
( )
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:

4*5+6
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)
(4*5)+6
20-21  46-48  65

Exercises

  1. Set a variable to a number.

    Compute the square and cube of that number and print them out.

    Take the square, consider it as a temperature in Fahrenheit and convert it to Centigrade. Print the value. The conversion formula is:

    
    
    
    Change the first number and rerun the program.

    Document your program with comments.

    Comment out the portion that does the temperature conversion.

    If you haven't done so already, make some syntax errors (missing ; or unbalanced (), misspell 'print'), look at them carefully, fix the errors, and rerun.

  2. Analyze the following Perl program and identify the various parts.
    Put everything into one of these categories:

    • comments
    • variable names
    • string constants
    • numeric constants
    • operators
    • delimiters
    • white space
    • built-in functions

    Every character should be accounted for.

    
    $i = 4;
    $the_name = "Joe";      # that's him alright...
    $count = 0x4fe;
    $val = ($i**4)/$count + 4_500;
    
    =comment
    We now output
    all of the 
    fascinating
    results.
    =cut
    
    print "Hello ", $the_name, "\n";
    print "You have ", $val, " units\n",
          "Yes!\a\n";
    

Back  Index  Next