in reply to Approach to remove brackets from config

Just as samtregar suggested, a two-pass approach seemed right to me too. First pass, parse(), parses the data into a hashref, and the second pass, get_command_args(), extracts the data from the hash ref to build the list of command arguments.

Two recursive subroutines in one perl program. I think I've been doing too much emacs lisp :-)

use strict; use warnings FATAL => 'all'; chomp (my @config = <DATA>); my $config = parse(); for my $server ( keys %$config ) { my @args = get_command_args( $config->{$server} ); print "enable $server $_\n" for @args; } sub get_command_args { my $cfg = shift; my @arg_list; for my $k ( keys %$cfg ) { my $v = $cfg->{$k}; my $args = "$k "; if ( ref $v ) { $args .= join " ", get_command_args( $v ); } else { $args .= "$v "; } push @arg_list, $args; } return @arg_list; } sub parse { my $hash; while ( my $line = shift @config ) { if ( $line =~ /(\S.*\S) \s* \{/x ) { # keywords + { my $k = $1; $hash->{$k} = parse(); } elsif ( $line =~ /(\S+) \s+ (\S+) \s* ; \s* $/x ) { # key valu +e; $hash->{$1} = $2; } elsif ( $line =~ /^ \s* } \s* $/x ) { # } return $hash; } else { die $line } # can't ha +ppen! } return $hash; } __DATA__ server { hostname test; console-type { type vt100; } account root { authentication-type { password "bubba"; } } }

Output:

enable server account root authentication-type password "bubba" enable server hostname test enable server console-type type vt100