Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to handle modules error?

Reply
Thread Tools

How to handle modules error?

 
 
SaltyBall
Guest
Posts: n/a
 
      06-17-2006
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;
}

 
Reply With Quote
 
 
 
 
pyh
Guest
Posts: n/a
 
      06-17-2006
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.


"SaltyBall" <(E-Mail Removed)> ???? news:4493da3b$1@127.0.0.1...
> 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;
> }
>



 
Reply With Quote
 
 
 
 
SaltyBall
Guest
Posts: n/a
 
      06-17-2006
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.
----------------------------------------------------------------------

pyh wrote:
> 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.
>
>
> "SaltyBall" <(E-Mail Removed)> ???? news:4493da3b$1@127.0.0.1...
>> 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;
>> }
>>

>
>

 
Reply With Quote
 
Bart Van der Donck
Guest
Posts: n/a
 
      06-17-2006
SaltyBall wrote:

> 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,

--
Bart

 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      06-17-2006
SaltyBall wrote:
> 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


--

perl -e "print scalar reverse q/(E-Mail Removed)/"
 
Reply With Quote
 
SaltyBall
Guest
Posts: n/a
 
      06-17-2006
thanks

Bart Van der Donck wrote:
> SaltyBall wrote:
>
>> 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,
>

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-17-2006
SaltyBall <(E-Mail Removed)> wrote:

> thanks


salty balls are normally down, as should be your replies.


--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Possible to handle web requests without an ASPX page? i.e. have DLL handle request. jdlwright@shaw.ca ASP .Net 2 05-31-2005 05:42 PM
how to handle command line output(not terminal handle) Leon Python 2 11-04-2004 05:16 AM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM



Advertisments