• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is "$$" for?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
man, you go extra extra miles for my questions, thanks a ton
 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic