Download presentation
Presentation is loading. Please wait.
1
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann
2
Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics Questions / comments? Go over answers to hw. Perl –subroutines –Scope of variables: my / local / package –implementing a recursive descent parser in Perl
3
Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Declaring and defining subroutines sub name { # code goes here… } Call subroutines $x = &name; &name( parm1, parm2, parm3); # any number of parameters allowed separated by commas name(parm1, parm2, parm3); # the ampersand is optional if using parameters in parentheses.
4
Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Parameters in subroutines are stored in a parameter array @_ The value returned from a subroutine is the value of the last evaluation done inside it. Be aware of this! Or you can explicitly use the return function to return a value and terminate the subroutine. Local variables in subroutines are declared by using the my keyword –When declaring more than one, you must put them in parentheses or else all but the first one will be considered global. –my ($parm1, $parm2); When calling the subroutine, the arguments passed in are put in the @_ special array for that subroutine.
5
Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 There are a multitude of ways to get at the contents of the elements in the parameter array @_ One of which is $_[0], $_[1], etc... Note well: These have NOTHING to do with the $_ special default variable which we have seen. Another way is to use shift. Shift will shift in the next array element into the variable on the LHS and remove that element from the array. e.g. my $filehand = shift; note: shift works for any array, not just the paramater array. e.g. shift @myarray Another way would be to do something like: my ($age, $height, $weight) = @_;
6
Perl (Scope Lexical (block) vs. Global.) Michael Eckmann - Skidmore College - CS 330 - Fall 2008 my forces variables to have block scope If you do not designate your variables with anything, then they are package variables and are global --- they can be used in this or any package. If the package variables we want to refer to are in the current package then we can (and have been) referring to them without the package name. But we can refer to them like this as well: $main::var1 The default package (if you don't specify one) is main. The local keyword does NOT create a local variable (my does). The local keyword when used on a package variable saves the current value of the package variable away somewhere and replaces it with whatever new value you are using. Then when it leaves the block scope, the original saved value is restored. Always use my, never use local. Only use local if my is not allowed (like with the special variables like $_ etc..)
7
Perl (Scope Lexical (block) vs. Global.) Michael Eckmann - Skidmore College - CS 330 - Fall 2008 my –creates local (block scope) variables my ($num, $age); # makes $num and $age “local” my $width; # makes $width “local” my $height, $weight; # makes $height “local”, and $weight “global” package variables –package variables are global and can be used in any package (by specifying the package followed by the variable name –default package is main $main::var1 # refers to $var1 in the main package $var1 # refers to $var1 in the main package if used inside the main package $pack1:var1 # refers to $var1 in the pack1 package
8
Perl (Scope Lexical (block) vs. Global.) Michael Eckmann - Skidmore College - CS 330 - Fall 2008 local –does NOT create local variables –used for already declared variables –saves a copy of the current value of the variable and this variable can then be used within the block and have its value changed and when the block ends execution, the original value is restored.
9
Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's look at this page: http://perl.plover.com/FAQs/Namespaces.htmlhttp://perl.plover.com/FAQs/Namespaces.html We'll start at Lexical Variables section and go through to my Variable Trivia section.
10
Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Common code problems I saw last year. Not using chomp on the input (and then comparing the input variable to some string that doesn’t have a newline after it.) Comparing strings using ==, !=, etc. instead of the correct way which is to use eq, ne, etc. When comparing non-numeric strings to numerics, the strings are evaluated as zero which trips people up. Solution would be to: Use chomp on the input variable Use the string comparison operators to compare strings (don’t use ==, !=,,etc. which should only be used to compare numeric data.)
11
Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Let's start to implement a recursive descent parser for the language in the text book (p. 179) What are the features of a recursive decent parser again? Assuming the thing we're parsing is stored in a file we would like to read in the whole file in one string (including \n's). The reason the reading of a file reads one line at a time is because Perl has a special variable, $/, that holds what “termination character(s)” which in our case is usually set to be “\n”. We can temporarily store what the termination character(s) are, then undef the $/, then read the whole file into a scalar and then store the termination characters back into $/.
12
Perl Michael Eckmann - Skidmore College - CS 330 - Fall 2008 There's so much more about Perl that I haven't gone over. You should now be able to learn more on your own. There are many functions I haven't gone over that are useful –Substring –Split –Etc. –Find out about the functions (with examples) at these 2 pages: –http://www.unix.org.ua/orelly/perl/prog3/ch29_01.htmhttp://www.unix.org.ua/orelly/perl/prog3/ch29_01.htm –http://www.unix.org.ua/orelly/perl/prog3/ch29_02.htm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.