Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Text::Template problems continue

by Tuna (Friar)
on Aug 14, 2001 at 08:37 UTC ( [id://104673]=perlquestion: print w/replies, xml ) Need Help??

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

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 2 map tunnel:// tunnel:// 3 map 4 map tunnel:// tunnel:// 5 reverse_map http://www.digitalcity 6 reverse_map tunnel:// tunnel://
For each URI encountered in the file (ie,, 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 2 map tunnel:// tunnel:// 3 map 4 map tunnel:// tunnel:// 5 reverse_map http://www.digitalcity 6 reverse_map tunnel:// tunnel:// +++++++++++++++++++++++++++++++++++++++++++++ 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?

Replies are listed 'Best First'.
Re: Text::Template problems continue
by Aighearach (Initiate) on Aug 14, 2001 at 10:49 UTC

    I see two big bugs.

    • One:
    • You're using <DATA> in list context, which will only ever return anything(everything!) once, but you're using it in a while loop. The second loop iteration will assign an empty list to @lines, then evaluate false and leave the while loop. Just erase the while loop, and leave it's conditional as a non-loop-control statement, and this bug should be fixed.
    • Two:
    • The last foreach is rather borked. You're trying to use strings as hash refs. You should replace @lines with @{ $vars{lines} } I think, but I can't really tell what you're doing so I'm not totally sure.
    Also, are you really trying to throw away the reverse_map lines? It isn't clear. You should handle this case like the comment, if you're expecting that data and don't want it. That will make the code more clear. Also, consider starting that regex with ^\s* to speed the program up.
    Snazzy tagline here

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://104673]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-05-26 05:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found