While working on some code this evening, I came across an interesting problem that was entirely the result of my own doing and thought that I would share this with others.

The problem manifest itself with the failure of information stored in an array within a hash to be updated - For example, the following example illustrates the basic intention of my code, that is, to manipulate an array within a hash element, in this contrived example, to remove web page links from a user profile if the associated checkbox is ticked on a 'delete' form:

my %user; eval { my $session_id = $cgi->cookie( -name => 'mycookie', -path => '/' ) +; tie %user, 'Apache::Session::DB_File', $session_id, { 'FileName' = +> '.sessions' }; }; croak( 'Cannot retrieve user session ID from client-side cookie' ) if +$@; foreach (0..$#{$user{'links'}}) { if ($cgi->param('link_'.$_)) { splice( @{$user{'links'}}, $_, 1 ) } }

As the code above demonstrates, there is little in the code itself to raise any red flags, however, it simply wasn't working in the larger script - The array within the hash element remained unmodified with execution, when all logic and passed parameters suggested that array elements should be spliced from the data structure.

I freely admit, I was somewhat puzzled by this and pondered on it for some time. Then, as I stared vacantly at my computer screen, I remembered something that I read about tied-hashes long ago in the MLDBM man page - You cannot directly modify an existing reference within a tied hash interface; it must first be retrieved and stored in a temporary variable for further modifications. This limitation, according to the MLDBM man page, is because the Perl TIEHASH interface currently has no support for multi-dimensional ties.

Therein lies the problem with my code - One cannot directly edit data structures stored within a tied-hash. Sure enough, once I updated this section of code to copy the hash element to a temporary array prior to splicing modification, the code worked perfectly. As such, this node, while not illustrative enough on its own to form a tutorial (or necessarily pensive and/or thought-provoking enough to warrant a true meditation), may serve as a solution for others who come across a similar problem.

The lesson learnt - Context is everything when it comes to debugging! :-)

 


In reply to The importance of context in debugging by rob_au

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.