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>", }, }, }, }, }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

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
    This seems to work pretty well, except that it seems to get stuck when it parses into the next rule. I realized that I could have expanded my data example a bit. The rules I am trying to parse are sequential in the larry{} section. While your code works awesome, it seems to trip up just a little when encountering the next rule. Output example with my updated example above...
    { bob => { ed => { larry => { rule5 => { "action" => "allow", "application" => "[ any ]", "category" => "[ any ]", "destination" => "[ any ]", "from" => "[ prod-L3 ]", "hip-profiles" => "[ any ]", "log-end" => "yes", "log-setting" => "orion_log", "log-start" => "no", "negate-destination" => "no", "negate-source" => "no", "option" => { "action" + => "allow", "application" + => "[ any ]", "category" + => "[ any ]", "destination" + => "[ any ]", "disable-server-respo +nse-inspection" => "no", "from" + => "[ prod-L3 ]", "hip-profiles" + => "[ any ]", "log-end" + => "yes", "log-setting" + => "orion_log", "log-start" + => "no", "negate-destination" + => "no", "negate-source" + => "no", "service" + => "[ any ]", "source" + => "[ any ]", "source-user" + => "[ any ]", "to" + => "[ corp-L3 ]", "}" + => "rule6 { \n opt ion { \n disable-server-response-inspection no", }, "service" => "[ any ]", "source" => "[ any ]", "source-user" => "[ any ]", "tag" => "[ some_tag ]", "to" => "[ corp-L3 ]", }, }, }, }, }
      except that it seems to get stuck when it parses into the next rule.

      Yes. I see the problem. But I do not see an immediate solution and I'm rather involved in something else at the moment. (Sorry!)

      If you have other avenues of attack, pursue them rather than wait for this; as it might be a while.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.