Kjetil has asked for the wisdom of the Perl Monks concerning the following question:

Oh, deeply insightful ones, may I humbly request your attention?

I'm working on an AxKit Provider, and I'm creating data structures of threaded comments. To get the comments in the right order, I'm using Tie::IxHash.

I have the following code:

warn ("this comment: " . Dumper(${$comments}{$key})); my $comment = Tie::IxHash->new(${$comments}{$key}); warn ("comment: " . Dumper($comment));

where the two warnings are merely for debugging. When I dump the first ${$comments}{$key}, it results in the following output (in my Apache error log):

this comment: $VAR1 = { 'title' => 'Kommentarens tittel 5', 'content' => 'Dette er kommentar fem', 'user' => { 'username' => 'bamse', 'name' => '' }, 'storyname' => 'test1', 'timestamp' => '2003-03-07 00:00:00+01', 'sectionid' => 'tester', 'commentpath' => '/bamse' };

Then, I've tied it up, and the second dump results in this dump:

comment: $VAR1 = bless( [ { 'HASH(0x85380bc)' => 0 }, [ { 'title' => 'Kommentarens tittel 5', 'content' => 'Dette er kommentar fem', 'user' => { 'username' => 'bamse', 'name' => '' }, 'storyname' => 'test1', 'timestamp' => '2003-03-07 00:00:00+01', 'sectionid' => 'tester', 'commentpath' => '/bamse' } ], [ undef ], 0 ], 'Tie::IxHash' );

But, great ones, why is it inserting things like the final 'Tie::IxHash', the undef, and the 'HASH(0x85380bc)' => 0? This looks like metadata to me...

This makes a serious mess of the rest of my program (making addressing this difficult, and confuses other packages), and I do not understand what is going on.

Yours confusedly,

Kjetil

Replies are listed 'Best First'.
Re: Tie::IxHash weird metadata?
by grantm (Parson) on Mar 29, 2003 at 20:12 UTC

    It looks like you're using the return value from tie. You don't need to do that, Tie::IxHash modifies the behaviour of the underlying hash (or at least it appears to you program that it does) so you don't need the tied object at all. This snippet illustrates the difference.

    use Tie::IxHash; use Data::Dumper; my %data; my $ref = \%data; my $tied_obj = tie(%data, 'Tie::IxHash'); $ref->{one} = 1; $ref->{two} = 2; $ref->{three} = 3; $ref->{four} = 4; print Dumper $ref; print Dumper $tied_obj;

    The first print Dumps what you want. The second Dumps what you're getting.

    It's usually safe to ignore the return value from tie.

      Aha, now something dawned on me. Thanks a lot!

      I can't just ignore the return value, because I have to Push something on it later, but I can of course work on a different variable. When I do that, it gets through XML::Simple beautifully indeed! :-) Jihaa!

      However, I get a new problem, all this happens in a recursive sub, to go through the response tree. But modifying the underlying hash apparently has the side effect of stopping the recursion... :-( So, now I have a different problem to look into...

      Guess I'd just get to work...

        I can't just ignore the return value, because I have to Push something on it later

        If you're just 'push'ing something on, then you still shouldn't need the tied object reference - just assign a value to a key and TieIxHash will still retain the order.

Re: Tie::IxHash weird metadata?
by omnibus (Scribe) on Mar 29, 2003 at 20:04 UTC
    The "strangeness" in the second dump is a result of dumping an object. In the line
    my $comment = Tie::IxHash->new(${$comments}{$key});
    $comment is assigned an object reference; it is a thingy that has been blessed as a 'Tie::IxHash'. This is why the dump of $comment looks like
    comment: $VAR1 = bless( [ ... blah blah blah ... ], 'Tie::IxHash' );

    The rest of the 'strangeness' in the second dump consists of other internal data structures in a Tie::IxHash instance.

    The real issuse here is using the object correctly. Basically, you want to use the standard TIEHASH interface ot tie the value of your hash to a Tie::IxHash instance. I.e.:

    tie (%comments, Tie::IxHash); warn ("comment " . Dumper($comments{$key}));

    see the perlfunc:tie and perltie documentation for a more in-depth explaination of tied variables