phillips@pacific.cs.ubc.ca (George Phillips) (05/03/90)
In article <1990Apr27.191209.12150@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes: >sub bub { # error return is (undef) > return shift ? @_ : (undef); >} > >sub try { > local(@ans) = &bub(@_); > if ((@ans != 1) || (defined $ans[$[])) { > print 0+@ans, " elements in @ans\n"; > } else { > print "an error return\n"; > } >} > > >&try(1,"a","normal","list"); >&try(1,"a single element"); >&try(1); # null list passed/returned >&try(0, "should","be","ignored"); # finally, trigger an error You don't need undef at all for your solution, just make sure that a non-empty list is returned for success and an empty list is returned for failure. The caller can act on () as failure and otherwise shift the list before using it. This is actually simpler for the caller because it only needs to test "shift @result". So, the rehacked (and very silly looking) version: sub bub { # error return is (), ok is (1, resultlist) shift ? unshift(@_, 1) : (@_ = ()); @_; } sub try { local(@ans) = &bub(@_); if (shift @ans) { print 0+@ans, " elements in @ans\n"; } else { print "an error return\n"; } } &try(1,"a","normal","list"); &try(1,"a single element"); &try(1); # null list passed/returned &try(0, "should","be","ignored"); # finally, trigger an error This works, but it sure would be nice if assignment to undef always undefines a variable. Subroutines returning arrays would be much cleaner and the language would make more sense. It's a feature I'd like to see unless it would require some major changes to perl. George Phillips phillips@cs.ubc.ca {alberta,uw-beaver,uunet}!ubc-cs!phillips