Hints.pm

filter: Statementstmt 75.6% 59 / 78 Branchbran 29.1% 7 / 24 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 81.2% 13 / 16 Podpod 100% 12 / 12 totaltotal 70.6% 94 / 133 CC 15 SCAR 21.5
CC
15
Cov
65%
load_from_file
16.8
random
12.0
forward
6.0
CRAP 8.6
« DBIx/SystemCatalog/Oracle.pm ↑ Summary DBIx/SystemCatalog/XBase.pm »
1 package Hints;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 $VERSION = '0.02';
9  
10 =head1 NAME
11  
12 Hints - Perl extension for hints databases
13  
14 =head1 SYNOPSIS
15  
16 use Hints;
17  
18 my $hints = new Hints;
19  
20 $hints->load_from_file('my.hints');
21  
22 print $hints->random();
23  
24 =head1 DESCRIPTION
25  
26 In many programs you need hints database and methods for accessing this
27 database. Extension Hints is object oriented abstract module, you can
28 use file-base of hints or make descendant with own base.
29  
30 =head1 THE HINTS CLASS
31  
32 =head2 new
33  
34 Constructor create instance of Hints class. Than call C<init()> constructor
35 for build implicit database (descendant ussually re-implement these method).
36  
37 my $hints = new Hints;
38  
39 =cut
40  
41 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
42 4 my $class = shift;
43 4 my $obj = bless { base => [], last => 0 }, $class;
44 4 $obj->clear();
45 4 srand (time() ^ ($$ + ($$ << 15)));
46 4 return $obj->init(@_);
47 }
48  
49 =head2 init
50  
51 This method was called from C<new()> constructor for building implicit
52 database. Base class define only abstract version. Return value of C<init()>
53 method must be instance (typically same as calling instance). You can use
54 this to change class or stop making instance by returning of undefined value.
55  
56 =cut
57  
58 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
59 1 return shift;
60 }
61  
62 =head2 load_from_file (FILE, SEPARATOR)
63  
64 Loading all hints from file specified as first argument. Hints separator is
65 determined by second argument. If separator is undefined than default separator
66 is used (^---$). Separator argument is regular expression.
67  
68 You can also use file handle or reference to array instead of filename.
69  
70 $hints->load_from_file('my.hints','^**SEPARATOR**$');
71 $hints->load_from_file(\*FILE,'^**SEPARATOR**$');
72 $hints->load_from_file(\@lines,'^**SEPARATOR**$');
73  
74 =cut
75  
76 4 ▶sub load_from_file {
Subroutine load_from_file: called CC=11 SCAR=28.2
Pod documented
77 4 my $obj = shift;
78 4 my $file = shift;
79 4 ▶ my $separator = shift || '^---$';
Condition100%2/2
shift() || '^---$'
l!l
13
MC/DC100%1/1
shift() || '^---$'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
80 4 my $ioref;
81  
82 4 ▶ return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 4 4
83 4 my @lines = ();
84 4 ▶ if (ref $file eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file eq 'ARRAY') { } 4 0 4
elsif (ref $file) { } 0 0 0
85 4 @lines = @$file;
86 } elsif (ref $file) {
87 0 eval {
88 0 $ioref = *{$file}{IO};
89 };
90 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
91 0 @lines = <$ioref>;
92 } else {
93 0 ▶ return unless $ioref = new IO::File $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $ioref = "IO::File"->new($file) 0 0 0
94 0 @lines = <$ioref>;
95 }
96  
97 4 my @current = ();
98 4 for (@lines) {
99 52 chomp;
100 52 ▶ if (/$separator/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$separator/) { } 16 36 52
101 16 push @{$obj->{base}},[ @current ];
102 16 @current = ();
103 } else {
104 36 push @current,$_;
105 }
106 }
107 4 ▶ $ioref->close() unless ref $file;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 4 4
108 4 ▶ push @{$obj->{base}},\@current if @current;
Branch: 1 branch
BranchTrueFalseTotal
if @current 3 1 4
109 }
110  
111 =head2 clear
112  
113 This method clear hints database.
114  
115 $hints->clear;
116  
117 =cut
118  
119 5 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
120 5 my $obj = shift;
121  
122 5 $obj->{base} = [];
123 }
124  
125 =head2 format
126  
127 Method is used for formatting hint before returning. Ussually redefined by
128 descendant. In abstract class making one long line from multilines.
129  
130 =cut
131  
132 3 ▶sub format {
Subroutine format: called CC=1 SCAR=0.0
Pod documented
133 3 my $obj = shift;
134 3 my $output = join ' ',@_;
135 3 $output =~ s/\s+$//;
136 3 return $output;
137 }
138  
139 =head2 first
140  
141 Return first hint from database.
142  
143 my $hint = $hints->first;
144  
145 =cut
146  
147 1 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
148 1 my $obj = shift;
149 1 $obj->{iterator} = 0;
150 1 return $obj->next;
151 }
152  
153 =head2 next
154  
155 Return next hint from database (used after first).
156 If no hint rest undefined value is returned.
157  
158 my $hint = $hints->first;
159 do {
160 print $hint."\n";
161 } if (defined $hint = $hints->next);
162  
163 =cut
164  
165 2 ▶sub next {
Subroutine next: called CC=1 SCAR=0.0
Pod documented
166 2 my $obj = shift;
167 2 $obj->{last} = $obj->{iterator};
168 2 return $obj->item($obj->{iterator}++);
169 }
170  
171 =head2 random
172  
173 Return random hint from database.
174  
175 my $hint = $hints->random;
176  
177 =cut
178  
179 0 ▶sub random {
Subroutine random: not called CC=3 SCAR=24.8
Pod documented
180 0 my $obj = shift;
181 0 my $l;
182 do {
183 0 $l = rand($obj->count());
184 0 ▶ last if $obj->count() == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->count == 1 0 0 0
185 0 ▶ } while ($l == $obj->{last});
Branch: 1 branch
BranchTrueFalseTotal
$l == $obj->{"last"} and do { do { $l = rand $obj->count; last if $obj->count == 1 } } 0 0 0
186 0 return $obj->item($obj->{last} = $l);
187 }
188  
189 =head2 count
190  
191 Return number of hints in database.
192  
193 my $number = $hints->count;
194  
195 =cut
196  
197 5 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
198 5 my $obj = shift;
199 5 return scalar @{$obj->{base}};
200 }
201  
202 =head2 item NUMBER
203  
204 Return NUMBER. item from database.
205  
206 # return last hint
207 my $hint = $hints->item($hints->count - 1);
208  
209 =cut
210  
211 3 ▶sub item {
Subroutine item: called CC=1 SCAR=0.0
Pod documented
212 3 my $obj = shift;
213 3 my $number = shift;
214 3 $obj->{last} = $number;
215 3 return $obj->format(@{$obj->{base}->[$number]});
216 }
217  
218 =head2 forward
219  
220 Return next hint after last wanted hint from database.
221  
222 my $random_hint = $hints->random;
223 my $next_hint = $hints->forward;
224  
225 =cut
226  
227 0 ▶sub forward {
Subroutine forward: not called CC=2 SCAR=17.9
Pod documented
228 0 my $obj = shift;
229 0 ▶ $obj->{last} = 0 if ++$obj->{last} >= $obj->count;
Branch: 1 branch
BranchTrueFalseTotal
if ++$obj->{"last"} >= $obj->count 0 0 0
230 0 return $obj->item($obj->{last});
231 }
232  
233 =head2 backward
234  
235 Return previous hint before last wanted hint from database.
236  
237 my $random_hint = $hints->random;
238 my $prev_hint = $hints->backward;
239  
240 =cut
241  
242 0 ▶sub backward {
Subroutine backward: not called CC=2 SCAR=17.9
Pod documented
243 0 my $obj = shift;
244 0 ▶ $obj->{last} = $obj->count() - 1 if --$obj->{last} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$obj->{"last"} < 0 0 0 0
245 0 return $obj->item($obj->{last});
246 }
247  
248 2 1;
249  
250 __END__
« DBIx/SystemCatalog/Oracle.pm ↑ Summary DBIx/SystemCatalog/XBase.pm »