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;
| [reply] [d/l] |
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;
| [reply] [d/l] [select] |
|
|
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.
| [reply] [d/l] |
|
|
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.
| [reply] |
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.
| [reply] [d/l] [select] |
|
|
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! :-})
| [reply] |
|
|
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.
| [reply] [d/l] [select] |
|
|
|
|
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;
| [reply] [d/l] |