For Programmers: Free Programming Magazines  


Home > Archive > PERL POE > January 2008 > objects passing and receiving events









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author objects passing and receiving events
Dave Schwartz

2008-01-23, 11:09 pm

Hi,

I stumbled upon this excellent framework while researching the net for
some work. I cant wait to start using it.
However, I am facing some hiccups on how best I should go about my needs.

In a simplified scenario of what I want to do, there are multiple perl
objects that need to talk to each other in a state machine sort of
way.
Esentially, objects should be able to send events and receive events.

In order to implement this:
A) I am thinking maybe I can simply pass the $kernel object created in
the main perl script to all my objects being created. This way all
my objects can post events since they will have $kernel to call the
yield() method.

Or
B) Should I follow a Wheel/Component strategy to do this. Also is
there a simple example to create component/wheel.
The ones in the cookbook are good on using the existing components and wheels.

Gracias,
Dece
Daisuke Maki

2008-01-23, 11:09 pm


no need to write that much code.
$kernel is globally available via $poe_kernel:

use POE; # automatically exports $poe_kernel

sub object_method {
$poe_kernel->post('foo' => 'bar');
}

to receive events, one way is to do something like

sub spawn {
my $class = shift;
my $self = $class->new(@_);

my $session = POE::Session->create(
object_states => [
$self => {
event => '_event_method'
}
]
)

# save value in case you want to send yourself an event
$self->{session_id} = $session->ID

$self;
}


sub method {
# the actual method
}

sub _event_method {
my ($self, $kernel) = @_[OBJECT, KERNEL];

$self->method();
}

Dave Schwartz wrote:
> Hi,
>
> I stumbled upon this excellent framework while researching the net for
> some work. I cant wait to start using it.
> However, I am facing some hiccups on how best I should go about my needs.
>
> In a simplified scenario of what I want to do, there are multiple perl
> objects that need to talk to each other in a state machine sort of
> way.
> Esentially, objects should be able to send events and receive events.
>
> In order to implement this:
> A) I am thinking maybe I can simply pass the $kernel object created in
> the main perl script to all my objects being created. This way all
> my objects can post events since they will have $kernel to call the
> yield() method.
>
> Or
> B) Should I follow a Wheel/Component strategy to do this. Also is
> there a simple example to create component/wheel.
> The ones in the cookbook are good on using the existing components and wheels.
>
> Gracias,
> Dece
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com