Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

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?

like image 225
Kromey Avatar asked Mar 24 '14 20:03

Kromey


People also ask

What does $$ mean in perl?

Normally the $$ is used to print the current process ID. print $$;

What is$@ in perl?

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.

What does=~ do in perl?

The operator =~ associates the string with the regex match and produces a true value if the regex matched, or false if the regex did not match.

What does percent sign mean in perl?

Perl uses the "percent" symbol and curly braces with respect to the name of an associative array as a whole, whereas individual elements within an array are referred to as scalars and the index is still placed in curly braces.


2 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"; 
like image 61
Miller Avatar answered Sep 22 '22 21:09

Miller


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; 
like image 31
froodley Avatar answered Sep 22 '22 21:09

froodley