If you have a collection of subroutine names stored in a database, and you want a query from the database to trigger a call to a particular subroutine, I would do it this way:
# define a hash of sub refs, keyed by sub name my %dbsub = ( foo => \&foo, bar => \&bar, baz => \&baz, ); # now run your query, and then: while ( my @db = $result->fetchrow ) { $LHC_OPTIONS .= ( exists( $dbsub{$db[2]} )) ? $dbsub{$db[2]}->() : join(':', @db[0,1]).'<BR>'; }
Somehow it just feels safer to me, knowning that I'll only attempt a subroutine call when I know the sub is really there.

Update: on second thought, I would actually prefer the while loop to do something like this:

while ( my @db = $result->fetchrow ) { if ( exists( $dbsub{$db[2]} )) { $LHC_OPTIONS .= $dbsub{$db[2]}->(); } else { warn "Unknown sub name in DB: $db[2]\n" if ( $db[2] ); $LHC_OPTIONS .= join(':', @db[0,1]).'<BR>'; } }
The point being, I'd like to know when the database contains something unexpected in the "sub" field. (Of course, since you seem to be running this as a CGI script, you'll either want to make a habit of scanning the web server's error log, or else you'll want this warning to show up some place where you'll be sure to see it.)

In reply to Re: Perl/MySQL - calling sub from name in dbase by graff
in thread Perl/MySQL - calling sub from name in dbase by meetn2veg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.