Presentation is loading. Please wait.

Presentation is loading. Please wait.

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”;

Similar presentations


Presentation on theme: "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”;"— Presentation transcript:

1 perl Basics

2 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

3 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: perl symbol table: Variable Name Address X 0x012345 Variable Name VariableType Address X $@%$@% 0x012345 not defined 0x098765 typeglob

4 Scalars and Lists: The following is possible: In other words, two variables with the same name $X = “apple”; @X = ('apple', 1, 'orange', 2, 'pear', 3);

5 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.

6 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

7 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 not? @L = ('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 a @X and so $X[2] must be the third element in this list; use substr($X,2,1) instead.

8 Exercise: The perl substr() function is a whole study in itself; take a look at http://perlmeme.org/howtos/perlfunc/substr.html

9 List Variables 2: Suppose we execute then inside myprog.pl @ARGV is a predefined variable whose value is To access the elements of @ARGV 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

10 List Variables 3: A list of lists is just a list: A list used in a scalar context returns the list length @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 printf @L1. “\n”; # scalar context printf “@L1 \n”; # list context output: 9 1 2 3 4 5 6 7 8 9 string concatenation operator

11 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

12 Perl hash HowTo: http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/

13 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 \n"; @list = %myHash; printf @list. "\n"; printf "@list \n"; printf "ENV == %ENV \n"; # output A1B2C3D4 3/8 %myHash 8 A 1 D 4 C 3 B 2 ENV == 0.000000E+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

14 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

15 on-line help: http://www.cs.mcgill.ca/~abatko/computers/programming /perl/howto/hash/http://www.cs.mcgill.ca/~abatko/computers/programming /perl/howto/hash/


Download ppt "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”;"

Similar presentations


Ads by Google