LPM: Subroutine references, or something ...
grdodson at lexmark.com
grdodson at lexmark.com
Thu Jul 15 15:20:35 CDT 1999
The syntax:
($foo, $bar) = &{$routine}(@args);
is what is called an "symbolic reference" and used to be the only way to do
references (perl 4). The downside is that it is slow and can be a
sign that you have a bug (maybe you didn't want a reference in the first place).
The way around this that is OK with "strict refs" is to use
a "hard" reference. For example if I store a bunch of subs in a hash and
then call the sub I want based on a hash key:
use strict;
my %subhash = ( sub1 => sub { my $a = shift; print "Sub1 $a\n" },
sub2 => sub { my $a = shift; print "Sub2 $a\n" },
sub3 => sub { my $a = shift; print "Sub3 $a\n" },
);
&{$subhash{sub1}}("arg1");
&{$subhash{sub2}}("arg22");
&{$subhash{sub3}}("arg333");
my $s = "sub2"
&{$subhash{$s}}("args");
exit;
Produces the following output
Sub1 arg1
Sub2 arg22
Sub3 arg333
Sub2 args
Hope that helps. Check out the "Blue Cammel" page 254.
Thanks.
Graydon Dodson grdodson at lexmark.com
Lexmark International Inc.
----- Begin Included Message -----
More information about the Lexington-pm
mailing list