The eval function with quoted argument traps compile errors. You can examine the error messages in the perlvar $@. No other special magic is needed. This is not true of eval BLOCK.
Update: <shrug>If you want to avoid runtime on your eval string, just wrap it in a conditional:
I used $^C, the compile-time flag, for cli convenience, but some $debug would do as well.$ $ perl -Mstrict -e'$_="printed\n";my$foo=q(print);eval"if(\$^C){$foo}" +;print $@ if $@' -- #no compile or run error $ $ perl -Mstrict -e'$_="printed\n";my$foo=q(quux);eval"if(\$^C){$foo}"; +print $@ if $@' -- # compile error Bareword "quux" not allowed while "strict subs" in use at (eval 1) lin +e 1. $ $ perl -Mstrict -e'$_="printed\n";my$foo=q(&quux);eval"if(\$^C){$foo}" +;print $@ if $@' -- # runtime error $ $ perl -Mstrict -e '$_="printed\n";my$foo=q(print);eval"$foo";print $@ + if $@' -- # conditional removed, no errors printed $
After Compline,
Zaxo
In reply to Re: syntax check from within perl
by Zaxo
in thread syntax check from within perl
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |