0% found this document useful (0 votes)
2 views

Lecture7 PERL Subroutines

The document explains Perl subroutines, which allow programmers to group statements into mini-programs for reuse within larger programs. It covers the declaration, execution, and argument passing of subroutines, including handling arrays and hashes. Additionally, it discusses variable-length parameter lists and provides examples of subroutine usage in Perl.

Uploaded by

ulain7049
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture7 PERL Subroutines

The document explains Perl subroutines, which allow programmers to group statements into mini-programs for reuse within larger programs. It covers the declaration, execution, and argument passing of subroutines, including handling arrays and hashes. Additionally, it discusses variable-length parameter lists and provides examples of subroutine usage in Perl.

Uploaded by

ulain7049
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PERL Subroutines

Tamsila Parveen
Subroutines
• Subroutines provide a way for programmers to
group a set of statements, set them aside, and turn
them into mini-programs within a larger program.

• Perl uses the term subroutine, method, and function


interchangeably

• These mini-programs can be executed several times


from different places in the overall program

• It provides a simplest way to reuse the chunk of your


code in several ways.
Declaration of subroutine
Working with Subroutines
• You can create a subroutine by placing a group of
statements into the following format:
sub subroutine_name {
set of statements
}
• For example a outputTableRow subroutine
sub outputTableRow {
print “Hello Class!\n”;
}
• Execute the statements in this subroutine, by preceding
the name by an ampersand: &outputTableRow;
Programe (code) Design using
Subroutine (pseudo code)
Passing Arguments to a
Subroutine
 Perl automatically provides an array called @ARGV
(@_ ), that holds all the values from the command line.
You don't have to declare the variable.

 @ARGV is just a regular array in Perl. The only


difference from arrays that you create, is that it does
not need to be declared and it is populated by Perl
when your script starts.
Passing Arguments to a
Subroutine
 Aside from these issue, you can handle it as a
regular array.
 You can go over the elements using foreach, or
access them one by one using an index: $_[0] or
$ARGV[0]
 You can also use shift, unshift, pop or push on this
array.
More condensed without
comments
sularger {
my ($a, $b)= @_;
if ($a > $b) { print “$a\n”; } else {$b;}
}
larger (b 2, 5);
# even simpler
sub larger {
my ($a, $b) = @_; #subroutines often have this line
if ($a > $b) { $a; } else {$b;}
}
8
Passing Lists to Subroutines
• Because the @_ variable is an array, it can be used to
supply lists to a subroutine. However,
because of the way in which Perl accepts and parses
lists and arrays, it can be difficult to extract
the individual elements from @_.
Passing Hashes to Subroutines
• When you supply a hash to a subroutine or operator
that accepts a list, then hash is automatically
translated into a list of key/value pairs. For example
Variable-length Parameter Lists
• many traditional programming languages require subroutine
parameter lists to be strictly typed
• predefined number and type of parameters

• this may be enforced in perl as well

sub larger {
if(@_ != 2) { # scalar context
print "Warning: 2 arguments expected\n"
}
}

11
Another way:
Variable-length Parameter Lists
#!/usr/bin/perl
$max = &larger (3, 5, 10, 4, 5);

sub larger{
my ($largest) = shift @_; # shift element off of the LHS of
the array
foreach (@_) { # default variable used ($_)
if($_ > $ largest) {
$largest = $_;
}
}
print “$largest\n”;
}
12
Theory Assignment 2

What is the role of Perl subroutines


in Bioinformatics with 2 examples

• Email me this assignment before 31/03/2020


through CR

You might also like