1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
package Data::Find;
use warnings;
use strict;
use Carp;
use Data::Dumper;
use Scalar::Util qw( refaddr );
use base qw( Exporter );
our @EXPORT_OK = qw( diter dfind dwith );
=head1 NAME
Data::Find - Find data in arbitrary data structures
=head1 VERSION
This document describes Data::Find version 0.03
=cut
our $VERSION = '0.03';
=head1 SYNOPSIS
use Data::Find qw( diter );
my $data = {
ar => [1, 2, 3],
ha => {one => 1, two => 2, three => 3}
};
my $iter = diter $data, 3;
while ( defined ( my $path = $iter->() ) ) {
print "$path\n";
}
=head1 DESCRIPTION
=head1 INTERFACE
Nothing is exported by default. Use, eg,
use Data::Find qw( dwith );
to get the subroutines you need or call them with their fully
qualified name:
my $iter = Data::Find::diter $data;
=head2 C<< diter >>
Given an arbitrary data structure and (optionally) an expression to
match against elements in that structure returns an iterator which will
yield the path through the data structure to each matching element:
my $data = {
ar => [1, 2, 3],
ha => {one => 1, two => 2, three => 3}
};
my $iter = diter $data, 3;
while ( defined ( my $path = $iter->() ) ) {
print "$path\n";
}
would print:
{ar}[2]
{ha}{one}
In other words it returns paths to each element that contains the scalar
3. The returned paths can be used in conjunction with C<eval> to access
the matching elements.
The match expression can be
=over
=item * a scalar
=item * a regular expression
=item * a code reference
=item * C<undef>
=back
When the match expression is a code ref it will be passed each element
in the data structure in turn and should return true or false.
my $iter = diter $data, sub {
my $v = shift;
defined $v && !ref $v && $v % 2 == 1;
};
while ( defined ( my $path = $iter->() ) ) {
print "$path\n";
}
Note that the match code will see I<all> of the elements in the data
structure - not just the scalars.
If the match expression is C<undef> it will match those elements whose
value is also C<undef>.
=head3 Iterator
In a scalar context the returned iterator yields successive paths
within the data structure. In an array context it returns the path and
the associated element.
my $iter = diter $data;
while ( my ( $path, $obj ) = $iter->() ) {
print "$path, $obj\n";
}
=cut
sub diter {
my ( $obj, @match ) = @_;
my $matcher = @match ? _mk_matcher( @match ) : sub { !ref shift };
my @queue = ( [$obj] );
my %seen = ();
my %WALK = (
HASH => sub {
my ( $obj, @path ) = @_;
for my $key ( sort keys %$obj ) {
push @queue,
[ $obj->{$key}, @path, '{' . _fmt_key( $key ) . '}' ];
}
},
ARRAY => sub {
my ( $obj, @path ) = @_;
for my $idx ( 0 .. $#$obj ) {
push @queue, [ $obj->[$idx], @path, "[$idx]" ];
}
}
);
return sub {
while ( my $spec = shift @queue ) {
my ( $obj, @path ) = @$spec;
if ( my $ref = ref $obj ) {
unless ( $seen{ refaddr $obj}++ ) {
my $handler = $WALK{$ref} or croak "Can't walk a $ref";
$handler->( $obj, @path );
}
}
if ( $matcher->( $obj ) ) {
my $path = join '', @path;
return wantarray ? ( $path, $obj ) : $path;
}
}
return;
};
}
=head2 C<dfind>
Similar to C<diter> but returns an array of matching paths rather than
an iterator.
=cut
sub dfind {
my $iter = diter @_;
my @got = ();
while ( defined( my $path = $iter->() ) ) {
push @got, $path;
}
return @got;
}
=head2 C<dwith>
Similar to C<diter> but call a supplied callback with each
matching path.
dwith $data, qr/nice/, sub {
my ( $path, $obj ) = @_;
print "$path, $obj\n";
};
=cut
sub dwith {
my $cb = pop @_;
my $iter = diter @_;
while ( my ( $path, $obj ) = $iter->() ) {
$cb->( $path, $obj );
}
return;
}
sub _mk_matcher {
my $match = shift;
if ( ref $match ) {
if ( 'CODE' eq ref $match ) {
return $match;
}
elsif ( 'Regexp' eq ref $match ) {
return sub {
my $v = shift;
return unless defined $v && !ref $v;
return $v =~ $match;
};
}
}
if ( defined $match ) {
return sub { shift eq $match };
}
return sub { !defined shift }
}
sub _fmt_key {
my $key = shift;
return $key if $key =~ /^(?:\d+|[a-z]\w*)$/i;
chomp( my $rep
= Data::Dumper->new( [$key] )->Purity( 1 )->Useqq( 1 )->Terse( 1 )
->Dump );
return $rep;
}
1;
__END__
=head1 BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to
C<bug-data-find@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
=head1 AUTHOR
Andy Armstrong C<< <andy@hexten.net> >>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2009, Andy Armstrong C<< <andy@hexten.net> >>.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
|