Apr 27 2009
Perl on your Mac part 2
By Jon Gales
If you missed my last lesson read
it before you read this one. If not you have a good change of being lost. If
you don’t feel 100% about last weeks lesson that is ok, you will after today.
The last lesson was a quick into to the basics of Perl, scalar variables and
the print function. We will be covering them both in more detail as well as
standard input and comments.
Scalar variables:
• Always
marked with a $ before ($scalar_variable)
• Can contain A-Z, a-z, 0-9, and underscores in the name and must
start with a letter.
• Integers and character strings can be assigned as the value
• Can be assigned different values in the same script such as:
$one = 1;
$one = 2; #$one now contains 2
• Are
case sensitive so $A and $a are two different scalars
• Can be used in conjunction with each other such as:
$one = 1;
$two = 2;
$answer = $one + $two;
The operators
for Scalar’s are
as follows:
| Add |
= |
+ |
| Subtract |
= |
- |
| Divide |
= |
/ |
| Multiply |
= |
* |
To increment
a scalar (has to be a number) add ++ or — after it like:
$value
= 6;
$value++; #the ++ increases the 6 to a 7
print $value; #will print 7
This is the
principal that web counters work on. When a visitor visits the page it triggers
a Perl script that reads a number from a file increases it by one prints it
to the web and then prints it to file for the next time a hit is occurred.
Print
function:
The
print function is probably one of the most
important things you will need to learn
in Perl. Thankfully it is simple. Perl doesn’t
actually print things (on your printer)
it outputs them on screen. To print something
just put the following:
"WHAT YOU WANT TO PRINT";You
can print scalars, strings, numbers, or
whole files (you will learn this at a later
date). Be careful to quote strings of text
(Perl will let you get away without quoting
scalar variables but not text strings).
Also don’t forget that ending semicolon.
Standard
Input:
We
briefly touched this in our last lesson
but not enough to thoroughly understand
it. The code for standard input looks like
this:$VARIABLE_NAME
= <STDIN>;
chomp($VARIABLE_NAME);That
chomp isn’t always needed but is a good
idea because sometimes input can get an
extra line in it and we don’t want that.
If there is nothing to chomp it does nothing.
This code picks up whatever was typed in
and dumps it into the $VARIABLE_NAME variable.
Comments:
Comments
are pieces of text that are solely meant for
human readers of the code. The computer automatically
ignores them. In Perl they are marked by a
pound sign #. Everything after a pound sign
is ignored. You have to comment every line
(there are no multi-line comments in Perl
like there are in C, PHP, and many other languages).
Commenting is a really really really really
(did I make my point?) good idea. Even if
you code looks good after your 5th Mountain
Dew at 3 a.m. I promise you it won’t look
good when your boss is looking over your shoulder
at 9 a.m. The first line of every Perl script
is a comment but this one isn’t ignored, it
tells the comp where to find Perl.
A
simple program using: Scalar Variables, Print
Function and Standard Input and comments is
below:
#!/usr/bin/perl
print "How old are you?
"; #prints
question
$age = <STDIN>; #puts response into
scalar
chomp($age);#get rid of that new line
$age_days = $age * 365; #times age by number
of days in a year
print "You are approximately $age_days
days old
"; #print answer
If
you are in X make the code a text file ending
in .pl and chmod it to 755. Then you can run
it by typing the path to file in the terminal.
Homework:
Your homework is to mess with the X
terminal. Practice writing stuff in pico and
chmoding it and moving around the directory.
Here are a few commands to get you started:
•
ls lists current directory
• cd changes directory (cd usr moves you
to the usr folder, cd / always moves you back
to the root directory).
If
you are on Classic you don’t have any homework
except to get OS X as soon as you can. It is
the future and kicks butt for Perl!
If you are having trouble or would like to ask me a question
please send me mail: jonknee@macmerc.com
or chat through AIM:
jonknee41 (add to buddy list)


























