At Penguicon this past weekend, I listened to Nat Torkington (of Perl Cookbook fame) explain our. To paraphrase, he said that our came from the old Perl 4 use var, "which was the ugliest possible way to declare global variables". He then immediately added, "Don't tell Damian I said that, or he'll come up with an even uglier way". So, of course, I felt the irresistable urge to put it forth here, where Damian (or anybody else) could think about an uglier way. :-)


--
Linux, sci-fi, and Nat Torkington, all at Penguicon 3.0
perl -e 'print(map(chr,(0x4a,0x41,0x50,0x48,0xa)))'

Replies are listed 'Best First'.
Re: Ugly ways to declare global variables?
by salva (Canon) on Apr 25, 2005 at 14:54 UTC
    my $foo; *foo=\$foo; $foo = 5; print $::foo;
Re: Ugly ways to declare global variables?
by itub (Priest) on Apr 25, 2005 at 15:25 UTC
    For something extra-ugly, you can do the same thing vars does without using the module (and you don't even need my!).
    use strict; BEGIN { package vars; no strict qw(refs); $a="main::x"; *$a = \$$a } $x = 5; print $x;
      More simply, since we don't need to pass names around as strings:
      use strict; use warnings; BEGIN { package vars; *::x = \$::x } $x = 5; print $x;

      Caution: Contents may have been coded under pressure.
        Thanks, good point. What happened was that I based my example too literaly in the vars.pm code, where the variable names are indeed passed around as strings.
Re: Ugly ways to declare global variables?
by Roy Johnson (Monsignor) on Apr 25, 2005 at 15:14 UTC
    The problem is, you don't declare global variables. You merely tell Perl (and in particular, strict) that you are going to be using them. So to me, use vars as a pragma is a better design choice. Perhaps an uglier yet more say-what-you-mean solution would be something like no strict 'vars:$a,@b,%c'

    One of the problems with our is that it looks like my, and people think they're getting a new variable out of it.


    Caution: Contents may have been coded under pressure.

      There's an implied "that you can use" modifying phrase there that you're not using.

      When you use "our" to declare a global variable under strictures, you are getting a "new variable out of it" that you can use. Without the "our" declaration (or the old "use vars" declaration), you cannot use the variable - the program does not run because of this. With the "our" declaration, you now can use the variable - it's syntactically as if it were a new variable. Which it mostly is - it's a new entry in the symbol table.

      The fact that, under the covers, all that "our" does is set up a value in a global symbol table such that strict doesn't complain, is really not that important. That's just an implementation detail. The syntactical sugar of "our" is a huge level of abstraction that allows people to think they're getting a new variable, and, under strictures, not be too far off the real effect.

      (++ for the uglier way to do it. Ewww! :-})

        you are getting a "new variable out of it" that you can use.
        There's nothing new about the variable, and that is important. It might already have a value. When you actually declare a variable, it's really new. It's fresh and unused from that point. When you specify our, you gain only the ability to use the variable without its package specifier.
        #!perl use strict; use warnings; $::z = 'bye'; our($z); print $z, " is ", $::z, "\n";

        Caution: Contents may have been coded under pressure.
Re: Ugly ways to declare global variables?
by Anonymous Monk on Apr 25, 2005 at 16:13 UTC
    BEGIN { *{join "::", __PACKAGE__, "var"} = \${join "::", __PACKAGE__, "var"}; } $var = "Hello, world\n"; print $var;