in reply to Using undef scalar as arrayref

You've been the victim of auto-vivification:

use strict; use warnings; my $arrayref; foreach (@$arrayref) { } print($arrayref\n"); # ARRAY(0x1abefc0)

scalar @$arrayref doesn't autovivify, therefore if (@$arrayref) { ... } gives an error.


If you don't distiguish between undefined and 0 elements, you could create a 0-element array when the function returns undef:

use strict; use warnings; my $arrayref = getArrayref(); # this sometimes returned undef # Manually vivify it. $arrayref ||= []; if (@$arrayref) { ... }

Otherwise, yes, you have to check if it's defined (or simply true) first:

use strict; use warnings; my $arrayref = getArrayref(); # this sometimes returned undef if ($arrayref && @$arrayref) { ... }