%args = (%hash1,%hash2);
Is this guarantee开发者_如何学编程d to overwrite %hash1
with %hash2
when conflict arises in Perl?
Yes, it is. Later keys overwrite earlier ones.
If you aren't seeing overwriting behavior, then you are doing something wrong:
#!/usr/bin/perl
use strict;
use warnings;
sub func { print "func\n" }
sub tion { print "tion\n" }
my %args = (
handler => \&tion,
);
my $dispatch = {
handler => \&func,
%args
};
$dispatch->{handler}();
From perldoc perlglossary
:
- list
An ordered set of scalar values.
From perldoc perldata
:
LISTs do automatic interpolation of sublists. That is, when a LIST is evaluated, each element of the list is evaluated in list context, and the resulting list value is interpolated into LIST just as if each individual element were a member of LIST. Thus arrays and hashes lose their identity in a LIST
and
A hash can be initialized using a literal list holding pairs of items to be interpreted as a key and a value.
The practical upshot is that a hash in list context yields an list of key/value pairs. Once it is a list it has an order (the order it was returned in), you are never guaranteed what order the keys and values will come back in, but lists guarantee order of their elements, so (a => 1, %h)
creates a list that starts with "a"
, followed by 1
, followed by the first key returned by %h
, followed by the first value returned by %h
, and so on. That list is then assigned to the target hash in the list order, which means if the key "a"
is in %h
then it will override the original.
No. %hash1 is not modified at all by that statement.
精彩评论