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

I have a situation where I have Perl code contained in strings that will be stored in a db and then later retrieved and eval'd.
They problem is that I would like to verify the syntax in the strings before I store them but I don't want to just eval the strings and ignore the results because of possible side effects.
Is there a clever way to simply check the syntax of a string? I had thought of wrapping the string in an anonymous subroutine definition, setting up the needed context and evaling the definition, assuming that if the function is built correctly the code inside is good.Is there a better way?

Any thoughts?

And yes, the strings are going to be eval'd in a Safe sandbox when finally used.

-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
•Re: Syntax Checking Perl Code in Strings
by merlyn (Sage) on Oct 13, 2002 at 06:18 UTC
    You can't compile a string without possibly executing some part of it, thanks to BEGIN blocks.

    If you're willing to live with that, the question has been asked and answered here before, with varying degrees of success.

    -- Randal L. Schwartz, Perl hacker

Re: Syntax Checking Perl Code in Strings
by dws (Chancellor) on Oct 13, 2002 at 04:08 UTC
    They problem is that I would like to verify the syntax in the strings before I store them but I don't want to just eval the strings and ignore the results because of possible side effects.

    Write each string to a temporary file and then invoke "perl -cw" on it?