On Mon, 2006-03-04 at 15:41 -0500, The Ghost wrote:
> based upon the string in a variable, I want to run a particular  
> subroutine:
> 
> my $var='cat';
> ....
> ....
> ....
> $var='fish';
> 
> &$var; # I want to run fish if $var is a fish or cat if $var is a cat...
> 
> sub cat { .... };
> sub dog { .... };
> ....
> sub fish { .... };
> 
> For some reason I think I'll be told this is a bad idea.  So what are  
> my options?
> 
> Ryan
> 

Yes, it is a bad idea. If $var comes from user input, it is tainted (see
`perldoc perlsec` for details). That means the user can run *ANY*
subroutine. If, for example, you had a subroutine that create SQL Select
statements, the user could abduct it to read any table in your database.
Not good.

You could do as Tom Phoenix suggested and create a dispatch hash. Or you
could do this:

  if( $var eq 'cat' ){
    cat( @some_args );
  }elsif( $var eq 'dog' ){
    dog( @other_args );
    pooper_scooper();
  }else{
    die "unknown life-form $var\n";
  }

This would allow you to send arguments to the subroutines. It also
allows you to call more than one subroutine for a single $var.


-- 
__END__

Just my 0.00000002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to