Phrasebook/Loader/Text.pm

filter: Statementstmt 100% 79 / 79 Branchbran 84.2% 32 / 38 Conditioncond 66.6% 22 / 33 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 84.4% 157 / 186 CC 32 SCAR 23.8
CC
32
Cov
89%
load
15.9
keywords
13.8
dicts
5.0
CRAP 10.8
« Phrasebook/SQL.pm ↑ Summary Phrasebook.pm »
1 package Data::Phrasebook::Loader::Text;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use base qw( Data::Phrasebook::Loader::Base Data::Phrasebook::Debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 $VERSION = '0.35';
10  
11 =head1 NAME
12  
13 Data::Phrasebook::Loader::Text - Absract your phrases with plain text files.
14  
15 =head1 SYNOPSIS
16  
17 use Data::Phrasebook;
18  
19 my $q = Data::Phrasebook->new(
20 class => 'Fnerk',
21 loader => 'Text',
22 file => 'phrases.txt',
23 );
24  
25 # use default delimiters (:variable)
26 my $phrase = $q->fetch($keyword,{variable => 'substitute'});
27  
28 # use Template Toolkit style delimiters
29 $q->delimiters( qr{ \[% \s* (\w+) \s* %\] }x );
30 my $phrase = $q->fetch($keyword,{variable => 'substitute'});
31  
32 =head1 DESCRIPTION
33  
34 This loader plugin implements phrasebook patterns using plain text files.
35  
36 Phrases can be contained within one or more dictionaries, with each phrase
37 accessible via a unique key. Phrases may contain placeholders, please see
38 L<Data::Phrasebook> for an explanation of how to use these. Groups of phrases
39 are kept in a dictionary. In this implementation a single file is one
40 complete dictionary.
41  
42 An example plain text file:
43  
44 foo=Welcome to :my world. It is a nice :place.
45  
46 Within the phrase text placeholders can be used, which are then replaced with
47 the appropriate values once the get() method is called. The default style of
48 placeholders can be altered using the delimiters() method.
49  
50 =head1 INHERITANCE
51  
52 L<Data::Phrasebook::Loader::Text> inherits from the base class
53 L<Data::Phrasebook::Loader::Base>.
54 See that module for other available methods and documentation.
55  
56 =head1 METHODS
57  
58 =head2 load
59  
60 Given a C<file>, load it. C<file> must contain a valid phrase map.
61  
62 my $file = 'english.txt';
63 $loader->load( $file );
64  
65 This method is used internally by L<Data::Phrasebook::Generic>'s
66 C<data> method, to initialise the data store.
67  
68 To utilise the dictionary framework for a Plain Text phrasebook, the idea is
69 to use a directory of files, where the directory is passed via the C<file>
70 argument and the dictionary, the specific name of the file, is passed via
71 the C<dictionary> argument.
72  
73 my $file = '/tmp/phrasebooks';
74 my $dictionary = 'english.txt';
75 $loader->load( $file, $dictionary );
76  
77 =cut
78  
79 12 my %phrasebook;
80  
81 17 ▶sub load {
Subroutine load: called CC=15 SCAR=27.6
Pod documented
82 17 my ($class, $file, @dict) = @_;
83 17 ▶ $class->store(3,"->load IN - @_") if($class->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $class->debug 1 16 17
84 17 ▶ $file ||= $class->{parent}->file;
Condition33%1/3
$file ||= $class->{"parent"}->file
l!l&&r!l&&!r
1700
MC/DC0%0/2
$file ||= $class->{"parent"}->file
$file $class->{"parent"}->file
Truth table33%1/3
A ||= B
A= $fileB= $class->{"parent"}->file
ABresult
1X1
011
000
85 17 ▶ @dict = $class->{parent}->dict unless(@dict);
Branch: 1 branch
BranchTrueFalseTotal
unless @dict 13 4 17
86 17 ▶ croak "No file given as argument!" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 17 17
87  
88 17 my @file;
89 17 ▶ if(@dict) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dict) { } 4 13 17
90 4 ▶ while(@dict) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dict) 6 3 9
91 6 my $dict = pop @dict; # build phrases in reverse order
92 6 $dict = "$file/$dict";
93 6 ▶ croak "File [$dict] not accessible!" unless -f $dict && -r $dict;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $dict and -r $dict 1 5 6
Condition66%2/3
-f $dict and -r $dict
!ll&&!rl&&r
105
MC/DC50%1/2
-f $dict and -r $dict
-f $dict -r $dict
Truth table66%2/3
A and B
A= -f $dictB= -r $dict
ABresult
0X0
100
111
94 5 push @file, $dict;
95 }
96 } else {
97 13 ▶ croak "File [$file] not accessible!" unless -f $file && -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file and -r $file 0 13 13
Condition33%1/3
-f $file and -r $file
!ll&&!rl&&r
0013
MC/DC0%0/2
-f $file and -r $file
-f $file -r $file
Truth table33%1/3
A and B
A= -f $fileB= -r $file
ABresult
0X0
100
111
98 13 push @file, $file;
99 }
100  
101 16 %phrasebook = (); # ignore previous dictionary
102  
103 16 for my $file (@file) {
104 18 ▶ my $book = IO::File->new($file) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $book = "IO::File"->new($file) or next 18 0 18
105 18 ▶ while(<$book>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $book)) 42 18 60
106 42 my ($name,$value) = (/(.*?)=(.*)/);
107 42 ▶ $phrasebook{$name} = $value if($name); # value can be blank
Branch: 1 branch
BranchTrueFalseTotal
if $name 38 4 42
108 }
109 18 $book->close;
110 }
111  
112 16 return;
113 }
114  
115 =head2 get
116  
117 Returns the phrase stored in the phrasebook, for a given keyword.
118  
119 my $value = $loader->get( $key );
120  
121 =cut
122  
123 22 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
124 22 my ($class, $key) = @_;
125 22 ▶ if($class->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->debug) 2 20 22
126 2 $class->store(3,"->get IN");
127 2 $class->store(4,"->get key=[$key]");
128 2 $class->store(4,"->get phrase=[$phrasebook{$key}]");
129 }
130 22 return $phrasebook{$key};
131 }
132  
133 =head2 dicts
134  
135 Having instantiated the C<Data::Phrasebook> object class, and using the C<file>
136 attribute as a directory path, the object can return a list of the current
137 dictionaries available as:
138  
139 my $pb = Data::Phrasebook->new(
140 loader => 'Text',
141 file => '/tmp/phrasebooks',
142 );
143  
144 my @dicts = $pb->dicts;
145  
146 or
147  
148 my @dicts = $pb->dicts( $path );
149  
150 =cut
151  
152 3 ▶sub dicts {
Subroutine dicts: called CC=5 SCAR=16.1
Pod documented
153 3 my ($self,$path) = @_;
154 3 ▶ $path ||= $self->{parent}->file;
Condition100%3/3
$path ||= $self->{"parent"}->file
l!l&&r!l&&!r
111
MC/DC100%2/2
$path ||= $self->{"parent"}->file
$path $self->{"parent"}->file
Truth table100%3/3
A ||= B
A= $pathB= $self->{"parent"}->file
ABresult
1X1
011
000
155 3 ▶ return () unless($path && -d $path && -r $path);
Branch: 1 branch
BranchTrueFalseTotal
unless $path and -d $path and -r $path 2 1 3
Condition83%5/6
$path and -d $path and -r $path
!ll&&!rl&&r
201
$path and -d $path
!ll&&!rl&&r
111
MC/DC66%2/3
$path and -d $path and -r $path
$path -d $path -r $path
Truth table75%3/4
A and B and C
A= $pathB= -d $pathC= -r $path
ABCresult
0XX0
10X0
1100
1111
156  
157 12 ▶ my @files = map { my $x = $_ ; $x =~ s/$path.//; $x } grep {/^[^\.]+.txt$/} glob("$path/*");
Subroutine BEGIN: called CC=1 SCAR=0.0
158 1 return @files;
159 }
160  
161 =head2 keywords
162  
163 Having instantiated the C<Data::Phrasebook> object class, using the C<file>
164 and C<dict> attributes as required, the object can return a list of the
165 current keywords available as:
166  
167 my $pb = Data::Phrasebook->new(
168 loader => 'Text',
169 file => '/tmp/phrasebooks',
170 dict => 'TEST',
171 );
172  
173 my @keywords = $pb->keywords;
174  
175 or
176  
177 my @keywords = $pb->keywords( $path, $dict );
178  
179 Note that $path can either be the directory path, where $dict must be the
180 specific file name of the dictionary, or the full path of the dictionary file.
181  
182 In the second instance, the function will not load a dictionary, but can be
183 used to interrogate the contents of a known dictionary.
184  
185 =cut
186  
187 7 ▶sub keywords {
Subroutine keywords: called CC=13 SCAR=26.3
Pod documented
188 7 my @keywords;
189  
190 7 ▶ if(@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 1 6 7
191 1 @keywords = sort keys %phrasebook;
192 1 return @keywords;
193 }
194  
195 6 my ($self,$file,$dict) = @_;
196 6 ▶ $file ||= $self->{parent}->file;
Condition66%2/3
$file ||= $self->{"parent"}->file
l!l&&r!l&&!r
510
MC/DC0%0/2
$file ||= $self->{"parent"}->file
$file $self->{"parent"}->file
Truth table66%2/3
A ||= B
A= $fileB= $self->{"parent"}->file
ABresult
1X1
011
000
197 6 ▶ $dict ||= $self->{parent}->dict;
Condition66%2/3
$dict ||= $self->{"parent"}->dict
l!l&&r!l&&!r
402
MC/DC50%1/2
$dict ||= $self->{"parent"}->dict
$dict $self->{"parent"}->dict
Truth table66%2/3
A ||= B
A= $dictB= $self->{"parent"}->dict
ABresult
1X1
011
000
198 6 ▶ croak "No file given as argument!" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 6 6
199  
200 6 ▶ $file = "$file/$dict" if(-d $file && defined $dict);
Branch: 1 branch
BranchTrueFalseTotal
if -d $file and defined $dict 3 3 6
Condition100%3/3
-d $file and defined $dict
!ll&&!rl&&r
213
MC/DC100%2/2
-d $file and defined $dict
-d $file defined $dict
Truth table100%3/3
A and B
A= -d $fileB= defined $dict
ABresult
0X0
100
111
201 6 ▶ croak "File [$file] not accessible!" unless -f $file && -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file and -r $file 5 1 6
Condition66%2/3
-f $file and -r $file
!ll&&!rl&&r
501
MC/DC50%1/2
-f $file and -r $file
-f $file -r $file
Truth table66%2/3
A and B
A= -f $fileB= -r $file
ABresult
0X0
100
111
202  
203 1 ▶ my $book = IO::File->new($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $book = "IO::File"->new($file) or return 1 0 1
204 1 ▶ while(<$book>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $book)) 2 1 3
205 2 ▶ push @keywords, $1 if(/(.*?)=/ && $1);
Branch: 1 branch
BranchTrueFalseTotal
if /(.*?)=/ and $1 2 0 2
Condition33%1/3
/(.*?)=/ and $1
!ll&&!rl&&r
002
MC/DC0%0/2
/(.*?)=/ and $1
/(.*?)=/ $1
Truth table33%1/3
A and B
A= /(.*?)=/B= $1
ABresult
0X0
100
111
206 }
207 1 $book->close;
208  
209 1 my %keywords = map { $_ => 1 } @keywords;
210 1 @keywords = sort keys %keywords;
211 1 return @keywords;
212 }
213  
214 12 1;
215  
216 __END__
« Phrasebook/SQL.pm ↑ Summary Phrasebook.pm »