I think the best approach is to split up your problem into several parts:

  1. Reading input files - you already have this using Path::Tiny
  2. Correlating the input data and replacing the input data in the template - this part maybe needs some more clarification but basically also works
  3. Writing the resulting output - again, Path::Tiny offers a way to write the files

Regarding issue 2, the input data: You haven't been explicitly clear about this, but I think from your statements that the first line of the users file corresponds to the first line of the IP addresses file. If that is the case, using a templating engine is even more advised, like HTML::Template, but you can also do with the simplest templating engine, s///e:

sub fill_template { my( $template, %values ) = @_; my $re_values = join "|", map { "\b$_\b" } reverse sort keys %valu +es; $template =~ s!($re_values)!exists $values{ $1 } ? $values{ $1 } : + $1!gre; } for my $linenumber (0..$#arr) { my $user_data = fill_template( $data, USER_C => $arr[$i], IP => $a +rr1[$i] ); print $user_data; # well, output to a file, but that's another pro +blem }

Update: s/Path::Class/Path::Tiny, spotted by marto


In reply to Re^5: Need to replace string from external file by Corion
in thread Need to replace string from external file by bhupi70007

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.