Skip to content

Instantly share code, notes, and snippets.

@AllanCochrane
Created June 9, 2015 13:39
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 AllanCochrane/1dd34f0fd3649ece7689 to your computer and use it in GitHub Desktop.
Save AllanCochrane/1dd34f0fd3649ece7689 to your computer and use it in GitHub Desktop.
Mojolicious lite app for callback discussion
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => sub {
my $c = shift;
$c->write("begin\n");
my $i = int($c->param('i'));
my $cb;
$cb = sub {
$c->write("cb: $i\n", $i-- == 0 ? undef : $cb);
};
$c->$cb();
$c->write("end\n");
};
app->start;
# Output:
# begin
# cb: 10
# end
# cb: 9
# cb: 8
# cb: 7
# cb: 6
# cb: 5
# cb: 4
# cb: 3
# cb: 2
# cb: 1
# cb: 0
@AllanCochrane
Copy link
Author

Invoke with

perl test1.pl get '/?i=10'

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