Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Sometimes undef is initialized and sometimes not when hash values are fed to grep

by haukex (Archbishop)
on Feb 12, 2020 at 12:20 UTC ( #11112863=note: print w/replies, xml ) Need Help??


in reply to Re: Sometimes undef is initialized and sometimes not when hash values are fed to grep
in thread Sometimes undef is initialized and sometimes not when hash values are fed to grep

This data is checked carefully on input and output. When undef gets somehow into hash then error is thrown.

Ok, I see. Although of course there are exceptions, I find it easiest to work with hashes in one of two ways: either a fixed set of possible keys, in which case one can avoid autovivification issues by not differentiating between exists and defined, so it doesn't matter whether a key is present or not, and undef simply indicates "no value". Or, the other situation is a set of keys that is unknown to the code beforehand, in which case one generally only iterates over the hash with keys etc., and one doesn't generally access specific hash keys by name.

It sounds like you might be mixing these two - although there are sometimes valid cases where a hash has a known set of keys and one does want to differentiate between exists and defined, I've found this to be a little annoying to work with. For example, in my module IPC::Run3::Shell, there are options that can be exists but not defined, but then there is no easy way to override these options so they no longer exist in the hash.

Anyway, In the code you showed, I might have gone with exists checks to prevent autoviv, even if that makes the code more verbose.

  • Comment on Re^2: Sometimes undef is initialized and sometimes not when hash values are fed to grep
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2022-12-07 10:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?