On Thu, 2004-01-29 at 19:33, Tim Wood wrote:
> 1. Can one send events from one thread (that has not called
> POE::Kernel->run()) to a POE loop running in another thread?  Let's
> say thread A uses POE, creates a POE::Session, saves $_[KERNEL] in a
> global, then calls POE::Kernel->run().  Thread B waits for the saved
> Kernel reference to appear (polling loop OK), and calls
> $savedKernelRef->post(...).   Will this work?  If not, what is the
> technique for messaging a POE loop from outside POE?  Is that allowed?
> 
> 2. How do I get a reference to "self" inside an event handler?  I
> found the following doesn't work:
>       sub myEventHandler {
>               my $self = shift;
>               doSomething($_[ARG0]);  # WRONG; ARG0 doesn't point to orig. 1st
> argument anymore
>       ...
> Instead I've been saving a reference to the object that defines the
> session & event handlers in $_[HEAP]->{'myPOEObject'}.  Is that the
> best way?  

# object method (constructor)
sub new {
     my $type = ref($_[0]) ? ref(shift) : shift;
     # ... set up your data
     my $self = { interval => 5 };
     bless $self, $type;
}

# object method, starts POE session, returns it
sub poe_session {
    my $self = shift;
    POE::Session->create(
        object_states => [qw( _start _stop process_submissions )]
    )
}

# POE events
sub _start { $_[KERNEL]->yield('process_submissions'); }
sub _stop { print "all done\n"; }

# can submit events regardless of POE session status
# object method
sub submit_event {
    my( $self, $submission ) = @_;
    push( @{$self->{events}}, $submission );
}

# rips through the list of submitted events when POE
# is running
# POE event
sub process_submissions {
    my( $self, $kernel ) = @_[ OBJECT, KERNEL ];
    while ( my $submission = shift @{$self->{events}} ) {
         # do something with them
    }
    $kernel->delay( 'process_submissions', $self->{interval} );
}





** ** **  PRIVILEGED AND CONFIDENTIAL  ** ** **
This email transmission contains privileged and confidential information 
intended only for the use of the individual or entity named above.  Any 
unauthorized review, use, disclosure or distribution is prohibited and 
may be a violation of law.  If you are not the intended recipient or a 
person responsible for delivering this message to an intended recipient, 
please delete the email and immediately notify the sender via the email 
return address or mailto:[EMAIL PROTECTED]  Thank you.

Reply via email to