Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Can you create *real* global variables?

by robin (Chaplain)
on Jan 25, 2002 at 23:50 UTC ( #141612=note: print w/ replies, xml ) Need Help??


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

Something that hasn't being mentioned is that all punctuation variables are always global, even if they aren't magical. It can be hard to find unused punctuation characters though!

%% = (foo => 23); package xxx; print $%{foo};
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.

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:

${'@$??!'} = "Weird!\n"; package SomewhereElse; print ${'@$??!'}
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:
${^Hello} = "Curious...\n"; print ${"\bello"};
Some food for obfu, maybe... :)


Comment on Re: Can you create *real* global variables?
Select or Download Code
Re: Can you create *real* global variables?
by Dominus (Parson) on Jan 26, 2002 at 20:42 UTC
    Said robin:
    Something that hasn't being mentioned is that all punctuation variables are always global, even if they aren't magical.
    I did say this.

    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.
    The actual rule is that any name that starts with a character other than a letter or underscore is global. (Unless your source code is written in unicode, in which case i don't know what happens.) So in particular, $0 and $1 are global. Variables like ${^Name} don't start with punctuation either: As robin said, this one appears to start with ^, but actually it starts with the control-N character. Since control-N isn't a letter or underscore, ${^Name} is global.

    There's one exception to that: you can also use :: as a name, so you can use the variable $:: which is of course global.
    Not so. $:: is actually the same variable as $main::main::. (Thanks to Abigail for tracking this down.) There's a special case in gv.c that treats an empty package name as main, and it gets invoked twice here.

    --
    Mark Dominus
    Perl Paraphernalia

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-12-22 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (110 votes), past polls