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

Josh Heumann pdx.pm at joshheumann.com
Thu Jun 27 11:56:25 PDT 2013


Those are good suggestions, too, however YAML wins big because we're
already using this method of interpolation elsewhere, so it doesn't
introduce inconsistency.

Plus, there's the age-old (and horrible), "installing new modules is a PITA
in this context because $reason", which, sadly, is in play in this
situation.

J


On Wed, Jun 26, 2013 at 2:57 PM, Michael G Schwern <schwern at pobox.com>wrote:

> Just to throw another into the mix: JSON::Path.  It's like XPath but for
> JSON.  That's your "regex over data structure".
> https://metacpan.org/module/JSON::Path
>
> And if performance IS a concern, here's Andy Armstrong's version in C.
> https://github.com/AndyA/jsondata
>
>
> On Wed, Jun 26, 2013 at 10:56 AM, Josh Heumann <pdx.pm at joshheumann.com>wrote:
>
>> Reporting in!
>>
>> All of these methods worked really well.
>>
>> Data::Dumper.  Intriguing that it works, I never would have thought about
>> it.  In a post on stack overflow, Schwern mentions that Data::Serializer is
>> a better way to go (thought I didn't try it).
>>
>> Data::Visitor::Callback.  The most elegant solution.  I love that there's
>> a module for this, and it does what it says on the box.
>>
>> YAML.  Probably what we'll go with, since it's a technique that we're
>> using elsewhere in this library, and clearly performance isn't a concern
>> there.
>>
>> Thanks everyone for your suggestions.  This is why I love this community.
>>
>> J
>>
>>
>>
>> On Sat, Jun 15, 2013 at 1:02 PM, Jason Messer <jasoncmesser at gmail.com>wrote:
>>
>>> I somewhat regularly use something like the following
>>>
>>> use Data::Dumper;
>>> $Data::Dumper::Terse = 1;
>>>
>>> $foo = {
>>>         'onexx' => 1,
>>>         'twoxx' => 2,
>>>         'threexx' => 3,
>>>         'fourxx' => ["axxb", "xbx", 'c', 'x', "d", "e", 'f', 42],
>>>         'inc' => \@INC,
>>>         'env' => \%ENV
>>> };
>>>
>>> $x = Dumper($foo);
>>>
>>> $x =~ s/xx/__XX__/g;
>>> $x =~ s/perl/##PERL##/g;
>>> $e = eval($x);
>>> print Dumper($e)
>>>
>>> This has the advantage of simplicity, you can dump the string to a file
>>> and monkey with it with an editor before evaling it back.
>>>
>>> It has all the problems of unstructured text, and it's definitely on the
>>> quick and dirty side of performance considerations, but it works and is
>>> simple.
>>>
>>> -Jason
>>>
>>>
>>>
>>> On Sat, Jun 15, 2013 at 12:32 PM, Josh Heumann <pdx.pm at joshheumann.com>wrote:
>>>
>>>> To be more clear, searching hash keys isn't required as the string will
>>>> have a sigil to identify strings to be interpolated, but if the strategy
>>>> avoided looking at hash keys, that would help avoid potentially
>>>> catastrophic mistakes.
>>>>
>>>> J
>>>>
>>>>
>>>> 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
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> 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
>>>
>>
>>
>> _______________________________________________
>> 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/20130627/76a6e2fe/attachment-0001.html>


More information about the Pdx-pm-list mailing list