File Coverage

lib/Perlmazing/Engine/Exporter.pm
Criterion Covered Total %
statement 46 86 53.4
branch 11 40 27.5
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 65 135 48.1


line stmt bran cond sub pod time code
1             package Perlmazing::Engine::Exporter;
2 32     32   286 use strict;
  32         65  
  32         1069  
3 32     32   168 use warnings;
  32         82  
  32         937  
4 32     32   161 use Carp;
  32         58  
  32         3957  
5             our $VERSION = '1.2810';
6             my $package = __PACKAGE__;
7             my $imports;
8              
9             sub import {
10 1418     1418   2261 my $self = shift;
11 1418         11169 my @call = caller 0;
12 1418         2799 my $pack = $call[0];
13 32     32   202 no strict 'refs';
  32         63  
  32         10076  
14 1418 100       2941 if ($self eq $package) {
15 32         48 my $in_isa = grep { /^\Q$package\E$/ } @{"${pack}::ISA"};
  0         0  
  32         202  
16 32 50       137 unshift (@{"${pack}::ISA"}, __PACKAGE__) unless $in_isa;
  32         1033  
17             } else {
18 1386 50       10036 if (my @call = caller 1) {
19 1386 50       3936 $pack = $call[0] if $call[3] eq "${self}::import";
20             }
21 1386 50       4110 return if $imports->{$pack}->{$self};
22 1386         1818 for my $i (@{"${self}::EXPORT"}) {
  1386         3486  
23 21171         36666 $package->export($self, $i, $pack);
24             }
25 1386         209040 $imports->{$pack}->{$self}++;
26             }
27             }
28              
29             sub export {
30 21171     21171 0 26078 my $self = shift;
31 21171         31074 my ($from, $symbol, $to) = (shift, shift, shift);
32 21171         22865 my $sigil = '&';
33 21171         33603 $symbol =~ s/^(:|\&|\$|\%|\@|\*)/$sigil = $1; ''/e;
  0         0  
  0         0  
34 21171 50       37532 croak "Unknown symbol type for expression '$symbol' in EXPORT" if $symbol =~ /^\W/;
35 32     32   235 no strict 'refs';
  32         82  
  32         887  
36 32     32   152 no warnings 'once';
  32         67  
  32         16956  
37 21171 50       35985 if ($sigil eq ':') {
    50          
    0          
    0          
    0          
    0          
38 0         0 my $tags = \%{"${from}::EXPORT_TAGS"};
  0         0  
39 0 0       0 if (not exists $tags->{$symbol}) {
40 0         0 croak "Export tag '$symbol' is not defined in package $from";
41             }
42 0 0       0 unless (ref($tags->{$symbol}) eq 'ARRAY') {
43 0         0 croak "Export tags should contain array refs";
44             }
45 0         0 for my $i (@{$tags->{$symbol}}) {
  0         0  
46 0         0 $self->export($from, $i, $to);
47             }
48             } elsif ($sigil eq '&') {
49 21171 50       21147 if (not defined *{"${from}::$symbol"}{CODE}) {
  21171         49818  
50 0         0 eval "sub ${from}::$symbol"; ## no critic
51 0 0       0 croak "Cannot create symbol for sub ${from}::$symbol: $@" if $@;
52             }
53 21171 50       23974 if (defined *{"${to}::$symbol"}{CODE}) {
  21171         73092  
54 0         0 croak "Cannot define symbol &${to}::$symbol: symbol is already defined under the same namespace and name";
55             } else {
56 21171         24592 *{"${to}::$symbol"} = *{"${from}::$symbol"}{CODE};
  21171         57908  
  21171         33948  
57             }
58             } elsif ($sigil eq '$') {
59 0 0         if (not defined *{"${from}::$symbol"}{SCALAR}) {
  0            
60 0           ${"${from}::$symbol"} = undef;
  0            
61             }
62 0           *{"${to}::$symbol"} = *{"${from}::$symbol"}{SCALAR};
  0            
  0            
63             } elsif ($sigil eq '@') {
64 0 0         if (not defined *{"${from}::$symbol"}{ARRAY}) {
  0            
65 0           @{"${from}::$symbol"} = ();
  0            
66             }
67 0           *{"${to}::$symbol"} = *{"${from}::$symbol"}{ARRAY};
  0            
  0            
68             } elsif ($sigil eq '%') {
69 0 0         if (not defined *{"${from}::$symbol"}{HASH}) {
  0            
70 0           %{"${from}::$symbol"} = ();
  0            
71             }
72 0           *{"${to}::$symbol"} = *{"${from}::$symbol"}{HASH};
  0            
  0            
73             } elsif ($sigil eq '*') {
74 0           *{"${to}::$symbol"} = *{"${from}::$symbol"};
  0            
  0            
75             } else {
76 0           croak "I don't know how to handle '$symbol' in EXPORT";
77             }
78             }
79              
80             1;