130 likes | 162 Views
Learn about Perl functions, subroutines, variable scopes, references, and passing arrays in this comprehensive guide. Understand how to create, call, and manage functions effectively in Perl programming.
E N D
Functions • Functions also called subroutines are “free flowing”. • The returned value from a function can be interpreted in many different ways. • Anatomy of a subroutine sub subroutineName { my (@argsToSubroutine) = @_; # arguments are passed in @_. # @_ is the argument stack, the arguments passed &callSub2; # Calling another subroutine return (@returnValues); # returning values. #An explicit return is not necessary, by default, the value of the # last expression is returned. } Calling Functions: $result = &subroutine (@args); $result = subroutine (@args); $result = subroutine; $result = subroutine @args; $result = subroutine ();
Functions sub showArgs { sub showArgs { $first = shift; print "$first \n"; $second = pop @_; print "$second \n"; $third = shift; print "$third \n"; @theRest = @_; print "@theRest"; } showArgs (“A”,”B”,”C”,”D”,”E”); Output: A E B C D
sub changeToCaps { my ($line) = $_[0]; $line =~ tr/a-z/A-Z/; return $line; } $text = "this is a test"; $text = &changeToCaps ($text); print $text; print "\n";
Scope of variables • Global variables are declared outside of any subroutine or function and can be accessed by any part of perl program. • Local variables are private to the subroutine in which they are declared. Are of two types: • Dynamic variables are defined with local keyword and are visible to any subroutines called within the subroutine in which they are declared. • Lexical variables are private variables and are seen only in the subroutine in which they are declared. They are declared with keyword my.
Functions $global = 100; sub one{ local $bush = "rose"; my $tree = "cherry"; two ($tree); return ++$global; } sub two { my $shrub = $bush; print $shrub; print "\n"; my $a = shift(@_); print "\n"; } &one; #output rose cherry
$one = 1234; $two = 6789; $three = 0011; $best = &Max($one,$two,$three); print $best , "\n"; output: 6789 sub max { my $max = shift (@_); foreach $val (@_){ $max = $val if $val > $max;} return $max; }
sub fun3 { $filename = shift @_; open (FHANDLE,$filename) || die "can't open the file\n"; #all the lines in the # file are copied into # the array, allLines. @allLines = <FHANDLE>; } sub four ($$){ my ($n1,$n2) = ($_[0],$_[1]); my $sum = $n1 + $n2; return $sum; } Functions
References • Creating References: Examples: $data = “abc”; $a_ref = \$data; #stores the address of data in a_ref @array = (“one”,”two”,”three”); $b_ref = \@array; %hash = (“Mon” =>2,”Tue” => 3); $c_ref = \%hash;
De-referencing print $$a_ref; or print ${a_ref}; print $$b_ref[1]; or print ${b_ref}[1]; or $b_ref->[1]; print @$b_ref; Print $$c_ref{“Mon”}; or print $c_ref->{“Mon”}; or
sub fun4 { my ($array1,$array2) = @_; print "@$array1"; print "@$array2"; } @array1 = (1,2,3,4); @array2 = (5,6,7); fun4 (\@array1,\@array2); #Rewrite the function to switch arrays sub six{ my ($hashRef1) = @_; my %returnHash = (); foreach $key (keys %$hashRef1){ $returnHash{$key} = $hashRef1->{$key}; } return %returnHash; } %fruits = ("A"=>"apple","B"=>"Banana"); %retFruits = six(\%fruits); Passing References
Some Useful Functions Split Takes scalars and breaks them into arrays. Takes either a delimiter or a regular expression and divides the scalar on that boundary. Example 1: $line = "John Smith"; ($firstName,$lastName) = split / /,$line; print $firstName; print "\n"; print $lastName; Example 2: $line = "A,B,C"; @splitArray = split (",",$line); print "@splitArray"; Example 3: $line = "A,,B,,,C"; #extra commas @splitArray = split (",",$line); print "@splitArray"; # A B C. Example 4: $line = "A,,B,,,C"; @splitArray = split (m",+",$line); print "@splitArray"; #A B C
Some Useful Functions join: joins separate strings into a single string separated by a delimiter. $fullName = join ',',$lastName,$firstName; push: Adds a list to the end of the array. @line = ("A","B","C"); @text = ("D","E","F"); push @line,@text; print "@line"; #A B C D E F Pop: Returns the last element of the array. $element = pop @line; print $element;# F shift: Returns the first element of the array $element = shift @line; print $element;# A unshift: Prepends a list to the front of the array @line2 = ("X","Y","Z"); unshift @line,@line2; print "@line"; # X Y Z B C D E