in reply to Return Value from sub
You don't show the relevant code, but my guess is that the issue is scalar context vs. list context.
Most likely you have something like
sub nonblank { return grep { /\S/ } @_ } print nonblank('foo'); # list context through print() print $_ for nonblank('foo'); # explicit list context my $temp= nonblank('foo'); # scalar context print $temp; # still scalar ( $temp)= nonblank('foo'); # list context, discarding all but the firs +t element print $temp; # First element of that list $temp= (nonblank('foo'))[0]; # Scalar context in assignment, but we + only take the first element explicitly print $temp;
... and grep returns a list, even if you are really, really certain that there will only ever be one matched element. And that list, when evaluated in scalar context turns into the number of elements. Which is 1.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Return Value from sub
by Dr Manhattan (Beadle) on Sep 06, 2013 at 10:10 UTC | |
by Corion (Patriarch) on Sep 06, 2013 at 10:20 UTC | |
by Laurent_R (Canon) on Sep 06, 2013 at 10:16 UTC |