My question is similar to this one.

I have a file, in which I am trying to check syntax, 6 lines at a time. The file will vary in length and may contain comments and/or empty lines.

I'm using Text::Template (thanks to a few of you) to create a template of what each 6-line block should look like:

{$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri1} {$proto}://{$uri2} {$map} {$proto}://{$uri2} {$proto}://{$uri1} {$map} {$proto}://{$uri2} {$proto}://{$uri1}

Here's some sample data that I am working with:

map http://chat.yahoo.com http://origin-chat.yahoo.com map tunnel://chat.yahoo.com tunnel://origin-chat.yahoo.com map http://10.0.2.7 http://origin-chat.yahoo.com map tunnel://10.0.2.7 tunnel://origin-chat.yahoo.com reverse_map http://origin-chat.yahoo.com http://chat.yahoo.com reverse_map tunnel://origin-chat.yahoo.com tunnel://chat.yahoo.com map http://shop.yahoo.com http://origin-shop.yahoo.com map tunnel://shop.yahoo.com tunnel://origin-shop.yahoo.com map http://10.0.2.8 http://origin-shop.yahoo.com map tunnel://10.0.2.8 tunnel://origin-shop.yahoo.com reverse_map http://origin-shop.yahoo.com http://shop.yahoo.com reverse_map tunnel://origin-shop.yahoo.com tunnel://shop.yahoo.com

Here's the program, thus far:

#!/usr/bin/perl -w use strict; use Text::Template; my %vars; my $lines; my @lines; my $file = "/home/trixee/remap.config"; my $template = new Text::Template (TYPE => 'FILE' , SOURCE => $file) or die "Couldn't construct template: $Text::Template::ERROR"; open FH, "$file" or die "$!\n"; while ($lines = <FH>) { chomp $lines; next if ($lines =~ /^\#/); if ($lines=~/(\d+)(\s+)(map|reverse_map)(\s+)(\w+)\:\/\/(.*?)(\s+)(\5 +)\:\/\/(.*?)$/) { $vars{'d'} = $1; $vars{'map'} = $3; $vars{'proto'} = $5; $vars{'uri1'} = $6; $vars{'uri2'} = $8; } } my $result = $template->fill_in(HASH => \%vars); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" }

Using the example data above, I would need to modify the existing code to read lines 1-6 and populate the template. Then, I would add logic to compare the populated template with lines 1-6 of the data file. If they are equal, then read in lines 7-12, and repeat the comparison logic.

Any ideas? Am I appoaching this efficiently?


In reply to Reading in N lines at a time by Tuna

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.