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

Using GLOBs it is possible to directly manipulate the symbol table for individual entries, e.g.:
sub foo { ... } ... *foo = sub { ... }; # install new version of sub foo
Is something similar possible for an entire package, i.e. renaming a package?
package Foo; sub bar { ... } package main; *::NewFoo = *::Foo; # or something like this NewFoo::bar(); # calls Foo::bar();

Replies are listed 'Best First'.
Re: dynamically renaming a package?
by almut (Canon) on Feb 03, 2010 at 15:59 UTC

    Maybe something like this  (it doesn't exactly rename the package, but rather just copies its symbol table hash):

    #!/usr/bin/perl package Foo; sub bar { print "bar() called.\n" } package main; BEGIN { %{*NewFoo::} = %{*Foo::} } NewFoo::bar(); # calls Foo::bar();

    or without BEGIN, in which case you need to postpone the compilation of NewFoo::bar() until runtime using eval:

    ... %{*NewFoo::} = %{*Foo::}; eval q|NewFoo::bar()|; # calls Foo::bar();