Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Is this a bug, or expected behavior?

by Fletch (Bishop)
on Mar 17, 2006 at 15:17 UTC ( [id://537470]=note: print w/replies, xml ) Need Help??


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

Nope. .. in scalar context is a flip-flop operator. A constant expression on either side is compared against $.; since you've not read any lines of input $. does not equal 1, so it's evaluating to a false value 0.

See perlop which explains all this in detail.

Replies are listed 'Best First'.
Re^2: Is this a bug, or expected behavior?
by fizbin (Chaplain) on Mar 17, 2006 at 15:24 UTC
    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@/
      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@/

      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

      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.

      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://537470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found