PrevIndexNext
Comments, Variables and Arithmetic
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:
=begin
the line above
begins a multi-line comment
and it
goes on
and on until
it finds a line
like the one below
=cut
Both =begin and =cut should have blank lines above and below.
Statements
Multiple statements are separated by a semicolon ';'
and are executed one after the other in sequence:
print "hello\n";
print "goodbye\n";
The \n above is for a newline.
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'.
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";
Remember that a lined up program is a happy program
because your EYES can help your MIND understand.
One time I complimented a student on how carefully she
was aligning her program. She said, "I'm just trying to not confuse myself!"
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.
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 'pre-declared'
and that one can assign an integer, floating point number
or string to any variable name.
Numeric Constants
There are many ways to specify a number:
1 3 143 # decimal
073 013 # octal
0x4E 0xFF 0x5C463f # hex
0b10111 # binary! = 23 decimal
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.
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)
Exercises
-
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.
-
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;
=begin
We now output
all of the
fascinating
results.
=cut
print "Hello ", $the_name, "\n";
print "You have ", $val, " units\n",
"Yes!\a\n";
PrevIndexNext