It lets you import all the subroutines (excluding a few dangerous ones) from one or more other packages. Using it is as simple as:
package B; use AllSubsFrom 'A';
which will import all the subs (including constants) from package A into package B.
# AllSubsFrom.pm - Perl module to allow importing of all subroutines f +rom # another package or packages. # # By Ned Konz, perl@bike-nomad.com # # Usage: # # package X; # use AllSubsFrom qw(A B C); # # can also be called explicitly on any package: # # MyPackage->importAllSubsFrom('SomeOtherPackage'); # # MyPackage->importAllSubsFrom qw(SomeOtherPackage YetAnotherPackag +e); # use strict; package AllSubsFrom; $AllSubsFrom::VERSION = '0.1'; # calls AllSubsFrom::import('A', 'B', 'C'); sub import { UNIVERSAL::importAllSubsFrom( caller, @_ ); } # This is in UNIVERSAL, the root of all evil. # That way, it can be called as a class method on any package. # Export all the not-already-defined subroutines from one # package to another package: # To->importAllSubsFrom('From1', 'From2'); # importAllSubsFrom('To', 'From1', 'From2'); sub UNIVERSAL::importAllSubsFrom { my $to = shift; foreach my $from (@_) { no strict 'refs'; # to put it mildly. my $fromSymtab = \%{ 'main::' . $from . '::' }; foreach my $key ( keys %$fromSymtab ) { # avoid dangerous subs next if $key =~ m{^(?:BEGIN|END|INIT|CHECK|AUTOLOAD|DESTRO +Y|__.*)$}; my $codeRef = *{ $from . '::' . $key }{CODE}; next if !defined($codeRef) # symbol b +ut no sub || defined( *{ $to . '::' . $key }{CODE} ); # don't cl +obber dest *{ $to . '::' . $key } = $codeRef; # replace just the C +ODE ref } } } # This imports all the subs to the caller's package # From->exportAllSubsToMe; sub UNIVERSAL::exportAllSubsToMe { UNIVERSAL::importAllSubsFrom( caller, shift ); } "now wasn't that an exciting trip?";
|
|---|