Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Can you create *real* global variables?

by Dominus (Parson)
on Jan 25, 2002 at 18:55 UTC ( #141502=note: print w/ replies, xml ) Need Help??


in reply to Can you create *real* global variables?

You may want to read the section at the very end of perlvar titled Technical Note on the Syntax of Variable Names.

As tilly pointed out, variables such as ${^Foo} and @{^_I_Like_Pie} are not only global, but also ignore package declarations, and are the same in every package. ${^Foo} means $main::^Foo, regardless of what package declaration is in scope. But note also that they are all reserved for future extensions, except the ones beginning with ^_. So the official answer to your question is either

Write the empty package qualifier explicitly, as $::foo or $'foo
or
Use a variable like ${^_foo}
That's two ways to accomplish the same effect, and should be enough to solve any practical problem you might have.

As the man page mentions, there are a few other variable names that are interpreted independent of the current package: %ENV, STDIN, @ARGV, and so on. There is absolutely no way to create new special variables of this type without modifying the Perl core. (See the code in the vicinity of line 588 of gv.c to see why.) But if you really want to, you can take over the unused variables with the same names, such as $ENV, @STDIN, and %ARGV, and use those; they get the same special treatment even though Perl doesn't use them for anything. Similarly, punctuational variables such as @* and %: ignore the current package declaration and are always taken to be in package main.

Hope this all helps.

--
Mark Dominus
Perl Paraphernalia


Comment on Re: Can you create *real* global variables?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://141502]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2014-12-29 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (188 votes), past polls