Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How do I use a block as an ‘or’ clause instead of a simple die?

by linuxer (Curate)
on Apr 18, 2009 at 14:27 UTC ( #758479=note: print w/replies, xml ) Need Help??


in reply to How do I use a block as an ‘or’ clause instead of a simple die?

if ( !$ftp->put($myfile) ) { # do # your excessive exercise # her }
  • Comment on Re: How do I use a block as an ‘or’ clause instead of a simple die?
  • Download Code

Replies are listed 'Best First'.
Re^2: How do I use a block as an ‘or’ clause instead of a simple die?
by ikegami (Pope) on Apr 18, 2009 at 17:35 UTC
    The limit of if is encountered if you declare a variable.
    if (!open(my $fh, '<', $qfn)) { ... } print $fh $s; # XXX
    if (my ($y) = f($x)) { # Returns () on error. ... } print defined($y) ? $y : '[undef]'; # XXX

    Solutions:

    • Declare the var earlier:

      my $fh; if (!open($fh, '<', $qfn)) { ... }
      my $y; if (($y) = f($x)) { # Returns () on error. ... }
    • Save the result:

      my $success = open(my $fh, '<', $qfn); if (!$success) { ... }
      my $success = my ($y) = f($x); # Returns () on error. if (!$success) { ... }
    • Use do:

      open(my $fh, '<', $qfn) or do { ... };
      my ($y) = f($x) # Returns () on error. or do { ... };

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://758479]
help
Chatterbox?
[Corion]: Meh, I found another Amazon "used" scam reseller... If a product sells for "almost new" at half the price, it's a scam, most likely...
[ambrus]: Corion: that's not true. Actually for Christmas and Thanksgiving, a lot of people buy electronics such as cameras as present, then many of them figure out they don't need it,
[ambrus]: and the electronics gets reselled almost new, but it has to be sold at half price because otherwise everyone chooses to buy the new product which has fewer risk of selling damaged products labelled as almost new.
[ambrus]: You can actually get a lot of useful cheap really almost new products that way, with only a little risk of scams.
[ambrus]: That's what some of the "Black Friday" sales are about.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2017-11-21 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (304 votes). Check out past polls.

    Notices?