Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Text::Template iteration problem

by Tuna (Friar)
on Aug 11, 2001 at 00:53 UTC ( [id://104046]=perlquestion: print w/replies, xml ) Need Help??

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

How would one accomplish the following using Text::Template?

I am trying to confirm syntax on a particular config file. The code below populates the template, using values from the last iteration through %vars.

Thanks again, Steve

#!/usr/bin/perl -w use strict; use Text::Template; my %vars; my $lines; while ($lines = <DATA>) { next if ($lines =~ /^\#/); if ($lines =~ /(\d+)(\s+)map(\s+)(\w+)\:\/\/(.*?)(\4)\:\/\/(.*$)/){ $vars{'proto'} = $4; $vars{'uri1'} = $5; $vars{'uri2'} = $7; } } my $result = $template->fill_in(HASH => \%vars); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" } __DATA__ 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}

Replies are listed 'Best First'.
Re: Text::Template iteration problem
by PerlStalker (Initiate) on Aug 11, 2001 at 02:29 UTC

    Your post isn't very clear on what you are trying to accomplish. As you said, the code you posted will populate the template with the last .*map.* line. Although, you should probably add $vars{'d'} = $1; to fill out your template.

    If you just want to spit the file out again, then you can try something like this in your loop:

    $vars{'lines'} = []; push @{ $vars{'lines'} }, {'proto' => $4, 'uri1' => $5, 'uri2' => $7, 'd' => $1};

    Then you iterate in your template like so:

    { foreach $line (@lines) { $OUT .= "$line->{'d'} map $line->{'proto'}://$line->{'uri1'} $line +->{'proto'}://$line->{'uri2'}\n"; } }
    "To have something that works well, you have to reduce complexity. -- Werner Losh, FreeBSD Contributer

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-05-22 14:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found