1POE::Component(3) User Contributed Perl Documentation POE::Component(3)
2
3
4
6 POE::Component - event driven objects or subsystems
7
9 See specific components.
10
12 POE "components" are event-driven modules that generally encapsulate
13 mid- to high-level program features. For example,
14 POE::Component::Client::DNS performs message-based asynchronous
15 resolver lookups. POE::Component::Server::TCP is a basic asynchronous
16 network server.
17
18 The POE::Component namespace was started as place for contributors to
19 publish their POE-based modules without requiring coordination with the
20 main POE distribution. The namespace predates the -X convention,
21 otherwise you'd be reading about POEx instead.
22
23 As with many things in Perl, there is more than one way to implement
24 component interfaces. Newer components sport OO interfaces, and some
25 even use Moose, but older ones are solely message driven.
26
28 One way to create object-oriented components is to embed a POE::Session
29 instance within an object. This is done by creating the session during
30 the object's constructor, setting the session's alias to something
31 unique, and saving a copy of the alias in the object.
32
33 package Asynchrotron;
34
35 my $alias_index = 0;
36
37 sub new {
38 my $class = shift;
39 my $self = bless {
40 alias => __PACKAGE__ . " " . ++$alias_index;
41 }, $class;
42
43 POE::Session->create(
44 object_states => [
45 $self => {
46 _start => "_poe_start",
47 do_something => "_poe_do_something",
48 },
49 ],
50 );
51 return $self;
52 }
53
54 sub _poe_start {
55 $_[KERNEL]->alias_set($_[OBJECT]->{alias});
56 }
57
58 The alias allows object methods to pass events into the session without
59 having to store something about the session. The POE::Kernel call()
60 transfers execution from the caller session's context into the
61 component's session.
62
63 sub do_something {
64 my $self = shift;
65 print "Inside the caller's session right now: @_\n";
66 $poe_kernel->call($self->{alias}, "do_something", @_);
67 }
68
69 sub _poe_do_something {
70 my @args = @_[ARG0..$#_];
71 print "Inside the component's session now: @args\n";
72 $_[OBJECT]{count}++;
73 }
74
75 Both $_[HEAP] and $_[OBJECT] are visible within the component's
76 session. $_[HEAP] can be used for ultra-private encapsulation, while
77 $_[OBJECT] may be used for data visible by accessors.
78
79 sub get_count {
80 my $self = shift;
81 return $self->{count}; # $_[OBJECT]{count} above
82 }
83
84 Too many sessions may bog down object creation and destruction, so
85 avoid creating them for every object.
86
88 The SEE ALSO section in POE contains a table of contents covering the
89 entire POE distribution.
90
91 POE::Stage is a nascent project to formalize POE components, make
92 POE::Kernel more object-oriented, and provide syntactic and semantic
93 sugar for many common aspects of POE::Component development. It's also
94 easier to type. Please investigate the project. Ideas and tuits are
95 badly needed to help get the project off the ground.
96
98 Document the customary (but not mandatory!) process of creating and
99 publishing a component.
100
102 Each component is written and copyrighted separately.
103
104 Please see POE for more information about authors and contributors.
105
106
107
108perl v5.30.0 2019-07-26 POE::Component(3)