The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Dancer2::Manual::DSL - Dancer2's DSL keywords

VERSION

version 0.06

DESCRIPTION

Dancer2 provides you with a DSL (Domain-Specific Language) which makes implementing your web application trivial.

For example, take the following example:

    use Dancer2;

    get '/hello/:name' => sub {
        my $name = params->{name};
    };
    dance;

get and params are keywords provided by Dancer2.

This document lists all keywords provided by Dancer2. It does not cover additional keywords which may be provided by loaded plugins; see the documentation for plugins you use to see which additional keywords they make available to you.

KEYWORDS

any

Defines a route for multiple HTTP methods at once:

    any ['get', 'post'] => '/myaction' => sub {
        # code
    };

Or even, a route handler that would match any HTTP methods:

    any '/myaction' => sub {
        # code
    };

cookies

Accesses cookies values, it returns a HashRef of Dancer2::Cookie objects:

    get '/some_action' => sub {
        my $cookie = cookies->{name};
        return $cookie->value;
    };

In the case you have stored something else than a Scalar in your cookie:

    get '/some_action' => sub {
        my $cookie = cookies->{oauth};
        my %values = $cookie->value;
        return ($values{token}, $values{token_secret});
    };

Accesses a cookie value (or sets it). Note that this method will eventually be preferred over set_cookie.

    cookie lang => "fr-FR";              # set a cookie and return its value
    cookie lang => "fr-FR", expires => "2 hours";   # extra cookie info
    cookie "lang"                        # return a cookie value

If your cookie value is a key/value URI string, like

    token=ABC&user=foo

cookie will only return the first part (token=ABC) if called in scalar context. Use list context to fetch them all:

    my @values = cookie "name";

config

Accesses the configuration of the application:

    get '/appname' => sub {
        return "This is " . config->{appname};
    };

content_type

Sets the content-type rendered, for the current route handler:

    get '/cat/:txtfile' => sub {
        content_type 'text/plain';

        # here we can dump the contents of param('txtfile')
    };

You can use abbreviations for content types. For instance:

    get '/svg/:id' => sub {
        content_type 'svg';

        # here we can dump the image with id param('id')
    };

Note that if you want to change the default content-type for every route, you have to change the content_type setting instead.

dance

Alias for the start keyword.

dancer_version

Returns the version of Dancer. If you need the major version, do something like:

  int(dancer_version);

debug

Logs a message of debug level:

    debug "This is a debug message";

See Dancer2::Core::Role::Logger for details on how to configure where log messages go.

dirname

Returns the dirname of the path given:

    my $dir = dirname($some_path);

engine

Given a namespace, returns the current engine object

    my $template_engine = engine 'template';
    my $html = $template_engine->apply_renderer(...);
    $template_engine->apply_layout($html);

error

Logs a message of error level:

    error "This is an error message";

See Dancer2::Core::Role::Logger for details on how to configure where log messages go.

false

Constant that returns a false value (0).

forward

Runs an internal redirect of the current request to another request. This helps you avoid having to redirect the user using HTTP and set another request to your application.

It effectively lets you chain routes together in a clean manner.

    get '/demo/articles/:article_id' => sub {

        # you'll have to implement this next sub yourself :)
        change_the_main_database_to_demo();

        forward "/articles/" . params->{article_id};
    };

In the above example, the users that reach /demo/articles/30 will actually reach /articles/30 but we've changed the database to demo before.

This is pretty cool because it lets us retain our paths and offer a demo database by merely going to /demo/....

You'll notice that in the example we didn't indicate whether it was GET or POST. That is because forward chains the same type of route the user reached. If it was a GET, it will remain a GET (but if you do need to change the method, you can do so; read on below for details.)

WARNING : Issuing a forward immediately exits the current route, and perform the forward. Thus, any code after a forward is ignored, until the end of the route. e.g.

    get '/foo/:article_id' => sub {
        if ($condition) {
            forward "/articles/" . params->{article_id};
            # The following code is never executed
            do_stuff();
        }

        more_stuff();
    };

So it's not necessary anymore to use return with forward.

Note that forward doesn't parse GET arguments. So, you can't use something like:

     return forward '/home?authorized=1';

