Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Dumping Tie Objects

by bichonfrise74 (Vicar)
on Nov 20, 2009 at 21:42 UTC ( [id://808533]=note: print w/replies, xml ) Need Help??


in reply to Dumping Tie Objects

Nevermind. The reason why the Dumper line didn't work because I have not defined the FIRSTKEY and NEXTKEY functions.
sub FIRSTKEY { my ($self) = @_; each %{ $self->{'value'} }; } sub NEXTKEY { my ($self) = @_; each %{ $self->{'value'} }; }
Once I added the two functions, then the Dumper worked. But interestingly, how come Perl didn't tell me that the two functions need to be defined at all?

Replies are listed 'Best First'.
Re^2: Dumping Tie Objects
by AnomalousMonk (Archbishop) on Nov 21, 2009 at 00:15 UTC
    I think (perhaps more knowledgeable monks can confirm) that the  FIRSTKEY function needs to be defined such that it always returns the first key of the hash. As it is, it's the same as  NEXTKEY and only returns the first key upon the first iteration or when iteration wraps around to the beginning again.

    Try (untested):

    sub FIRSTKEY { my ($self) = @_; scalar keys %$Self; return each %{ $self->{'value'} }; }
    Update: keys resets the internal iterator of a hash. In void context, this happens with no other overhead, so the use of scalar above is only for the purpose of self-documentation.

    Also: On second thought, I would be inclined to implement  FIRSTKEY in terms of  NEXTKEY (also untested):

    sub FIRSTKEY { my ($self) = @_; keys %$Self; return $self->NEXTKEY; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://808533]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 10:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found