use strict; use warnings; my $string = "First value"; my $ref_to_string = \$string; unless (fork) { do_subroutine ($ref_to_string); exit; } print "\nI am the invoking process and my PID is $$\n"; print "invoker $$: The value of \$ref_to_string is $ref_to_string\n"; print "invoker $$: The value of \$\$ref_to_string is $$ref_to_string\n\n"; my $waited_upon_child = wait; print "invoker $$: The PID of the child upon whom I waited was $waited_upon_child\n\n"; print "invoker $$: The value of \$ref_to_string is $ref_to_string\n"; print "invoker $$: After the call to the subroutine, the value of \$\$ref_to_string is finally $$ref_to_string\n\n"; print "invoker $$: I will now invoke the subroutine directly.\n\n"; do_subroutine ($ref_to_string); print "invoker $$: The value of \$ref_to_string is $ref_to_string\n"; print "invoker $$: After the call to the subroutine, the value of \$\$ref_to_string is finally $$ref_to_string\n\n"; sub do_subroutine { print "I am the subroutine and my PID is $$\n"; my $sub_ref_to_string = shift; print "Sub $$: The value of \$sub_ref_to_string is $sub_ref_to_string\n"; print "Sub $$: The value of \$\$sub_ref_to_string is $$sub_ref_to_string\n\n"; print "Sub $$: The preceding two messages confirm that I have access to the same\n"; print "Sub $$: memory location as the calling process even though I may be running\n"; print "Sub $$: in a different process from that of the calling process.\n\n"; # The following sleep instruction simulates time expended if this subroutine # were doing real work sleep (4); $$sub_ref_to_string = "Second value"; print "Sub $$: The value of \$sub_ref_to_string is still $sub_ref_to_string\n"; print "Sub $$: The value of \$\$sub_ref_to_string is now $$sub_ref_to_string\n"; print "Sub $$: I have changed the value of the scalar referenced by \$\$sub_ref_to_string (Heh-heh)\n\n"; }
I am the invoking process and my PID is 95864 invoker 95864: The value of $ref_to_string is SCALAR(0xe96dc0) invoker 95864: The value of $$ref_to_string is First value I am the subroutine and my PID is 95876 Sub 95876: The value of $sub_ref_to_string is SCALAR(0xe96dc0) Sub 95876: The value of $$sub_ref_to_string is First value Sub 95876: The preceding two messages confirm that I have access to the same Sub 95876: memory location as the calling process even though I may be running Sub 95876: in a different process from that of the calling process. Sub 95876: The value of $sub_ref_to_string is still SCALAR(0xe96dc0) Sub 95876: The value of $$sub_ref_to_string is now Second value Sub 95876: I have changed the value of the scalar referenced by $$sub_ref_to_string (Heh-heh) invoker 95864: The PID of the child upon whom I waited was 95876 invoker 95864: The value of $ref_to_string is SCALAR(0xe96dc0) invoker 95864: After the call to the subroutine, the value of $$ref_to_string is finally First value invoker 95864: I will now invoke the subroutine directly. I am the subroutine and my PID is 95864 Sub 95864: The value of $sub_ref_to_string is SCALAR(0xe96dc0) Sub 95864: The value of $$sub_ref_to_string is First value Sub 95864: The preceding two messages confirm that I have access to the same Sub 95864: memory location as the calling process even though I may be running Sub 95864: in a different process from that of the calling process. Sub 95864: The value of $sub_ref_to_string is still SCALAR(0xe96dc0) Sub 95864: The value of $$sub_ref_to_string is now Second value Sub 95864: I have changed the value of the scalar referenced by $$sub_ref_to_string (Heh-heh) invoker 95864: The value of $ref_to_string is SCALAR(0xe96dc0) invoker 95864: After the call to the subroutine, the value of $$ref_to_string is finally Second value
In reply to Perl forked processes and variable sharing by fireblood
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |