Subroutine References

Be the first to comment | 2I like it!
August 16, 2001, 12:00 AM —  ITworld — 

We have used subroutine references a couple of times in previous
articles, but we haven't looked at them directly. You can take a
reference to an existing (named) subroutine using the backslash
operator along the ampersand in the following manner:

sub foo {
print "This is foo\n";
}

my $sref = \&foo;

You can call the referenced routine by preceding the scalar holding it
with an ampersand, or by using the dereference arrow and parentheses:

&$sref; # or: &$sref('argument')
$sref->(); # or: $sref->('argument')

It is important to realize that you cannot take a reference to a
subroutine and pass it arguments at the same time:

my $sref = \&foo('argument');

What this actually does is return a reference to the return value of
that function (using the given argument), which probably isn't what you
wanted.

But what are subroutine references good for? A couple of common uses
are dispatch tables and passing routines as arguments to other
routines; we will consider the first here. A dispatch table is simply a
table (usually a hash) that allows you to select the routine to run.
Consider a user interface that queries the user to enter a command to
run:

#!/usr/bin/perl -w
use strict;

sub this { print "You picked 'this'\n" }
sub that { print "You picked 'that'\n" }
sub quit { exit }

print "Enter a command:\n";
while () {
chomp(my $cmd = $_);
if ($cmd eq 'this') {
this();
} elsif ($cmd eq 'that') {
that();
} elsif ($cmd eq 'quit' or $cmd eq 'exit') {
quit();
} else {
print "Unrecognized command: $cmd\n";
}
}
__END__

Now imagine that there are many more possible commands the user could
enter and you can see that the program would grow quite large. Using a
dispatch table simplifies all of the logic of the if/elsif statements
into the data structure:

#!/usr/bin/perl -w
use strict;

sub this { print "You picked 'this'\n" }
sub that { print "You picked 'that'\n" }
sub quit { exit }

my %dispatch = (
this => \&this,
that => \&that,
quit => \&quit,
exit => \&quit,
);

print "Enter a command:\n";
while () {
chomp(my $cmd = $_);
if ($dispatch{$cmd}) {
$dispatch{$cmd}->();
} else {
print "Unrecognized command: $cmd\n";
}
}
__END__

This program is actually a little longer, but it is simpler in design
and simpler to maintain and update; adding new commands involves only
defining a subroutine and adding another entry to the hash (we don't
have to add further elsif clauses as we would with the first example.

Next Week: Passing subroutine references

» posted by ITworld staff

ITworld

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff

VMware ESX Server in the Enterprise
By Edward L. Haletky
Published Dec 29, 2007 by Prentice Hall.
Enter now! | Official rules | Sample chapter

Green IT
By Toby Velte, Anthony Velte, Robert C. Elsenpeter
To be published Oct. 10, 2008 by McGraw Hill Professional
Enter now! | Official rules | About the book

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources