Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Is this a bug, or expected behavior?

by fizbin (Chaplain)
on Mar 17, 2006 at 15:24 UTC ( [id://537473]=note: print w/replies, xml ) Need Help??


in reply to Re: Is this a bug, or expected behavior?
in thread Is this a bug, or expected behavior?

Ah, right. Okay, so I can see why scalar(1..4) is ''; now my question is, why is 1..4 getting a scalar context above when I assign to $b? Shouldn't [ $a->[1..4] ] cause 1..4 to be evaluated in list context?
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Replies are listed 'Best First'.
Re^3: Is this a bug, or expected behavior?
by merlyn (Sage) on Mar 17, 2006 at 15:39 UTC
    The context applied to $a->[1..4] does not affect how the subscript is being evaluated, or else we'd go crazy! It's always going to apply scalar context to the subscript, much as $a[EXPR] always applies scalar context to EXPR.

    If you want a single element, use the arrow notation. If you want a slice, use the slice notation. That's the way it works.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Why? I mean, I can see that this:
      $b = [ $a->[1..4]->[11..12] ];
      Would require that 1..4 get evaluated in scalar context, but why would 11..12 also need to be evaluated in scalar context? Why doesn't the last thing in the -> chain inherit the context from the overall chain?
      --
      @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
        Does "by definition" and "it makes sense to Larry" help here?

        The index of a scalar access to a data structure (array or hash) is always evaluated in scalar context, because one and only one item comes back from the calculation.

        You don't expect $a->[3..5] to come back with 3 items, do you? If so, please reset your expectations to the way Larry implemented that design. If you want a slice, there's a slice notation. If you want a single element, you have a single element notation.

        Note that for Perl6, Larry changed his mind ("see rule #1"), and it acts differently, which I think will mean that DWIMmery will eventually lead to unpredictable behavior at that level. In fact, I saw some evidence of that in a Perl6 design discussion two years ago.

        However, for Perl5, it's very predictable, and doesn't depend on the context in which the item appears, which I think is quite nice.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        It does because it is a scalar context.

        Your problem is that you're not accepting/catching/groking that the parts inside the [] have their own context due to their nature (dereferencing a single element from an array reference) that's independent from the context of the larger expression. It doesn't matter how many of them you stack up, each of them is trying to retrieve a single element. Slices use @{ $aref }[ LIST ], single elements use ${ $aref }[ EXPR ]. The later is not the former, the former is not the later. They're two different things which impose different contexts on the subscripts.

        Update: let me rewrite your example using the more explicit brace-y syntax I used; that might help clear it up. Your:
        $a->[1..4]->[11..12]

        is the same as:
        ${ ${ $a }[ 1..4 ] }[ 11..12 ]

        A similar slice (which due to the indices you used wouldn't make much sense :) would be:
        @{ @{ $a }[ 1..4 ] }[ 11..12 ]

        But again, you don't have a slice, you're fetching a single element so it's scalar context inside the []s.

Re^3: Is this a bug, or expected behavior?
by Fletch (Bishop) on Mar 17, 2006 at 15:37 UTC

    No, you're dereferencing a single element of an array reference. Dereferencing a slice from an array reference would, though.

    $ cat wa sub ctx { my $w = wantarray; if( defined $w ) { print $w ? "array\n" : "scalar\n"; } else { print "void\n" } 1; } my $a = [ qw( a b c ) ]; $a->[ ctx( ) ]; @{$a}[ ctx( ) ]; $ perl wa scalar array
Re^3: Is this a bug, or expected behavior?
by xdg (Monsignor) on Mar 17, 2006 at 15:51 UTC

    The [1..4] in list context is used with slices -- it doesn't create a list context on its own. Because you ask for $a->, you're telling Perl to dereference $a to a single element, not a slice, thus 1..4 is interpreted in scalar context.

    In the case that works, you're dereferencing $a to an array, and then taking a slice of that -- and because you're slicing, 1..4 gets interpreted in list context.

    $a->[1..4]; # not a slice @{$a}[1..4]; # slice

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^3: Is this a bug, or expected behavior?
by ikegami (Patriarch) on Mar 17, 2006 at 16:51 UTC
    Unlike @{$a}[1..4], $a->[1..4] is not a array slice. This table should help you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found