1 / 13

PERL Functions

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.

kearley
Download Presentation

PERL Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PERL Functions

  2. 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 ();

  3. 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

  4. 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";

  5. 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.

  6. 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

  7. $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; }

  8. 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

  9. 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;

  10. 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

  11. 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

  12. 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

  13. 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

More Related