Hello Joe,

> i have the following code:
> 
>       $sth = $mysql_dbh->prepare("select subroutine_pointer from
> $database.equipment_manufacturer where
> manufacturer=\"$remedy_eqpt_mfgr\"");
>       $sth->execute();
>       $subroutine_pointer = $sth->fetchrow_array();
>       no strict "refs";
>       &$subroutine_pointer() unless $subroutine_pointer eq "";
>       use strict "refs";
> 
> this pulls a phrase from the database that matches a subroutine name
> that i want to call based on certain other criteria, then 
> displays that
> sub's output to a web page.  this works fine as is but i want to learn
> how to restructure this so i don't have to use "no strict" and "use
> strict" around the call.  all suggestions welcome *s*

Most problems of "using a variable named the same as this string I get from
the user / database" or "using a sub named the same as a string I get from
the user / database" can be solved using a hash. In your case, if you have
this:

sub this_is_a_name {
    # ... blah blah ...
}

sub this_is_another_name {
    # ... more blah blah ...
}

with the above code that will call either of these two subs based on which
sub's name was gotten from the database, you could do this :

# Create a hash that contains refs to your subs, where the key for each one
# is named the same as the sub (the string you get from the database).
my %all_subs = (
    this_is_a_name       => \&this_is_a_name,
    this_is_another_name => \&this_is_another_name,
);

my $string_from_database = ........   # get your string as usual.
# Call the sub if there is a key named the same as the string.
&all_subs{$string_from_database}() unless $string_from_database eq "";

That is strict-clean, and would enable you to do what you want. Check the
syntax of the subroutine call (last line above) from a ref in the perlref
manpage, I'm not sure that's exactly correct but it should be close.

Hope this helps,

J-S

-- 
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