With open (FH, "<$file1") or die "blahblah"; there is a possible security issue because maybe the variable $file1 might contain something that would cause problems. What goes in to the open() function is the interpolated string. You learned right, always use the < or > etc. In your example, if $file1 was ">ImportantFile", the open() will fail because the argument would evaluate to "<>ImportantFile" and open() won't like that filename! If you forgot the "<", then then open to ">ImportantFile" might succeed and it would be deleted or other various bad things could happen.

So if you use:  open (FH, '<', $file1) || die; putting the file mode explicitly there is a "small" thing that could save you big problems later.

If you are writing small one or two page programs, using a bare word like FH is no big deal. However, be aware that Perl like C (you have do it this way in C), can use lexical variables for filehandles. So you can open($infile, '<', $somefile)... and pass $infile to a subroutine just like any other Perl variable.

As far as $! in "die" messages, you might or might not want to put that there. Part of this depends upon how descriptive your part of the "die" message is! Ok, try some code:

#!/usr/bin/perl -w use strict; open(FH, '<', "bad") || die "your textXXX OS says: $!\n"; while (<FH>){} # prevents warning FH only used once
See what $! has to say. My OS prints, "your textXXX OS says: No such file or directory". I figure that "your textXXX" is way more important. Something like "can't open Budget.csv" is way more to the point than "No such file or directory"- most of the time the OS text is meaningless for the average user. Also notice what adding the trailing "\n" to the die message does.

Update: This thread has morphed into something else from the OP's original question. But I figure it is ok to comment on some of the comments to the comments!


In reply to Re^3: Reading two text files parallelly... by Marshall
in thread Reading two text files parallelly... by biswanath_c

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.