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 ...
Thoughts so far
# now lives in Foo's symbol table
$pkg_var = "a string";
# only lives until the end of the block
my $lex_var = "another string";
# set the global var $! (which is why we don't die)
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
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
Thanks in advance!
footnote: yes I stole this question format from larryk, but only because it's so darn cool!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||