On Thu, Aug 28, 2003 at 06:09:42AM -0500, Jake wrote:
> I must not be explaining myself, or it's so basic people are overlooking the
> obvious, thinking I want to do something more complicated.
>
> Here is what I want to do.
>
> I want to pass reference (my own) to one of the events. That way within the
> event I have access to reference passed.
>
> In non-poe code I'd do:
>
> sub one {
> my $data;
> two(\$data);
> }
>
> sub two {
> my $data = shift;
> $$data = "I'm setting it here"
> }
>
>
> In POE I'd like accomplish the same but I don't know how to get put an extra
> parameter in to the parameter list sent by an event. I tried this (but
> obviously it doesn't work):
>
> sub one {
> my $data;
>
> my $poe = POE::Component::Client::TCP->new(
> ...
> ## Notice the reference to $data in the line below
> ServerInput => \&serverResponse(\$data)
> );
> }
>
> sub serverResponse {
> # HERE I'd like to get at the reference to $data
> }
This goes a long way toward explaining your intention. There are at
least two ways to do it.
First, you can set up a Started handler, which will be called back when
the client component has started. When it is called, the contents of
the Args parameter will be passed to it as @_[ARG0..$#_].
There are at least two ways to do it:
sub one {
my $data;
my $client = POE::Component::Client::TCP->new(
...,
ServerInput => \&serverResponse,
Started => \&serverStarted,
Args => [ \$data ],
);
}
sub serverStarted {
my $data_ref = $_[ARG0];
...,
# And if you want it available from serverStarted:
$_[HEAP]->{data_ref} = $data_ref;
}
# Once it's in the "heap", it is available to all event handlers in
# the same session. Other sessions have their own, different heaps,
# so they will see different (or no) values for it.
sub serverResponse {
my $data_ref = $_[HEAP]->{data_ref};
}
Another way is to use a closure, which requires an inline anonymous sub
for the ServerInput handler. Closures can be difficult to work with,
and in this case will double the function-call overhead for each unit of
server input.
sub one {
my $data;
my $client = POE::Component::Client::TCP->new(
...,
ServerInput => sub { serverResponse($data, $_[ARG0]) },
);
}
As a side effect of the above example, your serverResponse() handler
will only receive two parameters:
my ($data_ref, $input) = @_;
If it needs access to a HEAP or KERNEL, it must get them elsewhere, or
you must pass them in yourself.
Finally, this is not related to your problem at hand, but it's a way to
pass information from one event handler to another without using a HEAP.
$kernel->post(some_session => some_event => \$data);
$kernel->yield(some_event => \$data);
sub some_event_handler {
my $data_ref = $_[ARG0];
...,
}
The yield() method is just a poorly named version of post() that assumes
"some_session" is the same one it's called from.
--
Rocco Caputo - [EMAIL PROTECTED] - http://poe.perl.org/