You could make your "success" return value(s) dual-type (see dualvar) with the numeric part being zero, in which case you could simply test if the value is less than zero:

use strict; use warnings; use Scalar::Util 'dualvar'; for(my $ii = 1; $ii < 4; $ii++) { my $a = foo($ii); if($a < 0) # <-- { print "Fail occured with code: $a\n"; } else { print "Success\n"; } } sub foo { my $num = shift; if($num == 1) { return -1; } elsif($num == 2) { return -2; } else { return dualvar 0, "true\n"; # <-- } }

The main purpose of this is that the code would run cleanly under strictures, i.e. without eliciting the warning 'Argument "true\n" isn't numeric in numeric lt (<)'.

In other words, your original regular scalars would in principle work, too (a string which doesn't look like a number, evaluates to zero), but you'd have to say no warnings 'numeric'; — which ultimately could get even more unwieldy than what you have now, in case you try to restrict it to the minimal scope required every time...


In reply to Re: return from subfunction by Eliya
in thread return from subfunction by fidesachates

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.