in reply to Re: Re^4: Determining what line a filehandle is on
in thread Determining what line a filehandle is on

Someone once told be a bit of wisdom: "Just because you can doesn't mean you should".

$. gets the job done, and it works consistently in a logical fasion. However, it's a shortcut, and it's not strictly necessary. If you want to show off, or you are in a tremendous rush where every keystroke counts because you are just doing something simple with -e, then by all means, use variables like it.

If you want to write something that is useful and comprehensible to others, be a sport and keep it simple.

If your favorite author suddenly started to use a heavy helping of very obscure words in their writing, would you go through all the trouble to look each and every one up? Does using fancy words make for a better book, or does it just make the author seem like some kind of deranged thesaurus addict?

In Code Complete, which frowns on "fancy code" heavily, an example given is the ?: operator which is fairly easy to understand, but can get way out of hand if used to an extreme. You can lose track of the meaning of the code even though it works:
if ($foo?$x?($y|$a<<2):$s&$z?$x==$v:0:$c) { ...
That might be the slickest, most compact, fastest executing way of expressing your particular problem, but to most people, it is going to be virtually impossible to find a problem with it. In some cases, "other people" means yourself a few years down the road when you've nearly forgotten how the program worked, yet have to fix a slight bug that was discovered.

Express it in a manner that is as simple as possible, but no simpler.

Replies are listed 'Best First'.
(ichimunki) re: $. versus $incremented_scalar
by ichimunki (Priest) on Jul 06, 2001 at 19:21 UTC
    I think your sample code is great in that it shows just how well the variable $. works when used correctly (in that if you are working with two filehandles, it really does work just like a pronoun by referring only to the most recently handled file).

    But I don't think $. is fancy code, any more than I think using
    sub my_func { my $arg1 = shift; my $arg2 = shift; ... }
    is fancy code for using an implied $_ in the argument assignements. $. is there so I, as a programmer, don't have to add code (and potentially bugs) to the process of counting which line I am on in a file, and as such it may help to make programs work better. I would say that using $. more than a couple of lines away from the filehandle is a bad idea-- same rules as for pronouns in spoken language.