blib/lib/Tie/Config.pm

filter: Statementstmt 95.2% 81 / 85 Branchbran 69.0% 29 / 42 Conditioncond 63.6% 7 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 84.0% 132 / 157 CC 26 SCAR 20.0
CC
26
Cov
85%
TIEHASH
12.6
DESTROY
8.6
STORE
5.8
CRAP 7.4
↑ Summary
1 ##############################################################################
2 #
3 # Copyright (c) 2001-2002 Jan 'Kozo' Vajda <Jan.Vajda@pobox.sk>
4 # All rights reserved.
5 #
6 ##############################################################################
7  
8 package Tie::Config;
9  
10 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 @ISA = qw(Tie::StdHash);
21  
22 6 $VERSION = '0.04';
23  
24 # Items to export into callers namespace by default
25 6 @EXPORT = qw();
26  
27 # Other items we are prepared to export if requested
28 6 @EXPORT_OK = qw();
29  
30 =head1 NAME
31  
32 Tie::Config - class definitions for tied hashes config file reading
33  
34 =head1 SYNOPSIS
35  
36 use Tie::Config;
37
38 tie %hash, 'Tie::Config', ".foo_rc", O_RDWR;
39
40
41 =head1 DESCRIPTION
42  
43 Tied config file reader
44  
45 =head1 USE
46  
47 use Tie::Config;
48
49 tie %hash, 'Tie::Config', ".foo_rc", O_RDWR;
50
51 print $hash{'key'};
52 $hash{'key'} = "newvalue";
53
54 untie %hash;
55  
56 Comments is handled internal and is wroted at the top of the file.
57 If ommited access mode default is O_RDONLY ( read only access ).
58 Currently supported mode is only O_RDONLY and O_RDWR.
59  
60 If config file is changed between tie and untie by other proces, any changes
61 will be lost.
62
63 =cut
64  
65 6 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=12 SCAR=25.3
66 6 my $class = shift;
67 6 my $file = shift;
68 6 ▶ my $access = shift || O_RDONLY ;
Condition100%2/2
shift() || 0
l!l
24
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
69  
70 6 my $hash = {};
71 6 bless $hash, $class;
72  
73 6 $hash->{_internal_filename} = $file;
74 6 $hash->{_internal_accessmode} = $access;
75
76 6 ▶ carp("File ".$hash->{_internal_filename}." does not exist.") unless ( -f $hash->{_internal_filename});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $hash->{"_internal_filename"} 0 6 6
77  
78 6 ▶ my $rc = IO::File->new($hash->{_internal_filename}, O_RDONLY) if ( -f $hash->{_internal_filename} );
Branch: 1 branch
BranchTrueFalseTotal
if -f $hash->{"_internal_filename"} 6 0 6
79 6 ▶ if ( defined $rc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc) { } 6 0 6
80  
81 6 my $separator = '\s*=\s*';
82
83 ### pre istotu vymazem komentare
84 6 $hash->{_internal_comments} = '';
85  
86 6 ▶ while (<$rc>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $rc)) 48 6 54
87 48 chomp;
88 #### Skip blank text entry fields
89 48 ▶ next if ( /^\s*$/o );
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/o 4 44 48
90 ### get comments
91 44 ▶ if ( /^\s*#/o || /^\s*\;/o) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*#/o or /^\s*\;/o) 10 34 44
Condition100%3/3
/^\s*#/o or /^\s*\;/o
l!l&&r!l&&!r
5534
MC/DC100%2/2
/^\s*#/o or /^\s*\;/o
/^\s*#/o /^\s*\;/o
Truth table100%3/3
A or B
A= /^\s*#/oB= /^\s*\;/o
ABresult
1X1
011
000
92 ### pridam do pola komentarov
93 10 $hash->{_internal_comments} .= $_ . "\n";
94 10 next;
95 }
96 ### Skip unless contain separator
97 34 ▶ next unless ( /${separator}/o );
Branch: 1 branch
BranchTrueFalseTotal
unless /$separator/o 0 34 34
98  
99 34 my ($key,$value) = /\s*(.*?)${separator}(.*?)\s*$/o;
100 34 my $length = length($value);
101  
102 ### skip empty keys
103 34 ▶ next if ( !$length || !$key );
Branch: 1 branch
BranchTrueFalseTotal
unless $length and $key 0 34 34
Condition33%1/3
$length and $key
!ll&&!rl&&r
0034
MC/DC0%0/2
$length and $key
$length $key
Truth table33%1/3
A and B
A= $lengthB= $key
ABresult
0X0
100
111
104
105 34 $hash->{$key} = $value;
106 }
107  
108 6 $rc->close;
109  
110 } else {
111 0 carp("Canot open file ".$hash->{_internal_filename});
112 }
113  
114 6 $hash;
115 }
116  
117 3 ▶sub STORE {
Subroutine STORE: called CC=5 SCAR=17.6
118 3 my ($self, $key, $val) = @_;
119  
120 # print STDERR "$self, $key, $val\n";
121
122 3 ▶ if ( $key =~ /^_internal/o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^_internal/o) 0 3 3
123 0 carp "invalid key [$key] in hash";
124 0 return;
125 }
126  
127 3 ▶ if ( !$self->{_internal_accessmode} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_internal_accessmode"}) 1 2 3
128 1 carp "hash is read only";
129 1 return;
130 }
131
132 2 ▶ return($val) if ( defined $self->{$key} && $self->{$key} eq $val);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$key} and $self->{$key} eq $val 0 2 2
Condition33%1/3
defined $self->{$key} and $self->{$key} eq $val
!ll&&!rl&&r
200
MC/DC0%0/2
defined $self->{$key} and $self->{$key} eq $val
defined $self->{$key} $self->{$key} eq $val
Truth table33%1/3
A and B
A= defined $self->{$key}B= $self->{$key} eq $val
ABresult
0X0
100
111
133 2 $self->{_internal_changed} = '1';
134
135 # print STDERR "hash content changed\n";
136
137 2 $self->{$key} = $val;
138 }
139  
140 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=8 SCAR=21.6
141 1 my $self = shift;
142 1 my ($key,$value);
143
144 # print STDERR "Destroyed\n";
145 # print STDERR Data::Dumper->Dump([$self],[qw(*destroyed)]);
146  
147 ### is read only
148 1 ▶ return() unless ($self->{_internal_accessmode});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_internal_accessmode"} 0 1 1
149  
150 ### is changed
151 1 ▶ return() unless ($self->{_internal_changed});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_internal_changed"} 0 1 1
152
153 # print STDERR "untied\n";
154  
155 1 my $rc = new IO::File $self->{_internal_filename}, O_CREAT|O_WRONLY|O_TRUNC;
156 1 ▶ if ( defined $rc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc) { } 1 0 1
157
158 ### zapiseme komentare ak existuju
159 1 ▶ print $rc $self->{_internal_comments} if $self->{_internal_comments};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_internal_comments"} 0 1 1
160
161 1 my $separator = ' = ';
162  
163 1 ▶ while (($key,$value) = each %{$self}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self;}) 4 1 5
164 4 ▶ print $rc "$key${separator}$value\n" unless ($key =~ /^_internal/o);
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^_internal/o 1 3 4
165 }
166 1 $rc->close;
167 1 ▶ carp "Can't close file ".$self->{_internal_filename} .": $1" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 1 1
168 } else {
169 0 carp "Can't open ".$self->{_internal_filename};
170 }
171 }
172  
173 1 ▶sub CLEAR {
Subroutine CLEAR: called CC=4 SCAR=13.9
174 1 my $self = shift;
175 1 my ($key,$value);
176  
177 # print STDERR "CLEAR !!\n";
178  
179 1 ▶ while (($key,$value) = each %{$self}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self;}) 10 1 11
180 10 ▶ delete $self->{$key} unless ($key =~ /^_internal/o);
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^_internal/o 7 3 10
181 }
182 1 ▶ delete $self->{_internal_comments} if $self->{_internal_comments};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_internal_comments"} 1 0 1
183 }
184  
185 #sub AUTOLOAD {
186 # my $self = shift;
187 # my $value = shift;
188 # my ($name) = $AUTOLOAD;
189 #
190 # ($name) = ( $name =~ /^.*::(.*)/);
191 #
192 # $self->{$name} = $value if ( defined $value );
193 #
194 # return($self->{$name});
195 #
196 #}
197  
198 ### set True
199 6 3.14;
200  
201 __END__
↑ Summary