Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Run in eval and out of eval

by wrc (Initiate)
on Apr 13, 2012 at 12:10 UTC ( [id://964905]=perlquestion: print w/replies, xml ) Need Help??

wrc has asked for the wisdom of the Perl Monks concerning the following question:

How can a perl object know when it is running in an eval compared to running outside an eval? So an object which is running something that generates a huge log. When the exception is thrown. it send a message and the catch will dump the log. eval{ $obj->run() };if ($@) { $obj->dump(); } And this is the way to run the object. But some people won't read the doc and simply run: $obj->run(); and expect the script to die with a log dump if it fails. Other than a $obj->set_verbose(); is there a way to determine if the object is running under an eval?

Replies are listed 'Best First'.
Re: Run in eval and out of eval
by eyepopslikeamosquito (Archbishop) on Apr 13, 2012 at 12:35 UTC

    I'm not convinced checking whether you are in an eval block or not is the best way to solve your problem. Normally you throw an exception when the library code does not know the best way to continue ("Abort, Retry, Ignore?" ... "Dump Object"? :). If you are certain you always want an object dump when you hit an exceptional condition, why not just code it that way inside your object?

    As for determining whether you are in an eval, see Determining depth of eval nesting.

Re: Run in eval and out of eval
by JavaFan (Canon) on Apr 13, 2012 at 12:46 UTC
    $ perl -wE 'say $^S; eval {say $^S}' 0 1 $
    Grepping for eval in man perlvar would have gotten you the answer.
Re: Run in eval and out of eval
by cdybedahl (Acolyte) on Apr 13, 2012 at 12:27 UTC
    caller() looks different in eval().
Re: Run in eval and out of eval
by wrc (Initiate) on Apr 13, 2012 at 22:57 UTC
    Thank You for the replies. I have to agree with the first comment in that this is wrong to do. The person who this is for was being very difficult and what he was asking for was just not right. He is not going to get it. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://964905]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found