Re: Subroutines and '&'



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
.



Relevant Pages

  • Re: Subroutines and &
    ... no strict; ... # else execute this sub ... misleading. ... You are making sure that $MODE points to a sub, ...
    (comp.lang.perl.misc)
  • Executing an external program.
    ... I was hoping the program below would execute a simple perl script ... use strict; ... sub do_back_up { ...
    (comp.lang.perl.tk)
  • Re: Access module does not continue past the Filter Method
    ... Execute the procedure, and single-step through it with F8 so you can trace ... Public Sub Apply_Tech_Filter ... Debug.Print "Past the Filter" ... Debug.Print "Before the Append Statement" ...
    (microsoft.public.access.modulesdaovba)
  • Re: new in CGI::Session::Driver::postgredsql nonexisting
    ... i would suggest you turn on strict and warnings in this example.. ... or die "cannot prepare"; ... use CGI ':standard'; ... sub makeHTML ...
    (comp.lang.perl.misc)
  • Re: Error:Invalid procedure call or argument
    ... > Number 0 everytiime I execute this code: ... > Private Sub Update() ... > On Error GoTo ErrHand ... > Following is an example of the completed SQL string. ...
    (microsoft.public.access.formscoding)