But forward supports an optional HashRef with parameters to be added to the actual parameters:

     return forward '/home', { authorized => 1 };

Finally, you can add some more options to the forward method, in a third argument, also as a HashRef. That option is currently only used to change the method of your request. Use with caution.

    return forward '/home', { auth => 1 }, { method => 'POST' };

from_dumper ($structure)

Deserializes a Data::Dumper structure.

from_json ($structure, %options)

Deserializes a JSON structure. Can receive optional arguments. Those arguments are valid JSON arguments to change the behaviour of the default JSON::from_json function.

from_yaml ($structure)

Deserializes a YAML structure.

from_xml ($structure, %options)

Deserializes a XML structure. Can receive optional arguments. These arguments are valid XML::Simple arguments to change the behaviour of the default XML::Simple::XMLin function.

get

Defines a route for HTTP GET requests to the given path:

    get '/' => sub {
        return "Hello world";
    }

Note that a route to match HEAD requests is automatically created as well.

halt

Sets a response object with the content given.

When used as a return value from a filter, this breaks the execution flow and renders the response immediately:

    hook before => sub {
        if ($some_condition) {
            halt("Unauthorized");
            # This code is not executed :
            do_stuff();
        }
    };

    get '/' => sub {
        "hello there";
    };

WARNING : Issuing a halt immediately exits the current route, and perform the halt. Thus, any code after a halt is ignored, until the end of the route. So it's not necessary anymore to use return with halt.

headers

Adds custom headers to responses:

    get '/send/headers', sub {
        headers 'X-Foo' => 'bar', X-Bar => 'foo';
    }

adds a custom header to response:

    get '/send/header', sub {
        header 'x-my-header' => 'shazam!';
    }

Note that it will overwrite the old value of the header, if any. To avoid that, see "push_header".

push_header

Do the same as header, but allow for multiple headers with the same name.

    get '/send/header', sub {
        push_header 'x-my-header' => '1';
        push_header 'x-my-header' => '2';
        will result in two headers "x-my-header" in the response
    }

hook

Adds a hook at some position. For example :

  hook before_serializer => sub {
    my $response = shift;
    $response->content->{generated_at} = localtime();
  };

There can be multiple hooks assigned to a given position, and each will be executed in order.

(For details on how to register new hooks from within plugins, see Dancer2::Manual::Hooks.) Supported before hooks (in order of execution):

before_deserializer

This hook receives no arguments.

  hook before_deserializer => sub {
    ...
  };
before_file_render

This hook receives as argument the path of the file to render.

  hook before_file_render => sub {
    my $path = shift;
    ...
  };
before_error_init

This hook receives as argument a Dancer2::Core::Error object.

  hook before_error_init => sub {
    my $error = shift;
    ...
  };
before_error_render

This hook receives as argument a Dancer2::Core::Error object.

  hook before_error_render => sub {
    my $error = shift;
  };
before

This hook receives one argument, the route being executed (a Dancer2::Core::Route object).

  hook before => sub {
    my $route_handler = shift;
    ...
  };
before_template_render

This is an alias to 'before_template'.

This hook receives as argument a HashRef, containing the tokens that will be passed to the template. You can use it to add more tokens, or delete some specific token.

  hook before_template_render => sub {
    my $tokens = shift;
    delete $tokens->{user};
    $tokens->{time} = localtime;
  };

is equivalent to

  hook before_template => sub {
    my $tokens = shift;
    delete $tokens->{user};
    $tokens->{time} = localtime;
  };
before_layout_render

This hook receives two arguments. The first one is a HashRef containing the tokens. The second is a ScalarRef representing the content of the template.

  hook before_layout_render => sub {
    my ($tokens, $html_ref) = @_;
    ...
  };
before_serializer

This hook receives as argument a Dancer2::Core::Response object.

  hook before_serializer => sub {
    my $response = shift;
    $response->content->{start_time} = time();
  };

Supported after hooks (in order of execution):

after_deserializer

This hook receives no arguments.

  hook after_deserializer => sub {
    ...
  };
after_file_render

This hook receives as argument a Dancer2::Core::Response object.

  hook after_file_render => sub {
    my $response = shift;
  };
after_template_render

This hook receives as argument a ScalarRef representing the content generated by the template.

  hook after_template_render => sub {
    my $html_ref = shift;
  };
after_layout_render

This hook receives as argument a ScalarRef representing the content generated by the layout

  hook after_layout_render => sub {
    my $html_ref = shift;
  };
after

This is an alias for after.

This hook runs after a request has been processed, but before the response is sent.

It receives a Dancer2::Core::Response object, which it can modify if it needs to make changes to the response which is about to be sent.

  hook after => sub {
    my $response = shift;
  };

This is equivalent to

  after sub {
    my $response = shift;
  };
after_error_render

This hook receives as argument a Dancer2::Core::Response object.

  hook after_error_render => sub {
    my $response = shift;
  };
on_handler_exception

This hook is called when an exception has been caught, at the handler level, just before creating and rendering Dancer2::Core::Error.

  hook on_handler_exception => sub {
    my $exception = shift;
  };
on_route_exception

This hook is called when an exception has been caught, at the route level, just before rethrowing it higher. This hook receives the exception as argument.

  hook on_route_exception => sub {
    my $exception = shift;
  };

info

Logs a message of info level:

    info "This is a info message";

See Dancer2::Core::Role::Logger for details on how to configure where log messages go.

load

Loads one or more perl scripts in the current application's namespace. Syntactic sugar around Perl's require:

    load 'UserActions.pl', 'AdminActions.pl';

load_app

Loads a Dancer package. This method sets the libdir to the current ./lib directory:

    # if we have lib/Webapp.pm, we can load it like:
    load_app 'Webapp';
    # or with options
    load_app 'Forum', prefix => '/forum', settings => {foo => 'bar'};

Note that the package loaded using load_app must import Dancer with the :syntax option.

To load multiple apps repeat load_app:

    load_app 'one';
    load_app 'two';

mime

Shortcut to access the instance object of Dancer2::Core::MIME. You should read the Dancer2::Core::MIME documentation for full details, but the most commonly-used methods are summarized below:

    # set a new mime type
    mime->add_type( foo => 'text/foo' );

    # set a mime type alias
    mime->add_alias( f => 'foo' );

    # get mime type for an alias
    my $m = mime->for_name( 'f' );

    # get mime type for a file (based on extension)
    my $m = mime->for_file( "foo.bar" );

    # get current defined default mime type
    my $d = mime->default;

    # set the default mime type using config.yml
    # or using the set keyword
    set default_mime_type => 'text/plain';

params

This method should be called from a route handler. It's an alias for the Dancer2::Core::Request params accessor. It returns an hash reference to all defined parameters. Check param below to access quickly to a single parameter value.

param

This method should be called from a route handler. This method is an accessor to the parameters hash table.

   post '/login' => sub {
       my $username = param "user";
       my $password = param "pass";
       # ...
   }

pass

This method should be called from a route handler. Tells Dancer to pass the processing of the request to the next matching route.

WARNING : Issuing a pass immediately exits the current route, and perform the pass. Thus, any code after a pass is ignored, until the end of the route. So it's not necessary anymore to use return with pass.

    get '/some/route' => sub {
        if (...) {
            # we want to let the next matching route handler process this one
            pass(...);
            # This code will be ignored
            do_stuff();
        }
    };

patch

Defines a route for HTTP PATCH requests to the given URL:

    patch '/resource' => sub { ... };

