SPUG: Subroutine references with "strict refs"

ced at carios2.ca.boeing.com ced at carios2.ca.boeing.com
Fri Oct 13 17:15:35 CDT 2000


> I am attempting to call a subroutine with the same name as the "action" 
> parameter.  Here is the snip of code:


>      my $action = $in{action}; # $in{action} holds the value of the 
> "action" parameter from the form.

>      eval{ &$action; }; # error trap
>      if ( ! $@ ){
>          &$action;
>      }
>          else {
>             # do something with the error
>          }

> This code works perfectly if I don't use strict; at the beginning of my 
> code, but if I use strict; I get this error:

> Error: Can't use string ("remove") as a subroutine ref while "strict refs" 
> in use at ....


> Any ideas?  The only other way I come up with is using a "Dispatch table" 
> hash of parameter->references-to-subroutines (as described on p 54 of 
> O'Reilly's Advanced Perl Programing), but I'd rather not do that if possible.

There's much to be said for the dispatch table, but if you
don't mind a slower runtime eval and a bit of do-it-yourself
laundering, this'll pass strict:

#-- launder the hidden param; ** See perldoc perlsec **
#-- explicitly allow only sub names

unless ($action =~ /^(?:sub1|sub2|sub3|sub4)$/ ) { 
   die "die evil sub passage....";
}
eval "&$action";
if ( $@ ) {
   # do something with the error
}


hth,
--
Charles DeRykus

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list