Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: How to do perl -c inside perl?

by rockyb (Scribe)
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.

Replies are listed 'Best First'.
Re^2: How to do perl -c inside perl?
by LanX (Bishop) 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 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.


        > 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 }; $hash = { lanx:/tmp$ perl -c /tmp/ Unmatched right curly bracket at /tmp/ line 1, at end of line syntax error at /tmp/ line 1, near "}" Missing right curly or square bracket at /tmp/ line 1, at end of + line syntax error at /tmp/ line 1, at EOF /tmp/ 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

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

        Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992890]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-06-23 22:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.