(PATCH is a relatively new and not-yet-common HTTP verb, which is intended to work as a "partial-PUT", transferring just the changes; please see http://tools.ietf.org/html/rfc5789|RFC5789 for further details.)

Please be aware that, if you run your app in standalone mode, PATCH requests will not reach your app unless you have a new version of HTTP::Server::Simple which accepts PATCH as a valid verb. The current version at time of writing, 0.44, does not. A pull request has been submitted to add this support, which you can find at:

https://github.com/bestpractical/http-server-simple/pull/1

path

Concatenates multiple paths together, without worrying about the underlying operating system:

    my $path = path(dirname($0), 'lib', 'File.pm');

It also normalizes (cleans) the path aesthetically. It does not verify the path exists.

post

Defines a route for HTTP POST requests to the given URL:

    post '/' => sub {
        return "Hello world";
    }

prefix

Defines a prefix for each route handler, like this:

    prefix '/home';

From here, any route handler is defined to /home/*:

    get '/page1' => sub {}; # will match '/home/page1'

You can unset the prefix value:

    prefix undef;
    get '/page1' => sub {}; will match /page1

For a safer alternative you can use lexical prefix like this:

    prefix '/home' => sub {
        ## Prefix is set to '/home' here

        get ...;
        get ...;
    };
    ## prefix reset to the previous version here

This makes it possible to nest prefixes:

   prefix '/home' => sub {
       ## some routes
       
      prefix '/private' => sub {
         ## here we are under /home/private...

         ## some more routes
      };
      ## back to /home
   };
   ## back to the root

Notice: once you have a prefix set, do not add a caret to the regex:

    prefix '/foo';
    get qr{^/bar} => sub { ... } # BAD BAD BAD
    get qr{/bar}  => sub { ... } # Good!

del

Defines a route for HTTP DELETE requests to the given URL:

    del '/resource' => sub { ... };

options

Defines a route for HTTP OPTIONS requests to the given URL:

    options '/resource' => sub { ... };

put

Defines a route for HTTP PUT requests to the given URL:

    put '/resource' => sub { ... };

redirect

Generates a HTTP redirect (302). You can either redirect to a complete different site or within the application:

    get '/twitter', sub {
        redirect 'http://twitter.com/me';
    };

You can also force Dancer to return a specific 300-ish HTTP response code:

    get '/old/:resource', sub {
        redirect '/new/'.params->{resource}, 301;
    };

It is important to note that issuing a redirect by itself does not exit and redirect immediately, redirection is deferred until after the current route or filter has been processed. To exit and redirect immediately, use the return function, e.g.

    get '/restricted', sub {
        return redirect '/login' if accessDenied();
        return 'Welcome to the restricted section';
    };

request

Returns a Dancer2::Core::Request object representing the current request.

See the Dancer2::Core::Request documention for the methods you can call, for example:

    request->referer;         # value of the HTTP referer header
    request->remote_address;  # user's IP address
    request->user_agent;      # User-Agent header value

send_error

Returns a HTTP error. By default the HTTP code returned is 500:

    get '/photo/:id' => sub {
        if (...) {
            send_error("Not allowed", 403);
        } else {
           # return content
        }
    }

WARNING : Issuing a send_error immediately exits the current route, and perform the send_error. Thus, any code after a send_error is ignored, until the end of the route. So it's not necessary anymore to use return with send_error.

    get '/some/route' => sub {
        if (...) {
            # we want to let the next matching route handler process this one
            send_error(..);
            # This code will be ignored
            do_stuff();
        }
    };

send_file

Lets the current route handler send a file to the client. Note that the path of the file must be relative to the public directory unless you use the system_path option (see below).

    get '/download/:file' => sub {
        return send_file(params->{file});
    }

WARNING : Issuing a send_file immediately exits the current route, and perform the send_file. Thus, any code after a send_file is ignored, until the end of the route. So it's not necessary anymore to use return with send_file.

    get '/some/route' => sub {
        if (...) {
            # we want to let the next matching route handler process this one
            send_file(...);
            # This code will be ignored
            do_stuff();
        }
    };

Send file supports streaming possibility using PSGI streaming. The server should support it but normal streaming is supported on most, if not all.

    get '/download/:file' => sub {
        return send_file( params->{file}, streaming => 1 );
    }

You can control what happens using callbacks.

First, around_content allows you to get the writer object and the chunk of content read, and then decide what to do with each chunk:

    get '/download/:file' => sub {
        return send_file(
            params->{file},
            streaming => 1,
            callbacks => {
                around_content => sub {
                    my ( $writer, $chunk ) = @_;
                    $writer->write("* $chunk");
                },
            },
        );
    }

You can use around to all get all the content (whether a filehandle if it's a regular file or a full string if it's a scalar ref) and decide what to do with it:

    get '/download/:file' => sub {
        return send_file(
            params->{file},
            streaming => 1,
            callbacks => {
                around => sub {
                    my ( $writer, $content ) = @_;

                    # we know it's a text file, so we'll just stream
                    # line by line
                    while ( my $line = <$content> ) {
                        $writer->write($line);
                    }
                },
            },
        );
    }

Or you could use override to control the entire streaming callback request:

    get '/download/:file' => sub {
        return send_file(
            params->{file},
            streaming => 1,
            callbacks => {
                override => sub {
                    my ( $respond, $response ) = @_;

                    my $writer = $respond->( [ $newstatus, $newheaders ] );
                    $writer->write("some line");
                },
            },
        );
    }

You can also set the number of bytes that will be read at a time (default being 42K bytes) using bytes:

    get '/download/:file' => sub {
        return send_file(
            params->{file},
            streaming => 1,
            bytes     => 524288, # 512K
        );
    };

The content-type will be set depending on the current MIME types definition (see mime if you want to define your own).

If your filename does not have an extension, or you need to force a specific mime type, you can pass it to send_file as follows:

    return send_file(params->{file}, content_type => 'image/png');

Also, you can use your aliases or file extension names on content_type, like this:

    return send_file(params->{file}, content_type => 'png');

For files outside your public folder, you can use the system_path switch. Just bear in mind that its use needs caution as it can be dangerous.

   return send_file('/etc/passwd', system_path => 1);

If you have your data in a scalar variable, send_file can be useful as well. Pass a reference to that scalar, and send_file will behave as if there was a file with that contents:

   return send_file( \$data, content_type => 'image/png' );

Note that Dancer is unable to guess the content type from the data contents. Therefore you might need to set the content_type properly. For this kind of usage an attribute named filename can be useful. It is used as the Content-Disposition header, to hint the brower about the filename it should use.

   return send_file( \$data, content_type => 'image/png'
                             filename     => 'onion.png' );

Note that you should always use return send_file ... to stop execution of your route handler at that point.

set

Defines a setting:

    set something => 'value';

You can set more than one value at once:

    set something => 'value', otherthing => 'othervalue';

setting

Returns the value of a given setting:

    setting('something'); # 'value'

session

Provides access to all data stored in the user's session (if any).

It can also be used as a setter to store data in the session:

    # getter example
    get '/user' => sub {
        if (session('user')) {
            return "Hello, ".session('user')->name;
        }
    };

    # setter example
    post '/user/login' => sub {
        ...
        if ($logged_in) {
            session user => $user;
        }
        ...
    };

You may also need to clear a session:

    # destroy session
    get '/logout' => sub {
        ...
        session->destroy;
        ...
    };

If you need to fetch the session ID being used for any reason:

    my $id = session->id;

splat

Returns the list of captures made from a route handler with a route pattern which includes wildcards:

    get '/file/*.*' => sub {
        my ($file, $extension) = splat;
        ...
    };

There is also the extensive splat (A.K.A. "megasplat"), which allows extensive greedier matching, available using two asterisks. The additional path is broken down and returned as an ArrayRef:

    get '/entry/*/tags/**' => sub {
        my ( $entry_id, $tags ) = splat;
        my @tags = @{$tags};
    };

This helps with chained actions:

    get '/team/*/**' => sub {
        my ($team) = splat;
        var team => $team;
        pass;
    };

    prefix '/team/*';

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

        # etc...
    };

    get '/score' => sub {
        return score_for( vars->{'team'} );
    };

