Re: What's up with the double dollar sign? ($$)

From: Chris Hope (chris_at_electrictoolbox.com)
Date: 04/27/04


Date: Wed, 28 Apr 2004 08:15:34 +1200

ScareCrowe wrote:
> I think it has something to do with globals, but what exactly is the
> purpose of the '$$' with variables?
> I mean what's the difference between '$var' and '$$var'?
> When would be the appropriate time to use '$$' instead of '$'?

$foo = 'bar';
$bar = 'xyz';
$zoo = 'abc';

print $foo . "\n";
print $bar . "\n";
print $$foo . "\n";

This will print out
        bar
        xyz
        xyz

$$foo is evaluated as $bar because the value of $foo is 'bar'. If $foo was
changed to $foo = 'zoo' then print $$foo would now print 'abc'

The purpose of doing so? I find it often obfuscates code but I'm sure there
must be some useful purpose for it sometimes.

Hope this all makes sense...

-- 
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/


Relevant Pages

  • Re: Private methods - only available to oneself?
    ... what's the purpose of 'protected' or 'private'?" ... def foo_one ... def bar ... to the effect of a private method. ...
    (comp.lang.ruby)
  • Re: Private methods - only available to oneself?
    ... what's the purpose of 'protected' or 'private'?" ... Or look at it this way: It makes it "more difficult" to access private ... def foo_one ... def bar ...
    (comp.lang.ruby)
  • Re: Derogatory Xenophobia
    ... >> something (often a line or bar) that sort of looks like text, ... and of course what purpose it serves. ...
    (alt.usage.english)
  • Re: How do you win at craps?
    ... "The bar point prevents the don't pass from being complimentary ... That is the purpose of the bar point." ... What is the chance of winning a passline wager? ...
    (rec.gambling.craps)
  • What exactly might this do?
    ... and the address in the title bar the name 'readme.eml' ... and would affect others using MS Outlook to read this. ... Can anybody tell me what purpose this might serve? ...
    (Security-Basics)