Let's say I have
my %foo;
Can I set keys foo
, bar
, baz
to a
b
c
by taking a slice and doing parallel assignment with postfix notation?
%foo->@{qw/foo bar baz/} = qw/a b c/
I used this syntax and I was told it was only "accidentally working". I don't see it generating a warning, and I also don't see it documented anywhere. Is this behavior supported or not?
Similar to the array, Perl hash can also be referenced by placing the '\' character in front of the hash. The general form of referencing a hash is shown below. %author = ( 'name' => "Harsha", 'designation' => "Manager" ); $hash_ref = \%author; This can be de-referenced to access the values as shown below.
@values = @{ $hash{"a key"} }; To append a new value to the array of values associated with a particular key, use push : push @{ $hash{"a key"} }, $value; The classic application of these data structures is inverting a hash that has many keys with the same associated value.
Perl Reference is a way to access the same data but with a different variable. A reference in Perl is a scalar data type which holds the location of another variable. Another variable can be scalar, hashes, arrays, function name etc.
The left side of ->
is supposed to be an expression that returns a reference. Use anything else at your own risk.
%foo->{a}
used to work.
$ 5.10t/bin/perl -e'my %foo; %foo->{a} = 1; print "ok\n";'
ok
This was deemed to be bug.
$ 5.12t/bin/perl -e'my %foo; %foo->{a} = 1; print "ok\n";'
Using a hash as a reference is deprecated at -e line 1.
ok
$ 5.20t/bin/perl -e'my %foo; %foo->{a} = 1; print "ok\n";'
Using a hash as a reference is deprecated at -e line 1.
ok
$ 5.22t/bin/perl -e'my %foo; %foo->{a} = 1; print "ok\n";'
Can't use a hash as a reference at -e line 1.
There's no reason to believe %foo->@{...}
is any more valid than %foo->{...}
.
Bug reported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With