Re: Subroutines and '&'
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxx
- Date: 8 Aug 2007 13:25:46 GMT
Paul Lalli <mritty@xxxxxxxxx> wrote in comp.lang.perl.misc:
On Aug 8, 6:50 am, anno4...@xxxxxxxxxxxxxxxxxxxxxx wrote:
<tru...@xxxxxxxxx> wrote in comp.lang.perl.misc:
my $MODE=param('Mode');
{
no strict;
$MODE="GetLogin" unless $MODE; # default sub to execute
&$MODE; # else execute this sub
}
This saves a lot of 'if-else' chains. Just my 2 Canadian cents.
I don't see how it saves an "if". The "else..." comment is
misleading.
You are making sure that $MODE points to a sub, and *then*
(not "else") you're executing it.
I'm assuming he meant it prevents the need for something like:
if ($MODE eq 'Foo') {
Foo();
} elsif ($MODE eq 'Bar') {
Bar();
} else {
GetLogin();
}
Really, he's talking about Symrefs here, which is not at all the same
issue as using an & or not on a subroutine.
Right, that makes sense. It would still be preferable to use a dispatch
table (untested):
my %func = map {
no strict 'refs';
( $_ => \ &{ $_ });
} qw( Foo Bar Baz);
my $mode = param( 'Mode');
( $func{ $mode} || \ &GetLogin)->()
That way the symrefs are restricted to the table setup where they will
only be used with the code-determined qw( Foo Bar Baz). Whatever
value $mode has at run time, only these functions or GetLogin will
be called. The original code could call anything an attacker happens
to know about.
Anno
.
- References:
- Subroutines and '&'
- From: Bill H
- Re: Subroutines and '&'
- From: trudge
- Re: Subroutines and '&'
- From: anno4000
- Re: Subroutines and '&'
- From: Paul Lalli
- Subroutines and '&'
- Prev by Date: Re: Matching problem
- Next by Date: Re: windows perl modules
- Previous by thread: Re: Subroutines and '&'
- Next by thread: Re: Subroutines and '&'
- Index(es):
Relevant Pages
|