This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Joined: Jun 07, 2002
Posts: 64
posted
0
man, you go extra extra miles for my questions, thanks a ton
Leslie Chaim
Ranch Hand
Joined: May 22, 2002
Posts: 336
posted
0
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