| 1 | package unconstant; | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use Sub::Util (); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use constant (); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | my $constant_import; | |||||||||||||||||||||||||||||||||||||||||||
| 7 | my $installed; | ||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | BEGIN { $constant_import = \&constant::import }; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | our %declared; | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | #======================================================================= | ||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | # Some names are evil choices. | ||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD UNITCHECK }; | |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | my %forced_into_main = map +($_, 1), | |||||||||||||||||||||||||||||||||||||||||||
| 25 | qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG }; | ||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | my %forbidden = (%keywords, %forced_into_main); | |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 4 | my $normal_constant_name = qr/^_?[^\W_0-9]\w*\z/; | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | my $tolerable = qr/^[A-Za-z_]\w*\z/; | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | my $boolean = qr/^[01]?\z/; | |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | sub unconstant_import { | ||||||||||||||||||||||||||||||||||||||||||||
| 34 | 9 | ▶ | return if $installed; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unconstant_import: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 6 | *constant::import = *constant_import; | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | $installed = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | sub unconstant_unimport { | ||||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | return unless $installed; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unconstant_unimport: called CC=2 SCAR=7.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | *constant::import = $constant_import; | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | $installed = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | sub constant_import { | ||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | 140 | ▶ | my $caller = caller(); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
constant_import: called CC=25 SCAR=43.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 140 | my $package = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 140 | my $flush_mro; | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 140 | ▶ | return unless @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 52 | 140 | my $multiple = ref $_[0]; | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 140 | my $constants; | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 140 | ▶ | if ( $multiple ) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | if ($multiple ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'"); | |||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | $constants = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 62 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 63 | 140 | ▶ | unless (defined $_[0]) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 24 | Carp::croak("Can't use undef as constant name"); | |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 67 | 138 | $constants->{+shift} = undef; | |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 138 | my $symtab; | |||||||||||||||||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 74 | 138 | $symtab = \%{$caller . "::"}; | |||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 138 | foreach my $name ( keys %$constants ) { | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 138 | my $pkg = $caller; | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 138 | my $symtab = $symtab; | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 138 | my $orig_name = $name; | |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 138 | ▶ | if ($name =~ s/(.*)(?:::|')(?=.)//s) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | $pkg = $1; | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | if ($pkg ne $caller) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | $symtab = \%{$pkg . '::'}; | |||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Normal constant name | ||||||||||||||||||||||||||||||||||||||||||||
| 91 | 138 | ▶ | if ($name =~ $normal_constant_name and !$forbidden{$name}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition33%1/3
$name =~ /$normal_constant_name/u and not $forbidden{$name}
MC/DC0%0/2
$name =~ /$normal_constant_name/u and not $forbidden{$name}
$name =~ /$normal_constant_name/u
not $forbidden{$name}
Truth table33%1/3
A and not(B)
A= $name =~ /$normal_constant_name/uB= not $forbidden{$name}
| |||||||||||||||||||||||||||||||||||||||||||||
| 92 | # Everything is okay | ||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | # Name forced into main, but we're not in main. Fatal. | ||||||||||||||||||||||||||||||||||||||||||||
| 96 | ▶ | elsif ($forced_into_main{$name} and $pkg ne 'main') { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$forced_into_main{$name} and $pkg ne "main"
MC/DC0%0/2
$forced_into_main{$name} and $pkg ne "main"
$forced_into_main{$name}
$pkg ne "main"
Truth table0%0/3
A and B
A= $forced_into_main{$name}B= $pkg ne "main"
| |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | Carp::croak("Constant name '$name' is forced into main::"); | |||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | # Starts with double underscore. Fatal. | ||||||||||||||||||||||||||||||||||||||||||||
| 102 | elsif ($name =~ /^__/) { | ||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | Carp::croak("Constant name '$name' begins with '__'"); | |||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | # Maybe the name is tolerable | ||||||||||||||||||||||||||||||||||||||||||||
| 108 | elsif ($name =~ $tolerable) { | ||||||||||||||||||||||||||||||||||||||||||||
| 109 | # Then we'll warn only if you've asked for warnings | ||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (warnings::enabled()) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ($keywords{$name}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | warnings::warn("Constant name '$name' is a Perl keyword"); | |||||||||||||||||||||||||||||||||||||||||||
| 113 | } elsif ($forced_into_main{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | warnings::warn("Constant name '$name' is " . | |||||||||||||||||||||||||||||||||||||||||||
| 115 | "forced into package main::"); | ||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | # Looks like a boolean | ||||||||||||||||||||||||||||||||||||||||||||
| 121 | # use constant FRED == fred; | ||||||||||||||||||||||||||||||||||||||||||||
| 122 | elsif ($name =~ $boolean) { | ||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | Carp::croak("Constant name '$name' is invalid"); | |||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | Carp::croak("Constant name looks like boolean value"); | |||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 132 | # Must have bad characters | ||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | Carp::croak("Constant name '$name' has invalid characters"); | |||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 138 | my $full_name = "${pkg}::$name"; | |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | # This is required to fool namespace::autoclean | ||||||||||||||||||||||||||||||||||||||||||||
| 141 | 138 | my $const_name = "constant::$name"; | |||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | 138 | $declared{$full_name}++; | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 138 | ▶ | if ($multiple || @_ == 1) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$multiple or @_ == 1
MC/DC0%0/2
$multiple or @_ == 1
$multiple
@_ == 1
Truth table33%1/3
A or B
A= $multipleB= @_ == 1
| |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 138 | ▶ | my $scalar = $multiple ? $constants->{$orig_name} : $_[0]; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | #$symtab->{$name} = sub () { $scalar }; | ||||||||||||||||||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||||||||||||||||||
| 149 | 6 | ▶ | no warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 150 | 138 | ▶ | *$full_name = Sub::Util::set_prototype( '', Sub::Util::set_subname("constant::$name", sub { $scalar } ) ); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
P_LOOSE: called CC=1 SCAR=0.0
Subroutine
INCR_M_C0: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 152 | 138 | ++$flush_mro->{$pkg}; | |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 154 | elsif (@_) { | ||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my @list = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||||||||||||||||||
| 157 | 6 | ▶ | no warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | *$full_name = Sub::Util::set_prototype( '', Sub::Util::set_subname("constant::$name", sub { @list } ) ); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | $flush_mro->{$pkg}++; | |||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 162 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | die 'should never hit this'; | |||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 166 | # Flush the cache exactly once if we make any direct symbol table changes. | ||||||||||||||||||||||||||||||||||||||||||||
| 167 | 138 | ▶ | if ($flush_mro) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 138 | mro::method_changed_in($_) for keys %$flush_mro; | |||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | { | ||||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | ▶ | no warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 175 | 4 | *import = \&unconstant_import; | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | *unimport = \&unconstant_unimport; | |||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||