in reply to Can you create *real* global variables?
I dimly remember that in Perl 4 any variable or function starting with an underscore was always global; but that changed when Perl 5 came along.%% = (foo => 23); package xxx; print $%{foo};
The situation with ${^Name} variables is a little more complicated: the fact is that any variable name that starts with a punctuation character is implicitly global, but the Perl parser only recognises single punctuation characters. There's one exception to that: you can also use :: as a name, so you can use the variable $:: which is of course global. (And no, it's not a package qualifier - the variable really is called ::. The reason it's allowed is because the root symbol table hash is called %::, so the variable name tokeniser needs to let it through.) But underneath, any string of characters which begins with punctuation will force globality:
What's special about ${^Thing} is that (in recent releases) the parser is able to parse the name directly, and it converts the first character into a control char! So the following will work, because control-H is the backspace character:${'@$??!'} = "Weird!\n"; package SomewhereElse; print ${'@$??!'}
Some food for obfu, maybe... :)${^Hello} = "Curious...\n"; print ${"\bello"};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can you create *real* global variables?
by Dominus (Parson) on Jan 26, 2002 at 20:42 UTC |