Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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

by runrig (Abbot)
on Feb 17, 2012 at 18:57 UTC ( #954588=note: print w/replies, xml ) Need Help??

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

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"; }

Replies are listed 'Best First'.
Re^7: RFC: Tutorial: use strict; now what!?
by tangent (Vicar) on Feb 17, 2012 at 19:16 UTC
    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
        ... in fact, they're off by default.

        That's no argument against either one. (I agree that some of the default warnings are questionable, but that's a different discussion.) Plenty of things have the wrong defaults: Unix crypt, C's string handling, Perl 5 optimizing its defaults for awk-replacement one-liners instead of programs, and more.

        Hi Xiong, thanks again. I was being a bit tongue in cheek when I said about "being an obedient student", though I do always use warnings in development and find it invaluable. runrig's comment showed me that by writing code just to have it comply with warnings could actually introduce more subtle errors. Taking from this, I have written a little test script to find these errors rather than weigh down my code.
        The most important abilities required to write less-buggy programs are (obliquely to Wall's dicta) humility, intellectual honesty...
        My reading of Larry suggests you have to follow your virtuous path in order to reach his. For example, I have learned to change my attitude to dealing with mistakes in my design or code (aka: error reports). Users are really quick at finding errors and unforeseen traps. Instead of being protective and defensive (hubris?), or blaming Internet Explorer, I will happily describe exactly how I've erred. Then, if I fix things quickly, I find the user has more faith in both me and the code. After many such iterations, finally I'm allowed some pride.
Re^7: RFC: Tutorial: use strict; now what!?
by tangent (Vicar) on Feb 17, 2012 at 19:28 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://954588]
[usemodperl]: definitely

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2018-06-24 17:08 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.