Data::Couplet - Yet another (But Hopefully Better) Key-Value Storage mechanism
version 0.02004314
use Data::Couplet; # Retain order. my $couplet = Data::Couplet->new( a => $b , c => $d ); my $output = $couplet->value('a'); # returns $b; my $hash = { 'this is a' => 'key' }; $couplet->set( $hash, "hello"); $couplet->value( $hash ); # hello
Lots of stuff is probably still broken, unimplemented, untested.
User beware
Why is this module different?
Tied hashes are IMO Ugly. Objects are far more handy for many things. Especially in moose world. You want tied hashes, do it yourself.
I want it to be possible, to retain arbitrary references used as keys.
Its not here yet, but there Will eventually be reordering functions.
I seriously looked all over CPAN for something that suited my needs and didn't find any.
I then tried with Tie::IxHash::ButMoreFun, and then discovered that how I was using Tie::IxHash wasn't even sustainable on different versions of Perl, and based on the 1997 release date, I gave up on seeing that fixed.
Create a new Data::Couplet entity using a series of ordered pairs.
$c = Data::Couplet->new( 'a' => 'b', 'c' => 'd' );
Record the association of a key ( any object that can be coerced into a string ) to a value.
New entries are pushed on the logical right hand end of it in array context.
# { 'a' => 'b', 'c' => 'd' } set( 'a', 'e' ); # { 'a' => 'e', 'c' => 'd' } set('e', 'a' ); # { 'a' => 'e', 'c' => 'd', 'e' => 'a' }
Entries are ripped out of the structure, and all items moved around to fill the void.
# { 'a' => 'b', 'c' => 'd','e'=>'f' } ->unset( 'c' ); # { 'a' => 'b', 'e'=>'f' } ->unset('a'); # { 'e' => 'f' }
Like ->unset, except you know where ( logically ) in the order off things the entry you wish to delete is.
->unset_at( 1 ); ->unset_at( 0 );
Should be identical to the above code.
This is what ->unset ultimately calls, except ->unset does implicit object_to_key conversion first. At present, that's not anything huge, its just $object to convert it to a string. But this may change at some future time. So use that method instead.
$object
Returns a value associated with a key object. See "unset" for the semantics of what object keys are.
Like value, but you need to know where in the data set the item is.
returns an array of all stored values in order.
Just some nice syntax for [$o->values]
Returns an ordered sequence of key,value pairs, just like that passed to the constructor.
my @d = $o->key_values() while( @d ){ my $key = shift @d; my $value = shift @d; print "$key => $value\n" }
Returns like ->key_values does but key/value is grouped for your convenience
for ( $o->key_values_paired() ){ my ( $key, $value ) = @{ $_ }; }
returns all known keys in order
Given an index, return the key that holds that place.
Given a string key, returns the object stored there.
This is probably very unhelpful to you unless you explicitly asked us for our internal key name.
As with key_object, except partially useful, because you can fetch by ID.
By default, this package imports a few methods from various plug-ins.
Data::Couplet::Plugin::KeyCount
Data::Couplet::Plugin::BasicReorder
Kent Fredric <kentnl at cpan.org>
This software is copyright (c) 2011 by Kent Fredric.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Data::Couplet, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Couplet
CPAN shell
perl -MCPAN -e shell install Data::Couplet
For more information on module installation, please visit the detailed CPAN module installation guide.