<?xml version="1.0" encoding="windows-1252"?>
<node id="141331" title="Can you create *real* global variables?" created="2002-01-24 20:00:28" updated="2005-08-15 06:51:51">
<type id="115">
perlquestion</type>
<author id="87452">
broquaint</author>
<data>
<field name="doctext">
&lt;b&gt;The Question&lt;/b&gt;&lt;br&gt;
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. 
&lt;p&gt;&lt;b&gt;What I/we know&lt;/b&gt;&lt;br&gt;
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&lt;sup&gt;&amp;trade;&lt;/sup&gt; variables which can be seen from *everywhere*. For example ...
&lt;p&gt;&lt;b&gt;The Code&lt;/b&gt;
&lt;code&gt;
{
    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";
&lt;/code&gt;
&lt;b&gt;Thoughts so far&lt;/b&gt;&lt;br&gt;
The only global variables that I know of are the one's created by perl, and they tend to be magical (e.g &lt;tt&gt;[perlman:perlvar|$!]&lt;/tt&gt; yields the current value of errno when in a numerical context, and an error message in a string context)&lt;/tt&gt;. 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 [http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search?new=Search;filetype=%20author%20name%20or%20id;join=and;arrange=file;download=auto;stem=no;case=clike;site=ftp.funet.fi;age=;idinfo=22|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 &lt;tt&gt;[id://115515|use strict]&lt;/tt&gt; :o)&lt;br&gt;
Thanks in advance!
&lt;p&gt;
broquaint
&lt;p&gt;
&lt;small&gt;&lt;b&gt;footnote&lt;/b&gt;: yes I stole this question format from [larryk], but only because it's so darn cool!&lt;/small&gt;</field>
</data>
</node>
