• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is "$$" for?

 
Allen Chan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I know this is not the place, but what is "$$" for in perl, a line reads $str = "this" ."$$ADS{'name'}";
 
Allen Chan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
man, you go extra extra miles for my questions, thanks a ton
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allen,
I suppose the closest point to post this would be here
Your question involves Using Hard References. If this is your first encounter to them, it will be a bit tough to grasp them the first time. (I have read it about five times, until they started to make sense, but afterwards its quite rewarding :cool
The '$' in Perl refers to a scalar variable, if there is more then one '$' they are parsed from right to left:
$$$var
is equivalent to ${${${var}}}
In English, you would interoperate that as:
$var is a scalar ref variable(1) which points to an anonymous scalar ref variable(2) which points to another anonymous scalar ref variable(3). Whew, what a mouthful!
So let's focus on:


First of all, the quotes around the name KEY is not necessary, unless the KEY has spaces in it, $hash{ KEY } is the same as $hash{KEY}.
From the code, you infer that $ADS is an anonymous hash that is most likely composed on the fly where your reference variable springs into existence (in Perl jargon you say they autovivify). The $ADS hash can also be set explicitly as in:

The {... } is the anonymous hash composer. To access any value you dereference it as $$hashref{KEY}.
You can also use any of these:
$$ADS{name}
${$ADS}{name}
$ADS->{name}
I prefer the last one for its clarity.
Here is some more food for thought (if you have a *nix OS)

Results in the following:

1:HASH(0x40015c50)
2:$ADS is a reference
3:
4:$ADS{name} is not a reference
5:Bob Smith
6:Bob Smith

Hope this helps�
Cheers,
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic