Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^6: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help

by xtpu2 (Acolyte)
on Apr 09, 2014 at 17:05 UTC ( #1081694=note: print w/replies, xml ) Need Help??

in reply to Re^5: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
in thread PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help

So this is the final version of the timer code that works:

... my $w_cond = AnyEvent->condvar; $w_cond->cb( sub { warn $_[0]->recv } ); my $w; $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { undef $w if (0); $session->write('5 seconds have passed'); $w_cond->send(); } ); ...

I changed undef $w; to undef $w if (0);. Now the big question is why does this work, but simply commenting out undef $w; causes the timer to not work? Is there a logical reason here, or is it voodoo? By "voodoo" I mean, code that has to be typed a certain way "just because".

Replies are listed 'Best First'.
Re^7: PSGI, Plack, Twiggy, AnyEvent and SockJS... I need help
by Corion (Pope) on Apr 09, 2014 at 17:27 UTC

    Now is the moment you want to read about "closures" in Perl. That is why I told you about "mentioning" $w in the subroutine or making $w a global variable.

      Interesting! I didn't quite understand what you meant about "mentioning" the variable first time around. Just now, I replaced undef $w if (0); with $w; and other than a warning about Useless use of private variable in void context, it worked fine. Is there a best practice for how to "mention" variables in this kind of context?

      For now, off to read about closures.

      Edited to add after reading about closures: So basically, the reason I want to mention the variable $w within the callback subroutine is so that the variable becomes part of the lexical scope of the closure? I'm not sure if that's the correct terminology... But basically, unless the variable is somehow being used within the subroutine, it will be destroyed once all the callbacks and event handlers have been set up?

        Exactly - once the value goes out of scope, it gets released. You want to prevent this and release all references to the value when you want to stop the timer (if ever).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081694]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2018-10-22 12:54 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (119 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!