'Convert hashref to array of kv pairs
Here's my initial code:
sub my_sub {
my $hash = {
age => 5, # default value for "age" key
@_ # the rest of the "hash" as an array
};
...
}
#used like so:
my_sub("age" => 42, ...);
But I'd like to also support taking in a hashref in addition to an array. So I've tried:
sub my_sub {
my $hash = {
age => 5, # default value for "age" key
ref(@_) eq "" ? @_ : %{$_[0]}
};
...
}
If you call it with an array like before, the ref(@_) eq "" check would be true and the conditional would evaluate to @_ like before. But I can't for the life of me get the false case to work. Currently, it says there are an odd number of elements in the anonymous hash. But when I print out the value of %{$_[0]} I see the expected flattened array of (age, 42, ...) - in other words it looks right in the context of printing it out, but it barfs when it's in the conditional, and I have no idea why. Halp.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
