If you want to get your hands dirty you can manipulate package main::'s symbol table. Below you'll see a number of examples of how to either manipulate your local symbol table, or access a function in another package without importing it or aliasing the symbols.
package AB; sub testing { print $_[0] . ": " . __PACKAGE__ . "\n"; } 1; package CD; sub testing { print $_[0] . ": " . __PACKAGE__ . "\n"; } 1; package main; use strict; use warnings; use 5.014; my $count = 0; foreach ( qw/ AB CD/ ) { no strict 'refs'; &{$_ . '::testing'}($count++); } *testing = \&AB::testing; # redefined warning testing($count++); *testing = \&CD::testing; # redefined warning testing($count++); $::{testing} = \&AB::testing; # redefined warning testing($count++); $main::{testing} = \&CD::testing; # redefined warning testing($count++); $main::{testing} = *AB::testing{CODE}; # redefined warning testing($count++); *testing = *CD::testing; testing($count++); *main::testing = *AB::testing; testing($count++); $main::{testing}= *CD::testing; testing($count++); foreach( qw/AB CD/ ) { no strict 'refs'; *main::testing = *{ $_ . "::testing" }; testing($count++); }
Not all of the above methods accomplish the same exact thing. Some alias an entire typeglob (an entire symbol). Others select only the subroutine for aliasing (leaving other "THING"s in the typeglob untouched. And some simply invoke the sub by fully qualifying its originally package name. I included a bunch of examples just to show some of the ways.
Note: Whenever Perl knows that you're specifically overwriting an existing declaration of a sub it will issue a warning. Typeglob to typeglob aliasing subverts that warning, but coderef to typeglob assignments do not subvert the warning. Consider it a feature, that Perl will complain to you when you do something silly if it sees you doing it.
One of the issues you face when using 'use' is that any subsequent 'use' that exports the same subroutine will mask the previous one. This is where carefully chosen exports is important, as well as knowing how to get at the fully qualified name.
Dave
In reply to Re: Recursive use statements?
by davido
in thread Recursive use statements?
by ellis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |