Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I was reading through Conway's OOP and remembered something about telling within a sub if it was "expected" to return() a value, and what types. For the life of me, I can't find it again in the book. Can anyone please tell me how this is done, or point me towards a page number? Its not in the glossary or index that I saw..

thanks

Replies are listed 'Best First'.
Re: Need to Return() ?
by merlyn (Sage) on Mar 15, 2001 at 08:29 UTC
      And so others can get the benefit of your wisdom w/o the OOPerl book (but buy it nonetheless, its a great book), pg 34 gives the example:
      sub listdir { # do file listing and the: return @missing_files if wantarray(); return $listed_count if defined(wantarray()); carp "subroutine &listdir was called in a void context"; } # sub listdir
      The really cool thing here: wantarray returns true if:
      @missing = listdir(@files);
      (called in a list context); returns "" (i.e. false but defined) if:
      $list_count = listdir(@files);
      (called in a scalar context) and returns undef if:
      listdir(@files);
      (called in a void context - get the carp).

      a