Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.

Similar presentations


Presentation on theme: "Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments."— Presentation transcript:

1 Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments into functions & return value from a function 3. To understand the scope of a Perl’s program

2 COMP111 Lecture 18 / Slide 2  A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1; statement2; statement3; }  Simple Example: sub hello{ print "hello world!\n"; } Defining a Function (1)

3 COMP111 Lecture 18 / Slide 3 Defining a Function (2)  Subroutine definitions can be anywhere in your program text (they are skipped on execution)  Within the subroutine body, you may use any variable from the main program (variables in Perl are global by default). #!/usr/local/bin/perl5 -w $user = "horner"; hello(); print "goodbye $user!\n"; sub hello{ print "hello $user!\n"; }

4 COMP111 Lecture 18 / Slide 4 Global Variable  You can also use variables from the subroutine back in the main program (it is the same global variable): $ cat sum1 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $sum = 0; sum_a_and_b(); print "sum of $a plus $b: $sum\n"; sub sum_a_and_b{ $sum = $a + $b; } $ sum1 sum of 1 plus 2: 3 $

5 COMP111 Lecture 18 / Slide 5 Sorting Question Revisited  Consider a %students{$ID}{‘CGA”} from last lecture  Print out the students’ ID’s and CGA’s in order of student CGA.  Modify sort function by order of student’s CGA foreach $ID ( sort by_CGA keys(%students) ) { … } sub by_CGA { ($students{$a}{‘CGA'} $students{$b}{‘CGA'}) || ($a cmp $b); }

6 COMP111 Lecture 18 / Slide 6 Returning Values (1)  You can return a value from a function, and use it any expression as in C++: $ cat sum2 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $c = sum_a_and_b() + 1; print "value of c: $c\n"; sub sum_a_and_b{ return $a + $b; } $ sum2 value of c: 4 $

7 COMP111 Lecture 18 / Slide 7 Returning Values (2)  A subroutine can also return a list of values: $ cat list1 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; @c = list_of_a_and_b(); print "list of c: @c\n"; sub list_of_a_and_b{ return ($a,$b); } $ list1 list of c: 1 2 $

8 COMP111 Lecture 18 / Slide 8 Returning Values (3)  Another example: $ cat max1 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = max_of_a_and_b(); print "max: $max\n"; sub max_of_a_and_b{ if($a > $b){ return $a; } else{ return $b; } } $ max1 max: 2 $

9 COMP111 Lecture 18 / Slide 9 Arguments (1)  You can also pass arguments to a subroutine.  The arguments are assigned to a list in a special variable @_ for the duration of the subroutine. $ cat max2 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = max($a, $b); print "max: $max\n"; sub max{ if($_[0] > $_[1]){ return $_[0]; } else{ return $_[1]; } } $ max2 max: 2

10 COMP111 Lecture 18 / Slide 10 Arguments (2)  A more general way to write max() with no limit on the number of arguments: $ cat max3 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = max($a, $b, 5); print "max: $max\n"; sub max{ $max = 0; foreach $n (@_){ if($n > $max){ $max = $n; } } return $max; } $ max3 max: 5

11 COMP111 Lecture 18 / Slide 11 Arguments (3)  Don ’ t confuse $_ and @_, they are unrelated.  Excess parameters are ignored if you don ’ t use them.  Insufficient parameters simply return undef if you look beyond the end of the @_ array.  @_ is local to the subroutine.

12 COMP111 Lecture 18 / Slide 12 Local Variables (1)  You can create local versions of scalar, array and hash variables with the my() operator. $ cat max4 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = 0; $max1 = max($a, $b, 5); print "max1: $max1\n"; print "max : $max\n"; sub max{ my($max,$n);# local variables $max = 0; foreach $n (@_){ if($n > $max){ $max = $n; } } return $max; } $ max4 max1: 5 max : 0

13 COMP111 Lecture 18 / Slide 13 Local Variables (2)  You can initialize local variables: $ cat max4 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = 0; $max1 = max($a, $b, 5); print "max1: $max1\n"; print "max : $max\n"; sub max{ my($max,$n) = (0,0); # initialized locals foreach $n (@_){ if($n > $max){ $max = $n; } } return $max; } $ max4 max1: 5 max : 0

14 COMP111 Lecture 18 / Slide 14 Local Variables (3)  You can also load local variables directly from @_ : $ cat max5 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = max($a, $b); print "max: $max\n"; sub max{ my($n1, $n2) = @_; if($n1 > $n2){ return $n1; } else{ return $n2; } } $ max5 max: 2

15 COMP111 Lecture 18 / Slide 15  You can force all variables to require declaration with my() by starting your program with: use strict; $ cat max5 #!/usr/local/bin/perl5 -w use strict; my $a = 1;# declare and initialize $a my $b = 2;# declare and initialize $b my $max = max($a, $b); # declare and initialize print "max: $max\n"; sub max{ my($n1, $n2) = @_; # declare locals from @_ if($n1 > $n2){ return $n1; } else{ return $n2; } } $ max5 max: 2 Keyword: use strict (1)

16 COMP111 Lecture 18 / Slide 16  use strict effectively makes all variables local.  Typing mistakes are easier to catch with use strict, because you can no longer accidentally reference $billl instead of $bill.  Programs also run a bit faster with use strict.  For these reasons, many Perl programmers automatically begin every Perl program with use strict.  It is up to you which style you prefer. Keyword: use strict (2)

17 COMP111 Lecture 18 / Slide 17 More on Parameters  Variables are passed by name in Perl, i.e., parameters from the caller can be changed. $ cat swap #!/usr/bin/perl -w sub swap { ($_[0],$_[1])=($_[1],$_[0]); } ($a,$b)=(1,2); swap($a, $b); print "$a, $b \n"; @x=(1,2); swap(@x); print "@x \n"; $ swap 2, 1 2 1

18 COMP111 Lecture 18 / Slide 18 Structures as Parameters  To pass arrays, hashes or other structures, use pointers: $ cat vsum #!/usr/bin/perl -w sub vsum{ return undef if (@{$_[0]} != @{$_[1]}); for ($i=0; $i < @{$_[0]}; $i++ ) { $sum[$i] = $_[0][$i] + $_[1][$i]; } return @sum; } @a=(1,2); @b=(3,4); @sum =vsum(\@a, \@b); print "Sum1: @sum "; @sum2=vsum([2,3],[4,5]); print "Sum2: @sum2\n"; $ vsum Sum1: 4 6 Sum2: 6 8 @{$_[0]} first array, @{$_[1]} second array @{… } gives element count in a scalar context @{$_[0]} first array, @{$_[1]} second array @{… } gives element count in a scalar context $_[0][$i] i-th element (cf, 2D array) [ ] returns reference of an anonymous array We cannot use ( ) here [ ] returns reference of an anonymous array We cannot use ( ) here

19 COMP111 Lecture 18 / Slide 19  Here is the skeleton of a program for recursive directory processing: #!/usr/local/bin/perl5 -w sub dirtree { my @files = ; # local variable required print "Directory listing for: $_[0]\n"; foreach (@files){ print "$_\n"; dirtree($_) if (-d $_); # recursion } dirtree($ARGV[0]); Recursive Directory Processing


Download ppt "Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments."

Similar presentations


Ads by Google