File Coverage

blib/lib/Config/File/Simple.pm
Criterion Covered Total %
statement 92 93 98.9
branch 32 54 59.2
condition n/a
subroutine 14 14 100.0
pod 2 9 22.2
total 140 170 82.3


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2             ##############################
3             # File: Simple.pm
4             # Copyright (C) by Kai Wilker
5             # $Id: Simple.pm,v 1.7 2008/02/16 18:49:25 foo Exp foo $
6             ##############################
7              
8             # This program is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation, either version 3 of the License, or
11             # (at your option) any later version.
12             #
13             # This program is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see .
20              
21              
22             package Config::File::Simple;
23              
24 1     1   1074 use 5.008;
  1         4  
  1         44  
25 1     1   6 use strict;
  1         2  
  1         72  
26 1     1   16 use warnings;
  1         2  
  1         46  
27 1     1   5 use Carp qw/ croak /;
  1         2  
  1         78  
28 1     1   1263 use Tie::File;
  1         24731  
  1         1440  
29              
30             our $VERSION = '1.00';
31              
32             sub new {
33 1     1 0 456 my ($class, $file) = @_;
34 1 50       7 croak "Need a Configuration file! Usage: my \$object = new Config::File::Simple(\$config:file);" if !defined $file;
35 1         4 my $self = bless { file => $file }, $class;
36 1         3 return $self;
37             }
38              
39             sub read {
40 5 100   5 0 15 if (@_ > 2) { # If there are more than one variables given use multiple_read instead of read
41 1         2 my $self = shift @_;
42 1         4 my %values = $self->multiple_read(@_);
43 1         7 return %values;
44             }
45              
46 4         9 my ($self, $variable) = @_;
47 4         5 my $value = 0; # This value will be returned
48              
49             # Tests: Is a variable given? Has the variable no special characters? Does the config file exist?
50 4 50       10 croak "No variable is given! Usage: \$self->read(\$variable)" if !defined $variable;
51 4 50       11 croak "The variable '$variable' has got special characters!" if $self->has_special_characters($variable);
52 4 50       70 croak "The configuration file '$self->{'file'}' doesn't exist!" if ! -e $self->{'file'};
53              
54 4 50       130 open my $CONFIG, "<", $self->{'file'} or croak "Can't open file '$self->{'file'}': $!";
55 4         66 while(my $line = <$CONFIG>) { # Now we parse the config file and search for the variable
56 15         20 chomp $line;
57 15         19 $line =~ s/ [^\\] \# .* //xms; # We don't need the comments
58 15         29 $line =~ s/ ^ \s+ //xms; # Delete all space at the beginnig
59 15         31 $line =~ s/ \s+ $//xms; # Delete all space at the end
60 15 100       111 next if $line !~ / ^ $variable \s* = /xms; # Is this the right variable?
61 3         11 $value = (split m/=/, $line)[1]; # We need the value
62 3         8 $value =~ s/ ^ \s+ //xms; # Delete all space at the beginnig of the value
63 3         8 $value =~ s/ \s+ $ //xms; # Delete all space at the end of the value
64 3         16 $value =~ s/\\#/#/g; # Unescape the escaped hashs
65             }
66            
67 4 50       53 close $CONFIG or croak "Can't close file '$self->{'file'}': $!";
68 4         25 return $value;
69             }
70              
71             sub multiple_read {
72 3     3 0 246 my ($self, @variables) = @_;
73 3         4 my %values; # This hash with the variables and values will be returned
74              
75             # Tests: Have the variables no special characters? Does the config file exist?
76 3         6 foreach my $variable (@variables) { # Check all variables for special characters
77 5 50       43 croak "The variable '$variable' has got special characters!" if $self->has_special_characters($variable);
78             }
79 3 50       43 croak "The configuration file '$self->{'file'}' doesn't exist!" if ! -e $self->{'file'};
80              
81 3 50       91 open my $CONFIG, "<", $self->{'file'} or croak "Can't open file '$self->{'file'}': $!";
82 3         32 while(my $line = <$CONFIG>) {
83 8         10 chomp $line;
84 8         10 $line =~ s/ [^\\] \# .* //xms; # Delete all comments
85 8         14 $line =~ s/ ^ \s+ //xms; # Delete all space at the beginnig
86 8         18 $line =~ s/ \s+ $//xms; # Delete all space at the end
87              
88 8         18 foreach my $variable (@variables) {
89 14 100       1022 next if $line !~ / ^ $variable \s* = /xms; # Is this the right variable?
90 5         8 my $value; # This value will be added to the hash: $values{$variable} = $value
91 5         12 $value = (split m/=/, $line)[1]; # We need the value, not the variable
92 5         16 $value =~ s/ ^ \s+ //xms; # Delete all space at the beginnig of the value
93 5         7 $value =~ s/ \s+ $ //xms; # Delete all space at the end of the value
94 5         8 $value =~ s/\\#/#/g; # Unescape the escaped hashs
95 5         25 $values{$variable} = $value;
96             }
97             }
98              
99 3 50       34 close $CONFIG or croak "Can't close file '$self->{'file'}': $!";
100 3         28 return %values;
101             }
102              
103             sub variable_exists {
104 3     3 0 5 my $self = shift @_;
105 3 100       76 return 0 if ! -e $self->{'file'}; # If the config file doesn't exist, the variable doesn't exist, too.
106 2         7 return $self->read(@_); # read() will return 0 if the variable doesn't exist, otherwise it'll return the value of the variable
107             }
108              
109             sub has_special_characters {
110 16     16 0 707 my ($self, $word) = @_;
111 16         99 return ( $word !~ /^ \w+ $/xms );
112             }
113              
114             sub set { # A wrapper for methods add() and change()
115 2     2 1 2633 my ($self, $variable, $value) = @_;
116              
117 2 50       8 croak "The variable '$variable' has got special charaters!"
118             if $self->has_special_characters($variable);
119 2         6 $value =~ s/#/\\#/g; # Escape the hashs in the value: # -> \#
120            
121             # If the variable exists change the value, otherwise add a new variable + value
122 2 50       8 if($self->variable_exists($variable)) {
123 0         0 $self->change($variable, $value);
124             } else {
125 2         7 $self->add($variable, $value);
126             }
127             }
128              
129             sub add {
130 3     3 0 8 my ($self, $variable, $value) = @_;
131              
132 3 50       10 croak "The variable '$variable' has got special charaters!"
133             if $self->has_special_characters($variable);
134              
135             # Add a new line at the end of the config file
136             # variable = value
137 3 50       202 open my $CONFIG, ">>", $self->{'file'} or croak "Can't open file $self->{'file'}: $!";
138 3 50       5 print {$CONFIG} "$variable = $value\n"
  3         24  
139             or croak "Can't write at file '$self->{'file'}': $!";
140 3 50       137 close $CONFIG or croak "Can't close file $self->{'file'}: $!";
141             }
142              
143             sub change { # Changes the value of a variable
144 1     1 0 3 my ($self, $variable, $value) = @_;
145              
146 1 50       4 croak "The variable '$variable' has got special charaters!"
147             if $self->has_special_characters($variable);
148              
149 1         3 my @config; # The content of the config file will be in this array
150 1 50       11 tie @config, 'Tie::File', $self->{'file'}
151             or croak "Can't tie file '$self->{'file'}': $!";
152             # Search for the variable and change it's value
153 1         212 foreach my $line (@config) {
154 2 100       400 if($line =~ / ^ ( \s* $variable \s* = \s* ) /xms) {
155 1         137 $line = "$1$value\n";
156             }
157             }
158 1 50       81 untie @config or croak "Can't untie file '$self->{'file'}': $!";
159             }
160              
161             sub add_comment { # Adds a comment at the end of the file
162 2     2 1 236 my $self = shift @_;
163              
164 2 50       83 open my $CONFIG, ">>", $self->{'file'} or croak "Can't open file '$self->{'file'}': $!";
165 2         6 foreach my $text (@_) {
166 3 50       4 print {$CONFIG} "# $text\n" or croak "Can't write at file '$self->{'file'}': $!";
  3         22  
167             }
168 2 50       73 close $CONFIG or croak "Can't close file '$self->{'file'}': $!";
169             }
170              
171             1;
172              
173             __END__