[Israel.pm] defined @array

Yossi.Itzkovich at ecitele.com Yossi.Itzkovich at ecitele.com
Wed Nov 17 06:32:49 PST 2004

Thanks for the educative answer.


                      Gaal Yahas                                                                                                  
                      <gaal at forum2.org         To:      Perl in Israel <perl at perl.org.il>                                         
                      >                        cc:                                                                                
                      Sent by:                 Subject: Re: [Israel.pm] defined @array                                            
                      perl-bounces at per                                                                                            
                      11/17/2004 14:17                                                                                            
                      Please respond                                                                                              
                      to Perl in                                                                                                  

On Wed, Nov 17, 2004 at 12:07:03PM +0200, Yossi.Itzkovich at ecitele.com
> I have a function that returns a list. The list could be empty.  When I
> have an error I want to return undef.
> In the calling code I want to know if the list is empty or I got undef,
> first I check:
> if (defined @array)....
> But -w gives me :
> "defined(@array) is deprecated at... May be you should just omit the
> defined ?"
> But then I will not be able to differ between empty list and undef, isn't
> it ?

The short answer is that you should consider changing your interface to
return an arrayref on success, and undef otherwise. An alternative
strategy is to throw anexception on error.

The long answer is that evaluating the function in list context (as
surely you are if you're expecting an array) means that an explicit
"return undef" yields a list with one member, whose value is undef;
and just "return" yields an empty list. You *might* be able to detect
errors by checking for "@result == 1 && not defined $return[0]", but
only if your application guarantees this sort of return value is invalid.

A list is a list is a list; if you want to return one you must define
what sort of list signals an error, because you are signaling in-band.

Hence my suggestion: return one value -- a reference to an array -- on
success, or no value -- "return" with no args -- on failure. This has
the nice feature of evaluating to true and false respectively when put
in an if:

     if (! $res_ref = my_func()) {
         # handle error
     # data is in $res_ref->[0] etc.

My other suggestion was to throw an exceptio ('die "oh no!\n", or with
an actual error message). This gives you caller code like this:

     eval {
         my @result = my_func();     # returns a list now
         # code here assumes the call worked
     if ($@) {
         die $@ unless $@ eq "oh no!\n" # propagate all other exceptions
         # handle the real failure here.

Use whichever works best for you<tm>.

Gaal Yahas <gaal at forum2.org>
Perl mailing list
Perl at perl.org.il

More information about the Perl mailing list