Schnee (schnee) wrote,

$_ again

Seems I'm not the only one who's annoyed by $_ in Perl being an aliased global. Rafael Garcia-Suarez wrote about it on perl5-porters earlier this year, and mentioned the reason why it is that in the first place:

$_ (or the ability to not specify an implicit topic, like in natural languages) is an essential part of the perlishness of perl. There is no intrinsic value in having $_ as a global at all; by essence a topic is local to its context. However $_ is global because, being a Perl 1 feature, it predates the very existence of lexical variables.

In other words, it was a mistake that was made and that can't (easily?) be fixed now because of the need for backwards compability; but at least I'm not alone in seeing it as a problem. BTW, in that older post, I also mentioned the option of localizing it to avoid problems; Rafael also mentioned that, in passing:

Having a global $_ creates a mismatch between the implementation (the _ glob) and the language design intent behind $_. In practice that is revealed by confusing action-at-distance bugs (either on $_ itself or via *_; or having "local $_" not working as intended).

I'm not sure what "not working as intended" would entail, but apparently it might not be the easy way out I (the tinkerer who uses Perl once in a blue moon and who is not deeply familiar with the language) thought it'd be.

Tags: perl, programming
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded