Hi peeps: I've a site written in Perl and am connecting to a MySQL db using Mysql Perl module. Connection works fine - no probs there. Also using strict and CGI::Carp('fatalsToBrowser');

One table in my db only contains 'widgets' for left-hand and right-hand colums for my pages, called up according to a page_id, the selected language of the visitor, etc...

If there's a subroutine name within the 'sub' db field, I want to call that sub from a previously required library.cgi file, else if there's no subroutine specified within the 'sub' db field, print the data that's in the 'title' and 'text' fields.

Here's the code:

##### Get LHC widgets based on $pid my $dbh = Mysql->connect($DBHOST2, $DBDATA2, $DBUSER2, $DBPASS2); my $qry = qq¿SELECT title,text,sub FROM widgets WHERE page_id = '$ +pid' AND user_lang = '$USER_LANG' AND type = 'lefthandcol' ¿; my $result = $dbh->query($qry); while (my @db = $result->fetchrow){ $LHC_OPTIONS .= ($db[2] ne "") ? &{$db[2]}() : qq¿$db[0]:$db[1 +]<BR>¿ ; }

The data in the 'sub' field is just the name of the routine - ie: build_table - no leading ampersand or trailing brackets.

Even though I'm using strict, I've already soft-referenced things without producing an error (I build my menus from flat-file db's and I use the same &{$array[0]}() structure). However, this code just produces a "Premature end of script headers" error.

Can anyone tell me how I can call a subroutine using the value contained within a database?


In reply to 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.