#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    18
    Rep 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    $$ is one of Perl's built-in vars. It holds the process ID of the running script.

    See: perldoc perlvar
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    18
    Rep 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.
  8. #5
  9. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep 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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    834
    Rep Power
    496
    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.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    thanks for ur help laurent R
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Dannemora Drive Sheffield
    Posts
    1
    Rep Power
    0
    anyone tell me , about Double dollar sign "method"?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    834
    Rep Power
    496
    The answer is given above. $$ is the special built-in variable containing the process ID of your script.

    Or you were looking for somethjing else?
  20. #11
  21. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    79
    Rep Power
    138
    Originally Posted by bobt_1234
    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.
    No, all versions of Perl use a much more random seed. You use srand() to get the same sequence of random numbers each run. This makes debugging easier. For production, of course, you remove the srand().
    Last edited by shawnhcorey; September 2nd, 2013 at 11:28 AM.
    __END__

    I love Perl; it's the only language where you can bless your thingy.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    834
    Rep Power
    496
    Originally Posted by shawnhcorey
    You use srand() to get the same sequence of random numbers each run. This makes debugging easier. For production, of course, you remove the srand().
    It is a bit more complicated. On previous versions of Perl (before 5.004), you had to call srand(), with no parameter, to get different a different seed each time, and therefore a different sequence of random numbers. Now, srand is automatically called with no parameter the first time rand is called, so that you get each time a different seed.

    If you want the same sequence of pseudo-random numbers, you have to call srand with a parameter (a number), which becomes the seed. And with the same seed, you get the same sequence of numbers.

    Code:
    $  perl -e 'push @c, int(rand 10) for 1..10; print "@c\n"'
    7 8 6 7 2 3 2 8 7 8
    
    $  perl -e 'push @c, int(rand 10) for 1..10; print "@c\n"'
    3 3 3 2 4 0 9 9 3 1
    
    $  perl -e 'srand; push @c, int(rand 10) for 1..10; print "@c\n"'
    3 3 6 5 5 4 0 3 4 5
    
    $  perl -e 'srand; push @c, int(rand 10) for 1..10; print "@c\n"'
    2 4 3 5 5 1 7 4 1 5
    
    $  perl -e 'srand 755; push @c, int(rand 10) for 1..10; print "@c\n"'
    6 7 6 3 6 8 7 7 7 7
    
    $  perl -e 'srand 755; push @c, int(rand 10) for 1..10; print "@c\n"'
    6 7 6 3 6 8 7 7 7 7
  24. #13
  25. wizard
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Location
    The Great White North
    Posts
    79
    Rep Power
    138
    Originally Posted by Laurent_R
    It is a bit more complicated. On previous versions of Perl (before 5.004), you had to call srand(), with no parameter, to get different a different seed each time, and therefore a different sequence of random numbers.
    Oops. I thought that behaviour changed with 5.0.0, not until 5.4.0. My mistake.
    __END__

    I love Perl; it's the only language where you can bless your thingy.

IMN logo majestic logo threadwatch logo seochat tools logo