File Coverage

lib/Perlmazing/Engine/Exporter.pm
Criterion Covered Total %
statement 46 77 59.7
branch 10 34 29.4
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 64 120 53.3


line stmt bran cond sub pod time code
1             package Perlmazing::Engine::Exporter;
2 31     31   230 use strict;
  31         61  
  31         1124  
3 31     31   163 use warnings;
  31         53  
  31         998  
4 31     31   173 use Carp;
  31         50  
  31         4242  
5             our $VERSION = '1.2810';
6             my $package = __PACKAGE__;
7             my $imports;
8            
9             sub import {
10 1372     1372   2643 my $self = shift;
11 1372         12669 my @call = caller 0;
12 1372         2843 my $pack = $call[0];
13 31     31   195 no strict 'refs';
  31         55  
  31         9933  
14 1372 100       3069 if ($self eq $package) {
15 31         60 my $in_isa = grep { /^\Q$package\E$/ } @{"${pack}::ISA"};
  0         0  
  31         266  
16 31 50       129 unshift (@{"${pack}::ISA"}, __PACKAGE__) unless $in_isa;
  31         1157  
17             } else {
18 1341 50       10776 if (my @call = caller 1) {
19 1341 50       4057 $pack = $call[0] if $call[3] eq "${self}::import";
20             }
21 1341 50       4433 return if $imports->{$pack}->{$self};
22 1341         1963 for my $i (@{"${self}::EXPORT"}) {
  1341         3744  
23 143380         228876 $package->export($self, $i, $pack);
24             }
25 1341         243223 $imports->{$pack}->{$self}++;
26             }
27             }
28            
29             sub export {
30 143380     143380 0 164830 my $self = shift;
31 143380         204737 my ($from, $symbol, $to) = (shift, shift, shift);
32 143380         149318 my $sigil = '&';
33 143380         220080 $symbol =~ s/^(\&|\$|\%|\@|\*)/$sigil = $1; ''/e;
  0         0  
  0         0  
34 143380 50       245304 croak "Unknown symbol type for expression '$symbol' in EXPORT" if $symbol =~ /^\W/;
35 31     31   229 no strict 'refs';
  31         63  
  31         870  
36 31     31   170 no warnings 'once';
  31         59  
  31         12767  
37 143380 50       202558 if ($sigil eq '&') {
    0          
    0          
    0          
    0          
38 143380 50       141856 if (not defined *{"${from}::$symbol"}{CODE}) {
  143380         331205  
39 0         0 eval "sub ${from}::$symbol"; ## no critic
40 0 0       0 croak "Cannot create symbol for sub ${from}::$symbol: $@" if $@;
41             }
42 143380 50       159760 if (defined *{"${to}::$symbol"}{CODE}) {
  143380         509116  
43 0         0 croak "Cannot define symbol &${to}::$symbol: symbol is already defined under the same namespace and name";
44             } else {
45 143380         162792 *{"${to}::$symbol"} = *{"${from}::$symbol"}{CODE};
  143380         380236  
  143380         232819  
46             }
47             } elsif ($sigil eq '$') {
48 0 0         if (not defined *{"${from}::$symbol"}{SCALAR}) {
  0            
49 0           ${"${from}::$symbol"} = undef;
  0            
50             }
51 0           *{"${to}::$symbol"} = *{"${from}::$symbol"}{SCALAR};
  0            
  0            
52             } elsif ($sigil eq '@') {
53 0 0         if (not defined *{"${from}::$symbol"}{ARRAY}) {
  0            
54 0           @{"${from}::$symbol"} = ();
  0            
55             }
56 0           *{"${to}::$symbol"} = *{"${from}::$symbol"}{ARRAY};
  0            
  0            
57             } elsif ($sigil eq '%') {
58 0 0         if (not defined *{"${from}::$symbol"}{HASH}) {
  0            
59 0           %{"${from}::$symbol"} = ();
  0            
60             }
61 0           *{"${to}::$symbol"} = *{"${from}::$symbol"}{HASH};
  0            
  0            
62             } elsif ($sigil eq '*') {
63 0           *{"${to}::$symbol"} = *{"${from}::$symbol"};
  0            
  0            
64             } else {
65 0           croak "I don't know how to handle '$symbol' in EXPORT";
66             }
67             }
68            
69             1;