Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Short circut operators in list context

by Anarion (Hermit)
on Nov 30, 2003 at 09:45 UTC ( [id://310998]=note: print w/replies, xml ) Need Help??


in reply to Short circut operators in list context

If you read somewhere down in perlop, you'll find:
In particular, this means that you shouldn't use this for selec +ting between two aggregates for assignment: @a = @b || @c; # this is wrong @a = scalar(@b) || @c; # really meant this @a = @b ? @b : @c; # this works fine, though
$anarion=\$anarion;

s==q^QBY_^=,$_^=$[x7,print

Replies are listed 'Best First'.
Re: Re: Short circut operators in list context
by Roger (Parson) on Nov 30, 2003 at 23:25 UTC
    @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 ];
      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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://310998]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2025-07-20 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.