Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by rockyb (Scribe)
on Aug 28, 2012 at 18:29 UTC ( [id://990299]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^5: How to do perl -c inside perl?
by BrowserUk (Patriarch) on Aug 28, 2012 at 18:53 UTC

    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?

        $ help rd Removes (deletes) a directory. RMDIR [/S] [/Q] [drive:]path RD [/S] [/Q] [drive:]path /S Removes all directories and files in the specified directo +ry in addition to the directory itself. Used to remove a dir +ectory tree. /Q Quiet mode, do not ask if ok to remove a directory tree wi +th /S

        See Safe, Safe::World

Re^5: How to do perl -c inside perl?
by rockyb (Scribe) on Aug 29, 2012 at 02:46 UTC

    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.

        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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://990299]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found