Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: how to check syntax of code inside a scalar?

by Crackers2 (Parson)
on Aug 04, 2010 at 21:04 UTC ( #852971=note: print w/replies, xml ) Need Help??


in reply to Re^2: how to check syntax of code inside a scalar?
in thread how to check syntax of code inside a scalar?

Bad idea... try it with this:

syntax_ok(qq{BEGIN { print "yay\n"; }});

and you'll see that the BEGIN block still gets run, bypassing your return statement. So I'd consider it rather unsafe.

Replies are listed 'Best First'.
Re^4: how to check syntax of code inside a scalar?
by snoopy (Deacon) on Aug 04, 2010 at 21:55 UTC
    This does however seem to match the perl -c syntax check. From perlrun:
           -c   causes Perl to check the syntax of the program and then exit
                without executing it.  Actually, it will execute "BEGIN",
                "UNITCHECK", "CHECK", and "use" blocks, because these are
                considered as occurring outside the execution of your program.
                "INIT" and "END" blocks, however, will be skipped.
    
    perl -c -e 'BEGIN { print "yay\n"; }; print "boo\n"'
    yay -e syntax OK
      What if you always inject a
      BEGIN {exit}
      will it still do the compile step?
      the hardest line to type correctly is: stty erase ^H
        I't wouldn't do the compile step, but because this happens early, it would pretty well bypass any syntax checking whatsoever:
        perl -c -e 'BEGIN{exit}; BEGIN{print "yay\n"};@&$^^#@!.' -e syntax OK

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://852971]
help
Chatterbox?
[Eily]: I just wrote a oneliner to solve a (non perl) colleague's problem, and it worked on the first try
[Eily]: and he wasn't impressed
1nickt adds gingersnaps to the platter on the sideboard.
[Eily]: sure it was a pretty standard one liner (of the $h{$1} = $2 if /regex/; END { pp \%h; } variety), but he's not supposed to know that!
[Eily]: what's the point of writing perl if people aren't impressed :P
[sierpinski]: Haha
[Discipulus]: i'm with you!
[Discipulus]: with eskimo greeting is a little bit impressing, anyway

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2017-04-26 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (482 votes). Check out past polls.