Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Dumping Tie Objects

by bichonfrise74 (Vicar)
on Nov 20, 2009 at 21:42 UTC ( #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 (Chancellor) 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
Node Status?
node history
Node Type: note [id://808533]
help
Chatterbox?
and the universe expands...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-07-19 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (421 votes). Check out past polls.

    Notices?