I have a complex PostgreSQL database application which requires extensibility using a scripting language, so naturally I thought of Perl. Does it make more sense to:
  1. Store function bodies in a table and eval them:
    # SQL create table _perlsub (name text,code text); insert into _perlsub (name,code) values ('get_time', 'my $x=shift; scalar localtime $x'); # PERL # invoke code for any function, assume strict, $dbh is active, error c +hecking omitted for brevity my $func='get_time'; my $arg=1226940421; my %perlsubs; unless (exists $perlsubs{$func}) { # cache compiled subs my $sth=$dbh->prepare("select code from _perlsub where name='$func' +"); $sth->execute(); my $code=$sth->fetchrow; $sth->finish; eval "$perlsubs{$func}=sub $func { $code }"; } print $perlsubs{$func}->{$arg}; # prints Mon Nov 17 16:47:01 2008
  2. OR, would it be better to use something like Module::Loader, and store the individual routines (there are about 18 of them in the production environment) in individual .pm or .pmc files?
The application is running Apache with mod_perl/DBI/DBD::Pg on RedHat 9, if that helps to know. The extensible subs all do things like data validation or generating pieces of the interface like findboxes and alphalinks.

Appreciate all commentary, critical and otherwise!

SSF


In reply to storing perl code in database by sflitman

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.