I had "use strict" in the program originally, but it seemed to do nothing but cause trouble;
it mostly meant that I had to put "my" before almost every variable, so I just got rid of it.
I'm also not sure what you mean by lexical file handles and 3-argument open.
The "my" is actually very useful because, apart from catching typos in misspelt variable names,
it limits the
scope of the variable.
A variable declared with "my" is known as a
lexical variable
because it has lexical scope; that is, its name is known from the point
of declaration to the end of the block it is declared in (or end of file if not declared in a block).
Hence my recommendation to use lexical file handles (e.g.
my $fhin) rather
than your global variable bareword file handle
IN.
Using lexical file handles is better style because:
- They are local variables and so avoid the generally evil programming practice of keeping state in global variables.
- They close themselves automatically when their lexical variable goes out of scope.
- They avoid the confusion of barewords. IMHO, barewords are an unfortunate Perl feature and should be avoided (except when writing poetry).
As for why the three-argument form of open is preferred, note that the
old two-argument form of open is subject to various security exploits as described
at Opening files securely in Perl.
See also the first four items of Perl Best Practices Chapter 10 (I/O), namely:
- Don't use bareword filehandles
- Use indirect filehandles
- If you have to use a package filehandle, localize it first
- Use either the IO::File module or the three-argument form of open
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.