On 27/07/2011 08:51, Shlomi Fish wrote:
On Tue, 26 Jul 2011 16:58:47 +0100 Rob Dixon<rob.di...@gmx.com>  wrote:
On 26/07/2011 16:39, Nikolaus Brandt wrote:
On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:

Another option would be to use eval { ... } and $@ to trap exceptions:

Thank you all for the replies.

I used the above mentioned eval-$@ solution which was absolutely  working
fine.

I think Shlomi may have been over-thorough in his list of options.

I've mentioned it for completeness sake.

Yes, that is what I assumed. But some of your options are less
appropriate, and I think it would have helped to say so.

Most Perl programmers would shudder at the sight of an eval, and
inthis case it is an ugly implementation of the try/catch idiom.

Eh, why? Have you made a survey that concluded that? I agree that eval { ... }
if ($@) in Perl has its limitations but using such abstractions as
http://search.cpan.org/dist/Exception-Class/ , it is good enough. And I think in
this case, it is appropriate because errors should result in exceptions.

That is a silly comment. You make many assertions yourself Shlomi, but I
wonder how many surveys you have conducted to establish them? It is
unnecessary to turn this into a battle and certainly not what I
intended, so please stop this rivalry.

John's

open my $fh, '>', "$basedir/$userdir/$outfile" or do {
   warn "Can't write: $!\n";
   return;
};

(With or without the warning) will do all that you want, and will
enamour you to all who read your code.


Well, one thing I dislike about it is that it is using "or do {...}" instead of
an "if ( ) { ... }". And I did mention something similar.

What exactly is wrong with "or do {...}"?

I believe it is the best option simply because is is comparable to the
common "open ... or die $!" idiom. The do is there only so that a
warning can be issued as well as the return, and

  open my $fh, '>', "$basedir/$userdir/$outfile" or return undef;

would be fine.

Rob



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to