Home arrow Perl Programming arrow Page 3 - Lists and Arguments in Perl

Named Parameters - Perl

In this conclusion to a six-part article series on subroutines and functions in Perl, you'll learn more about lists and arrays, and take a look at default argument values. This article was excerpted from chapter six of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Lists and Arguments in Perl
  2. Default Argument Values
  3. Named Parameters
By: Apress Publishing
Rating: starstarstarstarstar / 0
May 08, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

One of the more irritating things about calling subroutines is that you have to remember the order of the parameters. Was it username first and then password, or host first and then user-name, or . . . ?

Named parameters are a neat way of solving this. What we’d rather say is something like this:

logon( username => $name, password => $pass, host => $hostname);

and then give the parameters in any order. Now, Perl makes this really, really easy because that set of parameters can be thought of as a hash:

sub logon {
die "Parameters to logon should be even" if @_ % 2;
my %args = @_;
print "Logging on to host $args{hostname}\n";
...
}

Whether and how often you use named parameters is a matter of style; for subroutines that take lots of parameters, some of which may be optional, it’s an excellent idea. For those that take two or three parameters, it’s probably not worth the hassle.

Named parameters also help when we want to provide default values to our arguments. For instance, let’s say we write a function namedcollege_degree()and it expects three arguments:university,degree,year. We could call the function with all three arguments:

college_degree(
university => 'Illinois',
degree => 'MSEE',
year => 2000
);

Since we are using named parameters, the order of those three argument pairs is not important—they could be in any order we want. We could also call the function with only two pairs as in

college_degree(
degree => 'MSEE',
year => 2000
);

provided our function defaults the arguments. This implementation ofcollege_degree()ensures that the three arguments have default values:

sub college_degree {
my %args = @_;

$args{university} = 'Northwestern' unless exists $args{university};
$args{degree} = 'BSCS’
unless exists $args{degree};
$args{year} = 2004
unless exists $args{year};

...
}

Summary

Subroutines are a bit of code with a name, and they allow us to do two things: chunk our program into organizational units, and perform calculations and operations on pieces of data, possibly returning some more data. The basic format of a subroutine definition is

sub name BLOCK

We can call a subroutine by just sayingnameif we’ve had the definition beforehand. If the definition’s lower down in the program, we can sayname(), and you may see&nameused in older programs. Otherwise, we can use a forward definition to tell Perl thatname should be interpreted as the name of a subroutine. The conventional notation isname().

When we pass arguments into a subroutine, they end up in the special array@_—this contains aliases of the data that was passed so data is passed in by reference. We discussed ways of passing variables in by value (copying the arguments intomy()variables) and also how to implement default argument values with named parameters.

Exercises

  1. Write a program that computes the factorial of a number. Just to remind you, the factorial of a number is that number times that number minus 1 and so on, stopping at 1. For instance, the factorial of 5 is

    5! = 5 * 4 * 3 * 2 * 1

    The factorial of 0 is 1.
  2. Modify the seconds.plprogram seen earlier in the chapter so that it contains a second subroutine that asks the user for a number, puts it into a global variable, and converts that into hours, minutes, and seconds.

 



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: