ACCC Home Page ACADEMIC COMPUTING and COMMUNICATIONS CENTER
Accounts / Passwords Email Labs / Classrooms Telecom Software Computing and Network Services Education / Teaching Getting Help
 
Seminar - Perl II
0. Contents 1. Intro 2. Sources 3. Special Variables 4. Subroutines 5. Regexp
6. Ties & DBM 7. Functions 8. Eval 9. Larger Example 10. Exercises  

Subroutines

   
 
     
Definition
 

Subroutines are simply declared, anywhere, by the keyword sub, followed by the subroutine name and a block with the body.

 sub say_hi {
      print "hi\n";
 }
 
     
Calling Subroutines
 

    mysub(arg1, arg2...);  Call mysub with arguments
    mysub();               Call mysub with no arguments
    &mysub;                Call mysub with no arguments
 
     
Variable Scope
 

Most variables are automatically global. Use my or local to make a variable local to a given block or subroutine.

 $a = "be";
 $b = "he";
 $c = "me";
 sub say_boo {
    my $a = "boo";   # local to subroutine
       $b = "hoo";
    local $c = "moo";  # somewhat local
    checkvar();
    print "$a $b $c\n";
 }

 sub checkvar {
    print $a;       # be   global, not my
    print $b;       # hoo  global was reset
    print $c;       # moo  local still in scope
 }

my variables are lexically scoped. That is, the scope is determined by the containing block, and does not extend across subroutine calls. It never extends past the end of file.

local variables are actually global variables that have a temporary new value for the new scope. They do extend past subroutine calls.

Normally, my variables are preferred. But sometimes local is useful. And if you need to restrict the scope of special variables (e.g. $_), you must use local.

 
     
Argument Passing
 

Arguments are aliased to the global array @_ Subroutines simply use @_, most often by copying to local variables.

 sub sortme {
      my $first = shift;
      my @rest = @_ if (@_);
      return grep (/$first/, @rest);
 }

Argument passing by named argument is a standard perl trick. Suppose a subroutine takes several arguments, but instead of passing them all in order, you want to only pass some of them, (using defaults for the rest) and you don't want to remember the order. Do this:

   lunch( entree => 'ruben', 
          drink  => 'coffee', 
          side   => 'cole slaw');

    sub lunch {
	my %meal = (
	    drink   => 'coke',
	    entree  => 'hamburger',
	    dessert => 'flan',
	    @_
	);
	## Do stuff with %meal here
    }

Note that the subroutine does not define statically how many arguments to receive. Also note that @_ is aliased to the calling arguments. So if you modify @_, you will be modifying the passed arguments. But if you copy @_ into local variables, there is no danger. In most cases, if you want to modify the calling arguments, use explicit references, not @_.

 
     
Return Values
 

If a subroutine executes the statement return, it returns that value. Otherwise it returns the last value calculated. The returned value can be a scalar or list.

 sub two {
    2;
 }
 $b = sortme (@a);   # first element
 @b = sortme (@a);   # sorted array

 # returns sorted array, or first element, depending on context
 sub sortme {
    my @a = sort @_;
    return wantarray ? @a : $a[0] ;
 }
 
 

Perl II Previous: 3. Special Variables Next: 5. Regexp


1999-6-22  BobG
UIC Home Page Search UIC Pages Contact UIC