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

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
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://141502]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-03-30 21:44 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (364 votes). Check out past polls.