File Coverage

blib/lib/Slackware/Slackget/Config.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Slackware::Slackget::Config;
2              
3 2     2   144572 use warnings;
  2         6  
  2         69  
4 2     2   13 use strict;
  2         4  
  2         98  
5              
6             $XML::Simple::PREFERRED_PARSER='XML::Parser';
7 2     2   3299 use XML::Simple;
  0            
  0            
8              
9             =head1 NAME
10              
11             Slackware::Slackget::Config - An interface to the configuration file
12              
13             =head1 VERSION
14              
15             Version 1.0.1
16              
17             =cut
18              
19             our $VERSION = '1.0.1';
20              
21             =head1 SYNOPSIS
22              
23             This class is use to load a configuration file (config.xml) and the servers list file (servers.xml). It only encapsulate the XMLin() method of XML::Simple, there is no accessors or treatment method for this class.
24             There is only a constructor which take only one argument : the name of the configuration file.
25              
26             After loading you can acces to all values of the config file in the same way that with XML::Simple.
27              
28             The only purpose of this class, is to allow other class to check that the config file have been properly loaded.
29              
30             use Slackware::Slackget::Config;
31              
32             my $config = Slackware::Slackget::Config->new('/etc/slack-get/config.xml') or die "cannot load config.xml\n";
33             print "I will use the encoding: $config->{common}->{'file-encoding'}\n";
34             print "slack-getd is configured as: $config->{daemon}->{mode}\n" ;
35              
36             This module needs XML::Simple to work.
37              
38             =cut
39              
40             =head1 CONSTRUCTOR
41              
42             =head2 new
43              
44             The constructor take the config file name as argument.
45              
46             my $config = Slackware::Slackget::Config->new('/etc/slack-get/config.xml') or die "cannot load config.xml\n";
47              
48             =cut
49              
50             sub new
51             {
52             my ($class,$file) = @_ ;
53             return undef unless(-e $file && -r $file);
54             my $self= XMLin($file , ForceArray => ['li']) or return undef;
55             # use Data::Dumper;
56             # print "[Slackware::Slackget::Config]",Dumper($self);
57             return undef unless(defined($self->{common}));
58             if(exists($self->{'plugins'}->{'list'}->{'plug-in'}->{'id'}) && defined($self->{'plugins'}->{'list'}->{'plug-in'}->{'id'}))
59             {
60             my $tmp = $self->{'plugins'}->{'list'}->{'plug-in'};
61             delete($self->{'plugins'}->{'list'}->{'plug-in'});
62             $self->{'plugins'}->{'list'}->{'plug-in'}->{$tmp->{'id'}} = $tmp;
63             delete($self->{'plugins'}->{'list'}->{'plug-in'}->{$tmp->{'id'}}->{'id'});
64             }
65             if($ENV{SG_DAEMON_DEBUG}){
66             require Data::Dumper;
67             print "[Slackware::Slackget::Config]",Data::Dumper::Dumper( $self ),"\n";
68             }
69             bless($self,$class);
70             return $self;
71             }
72              
73             =head2 get_token
74              
75             Return the value associated to the given token.
76              
77             Tokens are requested through a path like syntax. For example, the following XML :
78              
79            
80            
81             value
82            
83            
84              
85             The element's value is accessed throught :
86              
87             print $config->get_token("/item/key"); # the root key is not kept by this class
88              
89              
90             **WARNING** even if it could look like XPath : IT IS NOT !
91              
92             =cut
93              
94              
95             sub get_token {
96             my ($self,$req) = @_ ;
97             my @R = split(/\//,$req);
98             my $token;
99             my $ref = $self;
100             while(@R){
101             $token = shift(@R);
102             next if($token =~ /^\s*$/);
103             $ref = $ref->{$token};
104             }
105             return $ref;
106             }
107              
108             =head2 set_token
109              
110             Following the same syntax as the get_token() method, it allows you to set a configuration token.
111              
112             $config->set_token("/item/key", "new value");
113              
114             The value can be anything fitting a scalar (number, strings, array ref, hash ref, etc.)
115              
116             =cut
117              
118             sub set_token {
119             my ($self,$req,$data) = @_ ;
120             my @R = split(/\//,$req);
121             my $token;
122             my $ref = $self;
123             my $c;
124             while(@R){
125             $token = shift(@R);
126             next if(!defined($token) || $token =~ /^\s*$/);
127             print "$c- $token ",scalar(@R)," ";
128             $c .= " ";
129             if(scalar(@R) >= 1){
130             $ref->{$token} = {} unless( defined($ref->{$token}) );
131             $ref = $ref->{$token} ;
132             print "(not last token)";
133             }else{
134             $ref->{$token} = $data;
135             print "(is the last token)";
136             }
137             print "\n";
138             }
139            
140             }
141              
142              
143             =head1 AUTHOR
144              
145             DUPUIS Arnaud, C<< >>
146              
147             =head1 BUGS
148              
149             Please report any bugs or feature requests to
150             C, or through the web interface at
151             L.
152             I will be notified, and then you'll automatically be notified of progress on
153             your bug as I make changes.
154              
155             =head1 SUPPORT
156              
157             You can find documentation for this module with the perldoc command.
158              
159             perldoc Slackware::Slackget
160              
161              
162             You can also look for information at:
163              
164             =over 4
165              
166             =item * Infinity Perl website
167              
168             L
169              
170             =item * slack-get specific website
171              
172             L
173              
174             =item * RT: CPAN's request tracker
175              
176             L
177              
178             =item * AnnoCPAN: Annotated CPAN documentation
179              
180             L
181              
182             =item * CPAN Ratings
183              
184             L
185              
186             =item * Search CPAN
187              
188             L
189              
190             =back
191              
192             =head1 ACKNOWLEDGEMENTS
193              
194             Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
195              
196             =head1 SEE ALSO
197              
198             =head1 COPYRIGHT & LICENSE
199              
200             Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
201              
202             This program is free software; you can redistribute it and/or modify it
203             under the same terms as Perl itself.
204              
205             =cut
206              
207             1; # End of Slackware::Slackget::Config