Reputation: 37098
It has been pointed out to me that ("key" => "value", "key2" => "value2")
is not a hash literal, but rather a list, and how it is interpreted depends on the context you use it in.
No other language I know personally has a key/value syntax like this that can be interpreted in any way other than that the author intended to create a hash/map/associative-array/dictionary/etc, so I find this aspect of perl somewhat difficult to grasp.
What contexts are there in perl where you would use that syntax without intending it to create such a data structure, where it is intended to work in such a way that key/value pairs have no role?
Upvotes: 1
Views: 339
Reputation: 386501
=>
and ,
are the same operator. They can be used interchangeably. They have nothing to do with hashes.
print($x, "\n"); === print($x => "\n");
$x=1, $y=2, $z=3; === $x=1 => $y=2 => $z=3;
The only difference is that =>
will auto-quote an identifier that immediately precedes it.
Upvotes: 4
Reputation: 118156
Any time you want to automatically quote a bareword to the left of a fat comma:
system ls => '-lh';
or
my $x = [ a => [ 1, 2 ], b => [ 3, 4 ] ];
Any time you think it makes the code easier to see
join ', ' => @data;
Any time you want to say "into":
bless { value => 5 } => $class;
=>
is a comma, plain and simple. You can use it anywhere you can use a comma. E.g.:
my $z = f($x) => g($y); # invoke f($x) (for its side effects) and g($y)
# assign the result of g($y) to $z
Just because one thing appears on the left side of a comma, and another thing appears on the right side of a comma, it doesn't mean that those two things constitute a key-value pair.
And, of course, if you are going to quote everything anyway:
my %x = ("key", "value", "key2", "value2");
or if you want to quote nothing
my %y = qw(key value key2 value2);
are both valid constructs.
So, the presence of a fat comma is neither a sufficient nor a necessary condition for the presence of a hash.
Upvotes: 7
Reputation: 22294
I regularly use this for calling system
:
my $rc = system $command,
-count => 24,
-file => $filename,
-verbose => # no value
-frobnitz => 'blat';
Any time I want a visual indication that these two values go together, I use the fat comma.
Upvotes: 4
Reputation: 17720
Even though that's probably not the reason this is the case, one possible use for this is for situations when you want to have more than one value for the same key, but still want to maintain a "flat" structure (i.e. no nested arrays or hashes).
You could then read such a list as key-value pairs.
Example:
do_something(user => 1, user => 2, user => 3, action => "whatever");
sub do_something
{
while (@_)
{
my $key = shift @_;
my $value = shift @_;
# ... do something with that key-value pair
}
}
As soon as you turn that list into a hash, duplicate keys will result in a single entry (the last one).
Also note that the "context" is really simple: if it's assigned to a hash:
%hash = (a => 1, b => 2)
or in a hashref literal by enclosing it in {}
:
$hashref = { a => 1, b => 2}
then you get a hash, otherwise it remains a list.
Note that the key => value
notation is just one of many possible notations, you could also use the qw
notation for instance:
%hash = qw(a 1 b 2);
Upvotes: 1