File Coverage

blib/lib/Debug/Helper/Flag.pm
Criterion Covered Total %
statement 41 43 95.3
branch 17 24 70.8
condition 1 3 33.3
subroutine 6 7 85.7
pod n/a
total 65 77 84.4


line stmt bran cond sub pod time code
1             package Debug::Helper::Flag;
2              
3 4     4   351314 use 5.010;
  4         17  
4 4     4   23 use strict;
  4         8  
  4         126  
5 4     4   24 use warnings;
  4         8  
  4         431  
6              
7             our $VERSION = '0.03';
8              
9             our @ISA = qw(Exporter);
10             our @EXPORT_OK = qw(DEBUG_FLAG);
11              
12 4     4   23 use Carp;
  4         11  
  4         1230  
13              
14             our $strict = $ENV{PERL_DEBUG_HELPER_FLAG_STRICT};
15              
16             my $Value;
17              
18             sub import {
19 5     5   116287 my $caller = shift;
20 5         17 my @args = @_;
21              
22 5 50       25 croak("Too many args") if @args > 3;
23              
24 5         12 my $exp;
25 5 100       20 if (@args % 2) {
26 3   33     12 $exp = shift(@args) // croak("Undefined module argument");
27 3 50       13 $exp eq 'DEBUG_FLAG' or croak("'$exp': invalid module argument");
28             }
29 5         11 my ($want_define, $val);
30 5 100       14 if (@args) {
31 1 50       4 croak("Undefined module argument") if !defined($args[0]);
32 1 50       3 croak("'$args[0]': invalid module argument") if $args[0] ne 'DEBUG_FLAG';
33 1         2 $want_define = 1;
34 1         3 $val = !!$args[1];
35             }
36 5 100       15 if ($want_define) {
37 1 50       4 if (defined($Value)) {
38 0 0       0 croak("Attempt to redefine DEBUG_FLAG with different value") if $val ne $Value;
39             }
40             else {
41 1         2 $Value = $val;
42             {
43 4     4   30 no strict 'refs'; ## no critic (ProhibitNoStrict)
  4         17  
  4         835  
  1         2  
44 1         2 my $const_val = $val;
45 1     0   9 *{__PACKAGE__ . "::DEBUG_FLAG"} = sub () {$const_val};
  1         6  
  0         0  
46             }
47             }
48             }
49 5 100       82 if ($exp) {
50 3 100       10 if (!defined($Value)) {
51 2         4 state $msg = "Attempt to export while constant is not yet defined";
52 2 100       8 if ($strict) {
53 1         256 croak($msg);
54             } else {
55 1         265 carp($msg);
56             }
57             }
58 2         2001 __PACKAGE__->export_to_level(1, $caller, $exp)
59             }
60             }
61              
62              
63              
64             1; # End of Debug::Helper::Flag
65              
66              
67             __END__