Search code examples
mysqlperleval

Can my Perl program execute code stored in a database?


Is it possible to save some Perl code in a database, retrieve it using a select statement, and execute that Perl code?

I have tried using eval, but that doesn't seem to work.

Here is what I'm trying right now and it doesn't seem to work:

my $temp = $qryResults[0];
print $temp . "\n";
eval{"$temp"};

The output is $con->Disconnect();exit;


Solution

  • You just need:

    eval $temp;
    

    The reason your version didn't work was due to the block form of eval evaluating it as if you had written a simple string:

    eval{"perl code here"}
    

    It is like writing this line of Perl:

    "Perl code here"
    

    It isn't code; it's a string.

    The block form evaluates what is inside the block. If a string is inside the block, it's just a string, not a script.

    The string form evaluates what is inside the string.