perl Basics
Document Sample


perl Basics
sh-bang !!!!
• Every perl program starts with a sh-bang line
must be first line in file
#!/usr/bin/perl
# hello.pl
all three lines
printf “Hello, world!\n”; do the same thing
printf STDOUT “Hello, world!\n”;
print “Hello, world!\n”;
# - shell
! – bang
NOTE: Don't forget to
chmod +x hello.pl
new line
executing
perl hello.pl
without sh-bang line is the
same as
./hello.pl Question: printf is a function;
with the sh-bang line what is strange about its appearance?
Variables:
• perl has three kinds of variables:
– scalar ($-prefix) -- numerics, strings, addresses, primitives
– list/array (@-prefix) -- lists, arrays, stacks, queues
– hashtable (%-prefix) -- key/value pairs
• Typical symbol table:
Variable Name Address
X 0x012345
• perl symbol table: typeglob
Variable Name VariableType Address
$ 0x012345
X @ not defined
% 0x098765
Scalars and Lists:
• The following is possible:
$X = “apple”;
@X = ('apple', 1, 'orange', 2, 'pear', 3);
• In other words, two variables with the same name
Scalar Variables:
• numeric, string, boolean, address are all scalars
$inum = 2; # numeric
$fnum = 2.0; # numeric
$fname = “Andrew”; # string double quotes expands
$lname = “Pletch; # string variables
$name1 = “$fname $lname”;
# value of $name1 is “Andrew Pletch”
$name2 = „$fname $lname‟; single quotes leaves
# value of $name2 is “$fname $lname” variables unexpanded.
if ($inum == $fnum) { … } # numeric comparison (true)
if ($name1 eq $name2) { … } # string comparison
$snum = “2”; # string
if ($inum == $snum) { … } # a string whose value is numeric
# can be treated like a number
eq == #!/usr/bin/perl
$X = 2; $Y = 2.0; $Z = "2"; $W = “2.0”;
if ( $X == $Y ) { printf "int == float\n"; }
if ( $X eq $Y ) { printf "int $X eq float $Y\n"; }
if ( $X == $Z ) { printf "int == string\n"; }
if ( $X eq $Z ) { printf "int eq string\n";}
if ( $Z == $Y ) { printf "string $Z == float $Y\n"; }
if ( $Z eq $Y ) { printf "string eq float\n"; }
if ( $X == $W ) { printf "int == string\n";}
if ( $Z == $W ) { printf “string == string\n";}
if ( $Z ne $W ) { printf “string ne string\n";}
# output
int == float
int 2 eq float 2
int == string
int eq string
string 2 == float 2
string eq float
int == string
string == string
string ne string
List Variables:
• Lists are lists of scalars.
@L = ('apple', 1, 'orange', 2, 'pear', 3);
• Notice that the things in a list don't have to be the same
sort of thing as in other programming languages -why not?
• List elements are accessed using [ ].
$L[0] is a fruit; $L[1] is a number.
We use $ because 'apple' is a scalar
• If $X = “apple” then $X[2] can not be 'p'; why not?
Because there might also be a @X and so $X[2] must
be the third element in this list; use substr($X,2,1) instead.
Exercise:
• The perl substr() function is a whole study in itself; take a
look at
http://perlmeme.org/howtos/perlfunc/substr.html
List Variables 2:
• Suppose we execute
> myprog.pl -t -a 3 –f myfile
• then inside myprog.pl @ARGV is a predefined variable
whose value is use ( ) to declare or express a list
( “-t”, “-a”, 3, “-f”, “myfile”)
• To access the elements of @ARGV we use indexes
$FName = $ARGV[4]; # indexing starts at 0
use $ because “myfile” is a scalar
List Variables 3:
• A list of lists is just a list:
@L1 = ( ( 1, 2, 3), (4, 5, 6), (7, 8, 9) ); # looks like a 3x3 matrix
# value of @L1 is ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ); # but it isn‟t
• A list used in a scalar context returns the list length
string concatenation operator
printf @L1 . “\n”; # scalar context
printf “@L1 \n”; # list context
output:
9
123456789
hash variables:
• %ENV is a predefined hash variable; it contains a copy
of all the environment variables and their values
inherited by any running perl script
function that returns a list
of all keys to the hash
for-loop syntax #!/usr/bin/perl
for lists
# prints out all inherited environment variables
# and their values ( ) part of for-loop syntax
and not part of list definition;
required
foreach $key (keys %ENV) {
printf "$key ==> $ENV{$key} \n";
}
[ ] for lists; { } for hashes
Perl hash HowTo:
http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
#!/usr/bin/perl
#hash_test.pl
hashes and lists:
%myHash; Prints out the
$myHash{A} = 1; contents all
$myHash{B} = 2; mashed together
$myHash{C} = 3;
$myHash{D} = 4;
using hash in
scalar context
print %myHash;
print %myHash . "\n"; printing a hash
does nothing but
print "%myHash \n"; treat it like a string
@list = %myHash;
assign hash to list
printf @list . "\n";
printf "@list \n";
printf "ENV == %ENV \n";
# output
3 == number of used buckets
A1B2C3D4
3/8 %E treated like 0.0E
8 == number of allocated buckets
%myHash
8
A1D 4C 3B 2
ENV == 0.000000E+00NV
perl symbol tables:
• hash consisting of (key, value) pairs
• key == identifier
• value == typeglob data structure
• typeglob contains reference to all three variables
$x; @x; %x; # three variables with same name
# %main is the default package symbol table called main
# $main{x} is a typeglob that accesses all three variables
# *main::x is the same typeglob
on-line help:
• http://www.cs.mcgill.ca/~abatko/computers/programming
/perl/howto/hash/
Get documents about "