|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab06 Example 00 # Description: Basic subroutine syntax example #========================================================================== &Error_msg; exit; sub Error_msg { print "Something is wrong\n"; } |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab06_0.pl Something is wrong [mark@iguana perl] |
In the example above, subroutine name is Error_msg and is executed by the statement &Error_msg; The leading & in the subroutine call is optional but is strongly recommened to differentiate beteween user defined subroutines and functions in perl or those of a module. When &Error_msg is called, all the code in the subroutine Error_msg will be executed. Subroutines can be placed anywhere in the program but it is best to use some type of organization rather than scattering them randomly throughout the program.
my
function. my
declares the
variable so that it cannot be accessed outside the block.
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab06 Example 01 # Description: Subroutine variables. #========================================================================== $counter = 0; print "Counter is $counter\n"; &Loopy; print "Counter is $counter\n"; exit; sub Loopy { my $counter = 5; my $i; for ($i = 1;$i < 6; $i++) { $counter++; print "Line $i, counter is $counter\n"; } } |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab06_1.pl Counter is 0 Line 1, counter is 6 Line 2, counter is 7 Line 3, counter is 8 Line 4, counter is 9 Line 5, counter is 10 Counter is 0 [mark@iguana perl] |
In the above example, the scalar variable $counter is global in the main part of the program. A separate variable with the same name is declared in the subroutine but does changes to its value does not have any effect on the $counter variable in the main.
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab06 Example 02 # Description: Subroutine arguments. #========================================================================== &Error_msg('It is broken.'); exit; sub Error_msg { print "Something is wrong\n"; print $_[0]."\n"; } |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab06_2.pl Something is wrong It is broken. [mark@iguana perl] |
Notice that the argument passes when calling the subroutine is
available inside the block as the list variable
$_[0]
. More on this below.
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab06 Example 03 # Description: Subroutine arguments and the @_ array. #========================================================================== @somevar = qw( alpha beta delta epsilon zeta eta iota gamma theta omega ); &Check_values(@somevar); # show any errors foreach $k (@somevar) { print "$k\n" if($k =~ /^Erro/); } exit; #----------------------------- sub Check_values { my $k; my $counter = 0; # show the values print "Array: @_\n"; # check the individual elements foreach $k (@_) { if($k =~ /m/) { # alter the value of an element if it contains an "m" $_[$counter] = 'Error: '.$k; } $counter++; } } |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab06_3.pl Array: alpha beta delta epsilon zeta eta iota gamma theta omega Error: gamma Error: omega |
It is important to remember that if scalar variables or arrays
are passed to the subroutine as arguments, any alteration of the
values of the elements of the @_ array will alter the global
values of the arguments. In the case above
$somevar[7,9]
were
altered by assigning values to elements of the @_ array by
prepending the text 'Error: '
.
|
|||
# Name: Mark Tucker # Assignment: Lab06 Example 04 # Description: Subroutine ruturn values. #========================================================================== do { print "Enter your age: "; chomp($age = <STDIN>); $valid = &Check_age($age); print "$age is $valid\n"; }while($valid ne 'valid'); exit; #--------------------------------- sub Check_age { if($_[0] > 105) { return('too high'); }elsif($_[0] < 0) { return('too low'); }else{ return('valid'); } } |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab06_4.pl Enter your age: 500 500 is too high Enter your age: -4 -4 is too low Enter your age: 35 35 is valid [mark@iguana perl] |
last updated: 18 Mar 2012 13:08