in reply to Parsing a config file with braces and nested braces
If you're not pathologically adverse to a non-CPAN solution:
#! perl -sl use strict; use Data::Dump qw[ pp ]; sub parseConfig { my( $source, %config ) = shift; $source =~ s<(\S+)\s*(\{)\s*(.+)\}|(\S+)(\s+)([^;]+);> < my( $key, $f, $rest ) = ( $1 || $4, $2 || $5, $3 || $6 ); $config{ $key } = $f eq "{" ? parseConfig( $rest ) : $rest; >seg; return \%config; } my $config = parseConfig( do{ local $/; <DATA> } ); pp $config; __DATA__ ## data per your OP
Produces:
C:\test>junk70 { bob => { ed => { larry => { rule5 => { action => "allow", application => "<a href=\"?nod +e=%20any%20\"> any </a>", category => "<a href=\"?nod +e=%20any%20\"> any </a>", destination => "<a href=\"?nod +e=%20any%20\"> any </a>", from => "<a href=\"?nod +e=%20prod-L3%20\"> prod-L3 </a>", "hip-profiles" => "<a href=\"?nod +e=%20any%20\"> any </a>", "log-end" => "yes", "log-setting" => "orion_log", "log-start" => "no", "negate-destination" => "no", "negate-source" => "no", option => { "disable-serv +er-response-inspection" => "no" }, service => "<a href=\"?nod +e=%20any%20\"> any </a>", source => "<a href=\"?nod +e=%20any%20\"> any </a>", "source-user" => "<a href=\"?nod +e=%20any%20\"> any </a>", tag => "<a href=\"?nod +e=%20some_tag%20\"> some_tag </a>", to => "<a href=\"?nod +e=%20corp-L3%20\"> corp-L3 </a>", }, }, }, }, }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing a config file with braces and nested braces
by IamtheGorf (Initiate) on Jan 07, 2015 at 19:07 UTC | |
by BrowserUk (Patriarch) on Jan 07, 2015 at 20:25 UTC |