lib/App/PerlShell/Config.pm

filter: Statementstmt 79.7% 71 / 89 Branchbran 62.0% 31 / 50 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 90.0% 9 / 10 Podpod 100% 6 / 6 totaltotal 69.4% 118 / 170 CC 30 SCAR 29.4
CC
30
Cov
70%
config_where
56.0
config
21.9
add
5.0
CRAP 19.0
« lib/App/PerlShell/Plugin/Macros.pm ↑ Summary lib/App/PerlShell/ModRefresh.pm »
1 package App::PerlShell::Config;
2  
3 ########################################################
4 # AUTHOR = Michael Vincent
5 # www.VinsWorld.com
6 ########################################################
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our %EXPORT_TAGS = ( 'config_where' => [qw( config_where )], );
15 3 our @EXPORT_OK = ( @{$EXPORT_TAGS{'config_where'}} );
16  
17 3 our @ISA = qw( Exporter );
18  
19 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod documented
20 1 my $self = shift;
21 1 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
22  
23 1 ▶ if ( ( @_ % 2 ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 1) { } 0 1 1
24 0 croak("Insufficient number of args - @_");
25 } else {
26 1 my %cfg = @_;
27 1 return bless \%cfg, $class;
28 }
29 }
30  
31 11 ▶sub config {
Subroutine config: called CC=13 SCAR=30.9
Pod documented
32 11 my $self = shift;
33 11 my ( $key, $value ) = @_;
34  
35 11 my $retType = wantarray;
36 11 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 10 1 11
37 10 ▶ if ( exists $self->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$key}) { } 7 3 10
38  
39 # Set
40 7 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 2 5 7
41 2 my $prev = $self->{$key};
42 2 ▶ if ( $value eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '') { } 0 2 2
43 0 undef $self->{$key};
44 } else {
45 2 $self->{$key} = $value;
46 }
47 2 ▶ return ( defined $prev ) ? $prev : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $prev ? : 2 0 2
48  
49 # Get
50 } else {
51 5 ▶ if ( !defined $retType ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retType) 0 5 5
52 printf "%-15s => %s\n", $key,
53 0 ▶ defined( $self->{$key} ) ? $self->{$key} : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$key} ? : 0 0 0
54 }
55 5 return $self->{$key};
56 }
57 } else {
58 3 ▶ if ( !defined $retType ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retType) 0 3 3
59 0 carp("Key not valid - `$key'");
60 }
61 3 return undef;
62 }
63  
64 # Get all
65 } else {
66 1 ▶ if ( !defined($retType) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $retType) { } 0 1 1
elsif ($retType) { } 0 1 1
67 0 for ( sort( keys( %{$self} ) ) ) {
68 printf "%-15s => %s\n", $_,
69 0 ▶ defined( $self->{$_} ) ? $self->{$_} : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$_} ? : 0 0 0
70 }
71 } elsif ($retType) {
72 0 return %{$self};
73 } else {
74 1 my %ret = %{$self};
75 1 return \%ret;
76 }
77 }
78 }
79  
80 5 ▶sub add {
Subroutine add: called CC=5 SCAR=16.1
Pod documented
81 5 my $self = shift;
82 5 my ( $key, $value ) = @_;
83  
84 5 my $retType = wantarray;
85 5 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 4 1 5
86 4 ▶ if ( !exists $self->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{$key}) { } 2 2 4
87 2 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 1 1 2
88 1 $self->{$key} = $value;
89 } else {
90 1 $self->{$key} = undef;
91 }
92 2 return 1;
93 } else {
94 2 ▶ if ( !defined $retType ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retType) 1 1 2
95 1 carp("Key exists");
96 }
97 }
98 } else {
99 1 carp("Key required");
100 }
101 3 return 0;
102 }
103  
104 4 ▶sub delete {
Subroutine delete: called CC=4 SCAR=13.9
Pod documented
105 4 my $self = shift;
106 4 my ($key) = @_;
107  
108 4 my $retType = wantarray;
109 4 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 3 1 4
110 3 ▶ if ( exists $self->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$key}) { } 1 2 3
111 1 delete $self->{$key};
112 1 return 1;
113 } else {
114 2 ▶ if ( !defined $retType ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retType) 1 1 2
115 1 carp("Key doesn't exist");
116 }
117 }
118 } else {
119 1 carp("Key required");
120 }
121 3 return 0;
122 }
123  
124 3 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.0
Pod documented
125 3 my $self = shift;
126 3 my ($key) = @_;
127  
128 3 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 2 1 3
129 2 ▶ if ( exists $self->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$key}) 1 1 2
130 1 return 1;
131 }
132 } else {
133 1 carp("Key required");
134 }
135 2 return 0;
136 }
137  
138 0 ▶sub config_where {
Subroutine config_where: not called CC=7 SCAR=40.3
Pod documented
139 0 my ( $config_file, $dir ) = @_;
140  
141 0 my $home_dir = $ENV{HOME};
142 0 ▶ if ( ( $^O eq 'MSWin32' ) and !( defined $home_dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" and not defined $home_dir) 0 0 0
Condition0%0/3
$^O eq "MSWin32" and not defined $home_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "MSWin32" and not defined $home_dir
$^O eq "MSWin32" not defined $home_dir
Truth table0%0/3
A and not(B)
A= $^O eq "MSWin32"B= not defined $home_dir
ABresult
0X0
100
111
143 0 $home_dir = $ENV{USERPROFILE};
144 }
145  
146 # 1 current working directory
147 0 ▶ if ( -e $config_file ) {
Branch: 3 branches
BranchTrueFalseTotal
if (-e $config_file) { } 0 0 0
elsif (-e $home_dir . '/' . $config_file) { } 0 0 0
elsif (defined $dir and -e $dir . '/' . $config_file) { } 0 0 0
148 0 return $config_file
149  
150 # 2 user home directory
151 } elsif ( -e $home_dir . "/" . $config_file ) {
152 0 return $home_dir . "/" . $config_file;
153  
154 # 3 provided directory
155 ▶ } elsif ( ( defined $dir ) and ( -e $dir . "/" . $config_file ) ) {
Condition0%0/3
defined $dir and -e $dir . '/' . $config_file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dir and -e $dir . '/' . $config_file
defined $dir -e $dir . '/' . $config_file
Truth table0%0/3
A and B
A= defined $dirB= -e $dir . '/' . $config_file
ABresult
0X0
100
111
156 0 return $dir . "/" . $config_file;
157  
158 # not found - indicate so in config
159 } else {
160 0 return;
161 }
162 }
163  
164 3 1;
165  
166 __END__
« lib/App/PerlShell/Plugin/Macros.pm ↑ Summary lib/App/PerlShell/ModRefresh.pm »