SPUG: Change a namespace after loading?

Joshua ben Jore twists at gmail.com
Sat Apr 14 21:22:44 PDT 2007

On 4/13/07, Bill Warner <billw at onedrous.org> wrote:
> I don't want to edit the clients, nor do I want to edit the deprecated
> code. I want to load the deprecated code and change its namespace then
> write some new code that dispatches to the deprecated code in the
> altered namespace.
> I'm thinking Safe.pm will do it.

I think you're in for a world of hurt. You *can* just wholesale copy a
bunch of stuff from one namespace to another.

  %{"${new_class}::"} = %{"${old_class}::"};

That clobbers the new class but its just a hash operation so I leave
it up to you to decide just how non-clobbering or merging you wish to
be. The things that are *just* data will probably go ok. You won't be
moving any lexicals but those are all embedded in the captured
environment of all those functions you copied.

Those functions are... problematic. Here's some perlguts. The CV roots
have pointers to the stash they were compiled in. That will remain
unchanged unless you do something about it. The compiled form of each
function will /also/ have pointers to the package it was compiled in.
This is relatively normal in perl - anything exported from one package
to another has pointers to stashes other than the one it is currently
in. This merely affects what things like caller(), __PACKAGE__,
__FILE__, __LINE__ are going to return.

*Also* the lexical environment is shared between your copies. You will
probably want to clone your pads somehow. I don't know the answer but
suspect PadWalker might be able to this without much trouble. You
*might* just get away with PadWalker+Storable::dclone.

Good luck! Write back about your experience, eh?


More information about the spug-list mailing list