Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: To warn or not to warn, that is the question

by extremely (Priest)
on Dec 03, 2000 at 14:11 UTC ( [id://44657]=note: print w/replies, xml ) Need Help??


in reply to To warn or not to warn, that is the question

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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://44657]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found