blib/lib/constant/tiny.pm

filter: Statementstmt 22.2% 12 / 54 Branchbran 4.5% 1 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 4 / 8 Podpod n/a totaltotal 16.3% 17 / 104 CC 16 SCAR 50.1
CC
16
Cov
15%
__ANON__
213.9
__ANON__
2.0
__ANON__
2.0
CRAP 149.3
↑ Summary
1 package constant::tiny;
2 2 ▶return 1 if $INC{"constant.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"constant.pm"} 2 0 2
3 0 $INC{"constant.pm"} = $INC{+__FILE__};
4 0 $VERSION = "1.02";
5  
6 package # hide from PAUSE
7 constant;
8 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 0 $constant::VERSION = $constant::tiny::VERSION;
13  
14 0 my %forbidden = map +($_, 1), qw<
15 BEGIN INIT CHECK END DESTROY AUTOLOAD UNITCHECK
16 STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG
17 >;
18  
19 0 my $normal_constant_name = qr/^_?[A-Za-z0-9][A-Za-z0-9_]+\z/;
20  
21  
22 #
23 # import()
24 # ------
25 # import symbols into user's namespace
26 #
27 # What we actually do is define a function in the caller's namespace
28 # which returns the value. The function we create will normally
29 # be inlined as a constant, thereby avoiding further sub calling
30 # overhead.
31 #
32 0 ▶*import = sub {
Subroutine __ANON__: not called CC=16 SCAR=53.7
33 0 my $class = shift;
34 0 ▶ return unless @_; # ignore "use constant;"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
35  
36 0 ▶ if (not defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 0 0 0
37 0 require Carp;
38 0 Carp::croak("Can't use undef as constant name");
39 }
40  
41 0 my $multiple = ref $_[0];
42 0 ▶ if ($multiple and $multiple ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiple and $multiple ne "HASH") 0 0 0
Condition0%0/3
$multiple and $multiple ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$multiple and $multiple ne "HASH"
$multiple $multiple ne "HASH"
Truth table0%0/3
A and B
A= $multipleB= $multiple ne "HASH"
ABresult
0X0
100
111
43 0 require Carp;
44 0 Carp::croak("Invalid reference type '$multiple'");
45 }
46  
47 0 ▶ my $constants = $multiple ? shift : { shift() => undef };
Branch: 1 branch
BranchTrueFalseTotal
$multiple ? : 0 0 0
48 0 my $flush_mro;
49 0 my $pkg = caller();
50 0 my $symtab;
51 0 ▶ { no strict 'refs'; $symtab = \%{$pkg . '::'} }
Subroutine BEGIN: called CC=1 SCAR=1.2
52  
53 0 foreach my $name (keys %$constants) {
54 # Normal constant name
55 0 ▶ if ($name !~ $normal_constant_name or $forbidden{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name =~ /$normal_constant_name/ or $forbidden{$name}) 0 0 0
Condition0%0/3
not $name =~ /$normal_constant_name/ or $forbidden{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $name =~ /$normal_constant_name/ or $forbidden{$name}
not $name =~ /$normal_constant_name/ $forbidden{$name}
Truth table0%0/3
not(A) or B
A= not $name =~ /$normal_constant_name/B= $forbidden{$name}
ABresult
1X1
011
000
56 0 require Carp;
57 0 Carp::croak("Invalid name '$name'");
58 }
59  
60 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 0 my $full_name = "${pkg}::$name";
62  
63 0 ▶ if ($multiple || @_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($multiple or @_ == 1) { } 0 0 0
elsif (@_) { } 0 0 0
Condition0%0/3
$multiple or @_ == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$multiple or @_ == 1
$multiple @_ == 1
Truth table0%0/3
A or B
A= $multipleB= @_ == 1
ABresult
1X1
011
000
64 0 ▶ my $scalar = $multiple ? $constants->{$name} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$multiple ? : 0 0 0
65  
66 0 ▶ if ($symtab && !exists $symtab->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symtab and not exists $symtab->{$name}) { } 0 0 0
Condition0%0/3
$symtab and not exists $symtab->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
$symtab and not exists $symtab->{$name}
$symtab not exists $symtab->{$name}
Truth table0%0/3
A and not(B)
A= $symtabB= not exists $symtab->{$name}
ABresult
0X0
100
111
67 0 Internals::SvREADONLY($scalar, 1);
68 0 $symtab->{$name} = \$scalar;
69 0 ++$flush_mro;
70 } else {
71 0 my $scalar = $scalar;
72 0 ▶ *$full_name = sub () { $scalar };
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 }
74 } elsif (@_) {
75 0 my @list = @_;
76 0 ▶ *$full_name = sub () { @list };
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 } else {
78 0 ▶ *$full_name = sub () { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 }
80 }
81  
82 0 ▶ mro::method_changed_in($pkg) if $flush_mro;
Branch: 1 branch
BranchTrueFalseTotal
if $flush_mro 0 0 0
83 0 };
84  
85  
86 0 q< IN CONSTANT TIME >
87  
88 __END__
↑ Summary