package Cpanel::CPAN::base::ball;

use strict;
use warnings;
use Carp;

use File::Spec;

use version; our $VERSION = qv('0.0.4');

require base;

sub import {
    shift;

    return if !@_;

    my $caller = caller(0);
    local $Carp::Level = ( $Carp::Level || 0 ) + 1;

    no strict 'refs';
  PKG:
    for my $pkg (@_) {
        eval qq{
            package $caller;
            base->import( '$pkg' );
        };
        Carp::croak $@ if $@;    # die for base()

        my $file = $pkg;
        $file = File::Spec->catdir( split( '::', $file ) );

        my $dir = $INC{ $file . '.pm' };
        $dir =~ s{\.pm$}{};

        if ( opendir my $pm_dh, $dir ) {
            my @pms = grep /\.pm$/, readdir($pm_dh);
            closedir $pm_dh;

            next PKG if !@pms;

          PMS:
            for my $pm ( sort @pms ) {
                my $ns = $pm;
                $ns =~ s{\.pm}{};

                my $imp = $pkg . '::' . $ns;

                eval qq{
	                package $caller;
	                Cpanel::CPAN::base::ball->import( '$imp' );
	            };
                Carp::croak $@ if $@;    # die for base()
            }
        }
    }
}

1;
