jbinaustin has asked for the wisdom of the Perl Monks concerning the following question:

I am not sure I have a problem as much as a question as to if it can be done. I have created an Apache/Mod_perl2/MySQL group of web modules that act as a framework for xml, xhtml, css, Ajax, etc. presentation of infrastructure and service related components. The framework is proving to be highly scalable and I would like to move other cgi presentation tools into the same framework and get out of the uniquely customer cgi’s for every solution. Scalability of management etc. Here is what I am trying to do…. I have an application interface /script which accepts a URI something like ?script=foo (browser initiated). The application then does some validation of the GET request and passes foo to the persistent database connection (within the handler) to retrieve the script from the database. My question is “within the same GET request, is it possible for the handler to take the perlscript foo, execute it, and pass the results back to the handler?”. I don’t have a problem getting the script but not sure how to actually execute the code. Anyone have any shareable experience on how to do this? Thanks in advance. JB

Replies are listed 'Best First'.
Re: Executing code stored in a database..
by ikegami (Patriarch) on Dec 06, 2007 at 22:36 UTC
    Perl code can be executed using eval.
    my $script = get_from_database(); eval "$script; 1" or die $@;
      I did try this and it did not work..
      my $sql = "SELECT script FROM scripts where name='$ref->{name}'" +; my $script; my $sth = $dbh->prepare("$sql"); if ($sth->execute) { while(my $row_hash = $sth->fetchrow_hashref) { $script = "$row_hash->{script}"; + } $post .= "$script"; $post .= eval "$script; 1" or warn $@; $sth->finish(); } ################################################ $r->puts(<<"END"); $post END

        Don't insert plain text into a SQL statement!!! Escape properly or use placeholders.

        my $sql = "SELECT script FROM scripts where name=?"; my $sth = $dbh->prepare($sql); $sth->execute($ref->{name});

        What's with putting everything in quotes?

        "$sql" -> $sql "$row_hash->{script}" -> $row_hash->{script} "$script" -> $script

        Or a here-doc?

        $r->puts(<<"END"); \ $post > $->puts($post) END /

        Why loop when you only want one variable?

        my $row_hash = $sth->fetchrow_hashref() or die(...); $script = $row_hash->{script};

        Finally, "doesn't work" is a very useless problem description. Please provide more details.

        ...although returning one probably won't help if you use it that way.

        my $result = eval $script; defined($result) or die(...); $post .= $result;
Re: Executing code stored in a database..
by wfsp (Abbot) on Dec 07, 2007 at 13:08 UTC
    I think I'd consider doing it the other way around.

    • customer instance script
    • customer specific application module (where the foo script would be)
    • base module (sessions, dbi, templates, logging, "presentation of infrastructure" etc.
    I'm sure I'd find the 'code in a db' method hard to debug, test and maintiain.

    shudder :-)

Re: Executing code stored in a database..
by aquarium (Curate) on Dec 06, 2007 at 22:35 UTC
    not sure what you mean by "within the same GET request".
    you can either do a new GET if the script is still available via HTTP or read the script into a variable (via standard filesystem open/read) and "eval" the variable.
    the hardest line to type correctly is: stty erase ^H
      I have tried eval, system, exec, putting the script to a file and calling the file etc.. Nothing seems to work.. Here is the mod_perl2 module which is loaded into Apache at startup.. The URL I would send in this example is http://someserver/dashboardscripts?name=loop
      package DashboardScripts::DashboardScripts; # File: DashboardScripts/DashboardScripts.pm use strict; use Apache2::RequestRec (); # for $r->content_type use Apache2::RequestIO (); # for $r->puts use Apache2::Const -compile => qw(OK DECLINED M_GET HTTP_METHOD_NOT_AL +LOWED); BEGIN { $| } use Data::Dumper; sub handler { my $r = shift; my $ref; my $post; #my $host = $r->get_remote_host; unless ($r->method_number == Apache2::Const::M_GET) { $r->allowed($r->allowed | (1<<Apache2::Const::M_GET)); return Apache2::Const::DECLINED; } $r->content_type('text/html'); map { $_ =~ s/\+/ /g; my ($key, $val) = split(/=/,$_,2); foreach ($key, $val) {$_ =~ s/%([A-Fa-f0-9]{2})/pack("c",h +ex($1))/ge}; $$ref{$key} .= (defined($ref->{$key})) ? "\0$val" : $val; } split/[&;]/,$r->args(); #mysql> desc defaultscripts; #+----------+-------------+------+-----+---------+-------+ #| Field | Type | Null | Key | Default | Extra | #+----------+-------------+------+-----+---------+-------+ #| name | varchar(25) | NO | PRI | | | #| script | text | YES | | NULL | | #+----------+-------------+------+-----+---------+-------+ #2 rows in set (0.01 sec) exit Apache2::Const::DECLINED if ($ref->{name} eq ""); my @Raw_DB_Data; my $dbh = DBI->connect("DBI:mysql:dashboards", 'id', 'passwd', { PrintError => 0, # warn( ) on errors RaiseError => 0, # don't die on error AutoCommit => 1, # commit executes immedi +ately } ); my $sql = "SELECT script FROM scripts where name='$ref->{name}'" +; my $script; my $sth = $dbh->prepare("$sql"); if ($sth->execute) { while(my $row_hash = $sth->fetchrow_hashref) { $script = "$row_hash->{script}"; + } $post .= "$script"; $post .= eval "$script; 1" or warn $@; $sth->finish(); } ################################################ $r->puts(<<"END"); $post END undef $post; undef $r; undef $sth; undef $dbh; return Apache2::Const::OK; } END { } 1;
      When I pass the URL, I want it to parse the args (works) go to the database and get the perlscript, execute the script and output back to the module to display the results. Sounds simple enough however, it simply is not working.. I have two examples in the database.
      #!c:/perl/bin/perl.exe use strict; for(my $x=0; $x<=100000;$x++){ print "$x<br>"; }
      and
      for(my $x=0; $x<=100000;$x++){ print "$x<br>"; }
      The above prints the script that it got back from the db just fine. But only returns the 1 after. Ideas on what I might be doing wrong here. Thanks
        I figured it out.. while executing the eval of the code referenced in the variable, the code snip was doing the print and returning that back to the handler, thus breaking the loop.. DOH.. as soon as I changed it to:
        my $thing; for(my $x=0; $x<=100000;$x++){ $thing .+ "$x<br>"; } print $thing;
        It worked just fine..