I experienced something this morning with the delete function that didn't seem to make sense. Here is the code in which the weirdness occurred:
my %subs = ( path => [ @{$session{hierarchy_path}} ] ); $subs{bottom_path_term} = ${pop(@{$subs{path}})}{term}; if (@{$subs{path}}) { delete $subs{path}[$#{$subs{path}}]{id}; } else { delete $subs{path}; }
Basically what is going on here is that I have an array of hashes that I want to manipulate without mutilating the orginal. The reference to the array is a value of one hash and I wanted to have a reference to the changed array be the value of another hash. (Maybe that's just a long way of saying that I wanted to make a copy of an array from one hash into a another hash.) Anyway, once I have a reference to the anonymous array, everything works as expected except the third line. Instead of deleting a key/value from the second hash (%subs), it deletes the key/value from the original hash (%session). I don't understand. If it was a problem with my dereferencing and referencing, I would expect pop to pop from the original hash but it doesn't. I used Data::Dumper to spew the structure of $session{hierarchy_path} both before and after this little snippet just to make sure I wasn't that crazy! Here's what it showed me:
Before: 'hierarchy_path' => [ { 'id' => 0, 'term' => 'Top of Hierarchy' }, { 'id' => '57000', 'term' => 'Education' } ], After: 'hierarchy_path' => [ { 'term' => 'Top of Hierarchy' }, { 'id' => '57000', 'term' => 'Education' } ],
Can anyone explain to me what's going on? BTW, I'm using Perl 5.6.1 on Linux

In reply to weirdness with delete by dsalada

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.