I am looking for the proper perl-ism for this issue. I can work around it but just have to ask...
I am using HTML::TreeBuilder and am using the look_down method. This returns an array or scalar depending on context and returns undef if no matching tag is found. Cool.
I want to do the following:
foreach my $tag ( @{ $head->look_down('_tag', 'link') } ) {
...
}
but if there is no link tag the function returns undef and generates the error Can't use an undefined value as an ARRAY reference at myCGI.cgi line ###. So I try this modification:
foreach my $tag ( @{ $head->look_down('_tag', 'link') || [] } ) {
...
}
My thought was that if the method returns undef then it will get changed into an empty array. This works when there are no link tags. But, if there is at least one expected tag then there is an error: Not an ARRAY reference at myCGI.cgi line ###.
Do I need to just bite the bullet and break the method call out of the loop and check for undef before entering the loop?