Re: Accessors

Rohan Almeida <[email protected]>
Newsgroups gmane.comp.lang.perl.poe
Organization Aarohan Technologies
Message-ID <[email protected]>
For simple accessors, you can also use Class::Accessor

package PoeWithAccessors;
use strict;
use warnings;
use base qw/ Class::Accessor /;

use POE::Session;

__PACKAGE__->mk_accessors(
     qw/
           state
           connection
           client_ip
       /
);

sub spawn
{
     my ( $class, $params_ref ) = @_;

     my $self = {};
     bless $self, $class;

     # initial state
     $self->state('CONNECTED');

     # client IP address
     $self->client_ip( $params_ref->{client_ip} );

     # Create a new POE Session
     POE::Session->create(
         object_states => [
             $self => [
                 qw/
                     _start got_data logged_out _stop
                   /
             ]
         ],
         heap => {
               %{$params_ref},
           },
       );

     return 1;
}

sub got_data
{
     my ( $kernel, $heap, $session, $self )
       = @_[ KERNEL, HEAP, SESSION, OBJECT ];


     $self->state('GOT_DATA');
}


Hope the above example makes sense :) Note OBJECT which gives you 
your blessed ref in your event handlers.

-- 
Rohan


John R. wrote:
>  From the 1.0 documentation (very good by the way):
> 
> "call() returns the value returned by the EVENT_NAME handler.  It can do
> this because the handler is invoked before call() returns.  call() can
> therefore be used as an accessor, although there are better ways to
> accomplish simple accessor behavior."
> 
> (from POE::Kernel)
> 
> I don't see reference to the better ways so they probably are standard 
> perlish ways of doing things that I am having difficulty figuring out. 
> What other ways are there to accomplish accessors that provide access to 
> POE things like the heap?  I am doing calls within a session and between 
> sessions.  Should I pass the heap, kernel, etc as arguments to regular 
> sub calls?
> 
> Thanks,
> 
> John
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.