# Package/Exporter.pm
package Package::Exporter;
use strict;
use warnings;
use Carp qw[confess];
use Data::Dumper;
sub import {
my( $class, @exports ) = @_;
# turn 'Foo::Bar' into 'Foo/Bar'
( my $class_path = $class ) =~ s{::}{/}g;
for my $export( @exports ) {
no strict 'refs';
# attempt to require 'Foo/Bar/Baz.pm'
eval { require "$class_path/$export.pm"; };
# die if there was an error
if( $@ ) {
confess __PACKAGE__ . "::import ${class}::import problem with $export - $@";
}
# warn __PACKAGE__ . "::import ${class}::$export";
# import, just in case
"${class}::$export"->import;
# loop through everything in 'Foo::Bar::Baz::'
my @all_in_namespace = keys %{"${class}::${export}::"};
# only the methods this time
my @methods_in_namespace = grep { *{"${class}::${export}::${_}"}{CODE} } @all_in_namespace;
for my $method( @methods_in_namespace ) {
# and assign 'Baz::method' to 'Foo::Bar::Baz::method'
# warn sprintf( q["%s::%s" = sub () { "%s::%s::%s"}], $export, $method, $class, $export, $method );
*{"${export}::${method}"}=\&{"${class}::${export}::${method}"};
}
}
}
1;
####
# Foo/Bar.pm
package Foo::Bar;
use strict;
use warnings;
use base 'Package::Exporter';
1;
####
# foo.pl
use Foo::Bar qw(Baz);
print Baz::quux(), "\n";