start

Starts the application or the standalone server (depending on the deployment choices).

This keyword should be called at the very end of the script, once all routes are defined. At this point, Dancer takes over control.

status

Changes the status code provided by an action. By default, an action will produce an HTTP 200 OK status code, meaning everything is OK:

    get '/download/:file' => {
        if (! -f params->{file}) {
            status 'not_found';
            return "File does not exist, unable to download";
        }
        # serving the file...
    };

In that example, Dancer will notice that the status has changed, and will render the response accordingly.

The status keyword receives either a numeric status code or its name in lower case, with underscores as a separator for blanks - see the list in "HTTP CODES" in Dancer2::Core::HTTP.

template

Returns the response of processing the given template with the given parameters (and optional settings), wrapping it in the default or specified layout too, if layouts are in use.

An example of a route handler which returns the result of using template to build a response with the current template engine:

    get '/' => sub {
        ...
        return template 'some_view', { token => 'value'};
    };

Note that template simply returns the content, so when you use it in a route handler, if execution of the the route handler should stop at that point, make sure you use 'return' to ensure your route handler returns the content.

Since template just returns the result of rendering the template, you can also use it to perform other templating tasks, e.g. generating emails:

    post '/some/route' => sub {
        if (...) {
            email {
                to      => 'someone@example.com',
                from    => 'foo@example.com',
                subject => 'Hello there',
                msg     => template('emails/foo', { name => params->{name} }),
            };

            return template 'message_sent';
        } else {
            return template 'error';
        }
    };

