How to capture an error with eval?

Discussion in 'Perl' started by Sean, Jul 7, 2004.

  1. Sean

    Sean Guest

    I know "eval" and "if ($@)" is the equivalent of a try/catch in the C
    programming world. My question is how to basically throw an error in
    one perl script that can be caught by another? For example:

    ScriptA.pl:
    -----------
    typical perl code but when we are done, do we exit, die, or warn so
    that ScriptB.pl can pick it up? EX:

    ScriptB.pl
    -----------
    eval {
    `ScriptA.pl`
    };

    if ($@) {
    print "Errors occured: $@";
    } else {
    print "ScriptA ran perfectly!";
    }

    The question is how to force an error/exit code in ScriptA so that
    ScriptB picks it up? I keep getting everything ran ok because
    technically ScriptA "can run"... it's just that I can't throw an error
    up a level. Thanks all!

    Sean
     
    Sean, Jul 7, 2004
    #1
    1. Advertising

  2. Sean wrote:
    > I know "eval" and "if ($@)" is the equivalent of a try/catch in the
    > C programming world. My question is how to basically throw an
    > error in one perl script that can be caught by another? For
    > example:
    >
    > ScriptA.pl:
    > -----------
    > typical perl code but when we are done, do we exit, die, or warn so
    > that ScriptB.pl can pick it up? EX:
    >
    > ScriptB.pl
    > -----------
    > eval {
    > `ScriptA.pl`
    > };
    >
    > if ($@) {
    > print "Errors occured: $@";
    > } else {
    > print "ScriptA ran perfectly!";
    > }


    Use require() instead of backticks.

    eval { require 'ScriptA.pl' };

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. patoban
    Replies:
    0
    Views:
    406
    patoban
    Jul 9, 2004
  2. Eric Newton
    Replies:
    3
    Views:
    9,495
    Brock Allen
    Apr 4, 2005
  3. Max
    Replies:
    7
    Views:
    9,157
  4. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    564
    Karl Seguin [MVP]
    Jun 16, 2006
  5. Stephen Waits

    How to capture eval's stdout in 1.8?

    Stephen Waits, Mar 7, 2006, in forum: Ruby
    Replies:
    3
    Views:
    202
    Stephen Waits
    Mar 7, 2006
Loading...

Share This Page