I have been working on a module that walks through the nodes of a deep structure and conditionally applies a filter callback to each node. Inline modification and Copy-On-Write is supported.

I am working on the final revisions of the code and fleshing out the POD some more before putting it on CPAN. Any feedback on naming, usefulness, etc would be appreciated. It has similarities to Data::Walk, but I feel it is sufficiently different to justify a separate module.


NAME

Data::DeepFilter - Deep modification of structures using callbacks. Optional Copy-On-Write.


SYNOPSIS

  use Data::DeepFilter qw(:all);
  deepfilter(
      data   => \%deep_hash,
      filter => filter_regex(qr/(abc)/, '\U\1')
      test   => name_in(qw(foo bar)),
  );


DESCRIPTION

Data::DeepFilter provides a mechanism for performing modifications to nodes within a deep structure. Optional support for Copy-On-Write, which protects the original structure.


deepfilter

  my $filtered_copy = deepfilter(
      data => \%deep_hash,
      filter => \&filter,
      test => \&test,
      safe => 1,
  );

Applies &filter to every node of $data that &test returns true for.

$data can be an arrayref or hashref.

The default behaviour is destructive in that it changes the structure that was passed. This behaviour can be overridden by specifying 'safe'.


Builtin filters

filter_regex()

  filter => filter_regex(qr/foo/,'bar')


Builtin tests

name_in()

  test => name_in(qw(foo bar))

name_not_in()

  test => name_not_in(qw(foo bar))

name_like()

  test => name_like(qr/foo/)

name_not_like()

  test => name_not_like(qr/foo/)


Filter callback specification

 sub filter_example {
     my ($value_ref) = @_;
     $$value_ref++;
 }


Test callback specification

 sub test_example {
     my ($name,$value_ref) = @_;
     return 1 if $name eq 'foo';
     return 1 if $$value_ref > 5;
     return;
 }

In reply to RFC - Data::DeepFilter by imp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.