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

NAME

Catalyst::ActionRole::NamedFields - Name your fields

SYNOPSIS

    package MyApp::Controller::Example;

    use Moose;
    use MooseX::MethodAttributes;

    extends 'Catalyst::Controller';

    sub fields :Local Args(1) Does('NamedFields') Field(id=>$args[0],search=>$query{q}) {
      # $_ is localized to $c
      $_->res->body("id is '$_{id}' and search is '$_{search}'");
    }

    __PACKAGE__->meta->make_immutable;

For https://localhost/example/fields/arg1?q=terms

    "id is 'arg1' and search is 'terms'"

DESCRIPTION

This is functionality I needed for a different upcoming distribution but it was clearly isolated well enough I thought it merited a stand alone release. By itself maybe not so useful but you can use it to build interesting experiments.

This defines an action attribute called 'Field', which can be used to map arguments and query parameters for a request to the '%_' special variable. The value of the attribute is evaluted into a callback, so be warned about what you put there. Its evaluated in a context where @args is the action arguments (typically defined by the Args or CaptureArgs attribute) and where %query is %{$c->req->query_parameters}. We also localize $_ to be the current context or '$c' which you might find a useful shorthand (although keeping mind that $_ gets clobbered easily in many common functions such as map.

The idea is to map some variables to %_ for ease of access. Although the syntax as shown above is actually more verbose than the old fashioned way, one can vision some additional action roles or attribute helpers that could build upon this for interesting experiments.

Documentation here is deliberately light since I imagine most won't have a direct use for it unless one can review the source and tests for meaningful information.

AUTHOR

  John Napiorkowski <jnapiork@cpan.org>
  

COPYRIGHT

Copyright (c) 2015 the above named AUTHOR

LICENSE

You may distribute this code under the same terms as Perl itself.