How to handle modules error?

Discussion in 'Perl Misc' started by SaltyBall, Jun 17, 2006.

  1. SaltyBall

    SaltyBall Guest

    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;
    }
     
    SaltyBall, Jun 17, 2006
    #1
    1. Advertising

  2. SaltyBall

    pyh Guest

    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" <> ???? 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;
    > }
    >
     
    pyh, Jun 17, 2006
    #2
    1. Advertising

  3. SaltyBall

    SaltyBall Guest

    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" <> ???? 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;
    >> }
    >>

    >
    >
     
    SaltyBall, Jun 17, 2006
    #3
  4. 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
     
    Bart Van der Donck, Jun 17, 2006
    #4
  5. SaltyBall

    Ch Lamprecht Guest

    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//"
     
    Ch Lamprecht, Jun 17, 2006
    #5
  6. SaltyBall

    SaltyBall Guest

    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,
    >
     
    SaltyBall, Jun 17, 2006
    #6
  7. SaltyBall

    John Bokma Guest

    SaltyBall <> wrote:

    > thanks


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


    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Jun 17, 2006
    #7
    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. Dietrich
    Replies:
    1
    Views:
    662
    Joe Smith
    Jul 22, 2004
  2. Leon
    Replies:
    2
    Views:
    554
  3. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    468
    hanumizzle
    Oct 6, 2006
  4. Stephen.Wu
    Replies:
    2
    Views:
    1,170
    Simon Hibbs
    Nov 19, 2009
  5. dt
    Replies:
    1
    Views:
    88
    Martijn Lievaart
    Apr 7, 2007
Loading...

Share This Page