Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 10th, 2010, 07:30 AM
bobt_1234 bobt_1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 16 bobt_1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old June 10th, 2010, 07:39 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,645 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 22 h 51 m 5 sec
Reputation Power: 1170
$$ is one of Perl's built-in vars. It holds the process ID of the running script.

See: perldoc perlvar

Reply With Quote
  #3  
Old June 10th, 2010, 07:42 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
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:
Code:
kill 9, $$;

That's a suicide Perl code: it finds its own process id and kills itself.

Reply With Quote
  #4  
Old June 10th, 2010, 07:50 AM
bobt_1234 bobt_1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 16 bobt_1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old June 10th, 2010, 07:55 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
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.

Reply With Quote
  #6  
Old January 8th, 2013, 02:38 AM
azsuselinux azsuselinux is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 azsuselinux User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old January 8th, 2013, 04:09 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 506 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 5 m 2 sec
Reputation Power: 385
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.

Reply With Quote
  #8  
Old January 8th, 2013, 11:29 PM
azsuselinux azsuselinux is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 azsuselinux User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 47 sec
Reputation Power: 0
thanks for ur help laurent R

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Double dollar sign "method"?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap