mogul has asked for the wisdom of the Perl Monks concerning the following question:
prints:foreach my $run ('a' .. 'c') { my $x = S($run); print "$run : $x\n"; } sub S { my $mode = shift; return 42 if $mode eq 'a'; return (42) if $mode eq 'b'; my @l = (42); return @l if $mode eq 'c'; }
a : 42 b : 42 c : 1
How do the caller tell if S returned a scalar or an array? And how do I make it detect it and take propper action?
If I just collect everything in an array how do I tell if it returned a scalar or a single element list?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Detecting type of output from subs
by ikegami (Patriarch) on Dec 24, 2010 at 18:37 UTC | |
by mogul (Novice) on Dec 24, 2010 at 18:53 UTC | |
by chromatic (Archbishop) on Dec 24, 2010 at 19:24 UTC | |
by ikegami (Patriarch) on Dec 24, 2010 at 19:06 UTC | |
|
Re: Detecting type of output from subs
by JavaFan (Canon) on Dec 24, 2010 at 19:54 UTC | |
|
Re: Detecting type of output from subs
by cdarke (Prior) on Dec 24, 2010 at 23:17 UTC | |
|
Re: Detecting type of output from subs
by Anonymous Monk on Dec 24, 2010 at 18:28 UTC |