in reply to Determining subroutine return type

You can't really coerce the return value listwise, because the value gets put in the context of the function call. Rather, it's much easier to simply have your function return the right thing depending on its calling context. You can tell what kind of thing you need to return with the unfortunately named wantarray function. (It should really be called wantlist.)

sub example { if(wantarray) { return (1,2,3); } return [1,2,3]; } my @array = example(); my $arrayref = example();

All references are scalar, so you will still need to check them with ref. (Although perhaps it would be easier to redefine your subs so you always know what kind of reference they return.)

Replies are listed 'Best First'.
Re^2: Determining subroutine return type
by satchm0h (Beadle) on Feb 24, 2005 at 22:10 UTC
    Agreed. It would certainly be much simpler to have your function "return the right thing". But in the case where you are wrapping/extending a legacy code base, that may not be an option.