How to handle modules error?

S

SaltyBall

Hi,

I am new to perl, I have write a simple "while loop" to call a perl
modules to do loop something, sometimes the modules generate error and
it exit the script completely.

How can I collect the error message and run the modules again if error
occur?

to summarize:

(current situation)
while something;
{ call the modules (it breaks when error occur) ; }

(improvement)
while something;
{call the modules;
=>if the modules fail, call it again;
}
 
P

pyh

Did you try calling 'eval'? you could do the action in an eval block,when
the eval executed false,it store the failed reasons in $@ .then you could
catch the $@ and re-run the actions.
 
S

SaltyBall

Thanks very much!
I have find the following about eval from google, I will try it later.
-----------------------------------------------------------------------
Perl's Built-In Exception Handling Mechanism

Perl has a built-in exception handling mechanism, a.k.a the eval {}
block. It is implemented by wrapping the code that needs to be executed
around an eval block and the $@ variable is checked to see if an
exception occurred. The typical syntax is:

eval {
...
};
if ($@) {
errorHandler($@);
}

Within the eval block, if there is a syntax error or runtime error, or a
die statement is executed, then an undefined value is returned by eval,
and $@ is set to the error message. If there was no error, then $@ is
guaranteed to be a null string.
----------------------------------------------------------------------
 
B

Bart Van der Donck

SaltyBall said:
I am new to perl, I have write a simple "while loop" to call a perl
modules to do loop something, sometimes the modules generate error and
it exit the script completely.

How can I collect the error message and run the modules again if error
occur?

This demonstrates the principle:

#!perl
use strict;
use warnings;
eval ('use myModule;');
if ($@) {
print 'module not loaded';
# reload here
}
else {
print 'module loaded';
}

You can keep trying to load the module again and again (same syntax as
above), but watch out for endless loops obviously. $@ holds the error
message (if any) from your most recent eval-call.

One thought about your design: if the module fails to load the first
time, why would it not fail the second time then ? Are you loading it
over network/internet or so, and not trusting the connection ?

Hope this helps,
 
C

Ch Lamprecht

SaltyBall said:
Thanks very much!
I have find the following about eval from google, I will try it later.

You don't need to google.
Perl documentation is available on your system:

perldoc -f eval

HTH,
Christoph
 
S

SaltyBall

thanks
This demonstrates the principle:

#!perl
use strict;
use warnings;
eval ('use myModule;');
if ($@) {
print 'module not loaded';
# reload here
}
else {
print 'module loaded';
}

You can keep trying to load the module again and again (same syntax as
above), but watch out for endless loops obviously. $@ holds the error
message (if any) from your most recent eval-call.

One thought about your design: if the module fails to load the first
time, why would it not fail the second time then ? Are you loading it
over network/internet or so, and not trusting the connection ?

Hope this helps,
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top