Skip to content

cromedome/Dancer2-Plugin-Syntax-ParamKeywords

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NAME

Dancer2::Plugin::Syntax::ParamKeywords - Parameter keywords for the lazy

VERSION

version 0.2.0

SYNOPSIS

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'
};

DESCRIPTION

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.

KEYWORDS

query_param

This is the same as calling query_parameters->get. You must pass a parameter name to get the value of.

query_params

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.

When called with a parameter name, this is the same as calling query_parameters->get_all.

body_param

This is the equivalent of body_parameters->get. You must pass a parameter name to get the value of.

body_params

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.

When called with a parameter name, this acts the same as calling body_parameters->get_all.

route_param

This is the equivalent of route_parameters->get. You must pass a parameter name to get.

route_params

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.

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.

SEE ALSO

AUTHOR

Jason A. Crome cromedome@cpan.org

COPYRIGHT AND LICENSE

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.

About

Handy shortcuts to long-named Dancer2 keywords. Laziness FTW!

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages