The only way I'm aware of to compile but not execute code in a variable is to wrap it in an anonymous subroutine block:
my $goodcode = <<'CODE';
print "Hello, how are you?\n"
CODE
my $badcode = <<'CODE';
pront "Hello, how are you\n?'
CODE
{
no strict;
local $^W = 0;
eval "sub {\n$goodcode\n}";
}
print "Goodcode error: $@\n" if $@;
{
no strict;
local $^W = 0;
eval "sub {\n$badcode\n}";
}
print "Badcode error: $@\n" if $@;
You can get more elaborate if you like, writing your own warning handler to capture them instead of printing them. It's late and I shouldn't be posting.
Oh, and the newlines are in there in case you have comments somewhere in your code string. I did once, and it commented out the ending brace of the sub. Beware.
Update: If you want to suppress or log warnings, you can get rid of the 'no strict' and 'local' lines, and use a variant of this instead:
local $SIG{__WARN__} = sub {};
It'll create and throw away a new anonymous sub (unless eval() checks for void context -- I dunno), but it'll do the compile-but-not-execute waltz. |