How to capture an error with eval?

S

Sean

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
 
G

Gunnar Hjalmarsson

Sean said:
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' };
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top