Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^5: RFC: Tutorial: use strict; now what!?

by tangent (Deacon)
on Feb 17, 2012 at 18:13 UTC ( #954577=note: print w/ replies, xml ) Need Help??


in reply to Re^4: RFC: Tutorial: use strict; now what!?
in thread RFC: Tutorial: use strict; now what!?

Thanks for taking the time to explain Xiong, and I get the connection now, even though I had to read through everything a few times. Being an obedient student, I always use warnings as well as strict, and note that I get a warning if I try to print $hash{pensionbalance} and also if I say something like:

my $debts = 100; my $money_left = $debts - $hash{pensionbalance}; # Use of uninitialized value in subtraction (-)
Am I safe to assume then that use warnings will always keep me safe?


Comment on Re^5: RFC: Tutorial: use strict; now what!?
Download Code
Re^6: RFC: Tutorial: use strict; now what!?
by runrig (Abbot) on Feb 17, 2012 at 18:57 UTC
    In this case it will. Or if you set all warnings to be fatal then you're extra "safe". Depending on how you use the value though, you may or may not get a warning. If you just use it as a boolean you do not get a warning:
    if ($hash{pensionbalance}) { print "Got a pension balance!\n"; }
      I kinda knew there would be something. And "if ($hash{pensionbalance})" is exactly the sort of thing I would put in to avoid all those "uninitialized value" errors. The plot thickens... I could change those conditions to:
      if (exists $hash{pensionbalance}) { print "It's there, there's just nothing in it\n" }
        Being an obedient student, I always use warnings as well as strict...
        The plot thickens...

        Sorry, tangent; neither strictures nor warnings are always appropriate. That's why they can be turned off -- in fact, they're off by default. Much power is hidden away by each. Usually you don't want, or need, to do such stuff. Sometimes you do.

        Also, no tool can guarantee that your code is bug-free or even typo-free. Some tools help to protect against some common mistakes but in the end, it's up to you. The most important abilities required to write less-buggy programs are (obliquely to Wall's dicta) humility, intellectual honesty, clean and consistent style, flexible focus, and raw experience.

        I would not recommend that you check every hash key for existence before each use. But if you want to do something like this, you can try Hash::Util or (if you tend to work with $hashrefs rather than actual %hashes) Data::Lock. Please note that these are not equivalent tools.

        Please don't take away from any comment here, especially from any comment of mine, that you must always or must never do anything. Software should be judged (in this order) by: (1) how well it works for the user, (2) how well it serves the needs of those who incorporate it as a component of other projects, and (3) how well it can be maintained, extended, and upgraded in future. Not on the list: impressing CS profs and didactic engineers busy evading their own work while prescribing your style.

        I'm not the guy you kill, I'm the guy you buy. —Michael Clayton
      Duplicate

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-08-20 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (120 votes), past polls