in reply to Grab return values of different type from a sub
Subs return lists; and both hashes and arrays flatten to lists. The only problematic one is the scalar:
#! perl -slw use strict; use Data::Dump qw[ pp ]; sub func { my $arg = shift; my %hash = 'a'..'f'; my @array = 0..9; my $scalar = 'scalar'; $arg == 1 and return %hash; $arg == 2 and return @array; $arg == 3 and return $scalar; } sub wrapper { my @rv = func( @_ ); return @rv == 1 ? $rv[ 0 ] : @rv; } my %hash = wrapper( 1 ); pp \%hash; my @array = wrapper( 2 ); pp\@array;; my $scalar = wrapper( 3 ); pp $scalar; __END__ c:\test>junk12 { a => "b", c => "d", e => "f" } [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] "scalar"
Of course, if you want to find out the current bucket allocation of the hash, or retrieve the last item of the array; you'll need to assign it to the appropriate variable in the calling code first.
|
|---|