ViceRaid has asked for the wisdom of the Perl Monks concerning the following question:
Hi
I'm perplexed; I'm using CGI to receive a list of parameters, which might arrive under one name or another. So I wrote:
my @things = $q->param('t') or $q->param('things');
I wanted the list of parameters in t, or if that parameter is absent, the ones in things. It's all good if t is present, but fails otherwise, with an empty list. I narrowed it down to the following test case:
sub foo { wantarray ? () : 1; } sub bar { wantarray ? (3, 4) : 3; } my @thingies = foo() or bar(); warn @thingies; # Expected (3,4) here, got () my @thingies = ( foo() ) or ( bar() ); warn @thingies; # Expected (3,4) here too, got ()
I don't understand! If I were using ||, sure, that puts the calls in scalar context; however, with or, why doesn't the first sub get called in list context, return false with the empty list, and let the second sub get called? What am I missing?
Thanks
ViceRaid
update: fixed cut-and-paste mistakio in code results comment, as pointed out by Abigail-II
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Precendence and wantarray puzzling me
by broquaint (Abbot) on Mar 04, 2004 at 15:47 UTC | |
by ViceRaid (Chaplain) on Mar 04, 2004 at 15:57 UTC | |
by broquaint (Abbot) on Mar 04, 2004 at 16:01 UTC | |
|
Re: Precendence and wantarray puzzling me
by borisz (Canon) on Mar 04, 2004 at 15:43 UTC | |
|
Re: Precendence and wantarray puzzling me
by matija (Priest) on Mar 04, 2004 at 15:48 UTC | |
|
Re: Precendence and wantarray puzzling me
by Abigail-II (Bishop) on Mar 04, 2004 at 15:53 UTC | |
by BUU (Prior) on Mar 04, 2004 at 20:59 UTC | |
by Abigail-II (Bishop) on Mar 04, 2004 at 21:27 UTC |