Skip to content

Instantly share code, notes, and snippets.

@minty
Created November 10, 2012 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minty/4051592 to your computer and use it in GitHub Desktop.
Save minty/4051592 to your computer and use it in GitHub Desktop.
Mojolicious helpers and Mojo::IOLoop
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::IOLoop;
use JSON::XS;
my $json = JSON::XS->new->utf8->pretty;
app->secret('as time goes by');
app->helper( pretty_json => sub {
my ($self, $ref) = @_;
$self->res->headers->header('Content-type' => 'application/json; charset=utf-8');
return $self->render(text => $json->encode( $ref ));
});
get '/' => sub { shift->render(json => { ok => 1 }) };
get '/pretty' => sub {
my ($self) = @_;
$self->render(text => $self->pretty_json({ ok => 1 }));
};
get '/delay' => sub {
my ($self) = @_;
my $counter = 0;
my $id = Mojo::IOLoop->recurring(1 => sub {
warn $counter;
if ($counter++ > 3) {
$self->render(json => { counter => $counter });
$self->finish;
}
});
$self->on(finish => sub { Mojo::IOLoop->remove($id) });
};
get '/pretty/delay' => sub {
my ($self) = @_;
my $counter = 0;
my $id = Mojo::IOLoop->recurring(1 => sub {
warn $counter;
if ($counter++ > 3) {
# This will generate these two lines in the server log
# substr outside of string at /usr/local/share/perl/5.10.1/Mojo/Message/Response.pm line 130.
# Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.10.1/Mojo/Transaction/HTTP.pm line 120.
$self->pretty_json({ counter => $counter });
$self->finish;
}
});
$self->on(finish => sub { Mojo::IOLoop->remove($id) });
};
app->start;
@minty
Copy link
Author

minty commented Nov 10, 2012

{
my $id;
sub clear_recurring {
Mojo::IOLoop->remove($id);
}

get '/pretty/delay' => sub {
    my ($self) = @_;

    my $counter = 0;
    $id = Mojo::IOLoop->recurring(1 => sub {
        warn $counter;
        if ($counter++ > 3) {
            # This will generate these two lines in the server log
            # substr outside of string at /usr/local/share/perl/5.10.1/Mojo/Message/Response.pm line 130.
            # Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl/5.10.1/Mojo/Transaction/HTTP.pm line 120.
            $self->pretty_json({ counter => $counter });
            clear_recurring();
        }
    });
};  

}

@minty
Copy link
Author

minty commented Nov 10, 2012

The above appears to fix it.

irc told me:

http://mojolicio.us/perldoc/Mojolicious/Controller#finish
http://mojolicio.us/perldoc/Mojolicious/Guides/Rendering#Rendering_text

and that I was doing it wrong, shouldn't be a nub, and should rtfm.

i wasn't requesting help because i already knew it, and the conceptual gap wasn't hugely helped by telling me i had the wrong problem/need, or repeatadly telling me to re-read the docs I'd already read. Hey ho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment