Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

VB "On Error" Equivalent in Perl

by Gorby (Monk)
on Nov 28, 2007 at 19:03 UTC ( [id://653629]=perlquestion: print w/replies, xml ) Need Help??

Gorby has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Monks,

In VB, it's possible to instruct the program to continue even if an unexpected error occurs. Is there an equivalent way to do that in Perl? My problem is that I'm using Net::FTP, and that module times out after a few seconds, and my Perl program halts and exists without giving me a choice to reconnect and try again. How can I handle errors like this and keep my Perl program running?

Thanks in advance.

Gorby

Replies are listed 'Best First'.
Re: VB "On Error" Equivalent in Perl
by jrsimmon (Hermit) on Nov 28, 2007 at 19:10 UTC
    Have you tried eval? It's similar to catch in C/C++. Details can be found in perldoc: "perldoc -f eval" on your cmd line.
Re: VB "On Error" Equivalent in Perl
by diotalevi (Canon) on Nov 28, 2007 at 19:49 UTC
Re: VB "On Error" Equivalent in Perl
by wind (Priest) on Nov 28, 2007 at 19:55 UTC
    Check out Conway's Perl Best Practices. From Chapter 13: Error Handling, Section Recoverable Failure, we find the following technique:
    use Carp qw(croak); use English qw(-no_match_vars); use Net::FTP; use Time::HiRes qw(sleep); use strict; my $MAX_TRIES = 15; my $ftp; TRY: for my $try (1..$MAX_TRIES) { # If successful, we're done. eval { $ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name: $@"; last TRY; }; # Report non-recoverable failure if no more tries croak( $EVAL_ERROR ) if $try == $MAX_TRIES; # Try again after a nap (approximate fibonacci) sleep( rand (1.618 ** $try) ); }
    - Miller

    Update: Missing Semi-colon.
      eval { $ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name: $@"; last TRY; }
      I'm curious about your use of $@ om the second line of the eval. How would $@ be set without causing an exception that would throw control outside the eval?

      If the die "Cannot ..." ever occurs, wouldn't $@ be empty?

      Surely $@ is only useful outside the eval?

      Also, eval {} is a statement and should be followed by a semicolon if further statements are to follow in the current block. Your code doesn't compile.

      -David

        That is explained in the documentation for Net::FTP.
        If the constructor fails undef will be returned and an error message will be in $@.
        All other Net::FTP methods save their error message in $ftp->message.

        I've also sometimes wondered about the logic of this. Nevertheless, that's the way it is.

        - Miller
Re: VB "On Error" Equivalent in Perl
by bdimych (Monk) on Nov 29, 2007 at 13:30 UTC
    I may be not exactly right, but in perl, quite the contrary, majority of errors do not make script exit. Most functions return false or undef on error and it can be checked just with "if" operator. Imho, there is rather opposite problem in perl - how to implement exceptions and try/catch

    I agree too, that 'eval'+module documentation is the guaranteed right way

    PS could you provide example

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://653629]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-19 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found