Home > Archive > PERL POE > June 2005 > How to modify HEAP variables in subs...
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 |
How to modify HEAP variables in subs...
|
|
|
| Hi, please sorry, i`m newbie in POE and have a lot of
problems...
Why doesn`t variables of HEAP changed?
There is a little chunk of program(2 subs):
sub one {
$_[HEAP]->{VARIABLE}=0;
for(1..4)
{
$_[KERNEL]->yield("two");
}
print "VARIABLE: ",$_[HEAP]->{VARIABLE};
}
sub two {
$_[HEAP]->{VARIABLE}++;
}
It will print "VARIABLE: 0", but why? I`ve updated it in
'two', it must be 1(cause of ++).
---
Professional hosting for everyone - http://www.host.ru
| |
| Pronichev Alexander 2005-06-09, 4:00 am |
| Hi, I think you have to use
$_[KERNEL]->call($_[SESSION],"two"); instead of
$_[KERNEL]->yield("two");
See "Synchronous Events" in perldoc POE::Kernel
On Mon, 16 May 2005 15:06:51 +0400
"Laura" <laura@zmail.ru> wrote:
> Hi, please sorry, i`m newbie in POE and have a lot of
> problems...
>
> Why doesn`t variables of HEAP changed?
> There is a little chunk of program(2 subs):
>
> sub one {
>
> $_[HEAP]->{VARIABLE}=0;
> for(1..4)
> {
> $_[KERNEL]->yield("two");
> }
> print "VARIABLE: ",$_[HEAP]->{VARIABLE};
>
> }
>
> sub two {
> $_[HEAP]->{VARIABLE}++;
> }
>
> It will print "VARIABLE: 0", but why? I`ve updated it in
> 'two', it must be 1(cause of ++).
> ---
> Professional hosting for everyone - http://www.host.ru
--
WBR dyker
Agava Software
| |
| Pronichev Alexander 2005-06-09, 4:00 am |
| Have you read perldoc POE::Kernel "Synchronous Events" section? :)
The point is that the post() method means put the message in POE queue and POE will handle it (maybe later) using FIFO. So you may write:
$_[KERNEL]->call($LADBI_ALIAS => "selectall",....);
call() methods means bypass FIFO and send event "right now".
On Mon, 16 May 2005 17:22:47 +0400
"Laura" <laura@zmail.ru> wrote:
> On Mon, 16 May 2005 15:21:25 +0400
> Pronichev Alexander <dyker@agava.com> wrote:
>
> Alexander, Thank you very much!
> This simple example is working, but if i try to add for
> example LaDBI post inside called sub, it doesn`t chages
> again.
>
> sub one {
>
> $_[HEAP]->{VARIABLE}=0;
> for(1..4)
> {
> $_[KERNEL]->yield("two");
> }
> print "VARIABLE: ",$_[HEAP]->{VARIABLE};
>
> }
>
> sub two {
> $_[KERNEL]->post{post($LADBI_ALIAS => "selectall",
> SuccessEvent => "display_results",
> HandleId => $dbh_id,
> Args => [ $_[HEAP]->{sql} ] );
> }
>
> sub display_results {
> $_[HEAP]->{VARIABLE}++;
> }
>
>
> Spasib.
> ---
> Professional hosting for everyone - http://www.host.ru
--
WBR dyker
Agava Software
| |
| Pronichev Alexander 2005-06-09, 4:00 am |
| On Mon, 16 May 2005 19:21:36 +0400
"Laura" <laura@zmail.ru> wrote:
> Yes, I use call, but SuccessEvent then post to next event,
> that don`t work :(
Why can't you use call() instead of post() in SuccessEvent?
>
> thank you very much, Aleksandr!
>
>
> On Mon, 16 May 2005 18:55:02 +0400
> Pronichev Alexander <dyker@agava.com> wrote:
>
> ---
> Professional hosting for everyone - http://www.host.ru
--
WBR dyker
Agava Software
| |
| Gabriel Kihlman 2005-06-09, 4:00 am |
| "Laura" <laura@zmail.ru> writes:
>
> Why doesn`t variables of HEAP changed?
> There is a little chunk of program(2 subs):
>
> sub one {
>
> $_[HEAP]->{VARIABLE}=0;
> for(1..4)
> {
> $_[KERNEL]->yield("two");
> }
> print "VARIABLE: ",$_[HEAP]->{VARIABLE};
>
> }
>
> sub two {
> $_[HEAP]->{VARIABLE}++;
> }
>
> It will print "VARIABLE: 0", but why? I`ve updated it in
> 'two', it must be 1(cause of ++).
In one() you schedule an event to run four times. When you return from
one() you give control back to POE. POE can then process the queue and
dispatch the four 'two'-events.
events are not object methods..
| |
| Matt S Trout 2005-06-09, 4:00 am |
| On Mon, May 16, 2005 at 03:06:51PM +0400, Laura wrote:
> Hi, please sorry, i`m newbie in POE and have a lot of
> problems...
>
> Why doesn`t variables of HEAP changed?
> There is a little chunk of program(2 subs):
>
> sub one {
>
> $_[HEAP]->{VARIABLE}=0;
> for(1..4)
> {
> $_[KERNEL]->yield("two");
> }
> print "VARIABLE: ",$_[HEAP]->{VARIABLE};
>
> }
>
> sub two {
> $_[HEAP]->{VARIABLE}++;
> }
>
> It will print "VARIABLE: 0", but why? I`ve updated it in
> 'two', it must be 1(cause of ++).
Er, no. "yield" means "put this event in the queue to be run", so your
"two" events aren't going to get called until after the "one" event
exits.
Replace the print with
$_[KERNEL]->yield("three");
and add
sub three {
print "VARIABLE: ",$_[HEAP]->{VARIABLE};
}
and I think you'll see the result you expect.
--
Matt S Trout Website: http://www.shadowcatsystems.co.uk
Technical Director E-mail: mst (at) shadowcatsystems.co.uk
Shadowcat Systems Ltd.
| |
| Matt S Trout 2005-06-09, 4:00 am |
| Note: This is a mailing list. Please hit "Reply All" so your mail goes to
the list as well otherwise my replies won't make sense to somebody browsing
the archives with the same problem later.
On Mon, May 16, 2005 at 07:02:19PM +0400, Laura wrote:
> Thank you very much! Now I know the mistake, i just need
> to use call instead of yield, but when i try for example
> to 'post' LaDBI, and increment($_[HEAP]->{VARIABLE}++) in
> next sub, it doesn`t work :(
No, you don't need to use call. You're still attempting to write a simple
linear system in an event-driven framework.
post will put an event on LaDBI's queue
LaDBI will then handle that in the background over the course of a while,
and post an event back to your session with the results.
It's a state machine - think how button clicks by the user fire events in
most GUI systems. This is kinda the same except you get to fire the events
from your code yourself.
What you want is to have another event handler that gets the reply from
LaDBI and does something useful with it - then you can yield from that
to increment your counter.
Same thing on disconnect - you can't decrement the connection counter until
you have *receieved* the disconnect acknowledgement event from LaDBI, which
means registering something to handle that event and waiting for LaDBI to
send it.
If you're not clear on the event-driven programming concepts, a quick google
for "event driven programming" provided a bunch of tutorials that might
help you there.
If it's any consolation, POE took me a while to wrap my brain around too :)
--
Matt S Trout Website: http://www.shadowcatsystems.co.uk
Technical Director E-mail: mst (at) shadowcatsystems.co.uk
Shadowcat Systems Ltd.
| |
|
| Hi, thank you.
I can`t do it in another event, cause i have variables in
main event that i need to save...
sub one {
....
foreach(keys %{$links})
{
$k={$#links}=$heap->{VARIABLE};
$_[KERNEL]->yield("next_event",@{$links}{$_}); # then i
need to insert that data into DB and increment
$heap->{VARIABLE}++. Then return that variable back and
save in $k next iteration.
}
}
thx
> Note: This is a mailing list. Please hit "Reply All" so
>your mail goes to
> the list as well otherwise my replies won't make sense
>to somebody browsing
> the archives with the same problem later.
>
> On Mon, May 16, 2005 at 07:02:19PM +0400, Laura wrote:
>
> No, you don't need to use call. You're still attempting
>to write a simple
> linear system in an event-driven framework.
>
> post will put an event on LaDBI's queue
>
> LaDBI will then handle that in the background over the
>course of a while,
> and post an event back to your session with the results.
>
> It's a state machine - think how button clicks by the
>user fire events in
> most GUI systems. This is kinda the same except you get
>to fire the events
> from your code yourself.
>
> What you want is to have another event handler that gets
>the reply from
> LaDBI and does something useful with it - then you can
>yield from that
> to increment your counter.
>
> Same thing on disconnect - you can't decrement the
>connection counter until
> you have *receieved* the disconnect acknowledgement
>event from LaDBI, which
> means registering something to handle that event and
>waiting for LaDBI to
> send it.
>
> If you're not clear on the event-driven programming
>concepts, a quick google
> for "event driven programming" provided a bunch of
>tutorials that might
> help you there.
>
> If it's any consolation, POE took me a while to wrap my
>brain around too :)
>
> --
> Matt S Trout Website:
>http://www.shadowcatsystems.co.uk
> Technical Director E-mail: mst (at)
>shadowcatsystems.co.uk
> Shadowcat Systems Ltd.
---
Professional hosting for everyone - http://www.host.ru
| |
| Matt S Trout 2005-06-09, 4:00 am |
| On Tue, May 17, 2005 at 05:46:13PM +0400, Laura wrote:
> Hi, thank you.
> I can`t do it in another event, cause i have variables in
> main event that i need to save...
>
> sub one {
>
> ...
>
> foreach(keys %{$links})
> {
> $k={$#links}=$heap->{VARIABLE};
> $_[KERNEL]->yield("next_event",@{$links}{$_}); # then i
> need to insert that data into DB and increment
> $heap->{VARIABLE}++. Then return that variable back and
> save in $k next iteration.
> }
>
> }
Why not just have the event that handles the return from the database do
the work with the data? If you need information from the main event then
either stick it in the HEAP or pass it along as an argument to the yield.
--
Matt S Trout Website: http://www.shadowcatsystems.co.uk
Technical Director E-mail: mst (at) shadowcatsystems.co.uk
Shadowcat Systems Ltd.
| |
|
| I can pass it along as an argument, but then event will
$_[KERNEL]->post(LADBI where i can`t pass that ARG...
thx
> On Tue, May 17, 2005 at 05:46:13PM +0400, Laura wrote:
>
> Why not just have the event that handles the return from
>the database do
> the work with the data? If you need information from the
>main event then
> either stick it in the HEAP or pass it along as an
>argument to the yield.
>
> --
> Matt S Trout Website:
>http://www.shadowcatsystems.co.uk
> Technical Director E-mail: mst (at)
>shadowcatsystems.co.uk
> Shadowcat Systems Ltd.
---
Professional hosting for everyone - http://www.host.ru
| |
|
| >stick it in the HEAP
foreach($links)
{
$_[KERNEL]->yield...
$heap->{stick}=$#links;
}
so in the next event $heap->{stick} will be last value of
$#links...
....i`m panic-stricken ;((
---
Professional hosting for everyone - http://www.host.ru
| |
| Matt S Trout 2005-06-09, 4:00 am |
| On Tue, May 17, 2005 at 06:19:40PM +0400, Laura wrote:
> I can pass it along as an argument, but then event will
> $_[KERNEL]->post(LADBI where i can`t pass that ARG...
LaDBI's events all take a UserData argument for just this purpose.
Have another look at the LaDBI docs :) -
sub success_event_handler {
...
my ($handle_id, $datatype, $data, $userdata) = @_[ARG0..ARG3];
...
}
$userdata is whatever you passed as UserData => to the original LaDBI post.
That do the trick?
--
Matt S Trout Website: http://www.shadowcatsystems.co.uk
Technical Director E-mail: mst (at) shadowcatsystems.co.uk
Shadowcat Systems Ltd.
|
|
|
|
|