in reply to Re^5: How to do perl -c inside perl?
in thread How to do perl -c inside perl?

This works:

package Syntax; our $VERSION = '1.00'; use B qw(minus_c save_BEGINs); sub import { eval q[ BEGIN { minus_c; save_BEGINs; close STDERR; open (STDERR, ">", \$Syntax::stderr); } ]; die $@ if $@; } 1;

And invoke like this:

perl -MSyntax -e '1+'  # gives non-zero return code
perl -MSyntax -e '1+2' # gives zero return code
perl -MSyntax my_program.pl 

When there is an error, that is captured in $Syntax::stderr above. The module could be extended to allow one to pass in a file name to write when there is an error.

A still open question is to remove the "perl" invocation and have available $Syntax::stderr. do looks close, but is still a ways off.

I may try to fill out the above and make a perl Module out of it.

Replies are listed 'Best First'.
Re^7: How to do perl -c inside perl?
by rockyb (Scribe) on Aug 31, 2012 at 01:58 UTC

    I've now dotted all the i's and crossed all the t's to make this a full-fledged package with tests and pod.

    That said, I am a little disappointed in the benefit of this over say system("perl -c $program 2>/dev/null"). Presumably it might work on more OS's.

    The main benefit I see putting this out there is for pedagogical purposes or for using this as a spring-board to do more — like avoiding the perl call altogether.

    Sigh. Sometimes life is like that. Better luck, next lifetime.