Perl is filled with people offering the best way to do things, often correctly, and sometimes totally not what you expect.
But since variety is the spice of life...
There are
three reasonably cohesive ways to return a list, depending on what you are doing one of them should be relevant.
Return normally as a list
The original way is to return the data as a list (returning an array does this), and for the error case just 'return', gives you 'false' in both list and scalar context, the null array in the case of a list context, and undef in scalar context. This is the simplest, but when you want to also signal an error, suffers from tricky testing. In list form, () == error, which can suck when you legally return a null array. In scalar context, well... you only get the number anyways and not the list
You also get similar problems when you 'return undef', as undef returns in list context as a single element array with an undef in it, which is probably not what you REALLY want anyways. And what if a single undef element is a legal return value?
Exceptions
Secondly, we get the exception method, which in perl is essentially "die"ing with the error message. You might use formal exceptions, but they just die underneath. Very clean as you don't have to test for errors everywhere, but if you don't want to be eval/try'ing all the time, or the 'policy' for the code doesn't not allow to use exceptions everywhere, this can get nasty.
Return by reference
My personal favourite, and I suspect the best answer in this situation, return just return the list by reference.
&mysub2(mysub1);
sub mysub1{
# Returning something
return \@data;
# on error
return undef;
}
sub mysub2{
my $result = @_;
unless($result){
print "Array is not defined" #Expected out come
}else{
print "Exists";
}
}
You can then continue as normal.
I actually do something else, where I return defined but false for a null list, which can come in handy for something like
my $rv = getsomething();
die "Error while getting something" unless defined $rv;
die "We did not find anything" unless $rv;
print "Found " . scalar(@$rv) . " thing(s)\n";
Anyway, I suspect that's something like what you want.
Of course, if you DON'T return an error, I prefer to just return as a list.
For suggestions on a comprehensive return value policy that effectively covers all possible combinations of return value and error(or not), you might like to see
http://ali.as/devel/code.html#6, part of my code style policy for myself and my company.