[dancer-users] serialiser scope

Zahir Lalani ZahirLalani at oliver-marketing.com
Thu Mar 19 22:43:23 GMT 2015

Thanks – that makes sense


From: dancer-users [mailto:dancer-users-bounces at dancer.pm] On Behalf Of Maxwell Carey
Sent: 19 March 2015 21:59
To: Perl Dancer users mailing list
Subject: Re: [dancer-users] serialiser scope

Have you read the Dancer advent calendar article about serializers? http://advent.perldancer.org/2014/11
You can use Plack::Builder to mount two separate apps together, and they can still share sessions:

$ cat MyApp/lib/MyApp.pm
package MyApp;
use Dancer2;

our $VERSION = '0.1';

get '/' => sub {
    session foo => 'bar';
    template 'index';


$ cat MyApp/lib/MyApp/API.pm
package MyApp::API;
use Dancer2;

set serializer => 'JSON';

get '/' => sub {
    my $foo = session('foo') // 'fail';
    return { foo => $foo };


$ cat MyApp/bin/app.psgi
#!/usr/bin/env perl

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";

use MyApp;
use MyApp::API;
use Plack::Builder;

builder {
    mount '/'    => MyApp->to_app;
    mount '/api' => MyApp::API->to_app;
And the test:

$ cat session_test
#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

use WWW::Mechanize;

my $mech = WWW::Mechanize->new;

my $base_url = 'http://localhost:5000/';
my $api_url = $base_url . 'api/';


say $mech->content(raw => 1);

$ ./session_test

On Thu, Mar 19, 2015 at 6:51 AM, Zahir Lalani <ZahirLalani at oliver-marketing.com<mailto:ZahirLalani at oliver-marketing.com>> wrote:
Hi All

A little confused about the serialiser scope. I get that in the config file, it will apply to the entire application. I have a need to setup template driven routes and ajax routes. So I have a number of route files, and all use the app name to link the routes into one application.  Had assumed that the “set serialiser “ would be active in the current package only (i.e. the ajax routes file) but it seems it applies to the application since all routes are linked into the same application.

I have hunted for clues, but in all honesty I am a little confused as to the solution. I know creating a separate app would work, but these routes are part of the same app and need to be centrally controlled in terms of session management etc.

Any advice most appreciated


dancer-users mailing list
dancer-users at dancer.pm<mailto:dancer-users at dancer.pm>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150319/d2964c41/attachment.html>

More information about the dancer-users mailing list