• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

examples/H28-Jul-2009-1,281789

lib/POE/H28-Jul-2009-4,1621,422

t/H28-Jul-2009-274205

CHANGESH A D28-Jul-20096.1 KiB187122

MANIFESTH A D28-Jul-2009949 3938

META.ymlH A D28-Jul-2009931 3433

Makefile.PLH A D28-Jul-20091.2 KiB4135

READMEH A D28-Jul-200917.2 KiB394295

README

1POE::Stage(3)         User Contributed Perl Documentation        POE::Stage(3)
2
3
4
5NNAAMMEE
6       POE::Stage - a base class for message-driven objects
7
8SSYYNNOOPPSSIISS
9               #!/usr/bin/env perl
10               {
11                       package App;
12                       use POE::Stage::App qw(:base);
13                       sub on_run {
14                               print "hello, ", my $arg_whom, "!\n";
15                       }
16               }
17               App->new()->run( whom => "world" );
18               exit;
19
20DDEESSCCRRIIPPTTIIOONN
21       POE::Stage is a set of base classes for message-driven objects.  It
22       cleanly implements standard patterns that have emerged from years of
23       working with POE and POE::Component modules.
24
25       As I hope the name implies, POE::Stage objects encapsulate discrete
26       steps, or stages, of a larger task.  Eventually they come together to
27       implement programs.
28
29       For example, HTTP requests are performed in four or so distinct stages:
30       1. The server's address is resolved.  2. The client establishes a
31       connection to the server.  3. The client transmits a request.  4. The
32       client receives a response.
33
34       By design, POE::Stage promotes the decomposition of tasks into
35       multiple, smaller stages.  If these stages are generic enough, new
36       tasks may be handled by reusing them in different configurations.
37
38       The hypothetical HTTP client might be a single stage composed of three
39       smaller ones:  A DNS resolver stage, which accepts DNS requests and
40       returns DNS responses.  A TCP client connection factory, which takes
41       socket endpoint descriptions and other parameters, and eventually
42       returns established connections.  Finally, there would be an HTTP
43       protocol stage that uses established connections to send requests and
44       parse responses.
45
46       These stages would be encapsulated by a higher-level HTTP client stage.
47       This would accept HTTP requests and return HTTP responses after
48       performing the necessary steps to gather them.
49
50       This will sound familiar to anyone working with objects.
51
52       These objects are asynchronous and message-driven, however.  The base
53       message class, POE::Request, and its subclasses, implement a standard
54       request/response interface between POE::Stage objects.  Where possible,
55       these messages attempt to mimic simpler, more direct call/return
56       syntax, albeit asynchronously.  POE::Stage also provides a powerful
57       closure-based system for maintaining request and response state, so you
58       don't have to.
59
60RREESSEERRVVEEDD MMEETTHHOODDSS
61       To do its job, POE::Stage requires some methods for its own.  To be
62       extensible, it reserves other methods for standard purposes.  To remain
63       useful, it reserves the least number of methods possible.
64
65       nneeww AARRGGUUMMEENNTT__PPAAIIRRSS
66
67       _n_e_w_(_) creates and returns a new POE::Stage object.  An optional set of
68       named ARGUMENT_PAIRS will be passed to the object's _i_n_i_t_(_) callback
69       before _n_e_w_(_) returns.
70
71       Subclasses should not override _n_e_w_(_) unless they're careful to call the
72       base POE::Stage's constructor.  Object construction is customized
73       through the _i_n_i_t_(_) callback instead.
74
75       iinniitt AARRGGUUMMEENNTT__PPAAIIRRSS
76
77       _i_n_i_t_(_) is a callback used to initialize POE::Stage objects after they
78       are constructed.  POE::Stage's _n_e_w_(_) constructor passes its named
79       ARGUMENT_PAIRS to _i_n_i_t_(_) prior to returning the new object.  The values
80       of these arguments will be available as $arg_name lexicals within the
81       _i_n_i_t_(_) callback:
82
83         my $object = POE::Stage::Something->new( foo => 123 );
84
85               package POE::Stage::Something;
86               sub init {
87                       print my $arg_foo, "\n";  # displays "123\n".
88               }
89
90       The _i_n_i_t_(_) callback is optional.
91
92       HHaannddlleerr
93
94       The Handler method implements an attribute handler that defines which
95       methods handle messages.  Only message handlers have access to the
96       closures that maintain state between messages.
97
98       The Handler method is used as a subroutine attribute:
99
100               sub some_method :Handler {
101                       # Lexical magic occurs here.
102               }
103
104               sub not_a_handler {
105                       # No lexical magic happens in this one.
106               }
107
108       Methods with names beginning with "on_" acquire Handler magic
109       automatically.
110
111               sub on_event {
112                       # Lexical magic occurs here.  No :Handler necessary.
113               }
114
115       eexxppoossee OOBBJJEECCTT,, LLEEXXIICCAALL [[,, LLEEXXIICCAALL[[,, LLEEXXIICCAALL ......]]]]
116
117       _e_x_p_o_s_e_(_) is a function (not a method) that allows handlers to expose
118       members of specific request or response OBJECT.  Each member will be
119       exposed as a particular LEXICAL variable.  OBJECTs must inherit from
120       POE::Request.
121
122       The LEXICAL's name is significant.  The part of the variable name up to
123       the leading underscore is treated as a prefix and ignored.  The
124       remainder of the variable name must match one of the OBJECT's member
125       names.  The sigil is also significant, and it is treated as part of the
126       member name.
127
128       The following example exposes the '$cookie' member of a POE::Request
129       object as the '$sub_cookie' lexical variable.  The exposed variable is
130       then initialized.  In doing so, the value stored into it is saved
131       within the request's closure.  It will be available whenever that
132       request (or a response to it) is visible.
133
134               use POE::Stage qw(expose);
135
136               sub do_request :Handler {
137                       my $req_subrequest = POE::Request->new( ... );
138                       expose $req_subrequest, my $sub_cookie;
139                       $sub_cookie = "stored in the subrequest";
140               }
141
142       LEXICAL prefixes are useful for exposing the same member name from
143       multiple OBJECTs within the same lexical scope.  Otherwise the variable
144       names would clash.
145
146UUSSIINNGG
147       TODO - Describe how POE::Stage is used.  Outline the general pattern
148       for designing and subclassing.
149
150DDEESSIIGGNN GGOOAALLSS
151       POE::Stage implements the most important and common design patterns for
152       POE programs in a consistent and convenient way.
153
154       POE::Stage hides nearly all of POE, including the need to create
155       POE::Session objects and explicitly define event names and their
156       handlers.  The :Handler subroutine attribute defines which methods
157       handle messages.  There's never a need to guess which message types
158       they handle:
159
160               # Handle the "foo" message.
161               sub foo :Handler {
162                       ...
163               }
164
165       POE::Stage simplifies message passing and response handling in at least
166       three ways.  Consider:
167
168               my $request = POE::Request->new(
169                       stage => $target_stage,
170                       method => $target_method,
171                       args => \%arguments,
172                       on_response_x => "handler_x",
173                       on_response_y => "handler_y",
174                       on_response_z => "handler_z",
175               );
176
177       First, it provides standard message clasess.  Developers don't need to
178       roll their own, potentially non-interoperable message-passing schemes.
179       The named \%arguments are supplied and are available to each handler in
180       a standard way, which is described later in the MAGICAL LEXICAL TOUR.
181
182       Second, POE::Stage provides request-scoped closures via $req_foo,
183       $rsp_foo, and _e_x_p_o_s_e_(_).  Stages use these mechanisms to save and access
184       data in specific request and response contexts, eliminating the need to
185       do it explicitly.
186
187       Third, response destinations are tied to the requests themselves.  In
188       the above example, responses of type "response_x" will be handled by
189       "handler_x".  The logic flow of a complex program is more readily
190       apparent.  It gets better, too.  See HANDLER NAMING CONVENTIONS.
191
192       The mechanisms of message passing and context management become
193       implicit, allowing them to be extended transparently.  This will be
194       extended across processes, hopefully with few or no seams.
195
196       POE::Stage includes object-oriented classes for low-level event
197       watchers.  They simplify and standardize POE::Kernel's interface, and
198       they allow watchers to be extended cleanly through normal OO
199       techniques.  The lifespan of each resource is tightly coupled to the
200       lifespan of each object, so ownership and relevance are clearly
201       indicated.
202
203       POE::Stage standardizes shutdown semantics for requests and stages.
204       Requests are canceled by destroying their objects, and stages are shut
205       down the same way.
206
207       POE::Stage simplifies the cleanup of complex, multi-stage activity.
208       Resources for a particular request should be stored within its closure.
209       Canceling the request triggers destruction of that closure and its
210       contents, which in turn triggers the destruction of the resources
211       allocated to that request.  These resources include stages and requests
212       created during the lifetime of the request.  They too are canceled and
213       freedm
214
215MMAAGGIICCAALL LLEEXXIICCAALL TTOOUURR
216       POE::Stage uses lexical aliasing to expose state data to message
217       handlers, which are specified by either the :Handler method attribute
218       or the use of an on_ prefix in the method's name.
219
220       Lexical variable prefixes indicate the data's origin.  For example,
221       $arg_name is the "name" argument included with a message:
222
223               my $request = POE::Request->new(
224                       method => "something",
225                       args => { name => "ralph" },
226                       ...,
227               );
228
229               sub something :Handler {
230                       my $arg_name;  # already contains "ralph"
231               }
232
233       The full list of prefixes and data sources:
234
235       TThhee ""aarrgg__"" lleexxiiccaall pprreeffiixx,, ee..gg..,, $$aarrgg__ffoooo
236
237       Argument (parameter) "xyz".  If an "args" parameter is passed to a
238       POE::Request constructor, its value must be a reference to a hash.
239       Usually it's an anonymous hashref.  Anyway, the hash's members are
240       named arguments to the message handler.  See above for an example.
241
242       TThhee ""rreeqq__"" lleexxiiccaall pprreeffiixx,, ee..gg..,, $$rreeqq__ffoooo
243
244       An incoming request may trigger more than one handler, especially if a
245       POE::Stage object calls itself, or sends sub-requests to a helper
246       stage.  The "req_" lexical prefix refers to data members within the
247       current request's scope.  Their values will magically reflect the
248       proper request scope, regardless what that is.
249
250       TODO - Example.
251
252       TThhee ""sseellff__"" lleexxiiccaall pprreeffiixx,, ee..gg..,, $$sseellff__ffoooo
253
254       The "self" scope refers to the currently active POE::Stage object.
255       Data may be stored there, in which case it's available from any and all
256       requests handled by that object.  This scope is useful for "singleton"
257       or static data that must be shared between or persistent between all
258       requests.
259
260       TODO - Example
261
262       TThhee ""rrsspp__"" lleexxiiccaall pprreeffiixx,, ee..gg..,, $$rrsspp__ffoooo
263
264       The "rsp" scope refers to data stored in a sub-request's scope, but
265       from the response handler's point of view.  That is, when persisting
266       data between a request to a substage and its response, one should store
267       the data in the substage's request, then retrieve it later from the
268       corresponding "rsp" variable.
269
270       TODO - Example.
271
272       TThhee $$sseellff, $req, and $rsp lexicals
273
274       Certain variables are standard:  $self refers to the current object; it
275       need not be initialized from @_.  $req refers to the higher-level
276       request we're currently handling.  When handling responses from
277       substages, $rsp refers to those responses.
278
279       All three variables are intended as invocatnts for method calls.  Other
280       prefixes exist to access data members within each object's scope.
281
282       TODO - Example.
283
284       The techniques used here have been abstracted and released as
285       Lexical::Persistence.
286
287HHAANNDDLLEERR NNAAMMIINNGG CCOONNVVEENNTTIIOONNSS
288       Message handlers are defined in one of two ways.  They may be named
289       anything as long as they have a :Handler attribute, or they may be
290       prefixed with "on_".  In both cases, they gain lexical persistence
291       magic, as discussed previously.
292
293               # Handle the "foo" message.
294               sub foo :Handler { ... }
295
296               # Handle the "on_foo" and "foo" messages.
297               sub on_foo { ... }
298
299       The _o_n___f_o_o_(_) method above handles both "on_foo" and "foo" messages.
300       Given both a _f_o_o_(_) and an _o_n___f_o_o_(_), however, _o_n___f_o_o_(_) will take
301       precedence.
302
303       Requests include on_* parameters that map response types to response
304       handlers.  For example, this request expects two return types,
305       "success" and "failure".  On success, the _h_a_n_d_l_e___s_u_c_c_e_s_s_(_) method is
306       called.  On failure, _h_a_n_d_l_e___f_a_i_l_u_r_e_(_) is called.
307
308               my $req_subrequest = POE::Request->new(
309                       ...,
310                       on_success => "handle_success",
311                       on_failure => "handle_failure",
312               );
313
314       Response types are specified by the "type" parameter to $req->_e_m_i_t_(_)
315       and $req->_r_e_t_u_r_n_(_).  "emit" and "return" are the default types for
316       _e_m_i_t_(_) and _r_e_t_u_r_n_(_), respectively.
317
318       Requests can also have roles, which are usually descriptive of the
319       transaction.  For example, consider a DNS request for a web client
320       component:
321
322               my $req_resolve = POE::Request->new(
323                       ...,
324                       role => "resolver",
325               );
326
327       This is the role of the request, not of the stage that will handle it.
328       In this case, there are no on_* parameters.  Success and failure come
329       back to methods named "on_" . $request_role . "_" . $response_type.  In
330       the previous example, they are:
331
332               sub on_resolver_success { ... }
333               sub on_resolver_failure { ... }
334
335       When subclassing a POE::Stage class, it's sometimes useful to intercept
336       _e_m_i_t_(_) and _r_e_t_u_r_n_(_) messages.  The subclass may implement handlers
337       directly, or it may override or extend the response.  This is done by
338       defining "on_my_" . $response_type methdos in the subclass.  For
339       example, a TCP connection stage might emit an "input" event, like so:
340
341               sub on_socket_readable {
342                       ...;
343                       $req->emit( type => "input", input => $data );
344               }
345
346       A subclass might implement the code to handle the input.  It can do so
347       by defining _o_n___m_y___i_n_p_u_t_(_):
348
349               sub on_my_input {
350                       # send a response here
351               }
352
353       Messages intercepted like this will not be rethrown automatically to
354       the caller.  If that's desired, _o_n___m_y___i_n_p_u_t_(_) will need to _e_m_i_t_(_) or
355
356       TODO - Make a better example.  Something that can tie all these things
357       together conceptually.
358
359BBUUGGSS
360       POE::Stage is not ready for production.  Check back here early and
361       often to find out when it will be.  Please contact the author if you
362       would like to see POE::Stage production-ready sooner.
363
364BBUUGG TTRRAACCKKEERR
365       https://rt.cpan.org/Dist/Display.html?Status=Active&Queue=POE-Stage
366
367RREEPPOOSSIITTOORRYY
368       http://thirdlobe.com/svn/poe-stage/
369
370OOTTHHEERR RREESSOOUURRCCEESS
371       http://search.cpan.org/dist/POE-Stage/
372
373SSEEEE AALLSSOO
374       POE::Stage is the base class for message-driven objects.  POE::Request
375       is the base class for POE::Stage messages.  POE::Watcher is the base
376       class for event watchers.
377
378       <http://thirdlobe.com/projects/poe-stage/> - POE::Stage is hosted here.
379
380       <http://www.eecs.harvard.edu/~mdw/proj/seda/> - SEDA, the Staged Event
381       Driven Architecture.  It's Java, though.
382
383AAUUTTHHOORRSS
384       Rocco Caputo.
385
386LLIICCEENNSSEE
387       POE::Stage is Copyright 2005-2009 by Rocco Caputo.  All rights are
388       reserved.  You may use, modify, and/or distribute this module under the
389       same terms as Perl itself.
390
391
392
393perl v5.10.0                      2009-07-28                     POE::Stage(3)
394