$code .= ";\n1;"; # ... ? my $perl = qx/ which perl /; my $result = qx/ perl -c $code /; # CAN this work? If so, how? I'm a s +hell-tard.

Yes, it can. The my $perl = qx/ which perl / is somewhat pointless, since your are already running perl, so how you've invoked it is in $^X.

my $result = qx/perl -c -e '$code' 2>&1/; # bundle STDOUT and STDERR

should give you either

-e syntax OK

or any error which you can then examine. For simple snippets without quotes, that is! Otherwise you'd grab File::Temp, write a tempfile and examine

my $result = qx/perl -c $tempfile 2>&1/;

update:

WRT evaling - you could switch to some other package before the eval

package My::Eval::Evil; eval $code; if ($@) { .... } package main; # or whatever package you've been in

and then throw away the symbol tables for My::Eval::Evil and the eval'ed code's package. That reduces the risk of name space pollution a bit. But the eval'ed code could still reach into your package (via examining %INC or writing into *main::), so the safest (for some value of safety) is writing a tempfile and shell out a brand new perl.

perl -e 'sub foo {print "foo\n" } package Foo; my $code = "*main::foo += sub {print \"bar\\n\" }"; eval $code; package main; foo()' bar

In reply to Re: How to do a "perl -c(w)" check instead of an "eval $string" on a code string by shmem
in thread How to do a "perl -c(w)" check instead of an "eval $string" on a code string by Your Mother

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.