There's nothing "wrong" with using $. since it
accurately represents the line number read of the last
file-handle read. You can even track more than one
file at the same time:
open (A, "ls|");
open (B, "ls|");
while (<A>)
{
print "A: $. ";
<B>;<B>;
print "B: $. ";
}
close (A);
close (B);
Which returns the output:
A: 1 B: 2 A: 2 B: 4 A: 3 B: 6 A: 4 B: 8 A: 5 B: 10
However, $. is what I would call a "magical" Perl variable
because it comes from nowhere and has no inherent meaning.
With
English, it is $INPUT_LINE_NUMBER, and with
IO::Handle
you can use the method
input_line_number,
but the $INPUT_LINE_NUMBER variable is still "magical",
and the
IO::Handle system just makes things more
difficult to implement.
"Magical" variables, or those that are effectively, though
not intuitively, linked to another variable or action are
certainly able to be used, but the issue I have with them
is that to the average programmer, especially one not
intimately familar with Perl, is that they don't make
any sense. Using them on a quick hack is one thing, but
using them on a program that might have to be maintained
by others is a counterproductive form of obfuscation.
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.