Perl Tips

You have to start with the following line for Unix systems to work properly

#!/usr/bin/perl -w

Comments are done with a pound sign

# This is a single line comment
# The same comment symbol is used for multiple lines as well
# (e.g. If you want multiple lines commented out, use the # sign on each line)


Variables are declared loosely using a dollar sign $
All Perl statements are ended with a semicolon ; after the statement
Varaible values (esp. text strings) are quoted (like in HTML)

$TheDB = 'edata.txt';

To check the version of Perl that you have installed (or if it is even installed at all):
perl -v

If it is installed, you should get something similar to the following (taken from Ubuntu Feisty Fawn):

brian@ubuntulaptop:~/AuthorCode$ perl -v

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

Copyright 1987-2006, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

brian@ubuntulaptop:~/AuthorCode$


Perl Test Questions


Q1:
-Print (@array[0]); means that I want the first value in the new array (it starts counting at 0, not 1)
-Sort (@array); will sort the items in the array according to alphabetical order (e.g. (y, w, x) becomes (w,x,y)
-Unshift (@array, "z"); will unshift the order of the items in the array, and cause the additional value "Z" to be placed first in the list
-With the sort command still in place, the values then become (z,w,x,y)
-Without the sort command, the values are (z,y,w,x)

Q2:
What is function of if ($i%2)
(it's the % symbol that I hold in question)
It causes $i to increase from 1 to 16, then increase by 2 ?? until it gets to 24, since $i must be < 25

Q3:
Passing arguments to functions is done using the following syntax:
getpass($arg2)
(get pass is the function, $arg2 is the argument passed to it)

Q4:
-Arrays are declared using @array instead of $array
@ is for array
$ is for scalar values (e.g. 1 value only)
% is for a hash (what's a hash??)

-Sorting is done in alphabetical order, but doing Upper Case first, then lower case


 

Back to Class Tips

Back to Home