开发者

Why would you assign to $$?

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 compatibi开发者_StackOverflow社区lity.)

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


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.


IPC::Messaging, which provides sorta kinda Erlang-like messaging (not performance-wise, syntax-wise) does that to $$ to replace it with an object which numifies to the original pid. This is done to have a convenient reference to a "self-process" which one can call methods on (= send messages to).

Full disclosure: I am the author of the module.


If you were the syscall implemenation of a fork() like system call you would need to assign to the global one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