[Edinburgh-pm] code style question

Murray perl at minty.org
Wed Aug 17 02:17:34 PDT 2011


On Tue, Aug 16, 2011 at 11:46:38AM +0100, Alex Brelsfoard wrote:
> I just came across some code that used some old-style coding practice, and I
> wanted to know if there was still any GOOD reason for using it:

I think smarter people may have already answered this better, but fwiw, a quick
trawl through the perldoc files on the topic interested me, so I thought I'd
share below in case anyone hadn't already seen them.

I don't claim to be an authority, but $::var used to ensure you're accessing
$var in the 'main' package smacks of coding without the safety-net.  Depends
how high up you are and how many rocks are beneath you, should you (or your
code) "fall".

Also consider those you're hoping will be following in your footsteps,
including "future-you" - when you look at code you wrote 6 months ago & say
WTF?

man perlfaq7

    How can I access a dynamic variable while a similarly named lexical is in
    scope?

    If you know your package, you can just mention it explicitly, as in
    $Some_Pack::var. Note that the notation $::var is not the dynamic $var in
    the current package, but rather the one in the "main" package, as though
    you had written $main::var.

    http://perldoc.perl.org/perlfaq7.html#How-can-I-access-a-dynamic-variable-while-a-similarly-named-lexical-is-in-scope?

man perlfunc

    package NAMESPACE

    package Declares the compilation unit as being in the given namespace.  The
    scope of the package declaration is from the declaration itself through the
    end of the enclosing block, file, or eval (the same as the "my" operator).
    All further unqualified dynamic identifiers will be in this namespace.  A
    package statement affects only dynamic variables--including those you've
    used "local" on--but not lexical variables, which are created with "my".
    Typically it would be the first declaration in a file to be included by the
    "require" or "use" operator.  You can switch into a package in more than
    one place; it merely influences which symbol table is used by the compiler
    for the rest of that block.  You can refer to variables and filehandles in
    other packages by prefixing the identifier with the package name and a
    double colon: $Package::Variable.  If the package name is null, the "main"
    package as assumed.  That is, $::sail is equivalent to $main::sail (as well
    as to "$main'sail", still seen in older code).

    See "Packages" in perlmod for more information about packages, modules, and
    classes.  See perlsub for other scoping issues.

    http://perldoc.perl.org/perlfunc.html#package-NAMESPACE

man perlsub

    Private Variables via my()

    In fact, a dynamic variable (also known as package or global variables) are
    still accessible using the fully qualified "::" notation even while a lexical
    of the same name is also visible:

        package main;
        local $x = 10;
        my    $x = 20;
        print "$x and $::x\n";

    That will print out 20 and 10.

    http://perldoc.perl.org/perlsub.html#Private-Variables-via-my%28%29

ps. because Google *still* has rubbish support for non-alphanumeric characters
in querys [ http://www.google.co.uk/search?q=%24%3A%3A ] I found this lot via
the following:

    $ perldoc perl

lists what docs are available.

    $ locate perlfunc

told me *where* there are files with "perlfunc" in the name.

    $ zgrep '\$::' /usr/share/man/man1/perl*

gave me some hints about where to start looking.


More information about the Edinburgh-pm mailing list