REVISED: Sunday, March 3, 2013
You will learn the basic fundamentals of the Perl 5 subroutine.
I. PERL 5 SUBROUTINE INTRODUCTION
A. PERL 5 SUBROUTINES
Perl 5 subroutines are user-defined functions.
Subroutines allow you to reuse blocks of code in a program over and over again.
Subroutine definitions can be anywhere in a Perl 5 program. However, you should pick a location and consistently place them in that location; e.g., either at the beginning or end of your program.
Subroutine definitions are global; they are accessible from every part of your program. A Perl 5 subroutine can return any kind of value. When creating a subroutine definition you can use a return statement but you do not have to. If you do not use a return statement, the return value of the subroutine is the value of the last statement executed.
A subroutine name begins with an ampersand (&) and consists of letters, digits, and underscores, but can not start with a digit. You use the subroutine name, with the prefix ampersand (&) from within an expression, to invoke or call the subroutine. Any time you call a subroutine, you implicitly call a subroutine which is inside the current package.
To define a subroutine, use the keyword sub, the name of the subroutine without the ampersand (&), then an indented block of code in curly braces ({}) which makes up the body of the subroutine. The concept of a block of code is important; therefore, remember the area between the two braces is called a block.
B. PERL 5 MODULE
A module is similar to a subroutine. The difference is that modules do not live in your program, they are their own separate script outside your code.
A module is incorporated into a program with use. You can use as many modules as you like in one program and you can write your own modules.
Make a new folder within the Perl 5 lib folder; e.g., myModules. Instead of saving to your new Perl 5 lib folder myModules with a .pl extension, save with a .pm extension for Perl Module. To use a module named firstModule.pm start your script with use myModules::firstModule;.
C. PERL 5 PACKAGE
A Perl 5 script (.pl file) must always contain exactly zero package declarations.
A Perl 5 module (.pm file) must always contain exactly one package declaration, corresponding exactly to its name and location. For example, module Mathematics/Powers.pm must begin with package Mathematics::Powers.
D. PERL 5 ARGUMENTS
All arguments in Perl 5 are passed by reference. Therefore, it is important to immediately assign the input-only arguments to local variables, and only work on the local variables.
In Perl 5, you can pass only one kind of argument to a subroutine: a scalar. To pass any other kind of argument, you need to convert it to a scalar. You do that by passing a reference to it. A reference to anything is a scalar.
E. REFERENCING AND DE-REFERENCING VARIABLES
-> This operator works the same as it does in C. It means "element so and so of the de-referenced reference". In-place de-referencing is provided by the -> operator. Most Perl 5 objects are references to a hash. One typical usage is an object containing a list of hashes. The list of hashes could easily represent a data table, with array elements being rows (records) and hash elements being columns (fields).
Whenever you call a subroutine, the perl 5 interpreter creates a list of any parameters passed to the subroutine and stores this list in a special version of the variable @_ that is usable only by the called subroutine. Arguments to a subroutine are accessible inside the subroutine as list @_. Any change the subroutine performs to @_ or any of its members like $_[0], $_[1], etc, are changes to the original argument. However, assigning @_ or its elements to other variables makes a separate copy. Changes to the separate copy are unknown outside of the subroutine.
The following table discusses the referencing and de-referencing of variables.
VARIABLE
|
INSTANTIATING
THE SCALAR
|
INSTANTIATING
REFERENCE TO SCALAR
|
REFERENCING SCALAR
|
DE-REFERENCING SCALAR
|
ACCESSING AN ELEMENT
|
$scalar
|
$scalar = "dog";
|
$ref = \"dog";
|
$ref = \$scalar
|
$$ref or
${$ref}
|
N/A
|
@list
|
@list = ("dog", "cat");
|
$ref = ["dog", "cat"];
|
$ref = \@list
|
@{$ref}
|
${$ref}[3]
$ref->[3]
|
%hash
|
%hash = ("animal" => "dog",
"camel" => "Bactrian");
|
$hash = {"animal" => "dog",
"camel" => "Bactrian"};
|
$ref = \%hash
|
%{$ref}
|
${$ref}{"rat"}
$ref->{"rat"}
|
FILE
|
$ref = \*FILE
|
{$ref} or scalar <$ref>
|
F. PERL 5 TWO STRING ARGUMENTS SUBROUTINE EXAMPLE
The following table shows the subroutine syntax for a subroutine with two string arguments:
TWO STRING ARGUMENTS
| |
CALL
|
SUBROUTINE
|
mysub($filename, $title); | sub mysub($$)
{
}
|
G. PERL 5 SUBROUTINE EXAMPLE
sub max {
my($max) = shift(@_);
foreach my $temp (@_) {
$max = $temp if $temp > $max;
}
return($max);
}
You have learned the basic fundamentals of the Perl 5 subroutine.
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment