Debian Codegraph @

lib/Acme/POE/Knee.pm

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package Acme::POE::Knee;
2 BEGIN {
3 $Acme::POE::Knee::VERSION = '1.12';
4 }
5
6 #ABSTRACT: Time sliced pony race using the POE kernel.
7
8 use strict;
9 use POE;
10
11 sub new {
12 my $class = shift;
13 my %args = @_;
14 my $self = { };
15 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 for my $key ( keys %args ) {
28 unless( exists $data->{$key} ) {
29 print "WARNING! Option $key is not supported in $class!\n";
30 }
31 }
32
33 ### bless the object into the class ###
34 bless $self, $class;
35
36 ### now we start adding the data ###
37 for my $key (keys %$data) {
38 if ( exists $args{$key} ) {
39 $self->{$key} = $args{$key};
40 } else {
41 $self->{$key} = $data->{$key};
42 }
43 }
44
45 ### fetch the data ###
46 sub dist { my $self = shift; $self->{dist} }
47 sub ponies { my $self = shift; $self->{ponies} }
48
49 return $self;
50
51 }
52
53 sub _start {
54 my ($kernel, $heap, $name, $delay, $dist) = @_[KERNEL, HEAP, ARG0, ARG1, ARG2];
55
56 $heap->{name} = $name;
57 $heap->{delay} = $delay;
58 $heap->{dist} = $dist;
59
60 printf "Starting pony %10s\n", $heap->{name};
61 $kernel->delay_add( run => rand($heap->{delay}) );
62 }
63
64 sub run {
65 my ($kernel, $heap) = @_[KERNEL, HEAP];
66 printf "Pony %10s has reached stage %3i\n", $heap->{name}, ++$heap->{stage};
67 die "$heap->{name} won the race!\n" if $heap->{stage} > $heap->{dist};
68 $kernel->delay_add( run => rand($heap->{delay}) );
69 }
70
71 sub race {
72 my $self = shift;
73
74 for my $name (@{[keys %{$self->ponies()}]} ) {
75
76 POE::Session->create (
77 inline_states => {
78 _start => \&_start,
79 run => \&run,
80 },
81 args => [ $name, $self->ponies()->{$name}, $self->dist() ],
82 );
83 }
84 $poe_kernel->run();
85 }
86
87 q[POE::Knee!];
88
89
90 __END__
91 =pod
92
93 =head1 NAME
94
95 Acme::POE::Knee - Time sliced pony race using the POE kernel.
96
97 =head1 VERSION
98
99 version 1.12
100
101 =head1 SYNOPSIS
102
103 #!/usr/bin/perl -w
104 use strict;
105
106 # Use POEny!
107 use Acme::POE::Knee;
108
109 # Every Acme::POE::Knee race will require a set of arguments.
110 # There are defaults but it's just more fun to set these
111 # yourselves. We set a distance the ponies must run and of course
112 # we name our race ponies! You'll have to specify the maximum
113 # delay a pony can have before reaching the next stage.
114 # The lower the delay, the higher the chances are the pony will
115 # win the race.
116
117 my $pony = new Acme::POE::Knee (
118 dist => 20,
119 ponies => {
120 'dngor' => 5,
121 'Abigail' => 5.2,
122 'Co-Kane' => 5.4,
123 'MJD' => 5.6,
124 'acme' => 5.8,
125 },
126 );
127
128 # start the race
129 $pony->race( );
130
131 exit;
132
133 =head1 DESCRIPTION
134
135 POE::Knee is an acronym of "Pony". We all like ponies. And wouldn't we
136 love to race ponies? Well, that's what Acme::POE::Knee is for!
137
138 It's great for those friday afternoons at the office, where you wonder
139 who will pay the beer tab. Whoever 'wins' the race, loses!
140
141 You specify a distance the ponies must run, and a maximum delay before
142 the pony will reach the next step. So, the bigger the delay, the bigger
143 the distance between multiple ponies can be.
144
145 Of course this wouldn't be any fun if we couldn't name the ponies
146 ourselves. Here, we simply put all our race ponies in an array
147 reference and the Acme::POE::Knee module will take care of the rest.
148
149 =for Pod::Coverage new
150 race
151 dist
152 ponies
153 run
154
155 =head1 QUICK LINKS
156
157 Please see the samples directory in POE's distribution for several
158 well-commented sample and tutorial programs.
159
160 Please see <http://www.perl.com/pub/2001/01/poe.html> for an excellent,
161 and more importantly: gradual, introduction to POE.
162
163 =head1 USING Acme::POE::Knee
164
165 Using Acme::POE::Knee is really easy.
166 This simple progam would already suffice:
167
168 use strict;
169 use Acme::POE::Knee;
170
171 my $pony = new Acme::POE::Knee;
172 $pony->race();
173 exit;
174
175 This will use the defaults of the POE::Knee module, but you can of
176 course specify your own arguments, as shown in the synopsis.
177
178 =head1 The Use of Acme::POE::Knee
179
180 Use, yes... Usefull? Probably not. This was written in response to a
181 rather persistant meme on #perl (you know who you are!).
182 Basicly, we all wanted ponies.
183 Well folks, here it is.
184
185 Its source might be interesting to look at for newcomers to POE to see
186 how this time slicing works.
187
188 =head1 Learning more about POE
189
190 =over 2
191
192 =item The POE Mailing List
193
194 POE has a mailing list at perl.org. You can receive subscription
195 information by sending e-mail:
196
197 To: [email protected]
198 Subject: (anything will do)
199
200 The message body is ignored.
201
202 All forms of feedback are welcome.
203
204 =item The POE Web Site
205
206 POE has a web site where the latest development snapshot, along with
207 the Changes file and other stuff may be found: <http://poe.perl.org/>
208
209 =back
210
211 =head1 AUTHORS
212
213 =over 4
214
215 =item *
216
217 Jos Boumans <[email protected]>
218
219 =item *
220
221 Rocco Caputo <[email protected]>
222
223 =back
224
225 =head1 COPYRIGHT AND LICENSE
226
227 This software is copyright (c) 2011 by Jos Boumans.
228
229 This is free software; you can redistribute it and/or modify it under
230 the same terms as the Perl 5 programming language system itself.
231
232 =cut
233