Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
There's more than one way to do things

Comment on


Dear monks,

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:

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

I can accomplish what I want with this clumsy mess:

{ my @temp = bad(); say scalar @temp; }

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.

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 wantarray in...), but I will if absolutely necessary.

In reply to Trouble getting size of list returned from sub by wanna_code_perl

and:  <code> code here </code>

  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    As of 2013-12-20 21:51 GMT
    Find Nodes?
      Voting Booth?

      How do you parse XML?

      Results (433 votes), past polls