File Coverage

blib/lib/No/Worries/Export.pm
Criterion Covered Total %
statement 53 62 85.4
branch 13 20 65.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 77 93 82.8


line stmt bran cond sub pod time code
1             #+##############################################################################
2             # #
3             # File: No/Worries/Export.pm #
4             # #
5             # Description: symbol exporting without worries #
6             # #
7             #-##############################################################################
8              
9             #
10             # module definition
11             #
12              
13             package No::Worries::Export;
14 22     22   76855 use strict;
  22         62  
  22         1185  
15 22     22   152 use warnings;
  22         61  
  22         1995  
16             our $VERSION = "1.5";
17             our $REVISION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
18              
19             #
20             # used modules
21             #
22              
23 22     22   8830 use Params::Validate qw(validate_with :types);
  22         202639  
  22         12226  
24              
25             #
26             # simple yet powerful export control
27             #
28              
29             sub export_control ($$$@) {
30 291     291 1 2957 my($callpkg, $pkg, $exported, @names) = @_;
31 291         627 my($name, $regexp, $ref);
32              
33 291         7757 validate_with(
34             params => \@_,
35             spec => [ { type => SCALAR }, { type => SCALAR }, { type => HASHREF } ],
36             allow_extra => 1,
37             );
38 291         2273 while (@names) {
39 478         1278 $name = shift(@names);
40             # special case for * and /.../ and x.y
41 478 100       3034 if ($name eq "*") {
    50          
    50          
42 19         65 unshift(@names, grep(!ref($exported->{$_}), keys(%{ $exported })));
  19         236  
43 19         109 next;
44             } elsif ($name =~ /^\/(.*)\/$/) {
45 0         0 $regexp = $1;
46             unshift(@names, grep(/$regexp/, grep(!ref($exported->{$_}),
47 0         0 keys(%{ $exported }))));
  0         0  
48 0         0 next;
49             } elsif ($name =~ /^\d/) {
50             # version checking via UNIVERSAL
51 0         0 $pkg->VERSION($name);
52 0         0 next;
53             }
54             die("\"$name\" is not exported by the $pkg module\n")
55 459 50       1634 unless defined($exported->{$name});
56 459         1147 $ref = ref($exported->{$name});
57 459 50       1151 if ($ref eq "") {
    0          
58             # normal symbol
59 459 100       2450 if ($name =~ /^(\w+)$/) {
    100          
    100          
    50          
60             # function
61 22     22   231 no strict qw(refs);
  22         192  
  22         944  
62 22     22   158 no warnings qw(once prototype);
  22         52  
  22         2418  
63 380         794 *{"${callpkg}::${1}"} = \&{"${pkg}::${1}"};
  380         93811  
  380         2153  
64             } elsif ($name =~ /^\$(\w+)$/) {
65             # scalar
66 22     22   176 no strict qw(refs);
  22         54  
  22         2560  
67 77         191 *{"${callpkg}::${1}"} = \${"${pkg}::${1}"};
  77         3891  
  77         438  
68             } elsif ($name =~ /^\@(\w+)$/) {
69             # array
70 22     22   166 no strict qw(refs);
  22         65  
  22         2035  
71 1         2 *{"${callpkg}::${1}"} = \@{"${pkg}::${1}"};
  1         6  
  1         3  
72             } elsif ($name =~ /^\%(\w+)$/) {
73             # hash
74 22     22   173 no strict qw(refs);
  22         58  
  22         5703  
75 1         2 *{"${callpkg}::${1}"} = \%{"${pkg}::${1}"};
  1         5  
  1         4  
76             } else {
77 0         0 die("unsupported export by the $pkg module: $name\n");
78             }
79             } elsif ($ref eq "CODE") {
80             # special symbol
81 0         0 $exported->{$name}->($name);
82             } else {
83 0         0 die("unsupported export by the $pkg module: $name=$ref\n");
84             }
85             }
86             }
87              
88             #
89             # export control
90             #
91              
92             sub import : method {
93 109     109   319 my($pkg, %exported);
94              
95 109         278 $pkg = shift(@_);
96 109         474 grep($exported{$_}++, qw(export_control));
97 109         481 export_control(scalar(caller()), $pkg, \%exported, @_);
98             }
99              
100             1;
101              
102             __DATA__