Perl $$var -- two dollar signs as a sigil?

This seems like something I should be able to easily Google, Bing, or DDG, but I'm coming up completely empty on everything.

Basically, I'm tasked with (in part) rewriting a set of old Perl scripts. Not being a Perl programmer myself, there's definitely a learning curve in reading these scripts to figure out what they're doing, but I've hit an absolute brick wall with lines like this one:

$comment = $$LOC{'DESCRIPTION'};

Near as I can tell, the right-hand side is a dictionary, or more precisely it's getting the value referenced by the key 'DESCRIPTION' in said dictionary. But what's with the "extra" dollar sign in front of it?

It looks suspiciously like PHP's variable variables, but after scouring search engines, assorted StackExchange sites, and perlvar, I can't find any indication that Perl even has such a feature, let alone that this is how it's invoked. The most I've turned up is that '$' is not a valid character in a variable name, so I know $$LOC is not merely calling up a variable that just happens to be named $LOC.

What is this extra dollar sign doing here?

Answers


This is an alternative form of dereferencing. Could (and honestly probably should) be written as:

$LOC->{'DESCRIPTION'};

You'll sometimes see the same with other all basic data types:

my $str = 'a string';
my %hash = (a => 1, b => 2);
my @array = (1..4);

my $strref = \$str;
my $hashref = \%hash;
my $arrayref = \@array;

print "String value is $$strref\n";
print "Hash values are either $$hashref{a} or $hashref->{b}\n";
print "Array values are accessed by either $$arrayref[0] or $arrayref->[2]\n";

Dereferencing is treating a scalar as the 'address' at which you can locate another variable (scalar, array, or hash) and its value.

In this case, $LOC{DESCRIPTION} is being interpreted as the address of another scalar, which is read into $comment.

In Perl,

my $str = 'some val';
my $ref = \$str;
my $val = ${$ref};
my $same_val = $$ref;

Need Your Help

How to iterate over arguments in a Bash script

bash command-line

I have a complex command that I'd like to make a shell/bash script of. I can write it in terms of $1 easily:

Where are shared preferences stored?

android sharedpreferences

Where in an Eclipse project might one encounter a shared preferences file?