$! is magic.

Its magic is documented in perlvar, but basically, what $! contains is the "errno" C runtime library value for the last system-related (I don't want to say "system" call, since I don't only mean that function) call that failed.

You CAN assign to errno, but only integer values, which you can find in your system's documentation, or in the errno.h C header file for your platform (if it's unix-ish, check /usr/include/errno.h).

The "string" value of $! is whatever string your system decides goes with a given integer, so none of them are going to be "these were not digits". Your best bets in that case might be 22 (EINVAL "Invalid argument") or 33 (EDOM, "Domain error in Math Function). At least, those are valid for AIX, Solaris, and Linux, and probably for other Unices, as well as for Windows. (A quick search finds that EDOM is actually required to exist per the C standard - of course, it's likely that nothing says it has to be 33 on your platform...)

To test, you can do:

perl -e'foreach(1..33){$!=$_; print "$_: $!\n"}'

Mike

In reply to Re: How do I return false and set an error in special variable $! by RMGir
in thread How do I return false and set an error in special variable $! by leocharre

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.