$ perl -le'undef $_; defined $_->{0}; print $_'
HASH(0x813f1c8)
Whenever you try to dereference an undefined value, Perl will helpfully summon an anonymous data type of the corresponding type and store it there. In your case, the culprit is probably
while( defined $bros->{ $find }->[ $index ]->{ $key } ) {
# ...
}
If you use exists there instead, your ghost hashes should go away. Actually, no, they won't. The hash is summoned by the mere attempt at dereferencing. You have to make sure that $bros->{ $find }->[ $index ] exists before attempting a dereference:
while(
ref $bros->{ $find }->[ $index ]
and defined $bros->{ $find }->[ $index ]->{ $key }
) {
# ...
}
However, in Perl, it's almost always a mistake to use a counter variable such as $index when you only ever use it as an index into a single structure. This is one of the red flags mentioned in Mark-Jason Dominus' excellent Program Repair Shop and Red Flags article series. You could have avoided the issue altogether by rewriting your inner loop to a foreach:
for my $year ( qw( 1958 1959 1960 1961 1962 1963 1964 ) ) {
for my $film ( @{ $bros->{ $year } } ) {
push @res, { brother => $film, year => $year }
if $film->{ $key } =~ /$data/i;
}
}
Depending on taste that can be transformed to a grep:
for my $year ( qw( 1958 1959 1960 1961 1962 1963 1964 ) ) {
push @res, (
map +{ brother => $_, year => $year },
grep $_->{ $key } =~ /$data/i,
@{ $bros->{ $year } },
);
}
Makeshifts last the longest.
|