There are a couple of approaches:
Solution 1: If you don't need tor return a row, simply return a false scalar:
sub sub_undef {
# xxx
return $error; # Already a true/false scalar
}
while (sub_undef()) { do_stuff(); }
Solution 2: Return an arrayref instead of an array, and false (undef) in case of an error
sub sub_undef {
# xxx
return $error ? undef : $rowArrayRef;
}
while (my $row = sub_undef()) { do_stuff(@$row); }
Solution 3: Return a tuple ($status, $rowArrayRef)
sub sub_undef {
# xxx
return ($error, $rowArrayRef);
}
while (my ($error, $row) = sub_undef()) { last if $error; do_stuff(@$row); }
Solution 4: Only works if row can NOT be empty unless an error happens based on your business case!
sub sub_undef {
# xxx
return $error ? () : @row;
}
while (my @row = sub_undef()) { do_stuff(@row); }
Solution 5: Use exceptions for error handling (Try::Tiny
)
# No example worth bothering that won't be covered by Synopsis of the module