[Pdx-pm] module recommendation: regex over data structure
Joshua Keroes
joshua at keroes.com
Sat Jun 15 11:36:39 PDT 2013
#!/usr/bin/env perl
use 5.10.0;
use strict;
use warnings;
use Data::Visitor::Callback;
use Data::Dumper;
my $ds = [ 1, 20, 300, \@INC, \%INC ]; # a complex data structure to play
with.
say "before:\n" . Dumper $ds;
my $v = Data::Visitor::Callback->new(
value => sub { s/\w+/-/g }, # handle everything but keys.
);
$v->visit($ds); # destructive to $ds
say "after:\n" . Dumper $ds;
On Sat, Jun 15, 2013 at 11:22 AM, benh <ben.hengst at gmail.com> wrote:
> I agree though in my reading that is an expected feature:
>
> > ...would replace foo with bar in all strings at all levels.
>
> Though again I could also be completely missing the intent here so it
> is a completely fair thing to mention.
>
> On Sat, Jun 15, 2013 at 10:10 AM, Braden Kelley <bmk at rentrak.com> wrote:
> > One potential problem with serializing using something like JSON::XS is a
> > simple regex replace would also end up replacing hash keys, not just
> their
> > values.
> >
> > # from Josh Heumann on Friday 14 June 2013:
> >>Using YAML/JSON isn't a bad idea, but I'm worried about the
> >>performance hit.
> >
> > As long as your replacement doesn't break quoting or otherwise trip on
> > the serialization, yeah. JSON::XS was super quick and the fastest
> > serializer last time I checked. You'll need the ram.
> >
> > Recursive implementation is easy enough.
> >
> > sub replace_deeply {
> > my ($data, $match, $replace) = @_;
> > my $r;
> > $r = sub {
> > my $ref = ref($_[0]) or return $_[0] =~ s/$match/$replace/;
> > if($ref eq 'ARRAY') {
> > $r->($_) for @{$_[0]}
> > }
> > elsif($ref eq 'HASH') {
> > $r->($_) for values %{$_[0]};
> > }
> > };
> > $r->($data);
> > return $data;
> > }
> >
> > --Eric
> > --
> > ---------------------------------------------------
> > http://scratchcomputing.com
> > ---------------------------------------------------
> > _______________________________________________
> > Pdx-pm-list mailing list
> > Pdx-pm-list at pm.org
> > http://mail.pm.org/mailman/listinfo/pdx-pm-list
> >
> > _______________________________________________
> > Pdx-pm-list mailing list
> > Pdx-pm-list at pm.org
> > http://mail.pm.org/mailman/listinfo/pdx-pm-list
>
>
>
> --
> benh~
>
> http://about.notbenh.info
>
> Stability is not a Regression.
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/pdx-pm-list/attachments/20130615/de9fd468/attachment.html>
More information about the Pdx-pm-list
mailing list