chunlou has asked for the wisdom of the Perl Monks concerning the following question:

Feel guilty to ask a seemingly babyish question but, either due to bad luck, drowsiness or sheer sloth, I couldn't seem to be able to locate documentation concerning something like $p'a = $a, which I came across in a Perl script I read off the Web. So, I tried that syntax out on a script in attempt to figure things out myself:
#! /usr/local/bin/perl -w use strict ; # ---------------------------------------------------------- my $a = "this\n"; sub this {$p'a = $a; print $p::a;} sub that {$p::a = "that\n"; print $p'a;} sub more {$p::a = "more\n"; print $a;} this(); # print "this" that(); # print "that" more(); # print "this" print $p'a; # print "more" print $p::a; # print "more" print $a; # print "this" # ---------------------------------------------------------- sub a'this {$p'u'a = "mooo\n"; print $p::u::a;} a::this(); # print "mooo" print $p'u'a; # print "mooo" # ---------------------------------------------------------- sub a::that {$p't'u = "wwoo\n"; return sub{print $p't'u}} $a'that = a'that(); $a'that->(); # print "wwoo" $a'that = a'that(); $p::t::u = "booo\n"; $a'that->(); # print "booo"
Does that mean $p'a is a namespace thing? I don't quite comprehend what scope $p'a is. Is it the same thing as $package::var_or_func?

Thanks. This has been a resourceful community here.

Replies are listed 'Best First'.
Re: what's $p'a=$a mean?
by Ovid (Cardinal) on Jun 12, 2003 at 22:19 UTC

    The single quote is a holdover from an earlier version of Perl. It, like the double colon, is a package name separator. Thus, $p'a is the same as $p::a.

    This single quote syntax is deprecated and will be gone by Perl 6.

    See the D'oh module :)

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      So, I can use $p::var as some arbitrary global variable even under "use strict" and even when "p" is not referring to any package at all? Is it an advisable thing to do? Thanks.
        So, I can use $p::var as some arbitrary global variable even under "use strict" and even when "p" is not referring to any package at all?

        In that example, "p" is is referring to a package. It doesn't matter that you didn't enter the package namespace anywhere with pacakge p; first though. Yes, you can use it even under use strict; because it is fully qualified. Even strict checking assumes that you know what you are doing when you fully qualify a variable name.

        Is it an advisable thing to do?

        Use global variables from random packages? No. And it is probably better to predeclare globals with use vars (or our which isn't exactly the same thing) than to not declare and use fully qualified names.

        -sauoq
        "My two cents aren't worth a dime.";
        

        There are rarely any hard or fast rules, but generally, variables should have as restricted a scope as is feasible. If other packages don't need your variable, don't make it a package variable. If they do need it, write subs that can set and fetch the variable (if something else changes the variable when you didn't expect it, you can then easily insert debugging code to find out what is changing it. That's tougher to do with package variables).

        Cheers,
        Ovid

        New address of my CGI Course.
        Silence is Evil (feel free to copy and distribute widely - note copyright text)

        > So, I can use $p::var as some arbitrary global variable even under "use strict" and even when "p" is not referring to any package at all?

        Well, first, $p::var isn't a global variable; it's a package variable. And p:: does refer to a package; it specifically refers to the package p. So I would reformat your question as follows:

        So, I can use $p::var as some arbitrary package variable even under "use strict" and even when "p" doesn't refer to a package that I've specifically declared (i.e., with the "package" keyword)?

        And the answer is, yes. I suppose you could consider that a deficiency of "use strict".

        > Is it an advisable thing to do?

        Well, personally I would say not. It's bound to confuse someone somewhere. And technically it's allowing you to autovivify a variable, which is what you're trying to avoid by use'ing strict. So I'd avoid it.