Data::XHash

Extended, ordered hash (commonly known as an associative array or map) with key-path traversal and auto...
Download

Data::XHash Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Brian Katzung
  • Publisher web site:
  • http://search.cpan.org/~bkatzung/

Data::XHash Tags


Data::XHash Description

Data::XHash is a Perl module that provides an object-oriented interface to tied, ordered hashes. Hash elements may be assigned keys explicitly or automatically in mix-and-match fashion like arrays in PHP.It also includes support for trees of nested XHashes, tree traversal, and conversion to and from native Perl data structures.Suggested uses include structured configuration information or HTTP query parameters in which order may at least sometimes be significant, for passing mixed positional and named parameters, or for porting PHP code.SYNOPSIS use Data::XHash; use Data::XHash qw/xhash xhashref/; use Data::XHash qw/xh xhn xhr xhrn/; $tiedhref = Data::XHash->new(); # A blessed and tied hashref # Note: Don't call "tie" yourself! # Exports are shortcuts to call Data::XHash->new()->push() # or Data::XHash->new()->pushref() for you. $tiedhref = xh('auto-indexed', { key => 'value' }); $tiedhref = xhash('auto-indexed', { key => 'value' }); $tiedhref = xhashref(); $tiedhref = xhn('hello', { root => { branch => } }); # (nested) $tiedhref = xhr(); $tiedhref = xhrn( } } ]); # (nested) # Note: $xhash means you can use either $tiedhref or the # underlying object at tied(%$tiedhref) ## Hash-like operations # Getting keys or paths $value = $tiedhref->{$key}; $value = $tiedhref->{\@path}; $value = $xhash->fetch($key); $value = $xhash->fetch(\@path); # Auto-vivify a Data::XHash at the end of the path $tiedhref2 = $tiedhref1->{ }; $tiedhref->{ }->$some_xh_method(...); $tiedhref = $xhash->fetch( ); $xhash->fetch( )->$some_xh_method(...); # Setting keys or paths $tiedhref->{$key} = $value; $tiedhref->{\@path} = $value; $xhash->store($key, $value, %options); $xhash->store(\@path, $value, %options); # Setting the next auto-index key $tiedhref->{[]} = $value; # Recommended syntax $tiedhref->{+undef} = $value; $tiedhref->{} = $value; # Any path key may be undef $xhash->store([], $value, %options); $xhash->store(undef, $value, %options); $xhash->store(, $value, %options); # Clear the xhash %$tiedhref = (); $xhash->clear(); # Delete a key and get its value $value = delete $tiedhref->{$key}; # or \@path $value = $xhash->delete($key); # or \@path # Does a key exist? $boolean = exists $tiedhref->{$key}; # or \@path $boolean = $xhash->exists($key); # or \@path # Keys and lists of keys @keys = keys %$tiedhref; # All keys; resets iterator @keys = $xhash->keys(%options); $key = $xhash->first_key(); $key2 = $xhash->next_key($key1); $key = $xhash->last_key(); $key = $xhash->next_index(); # The next auto-index key # Values @all_values = values %$tiedhref; @some_values = @{%$tiedhref}{@keys}; # or pathrefs @all_values = $xhash->values(); @some_values = $xhash->values(\@keys); # or pathrefs ($key, $value) = each(%$tiedhref); # Key/value iteration # Does the hash contain any key/value pairs? $boolean = scalar(%$tiedhref); $boolean = $xhash->scalar(); ## Array-like operations $value = $xhash->pop(); # last value ($key, $value) = $xhash->pop(); # last key/value $value = $xhash->shift(); # first value ($key, $value) = $xhash->shift(); # first key/value # Append values or { keys => values } $xhash->push(@elements); $xhash->pushref(\@elements, %options); # Insert values or { keys => values } $xhash->unshift(@elements); $xhash->unshiftref(\@elements, %options); # Export in array-like fashion @list = $xhash->as_array(%options); $list = $xhash->as_arrayref(%options); # Export in hash-like fasion @list = $xhash->as_hash(%options); $list = $xhash->as_hashref(%options); # Reorder elements $xhash->reorder($reference, @keys); # [] = sorted index_only # Remap elements $xhash->remap(%mapping); # or \%mapping $xhash->renumber(%options); ## TIEHASH methods - see perltie # TIEHASH, FETCH, STORE, CLEAR, FIRSTKEY, NEXTKEYProduct's homepage


Data::XHash Related Software