Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to do perl -c inside perl?

by philiprbrenan (Monk)
on Aug 28, 2012 at 21:22 UTC ( #990337=note: print w/ replies, xml ) Need Help??


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

Please prepend 'exit;', eval the string, then check $@ for errors. exit() prevents syntactically correct perl from being executed.

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); my $i = 'exit(); say "Hello" op'; eval $i; say $@;

Produces

Bareword found where operator expected at (eval 1) line 1, near ""Hello" op"


Comment on Re: How to do perl -c inside perl?
Download Code
Re^2: How to do perl -c inside perl?
by BrowserUk (Pope) on Aug 28, 2012 at 21:41 UTC
    exit() prevents syntactically perl from being executed.

    C:\test>perl -E"eval q[ exit(); BEGIN{ system 'echo oops!' }]" oops!

    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

      I think what was meant is that exit() sometimes can prevent syntactically (correct) code from being executed. It is also true that sometimes it does not. Human language can be frustratingly ambiguous and imprecise: the quoted statement could have implied sometimes but not all, or it could have been intended to mean all.

      One can gain a few Karma points on PerlMonks here and there by finding mistakes, possibly by a imposing a slightly different or more rigourous interpretation, and possibly attached to side points. Where it to be so easy to reach true Nirvana that way.

      What I have been seeking a deeper understanding what is going on inside Perl and perl -c with the hope to coming to a simpler, more reliable means to test whether a string is a Perl statement or expression. After all, eval() has to go through a syntax check its course its existance. If this question has been asked several times before and the best that had been suggested has been perl-c (which is really not that bad or the end of the world), then I think we can do very small bit better by creating a simple Syntax::Check module. That this might be of help to the broader community is more important to me than the PerlMonks Karma points in correcting someone here or there.

      I am also optimistic that there might (one day) be a way to just ask Perl to do syntax check part that it does for eval() without the evaluation part.

      With all of this behind, it has become clear that yet again, I have not fully explained my situtation and the problem I'm trying to solve.

      I really do want to just check whether something is syntactically correct. No evaluation should be done. Therefore solutions with eval() which can have side effects are probably not good. And devilish kinds of expressions and Perl programs probably also have to be properly handled

      So let me make try to again to explain this in a very specific setting without trying generalize. In contrast to the stock Perl debugger perl5db, Devel::Trepan has a front-end program called trepan.pl that does the setup for Perl debugger invocation. This allows one to specify simple debugger options like whether terminal highlighting is desired. But before trepan.pl takes the plunge into exec to reinvoke Perl with the appropriate flags, it checks to see if the eval string or Perl program is valid. That way it can report back a more reasonable and controllable error message.

      There are a couple of other situations where syntax-only checking is needed. Breakpoints can have conditional expressions associated with them that determine whether to respect or ignore the breakpoint. These expression are evaluated in a context that can be totally different from the context inside the debugger where a programmer specifies what to test for. So an eval inside the debugger of say $a may result in an unbound variable, whereas at the point a breakpoint occurs that variable may be defined and have a value. Similarly, one can ask the debugger to run specific Perl statements in the context of the current stopping point of the program. In gdb terms these are called display expressions. In perl5db, they are called actions.

        I'm not sure that I see your use case as warranting the check -- I doubt you can report the error any better than perl could when it runs it -- but may be I'm just not getting the full picture.

        Historically, the response to the question has been: not safely; but maybe you can do it using Safe which has already been mentioned; or may be you need the expertise and weight of PPI.

        If your use case is only ever going to accept strings from a programmer via the command line -- where he could easily just type whatever bad stuff he might give you directly into the command line, or perl, then you perhaps don't need to worry about it. Your call.

        But there has been someone (may be you?) asking about accepting code via a web-server and then running it, hence my caution.


        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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-12-18 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (59 votes), past polls