Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to do perl -c inside perl?

by rockyb (Beadle)
on Sep 11, 2012 at 01:15 UTC ( #992890=note: print w/ replies, xml ) Need Help??


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

The solution that satisfies the most is to wrap the code I want to try inside a eval "sub {$code}".

Starting with commit 0067b6c, I have been using this approach and have been most pleased with it. It will catch things like prototype mismatches which is not strictly a syntax checking thing. That may be good or bad depending on your needs.

This code: $x=1; __END__ $y= will be tagged as a syntax error when it is not. But this is not of great concern to me.


Comment on Re: How to do perl -c inside perl?
Download Code
Re^2: How to do perl -c inside perl?
by LanX (Canon) on Sep 11, 2012 at 11:16 UTC
    Thats more or less the approach I use in my patched perldb ("IPL") to automatically check for multiline input.

    Is that your use case?

    Sorry I thought you're looking for something more elaborate... :)

    Cheers Rolf

      There were a couple of use cases of which this is one.

      Another use is where you want to write a front-end to call a debugger, profiler, code coverage tool, or anything else that adds some sort of instrumentation (and thus, modules and options to Perl) before runnining it. In this situation, one may want to do a syntax check first before taking the plunge. So being able to correctly handle:

      __END__ $x =
      does matter.

      But in the situation inside the debugger where one needs to be able to check for valid expressions or statements, wrapping inside a sub{ .. } is useful.

      I hadn't thought about using this inside the debugger eval command to automatically figure out if a continuation is needed. So I will probably add that. Thanks!

      It can also be used as a more fool-proof way to determine a source-code line that is shown is complete over the method currently in use by perl5db.pl which is confused by:

      eval " # not a comment ";
      which thinks that the line inside the eval is a comment.

      If "the more or less" code you use is interesting and useful, please expand upon that. Also, I don't understand what you mean by thinking of something "more elaborate". All I was looking for was something to get the job done. If it is interesting, elaborate on what you mean by "more elaborate".

      It's only by having people work out various approaches that we've been able to weight the pros and cons of each and thus understand what the right thing is for particular situations. Thanks.

        Hi

        > Also, I don't understand what you mean by thinking of something "more elaborate"

        well for instance, you won't catch all possible syntax errors:

        lanx:/tmp$ cat tst.pl }; $hash = { lanx:/tmp$ perl -c /tmp/tst.pl Unmatched right curly bracket at /tmp/tst.pl line 1, at end of line syntax error at /tmp/tst.pl line 1, near "}" Missing right curly or square bracket at /tmp/tst.pl line 1, at end of + line syntax error at /tmp/tst.pl line 1, at EOF /tmp/tst.pl had compilation errors. lanx:/tmp$ perl $code = '}; $hash = {'; eval "sub { $code } "; print $@; __END__ lanx:/tmp$ perl $code = '}; $hash = '; eval "sub { $code } "; print $@; __END__ Unmatched right curly bracket at (eval 1) line 1, at end of line syntax error at (eval 1) line 1, at EOF

        you might be interested in my slides from YAPC::EU

        http://act.yapc.eu/ye2012/talk/4204

        unfortunately ATM I have no time to elaborate deeper... :(

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2014-12-29 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (193 votes), past polls