This question is a continuation of my problem stated in
this node
I have an ASCII text file, which I need to test for proper syntax. This is a snippet from one of the files:
1 map http://www.digitalcity.com http://origin-www.digitalcity.com
2 map tunnel://www.digitalcity.com tunnel://origin-www.digitalcity.com
3 map http://10.0.2.1 http://origin-www.digitalcity.com
4 map tunnel://10.0.2.1 tunnel://origin-www.digitalcity.com
5 reverse_map http://origin-www.digitalcity.com http://www.digitalcity
+.com
6 reverse_map tunnel://origin-www.digitalcity.com tunnel://www.digital
+city.com
For each URI encountered in the file (ie, www.digitalcity.com), there should be 6 corresponding lines, exactly as stated above.
To accomplish my task, I am planning to use Text::Template, as such:
# pseudo-code
open ASCII text file
read in entire file, line by line into an array
ignore comments
foreach line matching regex,
push each subexpression into a hash
populate template
if the completed template is seen in the file, we're ok,
if not, die, and take action.
Now, I realize that what I have written thus far does not include logic to actually compare the template to my config file, or logic to "take action". First, I'd like to be able to confirm that I am matching the contents of the file.
Here is the code that I have, including what was suggested to me
here.
#!/usr/bin/perl -w
use strict;
use Text::Template;
my $OUT;
my $line;
my %vars;
my $lines;
my @lines;
my $file = "/home/trixee/remap.tmpl";
my $template = new Text::Template (TYPE => 'FILE' , SOURCE => $file)
or die "Couldn't construct template: $Text::Template::ERROR";
while (@lines = <DATA>) {
foreach $lines (@lines) {
chomp $lines;
next if ($lines =~ /^\#/);
if ($lines =~ /(\d+)(\s+)map(\s+)(\w+)\:\/\/(.*?)(\s+)(\4)\:\/\/(.
+*?)/) {
$vars{'lines'} = [];
push @{ $vars{'lines'} },
{'proto' => $4,
'uri1' => $6,
'uri2' => $8,
'd' => $1};
}
}
}
foreach $line (@lines) {
$OUT .= "line = $line->{'d'} map $line->{'proto'}://$line->{'uri1'} $l
+ine->{'proto'}://$line->{'uri2'}\n";
}
__DATA__
# I am a comment;
1 map http://www.digitalcity.com http://origin-www.digitalcity.com
2 map tunnel://www.digitalcity.com tunnel://origin-www.digitalcity.com
3 map http://10.0.2.1 http://origin-www.digitalcity.com
4 map tunnel://10.0.2.1 tunnel://origin-www.digitalcity.com
5 reverse_map http://origin-www.digitalcity.com http://www.digitalcity
+.com
6 reverse_map tunnel://origin-www.digitalcity.com tunnel://www.digital
+city.com
+++++++++++++++++++++++++++++++++++++++++++++
TEMPLATE:
{$d} map {$proto}://{$uri1} {$proto}://{$uri2}
{$d} map {$proto}://{$uri1} {$proto}://{$uri2}
{$d} map {$proto}://{$uri1} {$proto}://{$uri2}
{$d} map {$proto}://{$uri1} {$proto}://{$uri2}
{$d} reverse_map {$proto}://{$uri2}://{$proto}://{$uri1}
{$d} reverse_map {$proto}://{$uri2} {$proto}://{$uri1}
When, run, the above code produces no output. When run, the code that I posted previously prints only the *last* matched template found in the file. Can anyone help out a stuck monk?