Personal Miscellaneous TCP/IP GRID Quality of Service  
VAIOHTMLLDAPC/C++LaTEX  

Basics


Variables in Perl

There are three types of variables in Perl: scalar, array and hash.

scalar

This is any straight string of characters or a number.
This is the most common type of variable. As in shell scripting, you start a variable with a "$".

However, note: there is a difference from most shell scripting, because the variable name always starts with a "$". This is whether you are assigning the variable or getting it's value!

e.g. $name="Bob"; $number=1024; print $name;


array

...also called a list
This is a list of items. The actual list values look like this:

@array = ("Item","Another","Last One");

@array = (1,2,4,8,16,32);

@array = ($var1,$var2);

This is unusual, but you will often do it in reverse, too:

($var1,$var2) = @array;
(The last line splits up the array, and puts item 1 in $var1, and item 2 in $var2.)

An array always starts with "@", unless you want a specific item from the array. Then you use "$array[2]".


hash

...also called an associative array
This "associates" pairs of scalars together. So an example of a hash is

%hash

$hash{ 'Item' }

Associate arrays, or hashes, are more complicated, so we won't worry about them too much yet.

 


Special Variables in Perl

Here are some of the more important special variables. Some of these will be very familiar to people who know shell scripting. Remember that

·         a scalar (number or string) starts with a "$"
·         an array (list) starts with an "@"
·         a hash (associative array) starts with a %

Make sure you read the special note about %ENV following this list!

Variable Long version Description
$_ $ARG the default string varibale -- represents the result of the last string action
$1-$9 (none) subpatterns from a previous string match
%ENV (none) The hash that represents the current set of environment variables.
$! $ERRNO the last error number returned
$$ $PROCESS_ID the process id of the current process

Here are some that are interesting, but not as useful.

Variable Long version Description
$] $PERL_VERSION the current version of Perl
$0 $PROGRAM_NAME the program name
$. $INPUT_LINE_NUMBER the line number of the current input line. "Reset on close()"
$, $OUTPUT_FIELD_SEPERATOR Output field seperator; this is printed in between fields in a "print $var,$var"
$\ $OUTPUT_RECORD_SEPERATOR Output record seperator; this is printed in-between records -- usually end-of-line (new-line)

The long names are new to Perl 5, and can be used if you include the statement "use English;" in your script. This statement reads in the module English.pm, which contains the long names for these system bariables.

Note that the ENV hash is particularly important. You use this system variable to access the environment variables of the shell you're in.

Since %ENV is a hash (or associative array), you need to pull out the individual items like this:

print $ENV{'HOME'};
The above will print the value of the HOME variable in the environment.

 


Example

This is a sample Perl script:

    
    #!/usr/local/bin/perl -w
    
    # this script reads in a text file that consists of
    # lines labeled "HEADER:" and "ITEM:". The Perl script 
    # prints the "HEADER" information into one format, and the
    # "ITEM" information into another format.
    
    $N = 1;
    
    print "--------------\n";
    
    while (<>) {
        ($field1,$field2) = split(/[:\n]/, $_, 9999);
        if (/^HEADER/) {
            print $N . '.  ' . $field2 . "\n";
            $N = $N + 1;
        }
        if (/^ITEM/) {
            print "\t" . $field2 . "\n";
        }
    }
    
    print "--------------\n";
    

There are a few key lines to note:

#!/usr/local/bin/perl -w
This line informs the system that the script is a Perl script. The "-w" flag produces extra output that will help you diagnose problems with your script. I always include the -w flag, but it's not necessary.

# this script reads in a text file that consists of
A "#" (pound sign) marks the rest of the line as a comment, just like in most shell scripts. The system ignores everything after the "#".

while (<>) {
This line starts a while loop. The "<>" is a short version of the command "", which means read a line from the file "FILE". The "while" loop causes it to repeat until there are no lines left. Since "<>" is empty (no FILE), then we will just read from Standard Input, which is usually just the keyboard.

($field1,$field2) = split(/[:\n]/, $_, 9999);
This splits the input line into seperate fields, seperated by ":". We'll go over the "split" command later, because it is very powerful. Note that $field1 is a scalar variable, and ($field1,$field2) is a list. More on variable types below.

if (/^HEADER/) {
The if statement is pretty normal, but the /^Header/ is new. The forward slashes mean to do a match to see if the Regular Expression between the slashes is in the current line. If so, then we do the code that follows.
The expression we're using here is "^HEADER", which means a start of line ("^"), followed by the letters "HEADER". So this will match any line that starts with "HEADER". Regular Expressions are a complicated topic, but very useful once you understand them.

print $N . '. ' $field1 . "\n";
This will print the variable $N, then a period, then the variable $field1, then a new-line.
Note that the period joins the strings together; in order to print a period, you have to put it in quotes. In the command above, only a single period will be printed; the rest mean to join the strings.

Note that the line ends with a ";"! This is one of the major differences between Perl and shell-scripting. Every command needs a ";" to finish!

(There is also a "printf" you can use instead of "print".


Assigning to Variables
You can add $x and $y with a line like this:

$z = $x + $y;

That line really performs the math, and stores the result in $z. So a program like this

    
    #!/usr/bin/perl
    $x = 1; $y = 2;
    $z = $x + $y;
    print "$x + $y = $z\n";

will once again print out

1 + 2 = 3


Getting Input From the User
Variables give your program flexibility. You can now change that program so it adds any two numbers you type in. The way to get input from the keyboard into a running program is with a line like this:

$x = <>;

When your program encounters a line like that, it will stop and wait for numbers (or letters, or anything else) to be typed in at the keyboard, until RETURN is hit. Then it will assign everything you just typed into $x, and the program can then play with it. So this program

    			  
    #!/usr/bin/perl
    print "Type in a number: "; $x = <>; chop($x);
    print "Type in a number: "; $y = <>; chop($y);
    $z = $x + $y; print "$x + $y = $z\n";

will wait for two numbers, add them up, and then print the result.

The one strange thing in that program is the chop command. When the value of $x comes in from the keyboard, it's stored along with the \n character from hitting RETURN. So if you typed 35 and then hit RETURN, the value of $x became

35\n

Chop just chops the last character off of a variable. Its most common use is for getting rid of that nasty "\n"--which, believe it or not, counts as one character. (Even more to the point: it counts as a charcter, which means "35\n" is not anything like "35". You can't add with it, you can't print it without getting an unwanted line break, and it won't do anything else you might expect. Chopping is good Perl practice.)

Sun, 30 September, 2001 19:09 Previous PageNext Page
 
 
    email me!
© 2001-2003, Yee-Ting Li, email: ytl@hep.ucl.ac.uk, Tel: +44 (0) 20 7679 1376, Fax: +44 (0) 20 7679 7145
Room D14, High Energy Particle Physics, Dept. of Physics & Astronomy, UCL, Gower St, London, WC1E 6BT