Compatibility notice: template was changed in version 1.3090 to immediately interrupt execution of a route handler and return the content, as it's typically used at the end of a route handler to return content. However, this caused issues for some people who were using template to generate emails etc, rather than accessing the template engine directly, so this change has been reverted in 1.3091.

The first parameter should be a template available in the views directory, the second one (optional) is a HashRef of tokens to interpolate, and the third (again optional) is a HashRef of options.

For example, to disable the layout for a specific request:

    get '/' => sub {
        template 'index', {}, { layout => undef };
    };

Or to request a specific layout, of course:

    get '/user' => sub {
        template 'user', {}, { layout => 'user' };
    };

Some tokens are automatically added to your template (perl_version, dancer_version, settings, request, params, vars and, if you have sessions enabled, session). Check Dancer2::Core::Role::Template for further details.

to_dumper ($structure)

Serializes a structure with Data::Dumper.

to_json ($structure, %options)

Serializes a structure to JSON. Can receive optional arguments. Thoses arguments are valid JSON arguments to change the behaviour of the default JSON::to_json function.

to_yaml ($structure)

Serializes a structure to YAML.

to_xml ($structure, %options)

Serializes a structure to XML. Can receive optional arguments. Thoses arguments are valid XML::Simple arguments to change the behaviour of the default XML::Simple::XMLout function.

true

Constant that returns a true value (1).

upload

Provides access to file uploads. Any uploaded file is accessible as a Dancer2::Core::Request::Upload object. You can access all parsed uploads via:

    post '/some/route' => sub {
        my $file = upload('file_input_foo');
        # file is a Dancer2::Core::Request::Upload object
    };

If you named multiple input of type "file" with the same name, the upload keyword will return an Array of Dancer2::Core::Request::Upload objects:

    post '/some/route' => sub {
        my ($file1, $file2) = upload('files_input');
        # $file1 and $file2 are Dancer2::Core::Request::Upload objects
    };

You can also access the raw HashRef of parsed uploads via the current request object:

    post '/some/route' => sub {
        my $all_uploads = request->uploads;
        # $all_uploads->{'file_input_foo'} is a Dancer2::Core::Request::Upload object
        # $all_uploads->{'files_input'} is an ArrayRef of Dancer2::Core::Request::Upload objects
    };

Note that you can also access the filename of the upload received via the params keyword:

    post '/some/route' => sub {
        # params->{'files_input'} is the filename of the file uploaded
    };

See Dancer2::Core::Request::Upload for details about the interface provided.

uri_for

Returns a fully-qualified URI for the given path:

    get '/' => sub {
        redirect uri_for('/path');
        # can be something like: http://localhost:3000/path
    };

captures

Returns a reference to a copy of %+, if there are named captures in the route Regexp.

Named captures are a feature of Perl 5.10, and are not supported in earlier versions:

    get qr{
        / (?<object> user   | ticket | comment )
        / (?<action> delete | find )
        / (?<id> \d+ )
        /?$
    }x
    , sub {
        my $value_for = captures;
        "i don't want to $$value_for{action} the $$value_for{object} $$value_for{id} !"
    };

var

Provides an accessor for variables shared between filters and route handlers. Given a key/value pair, it sets a variable:

    hook before sub {
        var foo => 42;
    };

Later, route handlers and other filters will be able to read that variable:

    get '/path' => sub {
        my $foo = var 'foo';
        ...
    };

vars

Returns the HashRef of all shared variables set during the filter/route chain with the var keyword:

    get '/path' => sub {
        if (vars->{foo} eq 42) {
            ...
        }
    };

warning

Logs a warning message through the current logger engine:

    warning "This is a warning";

See Dancer2::Core::Role::Logger for details on how to configure where log messages go.

AUTHOR

Dancer Core Developers

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.