Yashwanth Aluru Yashwanth Aluru - 4 months ago 9
Perl Question

What does $$ in perl actually return?

I'm new to Perl, learning from the beginning. I've read that

$$
returns


The pid of the Perl process running this script.


as per below link,

http://www.tutorialspoint.com/perl/perl_special_variables.htm

I've the following Perl Script which is being executed on a Windows machine,

sub test
{
my ($surrogate_name) = @_;
if((defined $surrogate_name) && ($surrogate_name == 1))
{
print "Done!"."\n";
}
return $surrogate_name;
}

sub t
{
my ($surrogate_name) = @_;
my $record;
my $surrogate_value;
$record = &test($surrogate_name);
print $record."\n";
if ($record)
{
print "B"."\n";
$surrogate_value = $$record{$surrogate_name};
}
print $surrogate_value."\n";
print "A"."\n";
return $surrogate_value;
}

&t(1);


In this code, I observed everything is printed except the value of
$surrogate_value
.

Please clarify me what does
$$
actually mean and why it's not returning anything in my script.

Thanks in advance...

Answer

Yes $$ returns the process id of currently running script.

but in your case $surrogate_value = $$record{$surrogate_name} which is completely different concept that is a dereferencing.

for example $$ used along with a variable name to dereference it.

my $a = 10; #declaring and initializing a variable.
my $b = \$a; #taking scalar value reference
print $$b;  #now we are dereferencing it using $$ since it is scalar reference it will print 10

my %hashNew = ("1" => "USA", "2" => "INDIA"); #declaring a hash
my $ref = \%hashNew; #taking reference to hash
print $$ref{2}; #this will print INDIA we derefer to hash here

For more understanding read referencing and dereferencing in perl.

Comments