Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Redefining function after fork

by gri6507 (Deacon)
on Jan 18, 2014 at 01:03 UTC ( [id://1071044]=perlquestion: print w/replies, xml ) Need Help??

gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I have a need to fork() and then redefine a function within the child, but not the parent. Here's my code.

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');
This prints:
modified foo(parent)
modified foo(child)
modified 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
      Thank you. This is what I wanted. However, I also need to do such "remapping" of functions within a child for a set of functions, where each remapping is exactly the same - ideally something like:
      foreach my $origName (qw(foo bar baz)) { my $newName = "modified_$origName"; sub $newName { print "modified $newName(" . join(',', @_) . ")\n"; } *$origName = *$newName; }
      However, this is not lexically correct. How does one create a subroutine whos name is a variable?

        I'm not sure just what you're asking for here, but maybe one of these will serve:

        >perl -wMstrict -le "foreach my $origName (qw(foo bar baz)) { my $newName = qq{gen_$origName}; my $newCode = sub { print qq{generated $origName(@_)}; }; no strict 'refs'; *$newName = $newCode; } ;; gen_foo(1, 2, 3); gen_bar(4, 5, 6); gen_baz(7, 8, 9); ;; my $name = 'also_works'; eval qq{sub $name { print qq{this also works: \@_}; } }; also_works(9, 8, 7); " generated foo(1 2 3) generated bar(4 5 6) generated baz(7 8 9) this also works: 9 8 7

        Update: This can be made a bit more concise (note the slightly tricky disambiguating semicolon in the
            *{; ... } = sub { ... }
        statement):

        >perl -wMstrict -le "foreach my $origName (qw(foo bar)) { no strict 'refs'; *{; qq{gen_$origName} } = sub { print qq{generated $origName(@_)} } +; } ;; gen_foo(1, 2, 3); gen_bar(4, 5, 6); " generated foo(1 2 3) generated bar(4 5 6)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1071044]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found