Dancer
view release on metacpan or search on metacpan
before
Deprecated - see the before hook.
before_template
Deprecated - see the before_template hook.
cookies
Accesses cookies values, it returns a HashRef of Dancer::Cookie
objects:
get '/some_action' => sub {
my $cookie = cookies->{name};
return $cookie->value;
};
In the case you have stored something other than a Scalar in your
cookie:
get '/some_action' => sub {
my $cookie = cookies->{oauth};
my %values = $cookie->value;
return ($values{token}, $values{token_secret});
};
cookie
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";
Note that if the client has sent more than one cookie with the same
value, the one returned will be the last one seen. This should only
happen if you have set multiple cookies with the same name but
different paths. So, don't do that.
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 Dancer::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 Dancer::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 :)
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 {
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 {
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 {
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:
send_file(params->{file}, content_type => 'image/png');
Also, you can use your aliases or file extension names on content_type,
like this:
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.
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 were a file with that contents:
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 browser about the
filename it should use.
send_file( \$data, content_type => 'image/png'
filename => 'onion.png' );
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'
set_cookie
Creates or updates cookie values:
get '/some_action' => sub {
set_cookie name => 'value',
expires => (time + 3600),
domain => '.foo.com';
};
In the example above, only 'name' and 'value' are mandatory.
( run in 0.809 second using v1.01-cache-2.11-cpan-524268b4103 )