[Pdx-pm] module recommendation: regex over data structure

Eric Wilhelm enobacon at gmail.com
Sat Jun 15 01:37:49 PDT 2013


# 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
---------------------------------------------------


More information about the Pdx-pm-list mailing list