broquaint has asked for the wisdom of the Perl Monks concerning the following question:
The Question
At some point in the past couple of days of hacking and pondering perl I came across an interesting quandry - is it possible to create a truly *global* variable? While I have no intention of actually doing so I wonder if it's possible.
The only global variables that I know of are the one's created by perl, and they tend to be magical (e.g $! yields the current value of errno when in a numerical context, and an error message in a string context). So I'm guessing you can probably roll your own global using some XS voodoo, or some other magical method. I'm almost certain that it's possible as there seems to be very little in perl these days that can't be done (just look at what TheDamian has been producing recently). I also hope that it's not easily possible, as mis-used package variables are bad enough (which why we use strict :o)
Thanks in advance!
At some point in the past couple of days of hacking and pondering perl I came across an interesting quandry - is it possible to create a truly *global* variable? While I have no intention of actually doing so I wonder if it's possible.
What I/we know
I'm aware of the difference between package (sometimes referred to as global) and lexical variables and know they can both only be seen from their respective scopes. There are, however, global magic™ variables which can be seen from *everywhere*. For example ...
The Code
Thoughts so far{ package Foo; # now lives in Foo's symbol table $pkg_var = "a string"; use strict; # only lives until the end of the block my $lex_var = "another string"; # set the global var $! (which is why we don't die) open(FH, "non_existant_file"); 1; } package main; print "package var - $Foo::pkg_var\n"; print "lexical var - $lex_var\n"; print "global var - $!\n";
The only global variables that I know of are the one's created by perl, and they tend to be magical (e.g $! yields the current value of errno when in a numerical context, and an error message in a string context). So I'm guessing you can probably roll your own global using some XS voodoo, or some other magical method. I'm almost certain that it's possible as there seems to be very little in perl these days that can't be done (just look at what TheDamian has been producing recently). I also hope that it's not easily possible, as mis-used package variables are bad enough (which why we use strict :o)
Thanks in advance!
broquaint
footnote: yes I stole this question format from larryk, but only because it's so darn cool!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re (tilly) 1: Can you create *real* global variables?
by tilly (Archbishop) on Jan 25, 2002 at 02:09 UTC | |
by japhy (Canon) on Jan 25, 2002 at 02:45 UTC | |
by tilly (Archbishop) on Jan 25, 2002 at 03:15 UTC | |
Re: Can you create *real* global variables?
by Dominus (Parson) on Jan 25, 2002 at 18:55 UTC | |
Re: Can you create *real* global variables?
by wog (Curate) on Jan 25, 2002 at 03:08 UTC | |
Re: Can you create *real* global variables?
by Ido (Hermit) on Jan 25, 2002 at 01:11 UTC | |
Re: Can you create *real* global variables?
by robin (Chaplain) on Jan 25, 2002 at 23:50 UTC | |
by Dominus (Parson) on Jan 26, 2002 at 20:42 UTC | |
Re: Can you create *real* global variables?
by moodster (Hermit) on Jan 25, 2002 at 19:01 UTC | |
by blazar (Canon) on Mar 12, 2007 at 11:52 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Seekers of Perl Wisdom