Reputation: 1193
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...
Upvotes: 4
Views: 20898
Reputation: 1413
The variable $$
is, as you've written, PID of current process:
print $$ . "\n";
What you've written in your script is $$record{$surrogate_name}
, which means accessing an element of a hash and is equivalent to $record->{$surrogate_name}
.
Other from that, $$name
usually means dereferencing a reference to a scalar. For example, if you have:
my $x = 1; # Scalar
my $y = \$x; # Scalar, value of which is a reference to another scalar (address)
my $z = $$y; # Dereference the reference, obtaining value of $x
It's equivalent to following operations on pointers in C
:
int x = 1;
int *y = &x;
int z = *y;
Read more on this here.
Upvotes: 1
Reputation: 543
Normally the $$ is used to print the current process ID.
print $$;
But the $ is having another work, which for dereferencing the scalar variable. For example:
use strict;
use warnings;
my $Input = 5;
my $Input_Refer = \$Input;
print "$Input\n";
print $$Input_Refer;
In the above example, we are have two scalar variable,
1. $Input
, which contains 5 as value.
2. $Input_Refer
, which contains the reference of an $Input variable as value.
So If we print the $Input
, It will give the value as 5,
If we print the $Input_Refer
, It will print the memory address of $Input.
So we have to dereference it, For that we have to use another $ like this,
print $$Input_Refer;
It will give the output as 5.
Upvotes: 1
Reputation: 1801
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.
Upvotes: 14