File Coverage

blib/lib/Slackware/Slackget/Local.pm
Criterion Covered Total %
statement 6 48 12.5
branch 0 12 0.0
condition 0 6 0.0
subroutine 2 11 18.1
pod n/a
total 8 77 10.3


line stmt bran cond sub pod time code
1             package Slackware::Slackget::Local;
2              
3 1     1   1194 use warnings;
  1         2  
  1         35  
4 1     1   6 use strict;
  1         3  
  1         924  
5              
6             require Slackware::Slackget::File ;
7             require XML::Simple;
8             $XML::Simple::PREFERRED_PARSER='XML::Parser' ;
9              
10             =head1 NAME
11              
12             Slackware::Slackget::Local - A class to load the locales
13              
14             =head1 VERSION
15              
16             Version 1.0.0
17              
18             =cut
19              
20             our $VERSION = '1.0.0';
21              
22             =head1 SYNOPSIS
23              
24             This class' purpose is to load and export the local.
25              
26             use Slackware::Slackget::Local;
27              
28             my $local = Slackware::Slackget::Local->new();
29             $local->load('/usr/local/share/slack-get/local/french.xml');
30             print $local->get('__SETTINGS') ;
31              
32             =cut
33              
34             sub new
35             {
36 0     0     my ($class,$file) = @_ ;
37 0           my $self={};
38 0           bless($self,$class);
39 0 0 0       if(defined($file) && -e $file)
40             {
41 0           $self->Load($file);
42             }
43 0           return $self;
44             }
45              
46             =head1 CONSTRUCTOR
47              
48             =head2 new
49              
50             Can take an argument : the LC_MESSAGES file. In this case the constructor automatically call the Load() method.
51              
52             my $local = new Slackware::Slackget::Local();
53             or
54             my $local = new Slackware::Slackget::Local('/usr/local/share/slack-get/local/french.xml');
55              
56             =head1 FUNCTIONS
57              
58             =head2 Load (deprecated)
59              
60             Same as load(), provided for backward compatibility.
61              
62             =cut
63              
64             sub Load {
65 0     0     return load(@_);
66             }
67              
68             =head2 load
69              
70             Load the local from a given file
71              
72             $local->load('/usr/local/share/slack-get/local/french.xml') or die "unable to load local\n";
73              
74             Return undef if something goes wrong, 1 else.
75              
76             =cut
77              
78             sub load {
79 0     0     my ($self,$file) = @_ ;
80 0 0 0       return undef unless(defined($file) && -e $file);
81 0           print "[Slackware::Slackget::Local] loading file \"$file\"\n";
82 0           my $data = XML::Simple::XMLin( $file , KeyAttr=> {'message' => 'id'}) ;
83 0           $self->{DATA} = $data->{'message'} ;
84 0           $self->{LP_NAME} = $data->{name} ;
85 0           return 1;
86             }
87              
88             =head2 get_indexes
89              
90             Return the list of all index of the current loaded local. Dependending of the context, this method return an array or an arrayref.
91              
92             # Return a list
93             foreach ($local->get_indexes) {
94             print "$_ : ",$local->Get($_),"\n";
95             }
96            
97             # Return an arrayref
98             my $index_list = $local->get_indexes ;
99              
100             =cut
101              
102             sub get_indexes
103             {
104 0     0     my $self = shift;
105 0           my @a = keys( %{$self->{DATA} });
  0            
106 0 0         return wantarray ? @a : \@a;
107             }
108              
109             =head2 Get (deprecated)
110              
111             Same as get(), provided for backward compatibility.
112              
113             =cut
114              
115             sub Get {
116 0     0     return get(@_);
117             }
118              
119             =head2 get
120              
121             Return the localized message of a given token :
122              
123             my $error_on_modification = $local->get('__ERR_MOD') ;
124              
125             Return undef if the token doesn't exist.
126              
127             You can also pass extra arguments to this method, and if their is wildcards in the token they will be replace by those values. Wildcards are %1, %2, ..., %x.
128              
129             Here is and example :
130            
131             # The token is :
132             # __NETWORK_CONNECTION_ERROR = Error, cannot connect to %1, the server said ``%2''.
133             my $localized_token = $local->get('__NETWORK_CONNECTION_ERROR', '192.168.0.42', 'Connection not authorized');
134             print "$localized_token\n";
135             # $localized_token contains the string "Error, cannot connect to 192.168.0.42, the server said ``Connection not authorized''."
136              
137              
138             =cut
139              
140             sub get {
141 0     0     my ($self,$token,@args) = @_ ;
142 0 0         if(@args)
143             {
144 0           @args = (0,@args);
145 0           my $tmp = $self->{DATA}->{$token}->{'content'};
146 0           for(my $k=1;$k<=$#args; $k++)
147             {
148 0           $tmp =~ s/%$k/$args[$k]/g ;
149             }
150 0           return $tmp;
151             }
152             else
153             {
154 0           return $self->{DATA}->{$token}->{'content'};
155             }
156             }
157              
158             =head2 to_XML (deprecated)
159              
160             Same as to_xml(), provided for backward compatibility.
161              
162             =cut
163              
164             sub to_XML {
165 0     0     return to_xml(@_);
166             }
167              
168             sub to_xml
169             {
170 0     0     my $self = shift;
171 0           my @msg = sort {$a cmp $b} keys(%{ $self->{DATA} });
  0            
  0            
172 0           my $xml = "\n{LP_NAME}\">\n";
173 0           foreach my $token (@msg)
174             {
175 0 0         unless(defined( $self->{DATA}->{$token}->{content} ))
176             {
177 0           print "token \"$token\" have no associate value.\n";
178 0           next;
179             }
180            
181 0           $xml .= "\t{DATA}->{$token}->{content}]]>\n";
182             }
183 0           $xml .= "";
184             }
185              
186             =head2 name
187              
188             Accessor for the name of the Local (langpack).
189              
190             print "The current langpack name is : ", $local->name,"\n";
191             $local->name('Japanese'); # Set the name of the langpack to 'Japanese'.
192              
193             =cut
194              
195             sub name
196             {
197 0     0     my $self = shift;
198 0           my $name = shift;
199 0 0         return $name ? ($self->{LP_NAME}=$name) : $self->{LP_NAME};
200             }
201              
202             =head1 AUTHOR
203              
204             DUPUIS Arnaud, C<< >>
205              
206             =head1 BUGS
207              
208             Please report any bugs or feature requests to
209             C, or through the web interface at
210             L.
211             I will be notified, and then you'll automatically be notified of progress on
212             your bug as I make changes.
213              
214             =head1 SUPPORT
215              
216             You can find documentation for this module with the perldoc command.
217              
218             perldoc Slackware::Slackget::Local
219              
220              
221             You can also look for information at:
222              
223             =over 4
224              
225             =item * Infinity Perl website
226              
227             L
228              
229             =item * slack-get specific website
230              
231             L
232              
233             =item * RT: CPAN's request tracker
234              
235             L
236              
237             =item * AnnoCPAN: Annotated CPAN documentation
238              
239             L
240              
241             =item * CPAN Ratings
242              
243             L
244              
245             =item * Search CPAN
246              
247             L
248              
249             =back
250              
251             =head1 ACKNOWLEDGEMENTS
252              
253             Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation.
254              
255             =head1 SEE ALSO
256              
257             =head1 COPYRIGHT & LICENSE
258              
259             Copyright 2005 DUPUIS Arnaud, All Rights Reserved.
260              
261             This program is free software; you can redistribute it and/or modify it
262             under the same terms as Perl itself.
263              
264             =cut
265              
266             1; # End of Slackware::Slackget::Local