Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created May 14, 2009 11:55
Show Gist options
  • Save jnthn/111625 to your computer and use it in GitHub Desktop.
Save jnthn/111625 to your computer and use it in GitHub Desktop.
role handler {
}
module LolDispatch {
my @routes;
multi trait_auxiliary:<is>(handler $trait, Block $block, $arg) is export {
@routes.push({:route($arg[0]), :block($block)});
}
sub dispatch($r) is export {
for @routes -> $item{
if $r ~~ $item<route> {
my $ret = $item<block>($r,$/);
return $ret;
}
}
warn "Could not dispatch $r";
}
}
use LolDispatch;
sub foo($request, $match) is handler(/wtf/) {
say 'dispatched to foo';
say $match.perl;
}
sub item($request, $match) is handler(/^\/item\/(\d+)/) {
say 'dispatched to item';
say $match.perl;
}
my $request = '/item/12345';
dispatch($request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment