Cow.pm

filter: Statementstmt 82.4% 61 / 74 Branchbran 55.0% 11 / 20 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 80.0% 12 / 15 Podpod 88.8% 8 / 9 totaltotal 74.6% 94 / 126 CC 14 SCAR 11.7
CC
14
Cov
75%
wrap
6.0
say
6.0
fill
6.0
CRAP 3.2
↑ Summary Cow/TextBalloon.pm »
1 package Acme::Cow;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 $Acme::Cow::VERSION = '0.2';
6  
7 # Preloaded methods go here.
8  
9 # Autoload methods go after =cut, and are processed by the autosplit program.
10  
11 # Below is stub documentation for your module. You better edit it!
12  
13 =head1 NAME
14  
15 Acme::Cow - Talking barnyard animals (or ASCII art in general)
16  
17 =head1 SYNOPSIS
18  
19 use Acme::Cow;
20  
21 $cow = new Acme::Cow;
22 $cow->say("Moo!");
23 $cow->print();
24  
25 $sheep = new Acme::Cow::Sheep; # Derived from Acme::Cow
26 $sheep->wrap(20);
27 $sheep->think();
28 $sheep->text("Yeah, but you're taking the universe out of context.");
29 $sheep->print(\*STDERR);
30  
31 $duck = new Acme::Cow(File => "duck.cow");
32 $duck->fill(0);
33 $duck->say(`figlet quack`);
34 $duck->print($socket);
35  
36  
37 =head1 DESCRIPTION
38  
39 Acme::Cow is the logical evolution of the old cowsay program. Cows
40 are derived from a base class (Acme::Cow) or from external files.
41  
42 Cows can be made to say or think many things, optionally filling
43 and justifying their text out to a given margin,
44  
45 Cows are nothing without the ability to print them, or sling them
46 as strings, or what not.
47  
48 =cut
49  
50 3 ▶use Acme::Cow::TextBalloon;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶use Text::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 3 $Acme::Cow::default_cow = <<'EOC';
55 {$balloon}
56 {$tl} ^__^
57 {$tl} ({$el}{$er})\_______
58 (__)\ )\/\
59 {$U} ||----w |
60 || ||
61 EOC
62  
63 =pod
64  
65 =head1 METHODS
66  
67 =head2 new
68  
69 =over 4
70  
71 =item Parameters
72  
73 A list of key-value pairs. If you plan to use an external file as
74 the template, you probably want to say:
75  
76 $x = new Acme::Cow(File => 'file.cow');
77  
78 =item Returns
79  
80 A blessed reference to an C<Acme::Cow>.
81  
82 =back
83  
84 =cut
85  
86 sub new
87 {
88 3 ▶ my $proto = shift;
Subroutine new: called CC=2 SCAR=7.2
Pod documented
89 3 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
90 3 my %args = @_;
91 3 my $self = {
92 wrap => 40,
93 mode => 'say',
94 fill => 1,
95 over => 0,
96 text => undef,
97 el => 'o',
98 er => 'o',
99 U => ' ',
100 %args,
101 };
102 3 bless $self, $class;
103 }
104  
105 =pod
106  
107 =head2 over
108  
109 Specify (or retrieve) how far to the right (in spaces) the text
110 balloon should be shoved.
111  
112 =over 4
113  
114 =item Parameters
115  
116 (optional) A number.
117  
118 =item Returns
119  
120 The new value, if set; the existing value if not.
121  
122 =back
123  
124 =cut
125  
126 sub over
127 {
128 1 ▶ my $self = shift;
Subroutine over: called CC=2 SCAR=7.0
Pod documented
129 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
130 1 $self->{'over'} = $_[0];
131 }
132 1 return $self->{'over'};
133 }
134  
135 =pod
136  
137 =head2 wrap
138  
139 Specify (or retrieve) the column at which text inside the balloon
140 should be wrapped. This number is relative to the balloon, not
141 absolute screen position.
142  
143 =over 4
144  
145 =item Parameters
146  
147 (optional) A number.
148  
149 =item Returns
150  
151 The new value, if set; the existing value if not.
152  
153 =item Notes
154  
155 The number set here has no effect if you decline filling/adjusting
156 of the balloon text.
157  
158 =back
159  
160 =cut
161  
162 sub wrap
163 {
164 0 ▶ my $self = shift;
Subroutine wrap: not called CC=2 SCAR=17.9
Pod documented
165 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
166 0 $self->{'wrap'} = $_[0];
167 }
168 0 return $self->{'wrap'};
169 }
170  
171 =pod
172  
173 =head2 think
174  
175 Tell the cow to think its text instead of saying it.
176  
177 =over 4
178  
179 =item Parameters
180  
181 (optional) Text to think.
182  
183 =item Returns
184  
185 None.
186  
187 =back
188  
189 =cut
190  
191 sub think
192 {
193 1 ▶ my $self = shift;
Subroutine think: called CC=2 SCAR=7.6
Pod documented
194 1 $self->{'mode'} = 'think';
195 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
196 0 $self->text(@_);
197 }
198 }
199  
200 =pod
201  
202 =head2 SAY
203  
204 Tell the cow to say its text instead of thinking it.
205  
206 =over 4
207  
208 =item Parameters
209  
210 (optional) Text to say.
211  
212 =item Returns
213  
214 None.
215  
216 =back
217  
218 =cut
219  
220 sub say
221 {
222 0 ▶ my $self = shift;
Subroutine say: not called CC=2 SCAR=17.9
Pod undocumented
223 0 $self->{'mode'} = 'say';
224 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
225 0 $self->text(@_);
226 }
227 }
228  
229 =pod
230  
231 =head2 text
232  
233 Set (or retrieve) the text that the cow will say or think.
234  
235 =over 4
236  
237 =item Parameters
238  
239 A list of lines of text (optionally terminated with newlines) to
240 be displayed inside the balloon.
241  
242 =item Returns
243  
244 The new text, if set; the current text, if not.
245  
246 =back
247  
248 =cut
249  
250 sub text
251 {
252 4 ▶ my $self = shift;
Subroutine text: called CC=2 SCAR=7.0
Pod documented
253 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 0 4
254 4 my @l = @_;
255 4 $self->{'text'} = \@l;
256 }
257 4 return $self->{'text'};
258 }
259  
260 =pod
261  
262 =head2 print
263  
264 Print a representation of the cow to the specified filehandle
265 (STDOUT by default).
266  
267 =over 4
268  
269 =item Parameters
270  
271 (optional) A filehandle.
272  
273 =item Returns
274  
275 None.
276  
277 =back
278  
279 =cut
280  
281 sub print
282 {
283 5 ▶ my $self = shift;
Subroutine print: called CC=2 SCAR=7.1
Pod documented
284 5 ▶ my $fh = shift || \*STDOUT;
Condition50%1/2
shift() || \*STDOUT
l!l
05
MC/DC0%0/1
shift() || \*STDOUT
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
285 5 print $fh $self->as_string();
286 }
287  
288 =pod
289  
290 =head2 fill
291  
292 Inform the cow to fill and adjust (or not) the text inside its balloon.
293 By default, text inside the balloon is filled and adjusted.
294  
295 =over 4
296  
297 =item Parameters
298  
299 (optional) A scalar; true if you want it to fill and adjust, false
300 otherwise.
301  
302 =item Returns
303  
304 The current fill/adjust state, or the new one after setting.
305  
306 =back
307  
308 =cut
309  
310 sub fill
311 {
312 0 ▶ my $self = shift;
Subroutine fill: not called CC=2 SCAR=17.9
Pod documented
313 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
314 0 $self->{'fill'} = $_[0];
315 }
316 0 return $self->{'fill'};
317  
318 }
319  
320 =pod
321  
322 =head2 as_string
323  
324 Render the cow as a string.
325  
326 =over 4
327  
328 =item Parameters
329  
330 (optional) A scalar that can be interpreted as a C<STRING> type
331 for C<Text::Template>.
332  
333 =item Returns
334  
335 An ASCII rendering of your cow.
336  
337 =item Notes
338  
339 If you're using an external file for a cow template, any difficulties
340 in processing the file will occur in this method.
341  
342 Every time this method is called, the result is recalculated; there
343 is no caching of results.
344  
345 =back
346  
347 =cut
348  
349 sub as_string
350 {
351 10 ▶ my $self = shift;
Subroutine as_string: called CC=5 SCAR=16.1
Pod documented
352 10 my $tmpl = shift;
353 10 ▶ if (not $tmpl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmpl) 8 2 10
354 8 ▶ if (defined $self->{'File'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'File'}) { } 2 6 8
355 2 $tmpl = _slurp_file($self->{'File'});
356 } else {
357 6 $tmpl = $Acme::Cow::default_cow;
358 }
359 }
360 10 my $b = $self->_create_balloon();
361 10 my $template = new Text::Template(TYPE => 'STRING', SOURCE => $tmpl);
362 10 chomp($Acme::Cow::_private::balloon = $b->as_string());
363 10 $Acme::Cow::_private::el = $self->{'el'};
364 10 $Acme::Cow::_private::er = $self->{'er'};
365 10 $Acme::Cow::_private::U = $self->{'U'};
366 10 ▶ $Acme::Cow::_private::tl = ($self->{'mode'} eq 'think') ? 'o' : '\\';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mode'} eq 'think' ? : 4 6 10
367 10 ▶ $Acme::Cow::_private::tr = ($self->{'mode'} eq 'think') ? 'o' : '/';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mode'} eq 'think' ? : 4 6 10
368 10 my $text = $template->fill_in(PACKAGE => 'Acme::Cow::_private');
369 10 return $text;
370 }
371  
372 sub _create_balloon
373 {
374 10 ▶ my $self = shift;
Subroutine _create_balloon: called CC=2 SCAR=6.9
375 10 my $b = new Acme::Cow::TextBalloon;
376 10 for my $i (qw(fill text over mode wrap)) {
377 50 $b->{$i} = $self->{$i};
378 }
379 10 return $b;
380 }
381  
382 sub _slurp_file
383 {
384 2 ▶ my $filename = shift;
Subroutine _slurp_file: called CC=1 SCAR=0.0
385 2 my $fh = new IO::File($filename);
386 2 local $/ = undef;
387 2 my $text = $fh->getline();
388 2 return $text;
389 }
390  
391 3 1;
392 __END__
↑ Summary Cow/TextBalloon.pm »