Interesting question! I'd say that unless all of your return statements take some form of return $one, $two, $three;, that's something you could only do at runtime instead of by looking at the source, as it gets less clear how many values will actually be returned in situations like return @foo; or return bar();.

How about something like Hook::LexWrap (see also http://www.perladvent.org/2003/3rd/)? I haven't really used it before but it seems like it should be possible to implement a check like you want:

use Hook::LexWrap 'wrap'; sub my_func { return ("foo") x shift } wrap my_func => post => sub { my $cnt = @{$_[-1]}; # assumes list context! print "Returning $cnt values\n"; }; print my_func(1), "\n"; print my_func(2), "\n"; print my_func(3), "\n"; __END__ Returning 1 values foo Returning 2 values foofoo Returning 3 values foofoofoo

It may also be worth taking a step back: How many returns do you have per sub? What's the largest number of values per return you have? If you've got a lot of returns per sub, then perhaps your logic is getting a little too complex? And if you have a large number of values per return, then perhaps you could look into returning complex data structures instead, like arrayrefs, hashrefs, or objects?


In reply to Re: Checking number of values returned by Anonymous Monk
in thread Checking number of values returned by jfrm

Title:
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, details, 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, summary, 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.