in reply to Scalar and void subroutines

Use wantarray inside the sub to tell how it was called. That will be undef for void context, true for list context, and false (but defined) for scalar context:
sub name { ... if (not defined wantarray) { # do your void context behavior } elsif (wantarray) { # do your scalar context behavior } else { # do your list context behavior } }
Phil

Replies are listed 'Best First'.
Re^2: Scalar and void subroutines
by ysth (Canon) on Nov 11, 2005 at 06:53 UTC
      if (not defined wantarray) {
    I try to use and, or, and not only for flow control to minimize the potential for precedence mishaps (e.g. not defined foo later being changed to not defined foo && defined bar when ! defined foo && defined bar was meant). It may look a little funny to have a ! before defined at first, but you do get used to it.