The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Double dollar sign "method"?
Discuss Double dollar sign "method"? in the Perl Programming forum on Dev Shed. Double dollar sign "method"? Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 10th, 2010, 07:30 AM
|
|
Registered User
|
|
Join Date: Jun 2010
Posts: 16
Time spent in forums: 2 h 39 sec
Reputation Power: 0
|
|
|
Double dollar sign "method"?
Hello everyone.
I encountered the following code and have no idea what it does, so maybe somebody here can help me.
Basically, it's a reference to the (method? variable?) "$$".
As in:
my $v = $$;
printf "%d\n", $v;
This displays a number. If I call it multiple times in the same script, it always returns the same number, as in:
for my ($i = 0; $i < 10; $i++) {
$v = $$;
printf "%d\n", $v;
}
I searched the world-wide-web over and couldn't find an answer to this puzzle.
|

June 10th, 2010, 07:39 AM
|
|
|
$$ is one of Perl's built-in vars. It holds the process ID of the running script.
See: perldoc perlvar
|

June 10th, 2010, 07:42 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
See perlvar. That's one of Perl's predefined variables and that one holds the process number of the script being run.
Incidentally, you'll see it under my username above:
That's a suicide Perl code: it finds its own process id and kills itself.
|

June 10th, 2010, 07:50 AM
|
|
Registered User
|
|
Join Date: Jun 2010
Posts: 16
Time spent in forums: 2 h 39 sec
Reputation Power: 0
|
|
|
Thanks.
I first saw it in a discussion about the random number generator "rand", as a parameter to the "seed" function "srand" that you call first to randomize it.
srand(time ^ $$);
Supposedly, newer versions of Perl automatically call this so you don't have to.
|

June 10th, 2010, 07:55 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
|
Yes, the current time and the process id are two things that are likely to be different each time you run a script so you're seeding rand in a different way each time. As you say, it's no longer necessary to do this yourself.
|

January 8th, 2013, 02:38 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 6 m 47 sec
Reputation Power: 0
|
|
|
hi everyone
im new to perl and want to know the meaning of the code :
$seed =(time ^$$ or time^($$+($$<15)));
help needed!!
thanks
|

January 8th, 2013, 04:09 PM
|
|
|
|
Hmmm, I can only tell you that ^ is the bitwise exclusive OR.
So your expression performs a bitwise exclusive OR between the system current time and the process ID to find the seed. In the (very unlikely, I would presume) event that this operation yields 0 (false), then the expression after the "or" is evaluated and presumably does just a more complicated bitwise operation to find another seed (although I am not entirely convinced on the way it is done, so I may be wrong on the purpose of this second part).
Just in case, a bitwise exclusive OR means that for each bit position in the variables being compared, if you have:
0 1 or 1 0, it returns 1
0 0 or 1 1, it returns 0.
The obvious case where this comparison could yield 0 is when all the bits in each variable are the same, meaning in effect that the two values being compared are identical (for ex. 132 ^ 132 = 0, but 132 ^133 = 1). This is probably unlikely when you are doing this with the value returned by time and $$. There may be other cases where you get 0, but I don't see any at first glance.
|

January 8th, 2013, 11:29 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 6 m 47 sec
Reputation Power: 0
|
|
|
thanks for ur help laurent R
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|