|Perl Monk, Perl Meditation|
Reference and $_ weirdnessby Marcello (Hermit)
|on Jun 27, 2005 at 09:15 UTC||Need Help??|
Marcello has asked for the wisdom of the Perl Monks concerning the following question:
This problem is very difficult to describe, I will try to give as many details as possible in the hope someone out there understands what is going on.
A large Perl application contains a hash which contains an array. The application forks of two child processes using this array:
After the fork, the child process creates several objects, one of which gets gets a reference to this hash. This process then calls an object function which alters the $_ variable. The reference to the hash is never changed nor used.
After this function call, the IP array in the hash has mysteriously changed in the child process! The Xth element (either 0 or 1) of the corresponding child process has the exact same value as $_, which was set in another object.
If I change the foreach to:
the IP array in the hash is not altered anymore. So it appears the $_ variable has something to do with it, but I do not see where this goes wrong. Especially because the IP array is not altered anywhere in the application and the $_ is changed in another package. I suspect it's a problem with the forking, the reference to the hash and the $_ variable.
Does anyone have a clue?