in reply to question about || operator and list context
I realise that your example is just that, but it is a strange choice of example. More to the point, even with the extra parens, it is a somewhat dangerous one.
I can only assume that the idea is that you die unless your vars $a and $b (not great choices in themselves.. see perlfunc:sort) get set to "some values". Now, in the example, the values in question are constants, which is pretty unlikely to fail, but it is just an example.
So, making a guess as to the context in which you made your discovery, you probably had something like this
( ($a, $b) = ( $somevar, $someothervar ) ) || die '$somevar or $someot +hervar not set!'; ...
However, I wonder if you realise exactly what it is that you are testing with this construct?
Try and guess which of the following pairs of values being assigned will result in the warning being issued, then run the code.
#! perl -slw use strict; for( ['a', 'b'], [1, 2], [0, 1], ['a', 'b'], [undef, undef], [1, undef], [undef, 1], [ ] #Intentionally empty. ) { my($a,$b); (($a, $b) = ( $_->[0], $_->[1] )) || warn "I would die when \$a=$a and \$b=$b\n"; }
Did you try it? Were you right? Surprising isn't it:)
ps. I wonder how many of the others posters above will be surprised too?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: question about || operator and list context
by markjugg (Curate) on Jun 22, 2003 at 04:14 UTC | |
by BrowserUk (Patriarch) on Jun 22, 2003 at 04:29 UTC | |
|
Re^2: question about || operator and list context
by Aristotle (Chancellor) on Jun 22, 2003 at 13:27 UTC |