File Coverage

blib/lib/App/PerlShell/Config.pm
Criterion Covered Total %
statement 66 84 78.5
branch 31 50 62.0
condition 1 9 11.1
subroutine 9 10 90.0
pod 6 6 100.0
total 113 159 71.0


line stmt bran cond sub pod time code
1             package App::PerlShell::Config;
2              
3             ########################################################
4             # AUTHOR = Michael Vincent
5             # www.VinsWorld.com
6             ########################################################
7              
8 2     2   142687 use strict;
  2         11  
  2         61  
9 2     2   10 use warnings;
  2         4  
  2         46  
10 2     2   10 use Carp;
  2         3  
  2         126  
11              
12 2     2   13 use Exporter;
  2         3  
  2         1760  
13              
14             our %EXPORT_TAGS = ( 'config_where' => [qw( config_where )], );
15             our @EXPORT_OK = ( @{$EXPORT_TAGS{'config_where'}} );
16              
17             our @ISA = qw( Exporter );
18              
19             sub new {
20 1     1 1 87 my $self = shift;
21 1   33     8 my $class = ref($self) || $self;
22              
23 1 50       6 if ( ( @_ % 2 ) == 1 ) {
24 0         0 croak("Insufficient number of args - @_");
25             } else {
26 1         5 my %cfg = @_;
27 1         4 return bless \%cfg, $class;
28             }
29             }
30              
31             sub config {
32 11     11 1 1944 my $self = shift;
33 11         23 my ( $key, $value ) = @_;
34              
35 11         20 my $retType = wantarray;
36 11 100       26 if ( defined $key ) {
37 10 100       24 if ( exists $self->{$key} ) {
38              
39             # Set
40 7 100       15 if ( defined $value ) {
41 2         5 my $prev = $self->{$key};
42 2 50       6 if ( $value eq '' ) {
43 0         0 undef $self->{$key};
44             } else {
45 2         5 $self->{$key} = $value;
46             }
47 2 50       11 return ( defined $prev ) ? $prev : undef;
48              
49             # Get
50             } else {
51 5 50       14 if ( !defined $retType ) {
52             printf "%-15s => %s\n", $key,
53 0 0       0 defined( $self->{$key} ) ? $self->{$key} : 'undef';
54             }
55 5         22 return $self->{$key};
56             }
57             } else {
58 3 50       10 if ( !defined $retType ) {
59 0         0 carp("Key not valid - `$key'");
60             }
61 3         11 return undef;
62             }
63              
64             # Get all
65             } else {
66 1 50       5 if ( !defined($retType) ) {
    50          
67 0         0 for ( sort( keys( %{$self} ) ) ) {
  0         0  
68             printf "%-15s => %s\n", $_,
69 0 0       0 defined( $self->{$_} ) ? $self->{$_} : 'undef';
70             }
71             } elsif ($retType) {
72 0         0 return %{$self};
  0         0  
73             } else {
74 1         10 my %ret = %{$self};
  1         9  
75 1         4 return \%ret;
76             }
77             }
78             }
79              
80             sub add {
81 5     5 1 618 my $self = shift;
82 5         11 my ( $key, $value ) = @_;
83              
84 5         11 my $retType = wantarray;
85 5 100       11 if ( defined $key ) {
86 4 100       10 if ( !exists $self->{$key} ) {
87 2 100       6 if ( defined $value ) {
88 1         3 $self->{$key} = $value;
89             } else {
90 1         3 $self->{$key} = undef;
91             }
92 2         8 return 1;
93             } else {
94 2 100       8 if ( !defined $retType ) {
95 1         78 carp("Key exists");
96             }
97             }
98             } else {
99 1         83 carp("Key required");
100             }
101 3         79 return 0;
102             }
103              
104             sub delete {
105 4     4 1 603 my $self = shift;
106 4         9 my ($key) = @_;
107              
108 4         7 my $retType = wantarray;
109 4 100       9 if ( defined $key ) {
110 3 100       8 if ( exists $self->{$key} ) {
111 1         3 delete $self->{$key};
112 1         7 return 1;
113             } else {
114 2 100       7 if ( !defined $retType ) {
115 1         83 carp("Key doesn't exist");
116             }
117             }
118             } else {
119 1         84 carp("Key required");
120             }
121 3         69 return 0;
122             }
123              
124             sub exists {
125 3     3 1 298 my $self = shift;
126 3         7 my ($key) = @_;
127              
128 3 100       8 if ( defined $key ) {
129 2 100       7 if ( exists $self->{$key} ) {
130 1         5 return 1;
131             }
132             } else {
133 1         184 carp("Key required");
134             }
135 2         53 return 0;
136             }
137              
138             sub config_where {
139 0     0 1   my ( $config_file, $dir ) = @_;
140              
141 0           my $home_dir = $ENV{HOME};
142 0 0 0       if ( ( $^O eq 'MSWin32' ) and !( defined $home_dir ) ) {
143 0           $home_dir = $ENV{USERPROFILE};
144             }
145              
146             # 1 current working directory
147 0 0 0       if ( -e $config_file ) {
    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 ) ) {
156 0           return $dir . "/" . $config_file;
157              
158             # not found - indicate so in config
159             } else {
160 0           return;
161             }
162             }
163              
164             1;
165              
166             __END__