perlquestion
wanna_code_perl
<p>Dear monks,</p>
<p>I'll confess to feeling rather foolish right now. I have to work with a sub that is returning a list directly, and I don't need the contents of the list; just the number of elements, but I can't figure out how to do so without a temp variable. Here's a simplified example:</p>
<code>sub good { my @a = qw/zero one two/; @a }
sub bad { qw/foo bar baz/ }
say scalar good(), "\t<== what I want";
say scalar bad(), "\t<== not";
__END__
Output:
3 <== what I want
baz <== not
</code>
<p>I can accomplish what I want with this clumsy mess:</p>
<code>{
my @temp = bad();
say scalar @temp;
}</code>
<p>Is there no way to avoid the temp variable? And will Perl actually create a copy of the list in that case? My typical solution set is in the thousands.</p>
<p>As for the sub itself, it's someone else's XS code that I'm loathe to modify (not just something I can stick a <code>wantarray</code> in...), but I will if absolutely necessary.</p>