Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jesse (2531)

jesse
  (email not shown publicly)

Journal of jesse (2531)

Wednesday November 29, 2006
02:02 PM

Genericish dispatcher/controller logic with Jifty

[ #31756 ]
In the past couple days, a few folks from the CGI::App and Catalyst communities have posted examples of their dispatcher/controller layout and syntax. Here's what a similar example might look like in Jifty:

use warnings;
use strict;

package MyApp::Dispatcher;
use Jifty::Dispatcher -base;

under '/admin/account' => run {
    before '*' => set object_type => 'account';
    before qr'/(\d+)' => run { set id => $1; };

    on '/'         => show 'crud/index';
    on '/create'   => show 'crud/create';
    on '/*/update' => show 'crud/update';
    on '/*'        => show 'crud/view';
};

Edit: jibsheet pointed out that I'd misunderstood the Catalyst example. the code above should now be a more faithful implementation

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Static rules such as:
    before '*' => run { set object_type => 'account' };

    on '/' => run { show 'crud/index' };
    Can be written instead as:
    before '*' => set object_type => 'account';

    on '/' => show 'crud/index';
    it's a bit more declarative that way. :-)
    • Also, with our extended glob syntax one can replace:

      before qr'/(.*?)/(.*)' => run { set id => $2; };

      with this:

      before '**/*' => run { set id => $2; };

      With both changes in place, it'd look like this:

      under '/admin/account' => run {
          before '*'    => set object_type => 'account';
          before '**/*' => run { set id => $2 };

          on '/'         => show 'crud/index';
          on '/list'     => s

  • Me likes, but you might want to extend your globs a bit.
    Range and hash support could make it look even better.

    before qr'/(\d+)' => run { set id => $1; };

    could become

    # hash, equivalent to /(\d+)
    before '/#' => run { set id => $1 }

    # range, for more specific matching (bit more complicated to implement but worth it imo)
    before '/[0-255]' => run { set id => $1 }

    # alternatives, would make sense too :)
    before '/{1|133|255}' => run { set id => $1 }

    • It's an interesting thought. Though once we get to ranges, it's almost easier to just use full regexps. (And everywhere we use globs, we can just use a full regexp and qr'' instead. Globs are really nice for the simple case. I'm not quite sure where to draw the line. But "#" feels really nice. The suggestion is much appreciated.
    • We already support the standard glob syntax:
      before '{1,133,255}'
      As well as character classes:
      before '[a-z]'
      So I'm afraid that numeric range needs some other syntax. The '#' shorthand is very nice, though. :-)
      • Ah, nice one, been some time since i last looked at Jifty::Dispatcher. :)
        • ----------------------------------------------------------------------
          r25678 (orig r2238):  audreyt | 2006-12-01 12:43:40 +0800

          * Jifty::Dispatcher - NUMBER SIGN (#) now captures one or more digit
            characters in the extended shellglob condition syntax.
            Suggested by: Sebastian Riedel
          ----------------------------------------------------------------------