twenty four merry days of Perl Feed

Dancer Goes Megasplat

Dancer's Megasplat - 2013-12-02

No, Santa Claus' sleigh just didn't have the mother of all reindeer-benders. This article is rather about the little-known but very useful feature of Dancer known as the megasplat.

So, what's the megasplat? Well, Dancer, being heavily influenced by Ruby's Sinatra, adopted its splat placeholder, which allows to capture elements of a route:


1: 
2: 
3: 
4: 
5: 
6: 
7: 

 

use Dancer;

get '/wishlist/*' => sub {
    my( $child ) = splat;

    return NortPole::DB->get_wishlist( $child );
};

 

That route will match /wishlist/timmy, and /wishlist/sandy, and the wishlist of every other child in the world, but what if we want to match several path elements? That's where we go mega:


1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 

 

use Dancer;

get '/giddy_up/**' => sub {
    my( $reindeers ) = splat;

    my @exclamations = (
        'Now', 'On', 'To the top of the porch', 'To the top of the wall'
    );

    return join '', map {
        sprintf "%s, %s!\n",
                $exclamations[rand @exclamations], $_
    } @$reindeers;
};

dance;

 

With that, we can hit /giddy_up/Prancer/Dancer/Vixen/Cupid and get back:

  Now, Prancer!
  To the top of the porch, Dancer!
  To the top of the wall, Vixen!
  On, Cupid!

Besides that, the megasplat can also be used to mimick Catalyst's chaining behavior:


1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 

 

use 5.10.0;

use Dancer;

my( @naughty, @nice, %gift, $child );

any '/child/*/**' => sub {
    ($child) = splat;
    pass;
};

prefix = '/child/*';

get '/naughty' => sub { push @naughty, $child; 'tsk tsk' };
get '/nice' => sub { push @nice, $child; 'nicely done' };

put '/gift/*' => sub { $gift{$child} = (splat)[1] };

get '/gift' => sub { $child ~~ $naughty ? 'coal' : $gift{$child} };

dance;

 

Which will give Santa a nice little basic web service for his Nice & Naughty book keeping:

  $ curl http://api.toydb.np/child/rjbs/nice
  nicely done
  $ curl http://api.toydb.np/child/yanick/naughty
  tsk tsk
  $ curl -X PUT http://api.toydb.np/child/rjbs/gift/pony
  noted
  $ curl -X PUT http://api.toydb.np/child/yanick/gift/pony
  noted
  $ curl http://api.toydb.np/child/yanick/gift
  coal
  $ curl http://api.toydb.np/child/rjbs/gift
  pony

See Also

Gravatar Image This article contributed by: Yanick Champoux <yanick@cpan.org>