I think your case is well defined, as long as your strings don't look like a negative number. (you should change your regex to /^-\d$/).

But for a general pattern I prefer to flag error condition with an empty list, and to use a list assignment. Like this you can still return undef or an empty string in a boolean context (thats especially good for iterators)

(see semipredicate problem)

You can use one of the special vars for errors ($! $@ ...) to tunnel the message or just define your own special var in a dedicated namespace like $Error::message. (like this you're free to add as many informations as you want)

for (1..4){ print "$_: "; if ( ($str) = func($_) ) { # list assignment only false for empty li +st print "Success $str\n"; } else { print "Error $Error::message"; } } sub func { $Error::message="Wrong wrong wrong!!"; goto "L".shift; L1: return ""; L2: return 0; L3: return undef; L4: return; # blank returns empty list } __DATA__ 1: Success 2: Success 0 Use of uninitialized value $str in concatenation (.) or string at /tmp +/tst2.pl line 4. 3: Success 4: Error Wrong wrong wrong!!

Frankly I don't understand this culture to rely on returning false scalars...

Of course jdporters pattern with exceptions is slightly cleaner, but this pattern fits better into the standard pattern to simultaniously execute and check within a boolean context (if,unless,while,until, and,...)

Cheers Rolf

UPDATE: extended code example


In reply to Re: return from subfunction (list assignment rules) by LanX
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.