satchm0h has asked for the wisdom of the Perl Monks concerning the following question:
What I need is some analog for non-reference return values. For instace, if the subroutine returns a list I don't want to evaluate it in scalar context and end up with the length.sub determineType { $sub_ref = shift; $rval = $sub_ref(@_); return @$rval if( ref($rval) eq 'ARRAY' ); return %$rval if( ref($rval) eq 'HASH' ); }
If you are interested in seeing the code I used to test this routine, drop me a line and I'll add it.sub execSubroutine { my $sub = shift; my @sub_ret = &{$sub}(@_); # Return an list if we get a list with more than one value # this will satisfy callers expecting lists and/or hashes return @sub_ret if (@sub_ret > 1); my $rval = $sub_ret[0]; # If we have a ref de-reference and return. if (my $type = ref($rval)) { return @$rval if ($type eq 'ARRAY'); return %$rval if ($type eq 'HASH'); } # If not a ref and the caller wants a list, give it to them if (wantarray) { return @sub_ret; } # Otherwise return a scalar return $rval; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Determining subroutine return type
by friedo (Prior) on Feb 24, 2005 at 21:38 UTC | |
by satchm0h (Beadle) on Feb 24, 2005 at 22:10 UTC | |
|
Re: Determining subroutine return type
by ikegami (Patriarch) on Feb 24, 2005 at 21:41 UTC | |
by satchm0h (Beadle) on Feb 24, 2005 at 22:43 UTC | |
|
Re: Determining subroutine return type
by phaylon (Curate) on Feb 24, 2005 at 21:37 UTC | |
|
Re: Determining subroutine return type
by Roy Johnson (Monsignor) on Feb 24, 2005 at 22:39 UTC | |
by sandfly (Beadle) on Feb 24, 2005 at 23:16 UTC | |
|
Re: Determining subroutine return type
by herveus (Prior) on Feb 25, 2005 at 15:18 UTC |