[32476] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3741 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jul 22 21:09:20 2012

Date: Sun, 22 Jul 2012 18:09:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 22 Jul 2012     Volume: 11 Number: 3741

Today's topics:
    Re: Error Handling in Net::SSH::Perl <ben@morrow.me.uk>
    Re: Error Handling in Net::SSH::Perl <rweikusat@mssgmbh.com>
    Re: Error Handling in Net::SSH::Perl <rvtol+usenet@xs4all.nl>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 22 Jul 2012 02:31:59 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Error Handling in Net::SSH::Perl
Message-Id: <fp1sd9-i16.ln1@anubis.morrow.me.uk>


Quoth "Dr.Ruud" <rvtol+usenet@xs4all.nl>:
> On 2012-06-22 21:25, dagomakoa wrote:
> 
> > my $ssh;
> > eval {
> >     $ssh = Net::SSH::Perl->connect( ... );
> > };
> > if ($@) {
> >    warn "Connect failed: $@\n";
> > }
> >
> > It does not work with me, should we remove the option -w on the
> shebang or remove use warnings / use strict?
> 
> Don't base code flow on $@.
> Write it like this:
> 
> my $ssh;
> eval {
>      $ssh= Net::SSH::Perl->connect( ... );
>      1;  # success
> }
> or do {
>      my $eval_error = $@ || 'Zombie Error';
>      warn "Connect failed: ", $eval_error, "\n";
> };

    use Try::Tiny;

    my $ssh = 
        try { Net::SSH::Perl->connect(...) }
        catch {
            warn "Connect failed: $_\n";
            undef;
        };

The final 'undef' is the value assigned to $ssh if the connection fails.

Ben



------------------------------

Date: Sun, 22 Jul 2012 09:56:53 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Error Handling in Net::SSH::Perl
Message-Id: <87d33o2my2.fsf@sapphire.mobileactivedefense.com>

"Dr.Ruud" <rvtol+usenet@xs4all.nl> writes:
> On 2012-06-22 21:25, dagomakoa wrote:
>
>> my $ssh;
>> eval {
>>     $ssh = Net::SSH::Perl->connect( ... );
>> };
>> if ($@) {
>>    warn "Connect failed: $@\n";
>> }
>>
>> It does not work with me, should we remove the option -w on the shebang or remove use warnings / use strict?
>
> Don't base code flow on $@.
> Write it like this:
>
> my $ssh;
> eval {
>     $ssh= Net::SSH::Perl->connect( ... );
>     1;  # success
> }
> or do {
>     my $eval_error = $@ || 'Zombie Error';
>     warn "Connect failed: ", $eval_error, "\n";
> };

This should still be: Don't use exceptions. That's just .... wrong!
The problem with this is, not everyone whose code you might need to
use at some point in time cares for these semi-religious convictions.
Eg, the mod_perl error handling is exception based. Since $@ can also
contain a reference to some object, there might even be some kind of
structured error handling based on some hierarchy of exception
classes. Even real error handling, such as automatically reconnecting
to some database server and re-executing queued statements, possibly,
after re-preparing them, in case the error was not caused by a
statement not just "Fuck! It didn't work!!" error handling aka 'Zombie
error! It returned zero and I don't know why!'


------------------------------

Date: Sun, 22 Jul 2012 12:02:07 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Error Handling in Net::SSH::Perl
Message-Id: <500bcf9f$0$6873$e4fe514c@news2.news.xs4all.nl>

On 2012-07-22 03:31, Ben Morrow wrote:
> "Dr.Ruud":
>> dagomakoa:

>>> my $ssh;
>>> eval {
>>>      $ssh = Net::SSH::Perl->connect( ... );
>>> };
>>> if ($@) {
>>>     warn "Connect failed: $@\n";
>>> }
>>>
>>> It does not work with me, should we remove the option -w on the
>>> shebang or remove use warnings / use strict?
>>
>> Don't base code flow on $@.
>> Write it like this:
>>
>> my $ssh;
>> eval {
>>       $ssh= Net::SSH::Perl->connect( ... );
>>       1;  # success
>> }
>> or do {
>>       my $eval_error = $@ || 'Zombie Error';
>>       warn "Connect failed: ", $eval_error, "\n";
>> };
>
>      use Try::Tiny;
>
>      my $ssh =
>          try { Net::SSH::Perl->connect(...) }
>          catch {
>              warn "Connect failed: $_\n";
>              undef;
>          };
>
> The final 'undef' is the value assigned to $ssh if the connection fails.

Try::Tiny is certainly fine for many situations.
http://www.perladvent.org/2011/2011-12-17.html
So use it if you can.


It presents itself as "bare bones" but it can't be,
mainly because of the minimal exception support in Perl.
So it needs to bring its own set of rules and exceptions and overhead.

At least check its Caveats section
http://search.cpan.org/perldoc?Try::Tiny
Also see: http://wonkden.net/dcbpw_ttch.html

-- 
Ruud



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3741
***************************************


home help back first fref pref prev next nref lref last post