@a = @b || @c; # this is wrong
@a = scalar(@b) || @c; # really meant this
Hang on, shouldn't you be using 'or' instead? '||' works in scalar context, while 'or' also works in list context. jweed didn't say that he used '||', he used 'or' instead in his program.
I have constructed the following test to show that you can use 'or' in list context.
Update: I have realized my error in the code below. The line my @c = @a or @b was wrong, it was not behaving as what I have expected, it was really doing this instead: (my @c = @a) or @b;. Ok, another lesson for me on precedence and misconception about the 'or' operator.
use strict;
use Data::Dumper;
my @a = (1, 2, 3);
my @b = (4, 5, 6);
my @c = @a or @b; # equivalent to (my @c = @a) or @b;
print Dumper(\@c);
And the output is as expected -
$VAR1 = [
1,
2,
3
];
jweed's problem wasn't on how he used the 'or', but rather how he used the brackets. Consider the following example:
use strict;
use Data::Dumper;
my @a = (1, 2, 3);
my @b = (4, 5, 6);
my @c = @a or @b;
my @d = (@a or @b);
print Dumper(\@c);
print Dumper(\@d);
The line my @d = (@a or @b) is 'logically wrong', because the brackets force what's inside to be evaluated in scalar context. The output of the above program is -
$VAR1 = [
1,
2,
3
];
$VAR1 = [
3
];
| [reply] [d/l] [select] |
You've fallen into a trap here. This is an easy
mistake even for the most adept to make.
The only difference between || and or is precedence.
Brackets aka parentheses only affect context in a very few limited cases and this isn't one of them. The only
effect they have here is grouping, preventing my @d = @a or @b from meaning (my @d = @a) or @b.
Here are some of the limited cases:
- () followed by [] create a list slice, even in
scalar context (where only the last element will be returned).
@x = foo(); # foo called with list context
$x = (foo()); # foo called with scalar context
# note parens have no effect
$x = (foo())[@foo]; # foo called with list context
- () followed by = create a list assignment (as do @a= and %h=), placing the right of the assignment in list context.
@x = foo(); # foo called in list context
$x = (foo()); # foo called in scalar context
# note parens have no effect
($x) = foo(); # foo called in list context, note lack
# of parens around foo has no effect
- lack of () around the left operand of x forces scalar context. With (), the external list or scalar context will propagate onto the left operand.
foo() x 2; # foo called in scalar context
$x = foo() x 2; # foo called in scalar context
@x = foo() x 2; # foo called in scalar context
(foo()) x 2; # foo called in void context
$x = (foo()) x 2; # foo called in scalar context
@x = (foo()) x 2; # foo called in list context
The converse of the ()-doesn't affect context principle can be stated: functions, operators, and control constructs (and, for, etc.) create context; parentheses don't. You can look at the above list as cases where the parentheses are part of the operator; that is, (...)[...] , (...)= , and (...)x are operators, and (in the case of the latter two) can apply context differently than without the parentheses.
Updated to try not to sound so brusque, and list some of the exceptions. Updated to add examples. And converse principle. And rewrote last paragraph.
| [reply] [d/l] [select] |
my @c = @a or @b;
That's not the same, the asignment has more precedence than the or.
And Deparse tells that the only difference between "or" and "||" are two parents:
perl -MO=Deparse,-p -e 'my @d = (@a or @b);'
(my(@d) = (@a || @b));
-e syntax OK
and it's evaluated in scalar context.
$anarion=\$anarion;
s==q^QBY_^=,$_^=$[x7,print
| [reply] [d/l] [select] |