File Coverage

blib/lib/Config/Setting/Chunk.pm
Criterion Covered Total %
statement 57 58 98.2
branch 15 20 75.0
condition 5 11 45.4
subroutine 12 12 100.0
pod 9 9 100.0
total 98 110 89.0


line stmt bran cond sub pod time code
1             # Copyright (C) 2004 by Dominic Mitchell. All rights reserved.
2             #
3             # Redistribution and use in source and binary forms, with or without
4             # modification, are permitted provided that the following conditions
5             # are met:
6             # 1. Redistributions of source code must retain the above copyright
7             # notice, this list of conditions and the following disclaimer.
8             # 2. Redistributions in binary form must reproduce the above copyright
9             # notice, this list of conditions and the following disclaimer in the
10             # documentation and/or other materials provided with the distribution.
11             #
12             # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13             # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15             # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
16             # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17             # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18             # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19             # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20             # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21             # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22             # SUCH DAMAGE.
23              
24             =pod
25              
26             =head1 NAME
27              
28             Config::Setting::Chunk - Representation of a configuration file
29              
30             =head1 SYNOPSIS
31              
32             use Config::Setting::Chunk;
33              
34             my $chunk = Config::Setting::Chunk->new;
35             $chunk->add_section( "login" );
36             $chunk->set_item( "login", "username", "fred" );
37              
38             my @sections = $chunk->sections;
39             my $username = $chunk->get_item( "login", "username" );
40              
41             =head1 DESCRIPTION
42              
43             This class is a representation of a configuration file. A chunk
44             consists of zero or more I, each of which consists of zero or
45             more I.
46              
47             =head1 METHODS
48              
49             =over 4
50              
51             =item new ( )
52              
53             Class Method. Constructor.
54              
55             =item add_section ( SECTION )
56              
57             Create a new section named SECTION. Has no effect if SECTION is already
58             present in this chunk.
59              
60             =item sections ( )
61              
62             Return a list of all sections in this chunk, in the order in which they
63             were added.
64              
65             =item has_section ( SECTION )
66              
67             Returns true or false if SECTION is present or not in this chunk.
68              
69             =item section_keys ( SECTION )
70              
71             Returns a list of all keys present in SECTION.
72              
73             =item set_item ( SECTION, KEY, VALUE )
74              
75             Set the item KEY to have VALUE in SECTION. if SECTION does not exist,
76             it will be created.
77              
78             =item get_item ( SECTION, KEY )
79              
80             Return the value of KEY in SECTION. Returns undef if KEY or SECTION
81             does not exist.
82              
83             =item get ( KEY )
84              
85             Return the value of KEY in the first section which contains it, or undef
86             if no section contains it.
87              
88             =item to_string ( )
89              
90             Returns the chunk in windows .INI style format. This may be useful for
91             debugging.
92              
93             =back
94              
95             =head1 AUTHOR
96              
97             Dominic Mitchell, Ecpan (at) happygiraffe.netE
98              
99             =head1 SEE ALSO
100              
101             L.
102              
103             =cut
104              
105             package Config::Setting::Chunk;
106              
107 4     4   1017 use strict;
  4         16  
  4         332  
108 4     4   21 use vars qw($VERSION $rcsid);
  4         7  
  4         184  
109              
110 4     4   20 use Carp;
  4         7  
  4         9947  
111              
112             $VERSION = ( qw( $Revision: 765 $ ) )[1];
113             $rcsid = '@(#) $Id: Chunk.pm 765 2005-08-31 20:05:59Z dom $ ';
114              
115             sub new {
116 10     10 1 5187 my $class = shift;
117 10         49 return bless {}, $class;
118             }
119              
120             sub add_section {
121 31     31 1 52 my $self = shift;
122 31         42 my ( $sect ) = @_;
123 31 50       73 croak "usage: add_section(sect)" unless $sect;
124 31 100       100 unless ( exists $self->{ Sections }{ $sect } ) {
125 14         42 $self->{ Sections }{ $sect } = {};
126 14   100     55 $self->{ SectionOrder } ||= [];
127 14         62 push @{ $self->{ SectionOrder } }, $sect;
  14         36  
128             }
129 31         70 return;
130             }
131              
132             sub sections {
133 15     15 1 27 my $self = shift;
134 15 100       20 return @{ $self->{ SectionOrder } || [] };
  15         96  
135             }
136              
137             sub has_section {
138 31     31 1 40 my $self = shift;
139 31         43 my ( $sect ) = @_;
140 31 50       64 croak "usage: has_section(sect)" unless $sect;
141 31         134 return exists $self->{ Sections }{ $sect };
142             }
143              
144             sub section_keys {
145 9     9 1 37 my $self = shift;
146 9         18 my ( $sect ) = @_;
147 9 50       39 croak "usage: section_keys(sect)"
148             unless $sect;
149 9 100       14 return sort keys %{ $self->{Sections}{$sect} || {} };
  9         95  
150             }
151              
152             sub set_item {
153 22     22 1 53 my $self = shift;
154 22         45 my ( $sect, $key, $val ) = @_;
155 22 50 33     157 croak "usage: set_item(sect,key,val)"
      33        
156             unless $sect && $key && $val;
157 22         47 $self->add_section( $sect );
158 22         60 $self->{ Sections }{ $sect }{ $key } = $val;
159 22         71 return;
160             }
161              
162             sub get_item {
163 27     27 1 99 my $self = shift;
164 27         45 my ( $sect, $key ) = @_;
165 27 50 33     145 croak "usage: get_item(sect,key)"
166             unless $sect && $key;
167             # Avoid autovivification.
168 27 100       59 return unless $self->has_section( $sect );
169 25         127 return $self->{ Sections }{ $sect }{ $key };
170             }
171              
172             sub get {
173 2     2 1 8 my $self = shift;
174 2         4 my ( $key ) = @_;
175 2         7 foreach my $sect ( $self->sections ) {
176 3         11 my $val = $self->get_item( $sect, $key );
177 3 100       18 return $val if defined $val;
178             }
179 0         0 return;
180             }
181              
182             sub to_string {
183 3     3 1 7 my $self = shift;
184 3         5 my $str = '';
185 3         7 foreach my $sect ( $self->sections ) {
186 2         5 $str .= "[$sect]\n";
187 2         6 foreach my $key ( $self->section_keys( $sect ) ) {
188 1         5 $str .= $key . '=' . $self->get_item( $sect, $key );
189 1         4 $str .= "\n";
190             }
191 2         5 $str .= "\n";
192             }
193 3         17 return $str;
194             }
195              
196             1;
197             __END__