print OUT "$var1_$var2\n"

The  '_' (underscore) character is a valid identifier, i.e., variable name, symbol. Therefore, the two variables you're using are  $var1_ (note trailing _), which is not defined, and  $var2, which is. Had you been using strictures as well as warnings, Perl would not have allowed this little oversight (see strict and warnings). The proper way to write this statement (if you really need the _) is
    print OUT "${var1}_$var2\n";
(Note that I've added a ; at the end; the absence of this statement terminator caused no syntactic problem in the given code, but would have eventually — trust me.)

open FH, "<infile.txt" or die $!;
open OUT, ">outfile.txt";

Update: You seem to be back-sliding. In contrast to your OPed code, this code uses global filehandles, two-parameter open, and does not check the status of the output file open. Tsk, tsk! IMHO, the OPed code used better practices.


Give a man a fish:   <%-(-(-(-<


In reply to Re^3: In place search and replace with a hash by AnomalousMonk
in thread In place search and replace with a hash by hkates

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.