| 1 | package Acme::POE::Knee; | ||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | $Acme::POE::Knee::VERSION = '1.12'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | #ABSTRACT: Time sliced pony race using the POE kernel. | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use POE; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 12 | 0 | my $class = shift; | |||||||||
| 13 | 0 | my %args = @_; | |||||||||
| 14 | 0 | my $self = { }; | |||||||||
| 15 | 0 | my $data = { | |||||||||
| 16 | dist => 10, | ||||||||||
| 17 | ponies => { | ||||||||||
| 18 | 'dngor' => 5, | ||||||||||
| 19 | 'Abigail' => 5.2, | ||||||||||
| 20 | 'Co-Kane' => 5.4, | ||||||||||
| 21 | 'MJD' => 5.6, | ||||||||||
| 22 | 'acme' => 5.8, | ||||||||||
| 23 | }, | ||||||||||
| 24 | }; | ||||||||||
| 25 | |||||||||||
| 26 | ### check for wrong input ### | ||||||||||
| 27 | 0 | for my $key ( keys %args ) { | |||||||||
| 28 | 0 | ▶ | unless( exists $data->{$key} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | print "WARNING! Option $key is not supported in $class!\n"; | |||||||||
| 30 | } | ||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | ### bless the object into the class ### | ||||||||||
| 34 | 0 | bless $self, $class; | |||||||||
| 35 | |||||||||||
| 36 | ### now we start adding the data ### | ||||||||||
| 37 | 0 | for my $key (keys %$data) { | |||||||||
| 38 | 0 | ▶ | if ( exists $args{$key} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | $self->{$key} = $args{$key}; | |||||||||
| 40 | } else { | ||||||||||
| 41 | 0 | $self->{$key} = $data->{$key}; | |||||||||
| 42 | } | ||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | ### fetch the data ### | ||||||||||
| 46 | 0 | ▶ | sub dist { my $self = shift; $self->{dist} } | ||||||||
|
Subroutine
dist: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 47 | 0 | ▶ | sub ponies { my $self = shift; $self->{ponies} } | ||||||||
|
Subroutine
ponies: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 48 | |||||||||||
| 49 | 0 | return $self; | |||||||||
| 50 | |||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub _start { | ||||||||
|
Subroutine
_start: not called CC=1 SCAR=6.9
| |||||||||||
| 54 | 0 | my ($kernel, $heap, $name, $delay, $dist) = @_[KERNEL, HEAP, ARG0, ARG1, ARG2]; | |||||||||
| 55 | |||||||||||
| 56 | 0 | $heap->{name} = $name; | |||||||||
| 57 | 0 | $heap->{delay} = $delay; | |||||||||
| 58 | 0 | $heap->{dist} = $dist; | |||||||||
| 59 | |||||||||||
| 60 | 0 | printf "Starting pony %10s\n", $heap->{name}; | |||||||||
| 61 | 0 | $kernel->delay_add( run => rand($heap->{delay}) ); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 65 | 0 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | |||||||||
| 66 | 0 | printf "Pony %10s has reached stage %3i\n", $heap->{name}, ++$heap->{stage}; | |||||||||
| 67 | 0 | ▶ | die "$heap->{name} won the race!\n" if $heap->{stage} > $heap->{dist}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | $kernel->delay_add( run => rand($heap->{delay}) ); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | sub race { | ||||||||
|
Subroutine
race: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 72 | 0 | my $self = shift; | |||||||||
| 73 | |||||||||||
| 74 | 0 | for my $name (@{[keys %{$self->ponies()}]} ) { | |||||||||
| 75 | |||||||||||
| 76 | POE::Session->create ( | ||||||||||
| 77 | inline_states => { | ||||||||||
| 78 | _start => \&_start, | ||||||||||
| 79 | run => \&run, | ||||||||||
| 80 | }, | ||||||||||
| 81 | 0 | args => [ $name, $self->ponies()->{$name}, $self->dist() ], | |||||||||
| 82 | ); | ||||||||||
| 83 | } | ||||||||||
| 84 | 0 | $poe_kernel->run(); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 1 | q[POE::Knee!]; | |||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | __END__ | ||||||||||