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


in reply to How to do perl -c inside perl?

However I have found that backtick sometimes isn't supported well enough on Windows, whereas system() more often is.

Que? backticks (and qx//) are both fully supported on windows. What makes you think otherwise?

Eg.:

C:\test>perl -E"say `perl -c -Mstrict -we\"my \$l = 365.25***3\"` " -e syntax OK

Of course, Aok from the syntax check doesn't always mean the code does what you expect or want :)


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

div

Replies are listed 'Best First'.
Re^2: How to do perl -c inside perl? (quoting)
by tye (Sage) on Aug 28, 2012 at 16:44 UTC

    Now pass an arbitrary chunk of Perl code to "perl -e" in a portable manner. Capture both STDOUT and STDERR from it. Not so trivial. I suspect I could manage that in relatively few lines of Perl code, but the complexities there are enough that I doubt I would get it completely right on the first try (and many of the details involved are not things I've actually found to be correctly documented).

    - tye        

      Now pass an arbitrary chunk of Perl code to "perl -e" in a portable manner.

      I was only demonstrating that windows supported backticks, not championing the idea of using them and -e as a useful mechanism.

      Capture both STDOUT and STDERR from it. Not so trivial.

      Capturing stderr also is trivial:

      C:\test>perl -E"say `perl -c -Mstrict -we\"my \$l = 365.25*\" 2>&1` " syntax error at -e line 1, at EOF -e had compilation errors.

      But I would have thought feeding the code to a piped-open and checking the exit code would suffice if all that is required is a yay or nay.

      And if you need to actually capture the error messages produced, then this might do the trick:

      #! perl -slw use strict; use Win32::Socketpair qw[ winopen2_5 ]; my( $pid, $sock ) = winopen2_5( 'perl.exe', '-c' ); print $sock <<'EOP'; my $x = 365.25**3; my $y = qx[ perl -c -e"say 'boo'"; ]; my $z = sub { print $sock <<; my $x = 365.25**3; my $y = qx[ perl -c -e"say 'boo'"; ]; my $z = sub { print $sock <<; my $x = 365.25**3; my $y = qx[ perl -c -e"say 'boo'"; ]; my $z = sub { 1; }; }; }; EOP shutdown $sock, 1; print while <$sock>; close $sock; ( $pid, $sock ) = winopen2_5( 'perl.exe', q[-E"say 'hello'; warn; die; +"] ); shutdown $sock, 1; print while <$sock>; close $sock; __END__ C:\test>winopen2_5 - syntax OK Warning: something's wrong at -e line 1. Died at -e line 1. hello

      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

      /iblockquote
Re^2: How to do perl -c inside perl?
by rockyb (Scribe) on Aug 28, 2012 at 16:16 UTC

    I stand corrected. Checking from Devel::Trepan test t/10test-condition.t, it is fork() followed by exec() that doesn't work on some versions of Strawberry Perl. I believe this is so from past CPANTS failures.

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

      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

        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.