Data::Overlay

Merge/overlay data with composable changes
Download

Data::Overlay Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Brad Bowman
  • Publisher web site:
  • http://search.cpan.org/~bowmanbs/

Data::Overlay Tags


Data::Overlay Description

Data::Overlay is a Perl module that can be used to apply a group of changes (also called an overlay) to a data structure, non-destructively, returning a shallow-ish copy with the changes applied. "Shallow-ish" meaning shallow copies at each level along the path of the deeper changes. $result = overlay($original, $overlay);The algorithm walks the overlay structure, either taking values from it, or when nothing has changed, retaining the values of the original data structure. This means that the only the overlay fully traversed.When the overlay is doesn't use any special Data::Overlay keys (ones starting with "="), then the result will be the merger of the original and the overlay, with the overlay taking precedence. In particular, only hashes will really be merged, somewhat like %new = (faults, %options), but recursively. This means that array refs, scalars, code, etc. will be replace whatever is in the original, regardless of the original type (so an array in the overlay will take precedence over an array, hash or scalar in the original). That's why it's not called Data::Underlay.Any different merging behaviour needs to be marked with special keys in the overlay called "actions". These start with an "=" sign. (Double it in the overlay to have an actual leading "=" in the result). The actions are described below, but they combine the original and overlay in various ways, pushing/unshifting arrays, only overwriting false or undefined, up to providing ability to write your own combining callback.SYNOPSIS#!perl -s #line 31 use strict; use warnings; use Data::Overlay qw(overlay compose); use Data::Dumper; $Data::Dumper::Sortkeys = 1; my $data_structure = { a => 123, b => { w => , x => "hello", y => \"world", }, c => , d => { da => [], db => undef, dc => qr/abc/ }, }; my %changes = ( f => 0, # add top level key a => '1, 2, 3', # overwrite key b => { z => '!' }, # nested operation c => { '=unshift' => 3.5 },# prepend array c => { '=push' => 7 }, # append array d => { da => , # replace w/ differing type db => { '=defor' => 123, # only update if undef }, }, ); # apply %changes to $data_structure (read-only ok), # returning a new data structure sharing unchanged data with the old my $new_data_structure = overlay($data_structure, \%changes); # Note sharing shown by Dumper print Dumper($data_structure, \%changes, $new_data_structure);Product's homepage


Data::Overlay Related Software