in reply to Re^4: referencing slices - love that DWIM
in thread referencing slices - love that DWIM

A list is an ephermeral thing; a transient set of values on the call stack. There's no way to take a reference to it since it has no permanent home to which to refer. An array on the other hand has a permanent location in memory to which a pointer can be obtained.

The cake is a lie.
The cake is a lie.
The cake is a lie.

  • Comment on Re^5: referencing slices - love that DWIM

Replies are listed 'Best First'.
Re^6: referencing slices - love that DWIM
by syphilis (Archbishop) on May 17, 2008 at 13:57 UTC
    A list is an ephermeral thing; a transient set of values on the call stack

    Ok ... so if, as the docs say, \(@foo) returns a list, then precisely what does [\(@foo)] return ? Is it (something like) "a reference to an array comprising the list returned by \(@foo)" ? (What's the correct terminology ?)

    Cheers,
    Rob

      That's a single scalar value, which is the anonymous array reference containing a list of references to the individual elements of the array @foo. Maybe this will clear it up.

      use Data::Dumper qw( Dumper ); my @foo = qw( a b c ); my $x = [ \( @foo ) ]; print Dumper( $x ), "\n"; ${ $x->[1] } = 'z'; print join( "\n", @foo ), "\n"; print Dumper( $x ), "\n";

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Re^6: referencing slices - love that DWIM
by DrHyde (Prior) on May 19, 2008 at 09:58 UTC
    However, from the point of view of a perl programmer they are as near as damnit the same thing.

      Except for in the edge cases, which is where thinking they are the same thing gets you into trouble:

      • the \( @array ) vs \@array behavior in question here
      • push|splice|etc require an actual ARRAY as their first argument, not a LIST
      • a LIST and an ARRAY will behave differently in scalar context (contrast scalar qw( a b c ) with scalar @{[ qw( a b c ) ]})

      Those are three that readily come to my pre-morning-caffeine brane, but there's certainly others. Yes in most cases they'll behave the same, but you need to understand that it is a simplification and know where the idea breaks down. Otherwise you'll be unpleasantly surprised after you have to go chasing down squirrelly context induced wonkyness.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.