Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you assign to $$?

Tags:

perl

perldoc perl5150delta says:

   $$ can be assigned to
       $$ was made read‐only in Perl 5.8.0.  But only sometimes: "local $$"
       would make it writable again.  Some CPAN modules were using "local $$"
       or XS code to bypass the read‐only check, so there is no reason to keep
       $$ read‐only.  (This change also allowed a bug to be fixed while
       maintaining backward compatibility.)

$$ is the current process ID, why in the world would you assign to it?

like image 964
Chas. Owens Avatar asked Aug 08 '11 01:08

Chas. Owens


Video Answer


1 Answers

There are only a couple (literally) places in CPAN where people want to assign to $$, and it's mostly for testing (I haven't understood IPC::Messaging yet). I don't like this feature, especially since there's a much better way to get the same effect. The Perl 5 Porters added this feature because they could and they would rather not make the couple of cases do a better job of testing. If you read the p5p thread, it's obvious that this feature wasn't driven by need.

I wrote about it in Hide low-level details behind an interface.

However, I could be wrong on this because I'm not that good at the low-level black magic. I know there is a need to coordinate PIDs, but so far I think that $$ isn't the only way to do that. If someone has a use case that they can explain to me, I'll update that post.

like image 69
brian d foy Avatar answered Sep 21 '22 05:09

brian d foy