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

Larry Wall compares $_, which is also magical, to the pronoun "it." Generalizing we can say that all magical variables of this kind are pronouns, e.g., "he", "she", "thou" etc.

One of the great things about Perl is that Larry Wall when explaining it uses ideas from linguistics. Pronouns exist in all spoken languages that I know of for a reason. They avoid repeating obvious information in a way that reduces readability.

It may be true that the "average" Perl programmer may not know all of the magical variables by heart, but he knows that an unusual short dollar sign symbol is a magical variable. Stripping the more arcane special variables out of Perl is like decreeing that from now on no one should use the word "thou" when speaking to others.

"To every thing there is a season" (King Solomon/Pete Seeger/Crosby and McGuinn)

  • Comment on Re: Re^4: Determining what line a filehandle is on

Replies are listed 'Best First'.
Re^6: Determining what line a filehandle is on
by tadman (Prior) on Jul 06, 2001 at 17:42 UTC
    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.
      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.