Looks like a bug in Perl but I can't say I blame Perl much for this one!

What you do above is take a reference to a non-existant hash value and then later try to use that as an anonymous hash in order to take another reference to a non-existant hash value. Then you assign to the last reference and expect all of the previous non-existant hash values to get autovivified into anonymous hashes!! I'm amazed it even works as well as it does!

This line: deref ($_[0]->{$1}, $_[1]); calls deref with a first argument of undef. Note that that line doesn't assign to $_[0]->{$1} so it doesn't autovivify anything at that point. Later you use that undef in that same line to get another undef. You are lucky that at this point Perl manages to autovivify the previous undef into an anonymous hash. It appears that in Perl 5.005 and 5.004, this isn't done perfectly so strange things can happen after this, somewhat at random (based on my testing).

You can fix the problem by removing the need to autovivify:

sub deref { return \$_[0] unless $_[1] =~ s/^-([\w\._]+)//; $_[0]->{$1} ||= {}; # Vivify an anonymous hash if needed. deref ($_[0]->{$1}, $_[1]); }

        - tye (but my friends call me "Tye")

In reply to (tye)Re: weird reference bug with perl 5.005_03 by tye
in thread weird reference bug with perl 5.005_03 by dash2

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.