File Coverage

blib/lib/Import/Export.pm
Criterion Covered Total %
statement 40 40 100.0
branch 22 22 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 72 72 100.0


line stmt bran cond sub pod time code
1             package Import::Export;
2              
3 6     6   326588 use Carp;
  6         45  
  6         255  
4 6     6   2039 use namespace::clean ();
  6         67555  
  6         2957  
5              
6             our $VERSION = '0.08';
7              
8             our %EX;
9              
10             our %EXTYPE = (
11             '&' => \&_export_code,
12             '$' => \&_export_scalar,
13             '@' => \&_export_array,
14             '%' => \&_export_hash,
15             '*' => \&_export_glob,
16             );
17              
18             sub import {
19 19     19   5632 my ($pkg, $options, $caller) = (shift, {}, caller());
20 19 100       158 return unless my @export = @_;
21             Carp::croak('define your %EX export hash')
22 13 100       18 unless (%EX) = %{"${pkg}::EX"};
  13         269  
23              
24 12 100       30 $options = pop @export if ref $export[-1];
25 12 100       29 $caller = $options->{-caller} if exists $options->{-caller};
26 12         25 my @exported = export($pkg, $caller, @export);
27              
28 10 100       2502 $options->{clean} and "$options->{clean}" eq "import"
    100          
29             ? namespace::clean->clean_subroutines( # sub import { p->import(@export, { clean => 'import' }) }
30             $caller,
31             @exported
32             )
33             : namespace::clean->import( # use p @export, { clean => 1 }
34             -cleanee => $caller,
35             @exported
36             );
37             }
38              
39             sub export {
40 17     17 1 30 my ($pkg, $caller, @exported) = (shift, shift);
41 17         42 while (my $ex = shift) {
42 20         26 my $type;
43 20 100       35 if ( ! $EX{$ex} ) {
44             my @export = grep {
45 6         15 grep { $_ =~ m{\Q$ex\E} } @{ $EX{$_} }
  9         13  
  9         58  
  9         17  
46             } keys %EX;
47             scalar @export
48 6 100       133 ? return export($pkg, $caller, (@export, @_))
49             : Carp::croak "$ex is not exported";
50             }
51              
52 14 100       56 $type = ($ex =~ s/^(\W)//) ? $1 : "&";
53 14 100       113 $caller->can($ex) and next;
54              
55 13 100       141 my $exporting = $EXTYPE{$type} or Carp::croak("Cant export symbol $type");
56 12         20 *{"${caller}::${ex}"} = $exporting->($pkg, $ex);
  12         25  
57 12         32 push @exported, $ex;
58             }
59 10         26 return @exported;
60             }
61              
62 7     7   7 sub _export_code { \&{"$_[0]::$_[1]"} }
  7         22  
63 2     2   2 sub _export_scalar { \${"$_[0]::$_[1]"} }
  2         6  
64 1     1   1 sub _export_array { \@{"$_[0]::$_[1]"} }
  1         3  
65 1     1   1 sub _export_hash { \%{"$_[0]::$_[1]"} }
  1         2  
66 1     1   2 sub _export_glob { *{"$_[0]::$_[1]"} }
  1         3  
67              
68             1;
69              
70             __END__