I'm trying to initialize a hashref containing the results of several expressions. I would expect expressions that return undefined results to assign undef to the appropriate key. Instead, the assignment just gobbles up the next key as though the expression was never there to begin with.
A quick example is probably easier to understand:
use Data::Dumper;
my $str = "vs";
my $contains = {
t => ($str =~ /t/i),
u => ($str =~ /u/i),
v => ($str =~ /v/i),
};
print(Data::Dumper->Dump([$contains]));
I would expect the code above to print:
$VAR1 = {
'v' => 1,
't' => undef,
'u' => undef
};
Instead, I get the following:
$VAR1 = {
't' => 'u',
'v' => 1
};
Adding an explicit undef to the assignment does get me the result I'm looking for:
use Data::Dumper;
my $str = "vs";
my $contains = {
t => ($str =~ /t/i || undef),
u => ($str =~ /u/i || undef),
v => ($str =~ /v/i || undef),
};
print(Data::Dumper->Dump([$contains]));
However, this seems a little counterintuitive to me. Can anybody explain this behavior?