Dancer
view release on metacpan or search on metacpan
lib/Dancer.pm view on Meta::CPAN
# code
};
=head2 before
Deprecated - see the C<before> L<hook|Dancer/hook>.
=head2 before_template
Deprecated - see the C<before_template> L<hook|Dancer/hook>.
=head2 cookies
Accesses cookies values, it returns a HashRef of L<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});
};
=head2 cookie
Accesses a cookie value (or sets it). Note that this method will
eventually be preferred over C<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
C<cookie> will only return the first part (C<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.
=head2 config
Accesses the configuration of the application:
get '/appname' => sub {
return "This is " . config->{appname};
};
=head2 content_type
Sets the B<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 C<content_type> setting instead.
=head2 dance
Alias for the C<start> keyword.
=head2 dancer_version
Returns the version of Dancer. If you need the major version, do something like:
int(dancer_version);
=head2 debug
Logs a message of debug level:
debug "This is a debug message";
See L<Dancer::Logger> for details on how to configure where log messages go.
=head2 dirname
Returns the dirname of the path given:
my $dir = dirname($some_path);
=head2 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);
=head2 error
Logs a message of error level:
error "This is an error message";
See L<Dancer::Logger> for details on how to configure where log messages go.
=head2 false
Constant that returns a false value (0).
=head2 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};
lib/Dancer.pm view on Meta::CPAN
streaming => 1,
callbacks => {
around_content => sub {
my ( $writer, $chunk ) = @_;
$writer->write("* $chunk");
},
},
);
}
You can use C<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 C<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 C<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 C<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 C<send_file> as follows:
send_file(params->{file}, content_type => 'image/png');
Also, you can use your aliases or file extension names on
C<content_type>, like this:
send_file(params->{file}, content_type => 'png');
For files outside your B<public> folder, you can use the C<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, C<send_file> can be useful
as well. Pass a reference to that scalar, and C<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 C<content_type>
properly. For this kind of usage an attribute named C<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' );
=head2 set
Defines a setting:
set something => 'value';
You can set more than one value at once:
set something => 'value', otherthing => 'othervalue';
=head2 setting
Returns the value of a given setting:
setting('something'); # 'value'
=head2 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.846 second using v1.01-cache-2.11-cpan-524268b4103 )