in reply to Executing code stored in a database..

Perl code can be executed using eval.
my $script = get_from_database(); eval "$script; 1" or die $@;

Replies are listed 'Best First'.
Re^2: Executing code stored in a database..
by Anonymous Monk on Dec 07, 2007 at 01:45 UTC
    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;