develooper Front page | perl.perl5.porters | Postings from June 2010

Why are subs matching /^[\W_]$/ global?

Thread Next
From:
Ævar Arnfjörð Bjarmason
Date:
June 2, 2010 05:46
Subject:
Why are subs matching /^[\W_]$/ global?
Message ID:
AANLkTinXPQTJGHagOw8KhTysXc_jd5Ik3OSt9og9YHZd@mail.gmail.com
Single-character non-alpha (plus _) subroutines are global

    $ perl -le '** = sub {warn "Hi"}; package Foo; &*'
    Hi at -e line 1.

    $ perl -le '*a = sub {warn "Hi"}; package Foo; &a'
    Undefined subroutine &Foo::a called at -e line 1.

and:

    $ perl -le '*_ = sub {warn "Hi"}; package Foo; &_'
    Hi at -e line 1.

    $ perl -le '*__ = sub {warn "Hi"}; package Foo; &__'
    Undefined subroutine &Foo::__ called at -e line 1

This came up when I was looking at _() in Perl as a potential Gettext
wrapper (I know about the _ filehandle).

_ I think I can understand (presumably the _ glob is global). But why
does this apply generally to all /^[\W_]$/ subs? Is it explicitly
documented somewhere? Perhaps it's something we'd want to deprecate?

1. http://search.cpan.org/~guido/libintl-perl-1.19/lib/Locale/libintlFAQ.pod#Why_does_Locale::TextDomain_use_a_double_underscore?_I_am_used_to_a_single_underscore_from_C_or_other_languages.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About