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

Error Handling.

by fjaenale (Acolyte)
on Sep 28, 2004 at 07:18 UTC ( [id://394480]=perlquestion: print w/replies, xml ) Need Help??

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

Hello:
I have a question about this piece of code (I know is the same as my last post):

$self->{ftp}->put($origin, $target) or $msg = $self->{ftp}->message(), $cod = $self->{ftp}->code(), print ("$cod: $msg\n"), return 1;

-- EDIT: Sorry there is a mistake on the print intruction. I am adding the parenthesis, in the real code it calls a function.--

I just want to know if the way I handle the error is correct. After thte "or" I separate the instructions with ","; I have been told that this is a risky practice. I just want to know if this is really a risky practice or is ok to use the "," as separators.

In case there is a better less risky way, what would be that way?.


Thank you very much,
Francisco Jaen
www.cgi.com

Replies are listed 'Best First'.
Re: Error Handling.
by ysth (Canon) on Sep 28, 2004 at 07:25 UTC
    It's only risky if you don't understand the precedence of the different operators you use or call functions that take a list without supplying parentheses.

    In your sample code, everything is great until the print; because you don't say

    print("$cod: $msg\n"), return 1;
    it will assume print("$cod: $msg\n", return 1) so the return will actually happen as it is trying to build the list of parameters for print, and the print itself will never be issued.

    This kind of mistake is a pain to find; since you've done it once, I'd eschew this style of error handling and just go with:

    if (error condition) { set stuff print message return }
Re: Error Handling.
by bobf (Monsignor) on Sep 28, 2004 at 07:41 UTC

    You could use an 'if' and set error flags (or test the error condition directly), or you could use a do{ }; block, which would avoid having to keep track of precedence (as ysth pointed out):

    $self->{ftp}->put($origin, $target) or do { $msg = $self->{ftp}->message(); $cod = $self->{ftp}->code(); print ("$cod: $msg\n"); return 1; };
Unless..
by htoug (Deacon) on Sep 28, 2004 at 07:59 UTC
    I'd do it with an unless statement. It allows you to keep things almost as with the or but without the pitfalls.
    unless ($self->{ftp}->put($origin, $target)) { $msg = $self->{ftp}->message(); $cod = $self->{ftp}->code(); print "$cod: $msg\n"; return 1; }
    You can even leave out the parenthesis on the print statement ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 03:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found