Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Contextual find and replace large config file

by trippledubs (Deacon)
on Jan 08, 2019 at 19:41 UTC ( #1228229=note: print w/replies, xml ) Need Help??


in reply to Contextual find and replace large config file

Not sure if this is too much or too little for you to plugin, but fun to learn some Parse::RecDescent. I could not figure out how to get the array list as the hash I wanted except to use unroll. Each parsing module requires it's own learning investment just browsing Regexp::Grammars from haukex's answer. If you need such a thing.
#!/usr/bin/env perl use strict; use warnings; use Parse::RecDescent; use Data::Dumper; $::RD_ERRORS = 1; $::RD_WARN = 1; $::RD_HINT = 1; #$::RD_TRACE = 1; #$::RD_AUTOACTION = q { print Dumper \@item }; my $grammar = q{ { use Data::Dumper; sub unroll { my @list = @{$_[0]}; my $unrolled; for my $href (@list) { for my $key (keys %{$href}) { $unrolled->{$key} = $href->{$key}; } } return $unrolled; }; } Expression: Object(s) { $return = unroll($item[1]) } Object: String '{' Param(s) '}' { $return = { $item[1] => unroll($item[3]) } } Param: String '=' String { $return = { $item[1] => $item[3] } } | Object(s) { $return = unroll($item[1]) } String: /[\w\d]+/ { $return = $item[1] } }; my $parser = Parse::RecDescent->new($grammar); my $text = do { undef $/; <DATA> }; my $tree = $parser->Expression($text) or die $!; $tree->{ObjectType1}{NestedObject}{DeeplyNested}{Param60} = 'tuna'; print Dumper $tree; __DATA__ ObjectType1 { Param1 = 8 Param2 = SomeText NestedObject { Param1 = 3 Param2 = MoreText DeeplyNested { Param50 = 500 Param60 = squid } } } ObjectType2 { Param1 = 3 Param2 = 40 }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1228229]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2020-10-20 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (209 votes). Check out past polls.

    Notices?