lib/Acme/Monkey.pm untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 36 totaltotal 0.0% 0 / 307 CC 9 SCAR 13.1
CC
9
Cov
0%
swing
12.0
new
12.0
_happiness
6.0
CRAP 3.7
1 package Acme::Monkey;
2  
3 =head1 NAME
4  
5 Acme::Monkey - Monkeys here, monkeys there, MONKEYS everywhere!
6  
7 =head1 ISOPONYS
8  
9 use Acme::Monkey;
10
11 my $conway = Acme::Monkey->new();
12 my $wall = Acme::Monkey->new();
13
14 $wall->groom( $conway );
15 $conway->dump();
16  
17 I so ponys, I so ponys.
18  
19 =head1 DESCRIPTION
20  
21 This module is a collaborative effort of several ValueClick Media
22 employees. We developed this module to coincide with the
23 YAPC::NA 2007 conference in Houston, TX. In the conference SWAG
24 bag we distributed about 275 monkey balls with the ValueClick logo
25 and a reference to this module.
26  
27 This module is better than sliced gravy.
28  
29 Make sure you check out the supporting scripts - monkey_life.pl
30 and monkey_ship.pl.
31  
32 =cut
33  
34 use strict;
35 use warnings;
36 use Time::HiRes qw(usleep);
37 use File::Find;
38  
39 $SIG{__WARN__} = sub{ print STDERR "grrrr\n"; };
40 $SIG{__DIE__} = sub{ print STDERR shift()."! eeek eeek!\n"; exit 1; };
41  
42 our $VERSION = 4.99;
43  
44 # Need...all other platforms
45 our %os_clrscr_commands = (
46 'linux' => 'clear',
47 'MSWin32' => 'cls',
48 );
49  
50 our $CLEAR_COMMAND = $os_clrscr_commands{$^O};
51  
52 =head1 METHODS
53  
54 =head2 new
55  
56 =cut
57  
58 sub new {
59 my $class = shift;
60 my $self = bless {}, $class;
61 $self->{hunger} = 80;
62 $self->{happiness} = 50;
63 $self->{drunkness} = 0;
64 $self->{sub} = undef;
65 return $self;
66 }
67  
68 sub monkey {
69 print "Monkey!\n";
70 }
71  
72 =head2 bastardize
73  
74 $monkey->bastardize( $object );
75  
76 Add some useful features to any object.
77  
78 =cut
79 #sub bastardize {
80 # $self = splice( @_, 0 ,1 );
81 # { # Retrieve arguments of parameter.
82 # no strict; $object = shift
83 # ;my @classes = $_[ 0 ] ;
84 # $class=$classes->[1 - 1 ] ;
85 # } #Then finsh for more stuff.
86 # no strict 'refs';*{$class.'::monkey' }=\$self;
87 # *{ $class . '::DESTROY' } = sub{$monkey->slap}
88 # ; return 'SUPERCALIFRAJULISTICEXPIALIDOTIOUS'
89 #}
90  
91 sub bastardize {
92 return 'SUPERCALIFRAJULISTICEXPIALIDOTIOUS'
93 }
94  
95 =head2 slap
96  
97 Poor monkey...
98  
99 =cut
100  
101 sub slap {
102 grrrr('Ouch!');
103 $_[0]->_happiness( -1 );
104 }
105  
106 sub fondle {
107 die('pervert');
108 }
109  
110 sub _happiness {
111 $_[0]->{happiness} += $_[1];
112 die('cry') if $_[0]->{happiness} <1;
113 }
114  
115 sub groom {
116 my $self = shift;
117 my $target = shift;
118 if (ref($target) eq 'Acme::Monkey') {
119 $target->_happiness(+1);
120 }
121 else {
122 die "Target is not a monkey!\n";
123 }
124 }
125  
126 sub dump {
127 my $self = shift;
128 use Data::Dumper;
129 print Dumper($self);
130 return;
131 }
132  
133 =head2 see
134  
135 Allows the monkey to see a function. See do
136  
137 sub shoot {
138 print "Bang!\n";
139 }
140 $monkey = Acme::Monkey->new();
141 $monkey->see(\&shoot);
142 $monkey->do();
143 $monkey->do();
144  
145 =cut
146  
147 sub see {
148 my $self = shift;
149 my $sub = shift;
150 $self->{sub} = $sub;
151 }
152  
153 =head2 do
154  
155 Does what the monkey see()s
156  
157 =cut
158  
159 sub do {
160 my $self = shift;
161 return $self->{sub}->() if defined $self->{sub};
162 }
163  
164  
165 sub _hologram {
166 print ' _ ###### _'."\n";
167 print ' / \ #(*)(*)# / \\'."\n";
168 print ' | {<#/ {} \#>} |'."\n";
169 print ' \_/#| |#\_/'."\n";
170 print ' #\======/#'."\n";
171 print ' ########'."\n";
172 print ' ####'."\n";
173 }
174  
175 =head2 swing
176  
177 $monkey->swing("/bin"); # Well, it sounds like vine. :)
178  
179 $monkey->swing(qw(/bin /var));
180  
181 Monkey seeks out bananas in given directory trees.
182  
183 =cut
184  
185 sub swing {
186 my $self = shift;
187 my @directory_trees = @_;
188  
189 our @bunch_o_nanas;
190  
191 $self->_hologram();
192 print "\nSearching for bananas...\n\n";
193 find(\&while_im_swinging_in, @directory_trees);
194  
195 # Bananas call back. Bananas find Monkey...
196 sub while_im_swinging_in {
197 if ($File::Find::name =~ m/.*banana.*/i) {
198 push @bunch_o_nanas, $File::Find::dir.$File::Find::name;
199 }
200 }
201  
202 # Report on my swinging
203 if (@bunch_o_nanas) {
204 print "NO, we found bananas at...\n";
205 print join("\n", @bunch_o_nanas);
206 print "\n";
207 }
208 else {
209 print "YES, we have no bananas.\n";
210 print "How about dropping some!\n";
211 }
212 }
213  
214 =head2 fling
215  
216 A verb.
217  
218 $monkey->fling();
219  
220 =cut
221  
222 sub fling {
223 my $fling_buffer = Acme::Monkey::FrameBuffer->new(W => 40, H => 10);
224  
225 system($CLEAR_COMMAND);
226 for my $seq(@{$Acme::Monkey::FlingFrames::sequence}) {
227 system($CLEAR_COMMAND);
228 $fling_buffer->clear();
229 $fling_buffer->put(@{$Acme::Monkey::FlingFrames::frames}[$seq], 2, 2);
230 $fling_buffer->put([__PACKAGE__], 1, 1);
231 $fling_buffer->draw();
232 usleep(120000);
233 }
234 }
235  
236  
237 use Exporter qw( import );
238 our @EXPORT = qw(grrrr bannana grubs wine beer vodka swing fling);
239  
240 =head1 SUBROUTINES
241  
242 Exporter is used to these on you.
243  
244 grrrr($stuff); # Like warn().
245 bannana(); # For feeding.
246  
247 =head2 CONSUMEABLES
248  
249 wine() # For happy monkeys.
250 grubs() # Yummy.
251 beer() # Have anything stronger?
252 vodka() # Ya baby!
253 bannana() # The usual fare.
254  
255 =cut
256  
257 sub grrrr { print STDERR join(' grrr ',@_)." GRRRR\n"; }
258 sub banana { return 'food', 1; }
259 sub grubs { return 'food', 2; }
260 sub wine { return 'drunk', 2; }
261 sub beer { return 'drunk', 1; }
262 sub vodka { return 'drunk', 5; }
263  
264 # Hmmm, Appears to be a Java inner class :)
265 {
266 package Acme::Monkey::FrameBuffer;
267  
268 use Carp qw(croak);
269  
270 # TODO: put all OO boilerplate...
271  
272 sub new {
273 my $class = shift;
274 my %params = @_;
275 my $self = {};
276
277 $self->{WIDTH} = $params{'width'} || $params{'W'} || undef;
278 $self->{HEIGHT} = $params{'height'} || $params{'H'} || undef;
279  
280 # TODO: Should we just default X,Y instead?
281 croak "Width required\n" if !defined($self->{WIDTH});
282 croak "Height required\n" if !defined($self->{HEIGHT});
283  
284 $self->{BUF_SIZE} = $self->{WIDTH} * $self->{HEIGHT};
285 $self->{BUFFER} = '';
286  
287 bless($self, $class);
288 }
289  
290 sub width {
291 my ($self) = shift;
292 return $self->{WIDTH};
293 }
294  
295 sub height {
296 my ($self) = shift;
297 return $self->{HEIGHT};
298 }
299  
300 sub get_buffer {
301 my $self = shift;
302 return $self->{BUFFER};
303 }
304  
305 sub clear {
306 my $self = shift;
307 $self->{BUFFER} = ' ' x $self->{BUF_SIZE};
308 }
309  
310 sub put {
311 my $self = shift;
312 my ($what, $xcoord, $ycoord) = @_;
313  
314 $xcoord -= 1;
315 $ycoord -= 1;
316
317 my $location = ($ycoord * $self->{WIDTH}) + $xcoord;
318
319 for my $line(@$what) {
320 substr($self->{BUFFER}, $location, length($line), $line);
321 $location += $self->{WIDTH};
322 }
323 }
324  
325 sub draw {
326 my $self = shift;
327  
328 for my $row(0..($self->{HEIGHT}-1)) {
329 my $line = substr($self->{BUFFER}, $row * $self->{WIDTH}, $self->{WIDTH});
330 print "$line\n";
331 }
332 }
333  
334 !(!(!0));
335 }
336  
337 {
338 package Acme::Monkey::FlingFrames;
339  
340 use strict;
341 use warnings;
342  
343 BEGIN {
344 use Exporter();
345 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
346 $VERSION = 0.01;
347 @ISA = qw(Exporter);
348 @EXPORT_OK = qw($sequence $frames);
349 }
350 our @EXPORT_OK;
351  
352 our $sequence = [0,0,0,1,2,3,2,1,4,5,6,7,8,9];
353  
354 our $frames = [
355 [
356 ' ',
357 ' o@o ',
358 '----|----',
359 ' | ',
360 ' === ',
361 ' | | ',
362 ],
363 [
364 ' ',
365 ' o@o ',
366 '----|----*',
367 ' | ',
368 ' === ',
369 ' | | ',
370 ],
371 [
372 ' * ',
373 ' o@o / ',
374 '----|--- ',
375 ' | ',
376 ' === ',
377 ' | | ',
378 ],
379 [
380 ' * ',
381 ' o@o | ',
382 '----|--- ',
383 ' | ',
384 ' === ',
385 ' | | ',
386 ],
387 [
388 ' ',
389 ' o@o ',
390 '----|---- *',
391 ' | ',
392 ' === ',
393 ' | | ',
394 ],
395 [
396 ' ',
397 ' o@o ',
398 '----|---- *',
399 ' | ',
400 ' === ',
401 ' | | ',
402 ],
403 [
404 ' ',
405 ' o@o ',
406 '----|----',
407 ' | *',
408 ' === ',
409 ' | | ',
410 ],
411 [
412 ' ',
413 ' o@o ',
414 '----|----',
415 ' | *',
416 ' === ',
417 ' | | ',
418 ],
419 [
420 ' ',
421 ' o@o ',
422 '----|----',
423 ' | ',
424 ' === *',
425 ' | | ',
426 ],
427 [
428 ' ',
429 ' o@o ',
430 '----|----',
431 ' | ',
432 ' === ',
433 ' | | *',
434 ],
435 [
436 ' ',
437 ' o@o ',
438 '----|----',
439 ' | ',
440 ' === ',
441 ' | | *',
442 ],
443 ];
444  
445 !(!(!0));
446 }
447  
448 {
449 package Acme::Monkey::ScreenBuffer;
450  
451 sub new {
452 my ($class, $width, $height) = @_;
453 my $self = bless {}, $class;
454 $self->{width} = $width;
455 $self->{height} = $height;
456 $self->clear_screen();
457 $self->clear_buffer();
458 return $self;
459 }
460  
461 sub clear_screen {
462 system( $CLEAR_COMMAND );
463 }
464  
465 sub put {
466 my ($self, $x, $y, $char) = @_;
467 $self->{buffer}->[$x]->[$y] = $char;
468
469 }
470  
471 sub get {
472 my ($self, $x, $y) = @_;
473 return $self->{buffer}->[$x]->[$y];
474 }
475  
476 sub display {
477 my ($self) = @_;
478  
479 my $out = '';
480 foreach my $y (1..$self->{height}) {
481 foreach my $x (1..$self->{width}) {
482 $out .= $self->{buffer}->[$x]->[$y];
483 }
484 $out .= "\n";
485 }
486 $self->clear_screen();
487 print $out;
488 }
489  
490 sub flush {
491 my ($self) = @_;
492 $self->display();
493 $self->clear_buffer();
494 }
495  
496 sub clear_buffer {
497 my ($self) = @_;
498 my $buffer = [];
499 foreach my $x (1..$self->{width}) {
500 $buffer->[$x] = [];
501 foreach my $y (1..$self->{height}) {
502 $buffer->[$x]->[$y] = ' ';
503 }
504 }
505 $self->{buffer} = $buffer;
506 }
507  
508 sub scroll_left {
509 my ($self) = @_;
510 foreach my $x (2..$self->{width}) {
511 foreach my $y (1..$self->{height}) {
512 $self->{buffer}->[$x-1]->[$y] = $self->{buffer}->[$x]->[$y];
513 }
514 }
515 }
516 }
517  
518 1;
519  
520 __END__