Dancer2::Plugin::Syntax::ParamKeywords - Parameter keywords for the lazy
version 0.2.0
use Dancer2::Plugin::Syntax::ParamKeywords; # Sample URL: http://localhost:3000/my_route/baz/blah get '/my_route/:foo/:bar' => sub { my $params = route_params; # Hash::MultiValue object # This will show in your app log debug "We got us some foo!" if route_param( 'foo' ) eq 'baz'; }; # Sample URL: http://localhost:3000/my_other_route?foo=baz&bar=blah&bar=bah get '/my_other_route' => sub { my $params = query_params; # Hash::MultiValue object # This will show in your app log debug "We got us some foo!" if query_param( 'foo' ) eq 'baz'; return join( ',', sort query_params( 'bar' ) ); # returns 'bah, blah' }; # Sample URL: http://localhost:3000/my_post_route # Posted data: foo => 'bar', bar => 'baz', bar => 'quux' post '/my_last_route' => sub { my $params = body_params; # Hash::MultiValue object # This will show in your app log debug "We got us some foo!" if body_param( 'foo' ) eq 'bar'; return join( ',', sort body_params( 'bar' ) ); # returns 'baz, quux' };
Let's face it: Dancer2's parameter-fetching keywords take so much typing to use! Why can't they be shorter? Well now, dear reader, they are!
This module provides a little syntactic sugar to make getting query, route, and body parameters just a little bit quicker. Instead of writing these:
query_parameters->get( ... ); query_parameters->get_all( ... ); query_parameters body_parameters->get( ... ); body_parameters->get_all( ... ); body_parameters route_parameters->get( ... ); route_parameters
You can write just these:
query_param( ... ); query_params( ... ); query_params body_param( ... ); body_params( ... ); body_params route_param( ... ); route_params
That's about 25-50% less typing in many cases. You're welcome.
This is the same as calling query_parameters->get. You must pass a parameter name to get the value of.
query_parameters->get
If called without a parameter name, this is just like calling query_parameters. It will return a Hash::MultiValue object containing all of the query parameters that were passed.
query_parameters
When called with a parameter name, this is the same as calling query_parameters->get_all.
query_parameters->get_all
This is the equivalent of body_parameters->get. You must pass a parameter name to get the value of.
body_parameters->get
When called without a parameter name, this is the same as calling body_parameters. It will return a Hash::MultiValue object containing all of the body parameters that were posted to your route.
body_parameters
When called with a parameter name, this acts the same as calling body_parameters->get_all.
body_parameters->get_all
This is the equivalent of route_parameters->get. You must pass a parameter name to get.
route_parameters->get
When called without a parameter name, this is the equivalent of calling route_parameters. It will return a Hash::MultiValue object containing all of the route parameters that were passed.
route_parameters
Unlike query_params and body_params, you cannot assign multiple values to a single named parameter through route parameters in Dancer2. As such, you cannot pass a parameter to route_params as you can with the other two methods - the underlying get_all functionality for route parameters does not exist.
query_params
body_params
route_params
get_all
Dancer2
Jason A. Crome <cromedome@cpan.org>
This software is copyright (c) 2021 by Jason A. Crome.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Dancer2::Plugin::Syntax::ParamKeywords, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer2::Plugin::Syntax::ParamKeywords
CPAN shell
perl -MCPAN -e shell install Dancer2::Plugin::Syntax::ParamKeywords
For more information on module installation, please visit the detailed CPAN module installation guide.