Lecture%2020%20-%20Advanced%20Perl%20Programming

Lecture%2020%20-%20Advanced%20Perl%20Programming - Lecture...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Copyright @ 2009 Ananda Gunawardena Lecture 20 Advanced Perl Programming In the previous lecture, we learned some basic constructs of perl programming including regex processing in Perl. Combining regex constructs with other high level programming capabilities of Perl is one of the main advantages of using Perl for tasks that require text processing. In this lecture we will cover few other miscellaneous topics of interests such as sub-routines, command line arguments, advanced data structures, reference variables in Perl 5 and system programming. Let us first understand how to define sub routines in perl. Subroutines Subroutines are part of many of the modern programming languages. C has functions and Java has methods and Pascal has procedures and functions for defining subroutines. Subroutines allow us to break the code into manageable pieces. Perl subroutines, like many of the high level languages, can have input parameters, local variables, and can return the answers back to the calling routine (eg: main). Let us start with a simple sub routine definition. sub name { statements; } Subroutines can be defined anywhere in your program. However, we recommend placing subroutines at the end of the file so main program can appear in the beginning. A subroutine can be called by simply using its name: name( ); Subroutines can be called by other subroutines and subroutines can return values that can be used by other subroutines. sub sum { return $a + $b; } So we can call this as: $a = 12; $b = 10; $sum = sum(); print “the sum is $sum\n”; Subroutines can also return a list of values such as sub list_of_values { return ($a,$b,$b,$a); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Copyright @ 2009 Ananda Gunawardena So we can write @arr = list_of_values( ); Passing Arguments A perl subroutine can be called with a list in parenthesis. For example, we can write a generic add function as follows. sub add { $tmp = 0; # this defines a global variable foreach $_ (@_) { $tmp += $_; } return $tmp; } In the above code @_ refers to the list passed to the function when it is called. For example, if we call the function as: add($a,$b,$c); or add(3,4,5,6); Then @_ = ($a,$b,$c) or @_ = (3,4,5,6) So $_[0] refers to $a, $_[1] refers to $b etc. Local variables Perl subroutines can define local private variables. Here is an example with a local variable. sub product { my ($x); # defines the local variable x foreach $_ (@_) { $x *= $_;} return $x; } You can have a list of local variables by simply expanding the list as: my ($x, $y, @arr); Command Line Arguments in Perl A Perl program can take command line arguments. One or more command line arguments can be passed when calling a perl program. For example, h perl program.pl infile.txt outfile.txt takes two command line arguments, infile.txt and outfile.txt. The number of command line arguments is given by $#ARGV + 1 and command line arguments are named $ARGV[0], $ARGV[1], etc. In the above example,
Background image of page 2
Copyright @ 2009 Ananda Gunawardena $ARGV[0] = infile.txt $ARGV[1] = outfile.txt
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

Lecture%2020%20-%20Advanced%20Perl%20Programming - Lecture...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online