This lecture notes is based on Prof Doreen De Leon's lectures.

Learning Perl, Chapter 4.

- Syntax: s
sub subname { statements; }

`subname`

is the name of the subroutine.- The statement block following the name is the definition.
- Can put subroutine definitions anywhere, but for this class we will put them at the end of the program.
- Subroutine definitions are global.
- By default, any variable referenced in a subroutine is a global variable.
- One subroutine may call another.
- invoke a subroutine from within any expression by using the subrountine name(with the ampersand &)

&hello; # Hello 1&hello; # Hello 2print $num, "\n"; #$num is global, 2$num=100; &hello; # Hello 101 &hello; # Hello 102sub hello { $num++; print "Hello $num!\n"; }

&hello2; # hellohello2 sub hello { print "hello"; } sub hello2 {&hello; # call another functionprint "hello2\n"; }

- A subroutine is always invoked as part of an expression, even if the result of that expression is not being used.
- calculation that is
**last**performed in a subroutine is automatically the return value - subroutine can return a list of values when evaluated in list context.

$a = 5; $b = 7; $c = &sum_of_a_and_b; print $c, "\n"; #12 $a = 10; $b = 20; $c = &sum_of_a_and_b; print $c, "\n"; #30sub sum_of_a_and_b { $a + $b; }

$a = 5; $b = 7; $c = &sum_of_a_and_b; print $c, "\n"; #1 $a = 10; $b = 20; $c = &sum_of_a_and_b; print $c, "\n"; #1 sub sum_of_a_and_b { $a + $b;print "bye!\n"; #the print function returns 1 #last calculation}

**Output**

bye! 1 bye! 1

$a = 5; $b = 7; $c = &max_of_a_and_b; print $c, "\n"; #7 $a = 20; $b = 18; $c = &max_of_a_and_b ; print $c, "\n"; #20sub max_of_a_and_b { if($a < $b) { $b; #last calculation if $a < $b } else { $a; #last calculation if $a >= $b } }

$a = 5; $b = 7; @c = &list_from_a_to_b; print @c, "\n"; #567 $a = 5; $b = 1; @c = &list_from_a_to_b; print @c, "\n"; #54321sub list_from_a_to_b { if($a < $b) { $a..$b; } else { reverse $b..$a; } }

- The subroutine invocation followed by a list within parentheses causes the list to be automatically assigned to a special variable, @_, for the duration of the subroutine.
- Can access this variable to find number and values of arguments.

$a = 5; $b = 7; $c = 13; $m = &max($a, $b); print $m, "\n"; #7 $m = &max($a, $b, $c); # the last element is ignored print $m, "\n"; #7sub max { if($_[0] < $_[1]) { $_[1]; } else { $_[0]; } }

@a = (1,2,3); print &sum(@a), "\n"; # 1+2+3 = 6 @a = (1..100); print &sum(@a), "\n"; #1 + .. + 100 = 5050 print &sum(2,3,4,10), "\n"; #2 + 3 + 4 + 10 = 19 sub sum { $sum=0;foreach $num(@_) {$sum+=$num; } $sum; }

- The my operator takes a list of variable names and instantiates (creates) local versions of them – called lexically scoped (hidden from outside the block, even to called subroutines).

$a=1; $b=2; print $a, " ", $b, "\n"; # 1 2 &test1; # 5 10 print $a, " ", $b, "\n"; # 5 10 $a=1; $b=2; print $a, " ", $b, "\n"; # 1 2 &test2; # 5 10 print $a, " ", $b, "\n"; # 1 2 sub test1 { $a = 5; $b = 10; print "test1: ", $a, " ", $b, "\n"; } sub test2 {my($a,$b);$a = 5; $b = 10; print "test2: ", $a, " ", $b, "\n"; }

**Output**

1 2 test1: 5 10 5 10 1 2 test2: 5 10 1 2

- Forces each variable to be declared before it is used.

$num_of_students = 5; print $num_of_students, "\n";$num_of_student+=1; #opps! spelling mistake, s is missingprint $num_of_student, "\n";

**use strict**

You can see several error message

use strict;$num_of_students = 5; print $num_of_students, "\n"; $num_of_student+=1; #opps! spelling mistake, s is missing print $num_of_student, "\n";

**Correct program**

use strict;my$num_of_students = 5; print $num_of_students, "\n"; $num_of_students+=1; #opps! spelling mistake, s is missing print $num_of_students, "\n";

- The return operator immediately returns a value from a subroutine.

$x = &smaller_than_5(11,12,3,4,5,6); print $x,"\n"; #return the first number that is smaller than 5 sub smaller_than_5 { foreach(@_) { if($_ < 5) {return $_;} } }

- If the compiler sees the subroutine definition before the invocation or if Perl can tell from the syntax that it is a subroutine call (i.e., it has a parameter list in parentheses), the call can be made without an ampersand.
- If the subroutine has the same name as a Perl built-in function, you must use the ampersand to call it.

use strict; my $a = 5; my $b = 7; my $m =max($a, $b);# no & print $m, "\n"; #7 sub max { if($_[0] < $_[1]) { $_[1]; } else { $_[0]; } }

hello; # can't omit &, no argumentsub hello { $num++; print "Hello $num!\n"; }