blink has asked for the wisdom of the Perl Monks concerning the following question:

I'm creating a process which will regenerate a configuration file each time it is run. If a certain variable is found in the original config file, I want to avoid clobbering the entry while I'm regenerating the config file.Here's what the config file looks like:
<users> <00149> <devsrv1> lastLogin Never logged in approvedBy SLS </devsrv1> <devsrv2> lastLogin Never logged in </devsrv2> </00149> </users>
This would be what the original config file would look like. Let's say I'm ready to regenerate the config file. While parsing throught the above, I want to ignore all records which contain the variable, "approvedBy", when I'm regenerating the config file.

Here's the code that I've got, so far:

## assume that I'm using strict, using warnings, and that all data str +uctures tus far, are populated correctly. I'm truncating the code for + the sake of brevity foreach my $inner (keys %{$users{$lastuid}}) { my $last = $users{$lastuid}{$inner}; genCtl($inner, $last, $lastuid); } print FH "\t\<\/m$newuid\>\n"; } print FH "\<\/users\>\n"; $conn->close; close FH; my $conf = new Config::General( -ConfigPath => \@path, -ConfigFile => "$ctlFile", -ExtendedAccess => 1, -InterPolateVars => 1 ); my %config = $conf->getall(); $conf->save_file("$tmpctlFile"); &procCtl; sub genCtl { my ($server, $last, $uid) = @_; print FH "\t\<$server\>\n"; print FH "\t\tlastLogin $last\n"; print FH "\t\<\/$server\>\n"; print FH "\t\tlastLogin $last\n"; print FH "\t\<\/$server\>\n"; } sub procCtl { my $tmpCtl = new Config::General( -ConfigPath => \@path, -ConfigFile => "$tmpctlFile", -ExtendedAccess => 1, -InterPolateVars => 1 ); my %tmpCtl = $tmpCtl->getall(); my $goldCtl = new Config::General( -ConfigPath => \@path, -ConfigFile => "$goldCtlFile", -ExtendedAccess => 1, -InterPolateVars => 1 ); my %goldCtl = $tmpCtl->getall(); my $tmpUser = $tmpCtl->obj("users"); my $goldUser = $goldCtl->obj("users"); foreach my $person ($tmpUser->keys("users")) { foreach my $server ($tmpUser->keys("$person")->obj("server")) { { my $approved = $tmpCtl->obj("approvedBy"); next if ($approved); } } $tmpCtl->save_file("$goldCtlFile"); } }

Replies are listed 'Best First'.
Re: Comparing two config files using Config::General
by blink (Scribe) on Jan 02, 2004 at 15:44 UTC
    Problem solved, thanks to a kind and selfless monk Here's how it was done:

    The last looping construct was replaced with:

    my $tmpCtl = new Config::General( -ConfigPath => \@path, -ConfigFile => $tmpctlFile, -ExtendedAccess => 1, -InterPolateVars => 1 ); my %tmpCtlhsh = $tmpCtl->getall(); foreach my $user (keys %tmpCtlhsh){ foreach my $server (keys %{ $tmpCtlhsh{$user} }){ if (exists $tmpCtlhsh{$user}{$server}{approvedBy}){ print "user $user is approved for $server\n"; } else { print "should I delete $user access to $server?\n"; } } }