Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

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

{ 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";
Thoughts so far
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!


In reply to Can you create *real* global variables? by broquaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found