List and scalar OOE

Discussion in 'Perl Misc' started by nemo@weathersong.net, Oct 17, 2005.

  1. Guest

    I don't understand the seeming discrepancy between this:

    @a = ('foo', 'bar', 'baz');
    @b = (@a, shift @a);

    and this:

    $a = 5;
    @b = ($a, ++$a, $a++);

    In the first case, OOE is intuitive. @b is first assigned the value -as
    it is then- of @a, and then the value of "shift @a", so @b ends up
    being foo, bar, baz, foo. In the second case however, the list elements
    seem to be evaluated in advance (or, as if they were now pointers, not
    copies), so @b is 7 7 6, instead of 5 6 6.

    Is the discrepancy intentional? That is, OOE behaves differently on
    lists and scalars? Or is there some other subtle difference I'm missing
    completely, here?
    , Oct 17, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > $a = 5;
    > @b = ($a, ++$a, $a++);
    > ... In the second case however, the list elements
    > seem to be evaluated in advance (or, as if they were now pointers, not
    > copies), so @b is 7 7 6, instead of 5 6 6.


    > Is the discrepancy intentional? That is, OOE behaves differently on
    > lists and scalars? Or is there some other subtle difference I'm missing
    > completely, here?


    I think the behavior is attributed to the fact that the "++" operator
    has a very high order of precedence, whereas a comma operator (as a
    list element separator) has a rather low order of precedence. The
    expression is evaluated according to precedence, so the "++" gets
    evaluated first, before the "listification" happens.
    , Oct 17, 2005
    #2
    1. Advertising

  3. Guest

    Ahh, it's not OOE at all. I was overthinking it. Thanks.
    , Oct 17, 2005
    #3
  4. wrote:
    > Ahh, it's not OOE at all. I was overthinking it. Thanks.


    i think Perl looks at the statement from right to left. is it really a
    matter of binding precedence?
    it_says_BALLS_on_your forehead, Oct 18, 2005
    #4
  5. Guest

    , Oct 18, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    297
    Rocco Moretti
    Aug 24, 2004
  2. Andy Dingley

    Newbie - How to iterate list or scalar ?

    Andy Dingley, Aug 8, 2006, in forum: Python
    Replies:
    7
    Views:
    766
    Bruno Desthuilliers
    Aug 9, 2006
  3. Delaney, Timothy (Tim)

    RE: Newbie - How to iterate list or scalar ?

    Delaney, Timothy (Tim), Aug 9, 2006, in forum: Python
    Replies:
    1
    Views:
    298
    Bruno Desthuilliers
    Aug 10, 2006
  4. Clint Olsen
    Replies:
    6
    Views:
    339
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    153
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page