in reply to Redefining function after fork

Yes. Here is one way:

... close CHILD; sub bar { my $bar = shift; print "modified foo($bar)\n"; } *foo = *bar; foo('child'); ...

Output:

12:39 >perl 844_SoPW.pl original foo(parent) modified foo(child) original foo(the end) 12:39 >

See perlmod#Symbol-Tables.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Redefining function after fork
by gri6507 (Deacon) on Jan 18, 2014 at 17:44 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)
        Many thanks! This is exactly what I was looking for.