in reply to Force 'sub' return to be treated in list context?
You haven't shown us a test case which fails so I'm guessing, but if you write:
my $thing = UnPack (1, $stringpack);
$thing will get a count instead of the "first" element of the returned array because UnPack is called in scalar context. Consider:
use strict; use warnings; my @single = (1); my @many = (1 .. 3); for my $ref (\@single, \@many) { my $count = listy(@$ref); my @elements = listy(@$ref); my ($firstElement) = listy(@$ref); print "Count $count, first: $firstElement, elements: @elements\n"; } sub listy { return @_; }
Prints:
Count 1, first: 1, elements: 1 Count 3, first: 1, elements: 1 2 3
If you write:
my ($thing) = UnPack (1, $stringpack);
I suspect your problem will be resolved. It's not what you return, it's the context that is important and the () provides a list context.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Force 'sub' return to be treated in list context?
by flexvault (Monsignor) on Apr 04, 2012 at 11:46 UTC |