Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Your case may be exceptional but, most often, the right thing to do is just return the array. That allows the user to get behavior he is already familiar with from dealing with plain old arrays.

sub foo { my @r = (42) x $_[0]; return @r; } my @a = foo(3); my $s = foo(3); my ($sl)= foo(3); print "\@a: @a\n"; print "\$s: $s\n"; print "\$sl: $sl\n"; my @t = (42,42,42); @a = @t; $s = @t; ($sl) = @t; print '-'x40,"\n"; print "\@a: @a\n"; print "\$s: $s\n"; print "\$sl: $sl\n"; __END__ @a: 42 42 42 $s: 3 $sl: 42 ---------------------------------------- @a: 42 42 42 $s: 3 $sl: 42

Consequently, in most cases, I think that if a user wants to call a function that is documented to return a list and assign it directly to a scalar, it is fair to ask him to remember what he is doing. By the way, I don't think the right way to do that is

my ($x) = foo(1);
but rather
my $x = (foo(1))[0];
The results may be the same but, though the latter is more verbose, it makes the desired result obvious. It doesn't look like it might have been a mistake.

All of that said, I think using wantarray is probably the right way to do it in your case. It seems unlikely that someone would want to evaluate the returned list in scalar context to get the number of elements, so you are using the facilities Perl provides to get more useful behavior out of your function.

"My two cents aren't worth a dime.";

In reply to Re: Context aware functions - best practices? by sauoq
in thread Context aware functions - best practices? by Aristotle

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [LanX]: wot wot?
    [thezip]: W0t n0t
    [perldigious]: w4st3 n0t w0t n0t

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (15)
    As of 2017-03-24 18:13 GMT
    Find Nodes?
      Voting Booth?
      Should Pluto Get Its Planethood Back?

      Results (305 votes). Check out past polls.