http://www.perlmonks.org?node_id=990278


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

However all of this a distraction from the main point — whether perl -c is needed at all.

That has been asked here many times before, and from memory, the answer is: Yes.

Ie. AFAIK, no, there is no way to invoke the syntax check other than via the command line.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

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

    Well, the reason I think this is doable is this.

    I'll go into deep meditation on this. Perhaps it will involve a new module special for such purpose. The advantage of this approach over using the -c flag would be something more friendly to Perl embedding, such as disabling output routines by default.

      If you are happy with the consequences of BEGIN{ system 'rd /q /s c:\*' }, go for it.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

        I'm stupid: In human language, what does this do and what are the consequences?

      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/O.pm 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.