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??

First of all, this is about pseudo-hashes. This (now long gone) feature lets you access an array reference as if it was a hash reference.

So what does $aref->{key} actually do? Well, this: $aref->[$aref->[0]{key}]. That is, it fetches the first element of the array (which must be a hashref) and uses it to map the key to an array index, which is then finally looked up in @$aref.

The other piece of the puzzle is autovivification (also explained in perldoc perlref). This is what automatically creates references for you when you try to dereference something that doesn't exist. Example:

my $foo; $foo->[1] = 42; # automatically sets $foo = [] before the # assignment so you end up with $foo = [undef, 42] instead of # dying with "Can't use undef as an ARRAY reference"

OK, now let's look at your code again. $a->{test} looks up "test" in { test => { val => 1 }} (the first element of @$a) and returns { val => 1 }. It then uses this reference as an array index, which converts it to a (very large) integer (the internal memory address of the hash).

$a->[0xDEADBEEF] obviously doesn't exist; the array only has two elements. But since you're dereferencing it (the ->{val} part), perl tries to autovivify a hashref for you. Unfortunately this involves resizing the array to have 0xDEADBEEF + 1 elements (the intervening elements are set to undef). At this point perl is running out of memory, as expected.


In reply to Re: Wrong dereference => Out of memory by Anonymous Monk
in thread Wrong dereference => Out of memory by schetchik

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (13)
    As of 2014-07-10 23:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (217 votes), past polls