Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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

by rockyb (Scribe)
on Aug 29, 2012 at 02:46 UTC ( #990373=note: print w/replies, xml ) Need Help??

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

Ok. I think I have gotten to the heart of it. And like other things of this ilk, it looks to me like a bug or misfeature.

I would have thought this would work:

package B::Syntax; sub compile { return sub { $O::BEGIN_output =~ /Syntax OK/ ? exit 0 : exit 1; } }; 1;

And then call with:

# $prog_or_e_option = ... ; system("perl -MO=-qq,Syntax $prog_or_e_option"); # Test $?

Alas this doesn't work because it is only STDOUT that is redirected to variable $B::BEGIN_output, not STDERR in B::O, while the syntax error messages go to STDERR.

I guess I will have to seek guidance from perl5-porters.

And if one journeys on that road, perhaps one can go all the way and figure out how to use or require B/ inside Perl.

Replies are listed 'Best First'.
Re^6: How to do perl -c inside perl?
by rockyb (Scribe) on Aug 29, 2012 at 06:51 UTC

    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 

    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.

      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990373]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2018-02-20 16:46 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (272 votes). Check out past polls.