arrays - What exactly mean the ${...} in the perl? -
thinking next. have list
qw(a b c);
now, assign list nameless (anonymous) array
[ qw(a b c) ]
so next
use 5.016; use warnings; use diagnostics; $x = [ qw(a b c) ]; ref $x; #array - $x array reference, , $x->[1]; #prints "b", , [ qw(a b c) ]->[1]; #works
but happens now?
use 5.016; use warnings 'all'; use diagnostics; ${[ qw(a b c) ]}[1];
it prints b
, but
my $y = ${[ qw(a b c) ]};
is error,
not scalar reference @ pepe line 6 (#1) (f) perl trying evaluate reference scalar value, found reference else instead. can use ref() function find out kind of ref was. see perlref. uncaught exception user code: not scalar reference @ pepe line 17.
so, whats mean contruction ${.... }
- it "works" in
say
(prints second element of anonymous array), don't understand why - but can't assign variable
and hint diagnostics
not helpful, because how should use ref
when can't assign? missed perlref
?
${ expr1 }[ expr2 ]
array index dereference. returns element @ index returned expr2
of array referenced reference returned expr1
.
${ $array_ref }[ ... ]
array references $array[...]
arrays.
${ expr }
that's not followed [
or {
scalar dereference. returns scalar referenced reference returned expr
.
${ $scalar_ref }
scalar references $scalar
scalars.
as can see, when dealing reference, can use same syntax would, except replace name of variables {$ref}
(keeping leading sigil).
as such, @{ $array_ref }
array references @array
arrays.
say @{[ qw(a b c) ]};
this essence of chart in earlier post mini-tutorial: dereferencing syntax. see also:
oops, thought had
say ${[ qw(a b c) ]}; # want print b c
you have
my $y = ${[ qw(a b c) ]};
you want
my $y = [ qw(a b c) ];
[]
creates array , reference array, , returns latter, kinda like
my $y = { @anon = qw(a b c); \@a };
Comments
Post a Comment