in reply to Exported subroutine redefine

I wrote an all of chapter 10 for Mastering Perl about how to do this (and chapter 4 deals with some fo that Carp stuff tilly mentioned).

I didn't cover the @EXPORT case, though, so maybe I should add that. As others pointed out, you have to export to the calling namespace to overwrite the functions something else exported. The problem is that now you have to use those modules in the right order or you'll get the wrong function (anyone remember the CGI.pm and URL.pm modules fighting it out to see whose url() would be the final one defined?).

See if you can get yourself to the point where you're only using redefine in the code so it loads the module it wants to override, does its work, then expors the final versions of everything. You won't have someone come along and list all of the modules in alphabetical order (yeah, some of us are a bit OCD like that) and screw up everything. :)

--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

Replies are listed 'Best First'.
Re^2: Exported subroutine redefine
by ribasushi (Pilgrim) on Nov 11, 2007 at 13:25 UTC
    I just ended up walking the entire symbol table, which turned out to be a trivial subroutine. Please let me know if I am doing something horribly wrong (the justification can be found in the big picture)
    _redef(); sub _redef { my $parent = shift || '::'; for my $ns (grep /^\w+::/, keys %{$parent}) { $ns = $parent . $ns; _redef($ns) unless $ns eq '::main::'; for my $sub (keys %redef) { *{$ns . $sub} = $redef{$sub} if (exists ${$ns}{$sub}); } } }
      This is really handy (and absolute evil of course). I find that it's a good idea to also check that the function you're replaceing not only has the same name, but is truely a pointer to the same function. Just a matter of comparing that \&{${$ns}{$sub}} == $origsub , where $origsub is a pointer to the function you want to replace.