in reply to Re: Executing code stored in a database..
in thread Executing code stored in a database..

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

Replies are listed 'Best First'.
Re^3: Executing code stored in a database..
by jbinaustin (Initiate) on Dec 07, 2007 at 03:07 UTC
    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..