blib/lib/Config/Vars.pm

filter: Statementstmt 89.2% 50 / 56 Branchbran 53.3% 16 / 30 Conditioncond 60.0% 9 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 70.8% 85 / 120 CC 21 SCAR 30.1
CC
21
Cov
74%
xform
32.9
import
6.2
BEGIN
1.0
CRAP 20.3
↑ Summary
1 =head1 NAME
2  
3 Config::Vars - A module for keeping configuration variables in a central perl file.
4  
5 =head1 VERSION
6  
7 This documentation describes version 0.01 of Config::Vars.pm, May 23, 2003.
8  
9 =cut
10  
11 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 package Config::Vars;
13 5 ▶use vars qw/$VERSION @CARP_NOT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 $VERSION = 0.01;
15  
16 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Filter::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 @CARP_NOT = qw(Filter::Simple);
19  
20 # Can we use Readonly?
21 5 ▶use vars '$RO_ok';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 my $nowarn; # if true, don't kvetch about Readonly not being available
23 my $export; # Exporter array name
24 5 eval { require 'Readonly.pm' };
25 5 ▶$RO_ok = 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 5 5
26  
27 # These get set by import() at the begining of each module; reset by xform.
28 5 my $usevars;
29 my $isa;
30  
31  
32 sub import
33 16 ▶{
Subroutine import: called CC=6 SCAR=18.3
34 16 my $pkg = shift;
35 16 my @errs;
36  
37 # Module-by-module init
38 16 $nowarn = 0;
39 16 $export = 'EXPORT_OK';
40  
41 16 foreach my $opt (@_)
42 {
43 2 ▶ if ($opt eq 'nowarn')
Branch: 2 branches
BranchTrueFalseTotal
if ($opt eq 'nowarn') { } 1 1 2
elsif ($opt eq 'exportall') { } 1 0 1
44 {
45 1 $nowarn = 1;
46 }
47 elsif ($opt eq 'exportall')
48 {
49 1 $export = 'EXPORT';
50 }
51 else
52 {
53 0 push @errs, $opt;
54 }
55 }
56 16 ▶ my $s = @errs > 1? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
@errs > 1 ? : 0 16 16
57 16 ▶ croak "Unknown Config::Vars option$s (@errs)" if @errs;
Branch: 1 branch
BranchTrueFalseTotal
if @errs 0 16 16
58  
59 16 $usevars = "\@$export \@ISA ";
60 16 $isa = "\n" . q(push @ISA, 'Exporter' unless grep $_ eq 'Exporter', @ISA;);
61 }
62  
63  
64 # Filter the code
65 # Change
66 # var $foo = 'bar';
67 # into
68 # use vars qw($foo);
69 # push @EXPORT_OK, qw($foo); # or, push @EXPORT, qw($foo);
70 # $foo = 'bar';
71 FILTER_ONLY
72 16 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 # print STDERR "BEFORE: [$_]\n";
74 16 s/^ \s* # optional leading whitespace
75 (var|ro) # 'var' or 'ro' keyword
76 \s+ # mandatory whitespace
77 ([^=;\s]+) # a variable name
78 \s* # optional whitespace
79 (\S) # = or semicolon
80 24 /xform($1,$2,$3)/gemx;
81 # print STDERR "AFTER: [$_]\n";
82 5 };
83  
84  
85 sub xform
86 24 ▶{
Subroutine xform: called CC=16 SCAR=34.9
Pod undocumented
87 24 my ($cmd, $var, $eqs) = @_;
88 24 ▶ croak qq{Can't process "$cmd $var $eqs" line} if $cmd ne 'var' && $cmd ne 'ro';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd ne "var" and $cmd ne "ro" 0 24 24
Condition66%2/3
$cmd ne "var" and $cmd ne "ro"
!ll&&!rl&&r
12120
MC/DC0%0/2
$cmd ne "var" and $cmd ne "ro"
$cmd ne "var" $cmd ne "ro"
Truth table66%2/3
A and B
A= $cmd ne "var"B= $cmd ne "ro"
ABresult
0X0
100
111
89 24 ▶ unless ($var =~ /^[\$\@\%][_[:alpha:]]\w+/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($var =~ /^[\$\@\%][_[:alpha:]]\w+/) 0 24 24
90 {
91 0 ▶ croak qq{May not declare individual hash or array elements with $cmd}
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /\[[^\]]\]/ or $var =~ /\{[^\}]+\}/ 0 0 0
Condition0%0/3
$var =~ /\[[^\]]\]/ or $var =~ /\{[^\}]+\}/
l!l&&r!l&&!r
000
MC/DC0%0/2
$var =~ /\[[^\]]\]/ or $var =~ /\{[^\}]+\}/
$var =~ /\[[^\]]\]/ $var =~ /\{[^\}]+\}/
Truth table0%0/3
A or B
A= $var =~ /\[[^\]]\]/B= $var =~ /\{[^\}]+\}/
ABresult
1X1
011
000
92 if $var =~ /\[[^\]]\]/ || $var =~ /\{[^\}]+\}/;
93 0 ▶ croak qq{May not declare globs with $cmd ("$var")} if substr($var,0,1) eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if substr($var, 0, 1) eq "*" 0 0 0
94 0 croak qq{Invalid variable name "$var"};
95 }
96 24 ▶ croak qq{Can't process "$cmd $var" line} if $eqs ne '=' && $eqs ne ';';
Branch: 1 branch
BranchTrueFalseTotal
if $eqs ne "=" and $eqs ne ";" 0 24 24
Condition66%2/3
$eqs ne "=" and $eqs ne ";"
!ll&&!rl&&r
1860
MC/DC0%0/2
$eqs ne "=" and $eqs ne ";"
$eqs ne "=" $eqs ne ";"
Truth table66%2/3
A and B
A= $eqs ne "="B= $eqs ne ";"
ABresult
0X0
100
111
97  
98 24 my $res = <<USE_AND_EXPORT;
99 use vars qw($usevars$var);$isa
100 push \@$export, qw($var);
101 USE_AND_EXPORT
102  
103 24 ▶ if ($cmd eq 'ro' && $RO_ok)
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq 'ro' and $Config::Vars::RO_ok) { } 0 24 24
Condition66%2/3
$cmd eq "ro" and $Config::Vars::RO_ok
!ll&&!rl&&r
12120
MC/DC0%0/2
$cmd eq "ro" and $Config::Vars::RO_ok
$cmd eq "ro" $Config::Vars::RO_ok
Truth table66%2/3
A and B
A= $cmd eq "ro"B= $Config::Vars::RO_ok
ABresult
0X0
100
111
104 {
105 0 $res .= "Readonly::Readonly \\$var";
106 0 ▶ $res .= ' => ' if $eqs eq '=';
Branch: 1 branch
BranchTrueFalseTotal
if $eqs eq "=" 0 0 0
107 }
108 else
109 {
110 24 ▶ carp "Readonly not available, making $var read/write" if $cmd eq 'ro' && !$nowarn;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "ro" and not $nowarn 9 15 24
Condition100%3/3
$cmd eq "ro" and not $nowarn
!ll&&!rl&&r
1239
MC/DC100%2/2
$cmd eq "ro" and not $nowarn
$cmd eq "ro" not $nowarn
Truth table100%3/3
A and not(B)
A= $cmd eq "ro"B= not $nowarn
ABresult
0X0
100
111
111 24 $res .= $var;
112 24 ▶ $res .= ' = ' if $eqs eq '=';
Branch: 1 branch
BranchTrueFalseTotal
if $eqs eq "=" 18 6 24
113 }
114 24 ▶ $res .= $eqs if $eqs eq ';';
Branch: 1 branch
BranchTrueFalseTotal
if $eqs eq ";" 6 18 24
115 24 $usevars = $isa = '';
116 24 return $res;
117 }
118  
119  
120 5 1; # Modules must return true, for silly historical reasons.
121 __END__
↑ Summary