My second programming teacher (Basic on CommodeDoor if you
insist I age myself) had a wonderful way of asking me
questions in the grand greek tradition to make me answer
my own questions. He rocked so in Mr McGuire's manner I
ask:
- You mean you aren't checking the return of a sub
before using what it returns?
- Why would you suppress the error system rather than
the error?
- Could you change the sub so that it didn't return
a non-numeric answer?
- Could you do as Mr. Fastolfe says and ensure the
value of $x with the lovely my $x= subr() || 0;?
- Do you want programming in perl to be safe or sloppy?
- Testing for "definedness" isn't irrelevant in and of
itself, do you really think that your testing of it was?
Did "-w" think it was?
Ok I have to be declarative, I can't stand it. =)
The flaw in your example is that you aren't controlling
the issue in the correct place. If you wish to use
some_sub() as numeric, make sure it returns
a numeric value. Warnings are a contract with yourself,
keep the contract at both ends. If you are explicitly
ignoring a return (i.e. if the sub could return both 0
and undef) then don't get hung up on making that choice
explict.
Also, while you are benchmarking, try the same test
with warnings on and off and try turning the warnings on
and off within the loop you test as well.
I'm willing to go out on a limb and bet the bit-flag
test that defined() does is way cheaper than
turning on and off warnings and _majorly_ cheaper than
printing errors.
Perl is still doing the test for defined, it has to
for greater than so it knows to promote $_ to 0 for
the comparison. All you did is ask for the result of
that test.
--
$you = new YOU;
honk() if $you->love(perl)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.