diff --git a/1to4-Nonblocking-Streaming/bin/five-times-evented.psgi b/1to4-Nonblocking-Streaming/bin/five-times-evented.psgi index 2ea48ef..4d83aea 100644 --- a/1to4-Nonblocking-Streaming/bin/five-times-evented.psgi +++ b/1to4-Nonblocking-Streaming/bin/five-times-evented.psgi @@ -2,18 +2,19 @@ use AnyEvent; use warnings; use strict; -my $watcher; my $timer_model = sub { my $writer = shift; my $count = 1; - $watcher = AnyEvent->timer( + my $timer; + $timer = AnyEvent->timer( after => 0, interval => 1, cb => sub { $writer->write(scalar localtime ."\n"); if(++$count > 5) { $writer->close; - undef $watcher; + # this cancels the timer, and breaks a circular reference + undef $timer; } }); }; @@ -26,6 +27,7 @@ my $psgi_app = sub { [200, [ 'Content-Type' => 'text/plain' ]]); $timer_model->($writer); - + # our timer lives on via a circular reference. + # return value is ignored }; };