coontie has asked for the wisdom of the Perl Monks concerning the following question:
which prints this:#!/usr/bin/perl -w use strict; my $testVar = 1; my $ptr = \$testVar; if (my $pid = fork) { while (1) { print "from parent: $testVar \n"; print "ptr from parent: $ptr \n"; sleep 2; } waitpid($pid, 0); } else { while (1) { print "from child: $testVar \n"; print "ptr from client: $ptr \n"; $$ptr++; sleep 2; } exit; }
Now, I expected the parent to see the newly incremented value of $testVar but I don't. Why is that? Doesn't the reference point to a location in memory that is shared by both the parent & the child?from child: 1 ptr from client: SCALAR(0x814db78) from parent: 1 ptr from parent: SCALAR(0x814db78) from child: 2 ptr from client: SCALAR(0x814db78) from parent: 1 ptr from parent: SCALAR(0x814db78) from child: 3 ptr from client: SCALAR(0x814db78) from parent: 1 ptr from parent: SCALAR(0x814db78) from child: 4 ptr from client: SCALAR(0x814db78) from parent: 1 ptr from parent: SCALAR(0x814db78)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: trying to understand references
by ikegami (Patriarch) on Dec 21, 2005 at 19:19 UTC | |
|
Re: trying to understand references
by BrowserUk (Patriarch) on Dec 21, 2005 at 19:24 UTC | |
|
Re: trying to understand references
by tirwhan (Abbot) on Dec 21, 2005 at 19:26 UTC |