[Israel.pm] Access to "-c" capabilities from within a Perl script

Gaal Yahas gaal at forum2.org
Mon Oct 25 09:39:01 PDT 2004


On Mon, Oct 25, 2004 at 02:53:57PM +0200, Offer Kaye wrote:
> "perl -c script.pl" will compile script.pl and let me know if the
> syntax is okay or if there are any problems.
> 
> Is there a way to access this capability from within a Perl script, to
> try it on a piece of code in a string? E.g.:
>    use Syntax::Check;
>    syntax_check($code) or die "That code sucks big-time!\n";

I don't know of a good solution for any arbitrary code, but for some
subset of the problem you can get away with something like this:


    package Syntax::Check;

    my $i;
    sub syntax_check {
        my($code) = @_;
        $i++;
        return eval <<__EO_CHECK__;
            package Syntax::Check::Blah::ns$i;
            sub blah {
                $code
            }
            1;
    __EO_CHECK__
    }

    1;

Obviously, this is too fragile for general use.

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list