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)
In reply to trying to understand references by coontie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |