gri6507 has asked for the wisdom of the Perl Monks concerning the following question:
I have a need to fork() and then redefine a function within the child, but not the parent. Here's my code.
This prints:use warnings; use strict; sub foo { my $bar = shift; print "original foo($bar)\n"; } if (my $pid = fork()) { close PARENT; foo('parent'); close CHILD; waitpid($pid, 0); } else { die "cannot fork: $!" unless defined $pid; close CHILD; # overload the function no warnings 'redefine'; sub foo { my $bar = shift; print "modified foo($bar)\n"; } foo('child'); close PARENT; exit(0); } foo('the end');
However, I was hoping that the redefinition of the foo() function would only affect the scope of the child process. In other words, I wanted to get
original foo(parent)
modified foo(child)
original foo(the end)
Is something like this possible?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Redefining function after fork
by Athanasius (Archbishop) on Jan 18, 2014 at 02:43 UTC | |
by gri6507 (Deacon) on Jan 18, 2014 at 17:44 UTC | |
by AnomalousMonk (Archbishop) on Jan 18, 2014 at 18:32 UTC | |
by gri6507 (Deacon) on Jan 18, 2014 at 18:46 UTC |