Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

What you're seeing is the effect of "autovivification". It's not the print that's doing this, it's the hash access. $hash{'key0'}{'key1'} means you're asking Perl what's stored in the hashref at the key key0 in %hash, but since that doesn't exist, Perl infers from $hash{key0}{...} that you want $hash{key0} to be a hashref, so it creates it for you. The same thing happens a level deeper at $hash{'key0'}{'key1'}{'key2'} - note how it doesn't create key2 for you.

If you want to avoid this in core Perl, then first note that autovivification, as a rule of thumb, happens in "lvalue" context, that is, in places where a value could be assigned to the hash. This is true in sometimes surprising contexts, such as for loops, because the loop variable is an alias to the values being looped over. If you want to avoid autovivification in such places, you need to use exists to check for the existence of hash keys before accessing them. An alternative is to use no autovivification from CPAN.


In reply to Re: "print" of nonexistent element is actually altering a hash by haukex
in thread "print" of nonexistent element is actually altering a hash by larrymenard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-12-08 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?