perl Basics
sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”; print “Hello, world!\n”; new line all three lines do the same thing # - shell ! – bang executing perl hello.pl without sh-bang line is the same as./hello.pl with the sh-bang line must be first line in file Question: printf is a function; what is strange about its appearance? NOTE: Don't forget to chmod +x hello.pl
Variables: perl has three kinds of variables: –scalar ($-prefix) -- numerics, strings, addresses, primitives –list/array -- lists, arrays, stacks, queues –hashtable (%-prefix) -- key/value pairs Typical symbol table: perl symbol table: Variable Name Address X 0x Variable Name VariableType Address X 0x not defined 0x typeglob
Scalars and Lists: The following is possible: In other words, two variables with the same name $X = = ('apple', 1, 'orange', 2, 'pear', 3);
Scalar Variables: numeric, string, boolean, address are all scalars $inum = 2; # numeric $fnum = 2.0; # numeric $fname = “Andrew”; # string $lname = “Pletch; # string $name1 = “$fname $lname”; # value of $name1 is “Andrew Pletch” $name2 = ‘$fname $lname’; # value of $name2 is “$fname $lname” 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 double quotes expands variables single quotes leaves variables unexpanded.
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. 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 [ ]. If $X = “apple” then $X[2] can not be 'p'; why = ('apple', 1, 'orange', 2, 'pear', 3); $L[0] is a fruit; $L[1] is a number. We use $ because 'apple' is a scalar Because there might also be 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
List Variables 2: Suppose we execute then inside is a predefined variable whose value is To access the elements we use indexes > myprog.pl -t -a 3 –f myfile ( “-t”, “-a”, 3, “-f”, “myfile”) $FName = $ARGV[4]; # indexing starts at 0 use $ because “myfile” is a scalar use ( ) to declare or express a list
List Variables 3: A list of lists is just a list: A list used in a scalar context returns the list = ( ( 1, 2, 3), (4, 5, 6), (7, 8, 9) ); # looks like a 3x3 matrix # value is ( 1, 2, 3, 4, 5, 6, 7, 8, 9 ); # but it isn’t “\n”; # scalar context printf \n”; # list context output: string concatenation operator
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 #!/usr/bin/perl # prints out all inherited environment variables # and their values foreach $key (keys %ENV) { printf "$key ==> $ENV{$key} \n"; } function that returns a list of all keys to the hash for-loop syntax for lists [ ] for lists; { } for hashes ( ) part of for-loop syntax and not part of list definition; required
Perl hash HowTo:
hashes and lists: #!/usr/bin/perl #hash_test.pl %myHash; $myHash{A} = 1; $myHash{B} = 2; $myHash{C} = 3; $myHash{D} = 4; print %myHash; print %myHash. "\n"; print "%myHash = %myHash; "\n"; printf \n"; printf "ENV == %ENV \n"; # output A1B2C3D4 3/8 %myHash 8 A 1 D 4 C 3 B 2 ENV == E+00NV using hash in scalar context printing a hash does nothing but treat it like a string assign hash to list 3 == number of used buckets 8 == number of allocated buckets Prints out the contents all mashed together %E treated like 0.0E
perl symbol tables: hash consisting of (key, value) pairs key == identifier value == typeglob data structure typeglob contains reference to all three variables %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: /perl/howto/hash/ /perl/howto/hash/