Simple role to provide a standard param method
package My::Template::System; use Moose; with 'MooseX::Param'; # ... my $template = My::Template::System->new( params => { foo => 10, bar => 20, baz => 30, } ); # fetching params $template->param('foo'); # 10 # getting list of params $template->param(); # foo, bar, baz # setting params $template->param(foo => 30, bar => 100);
This is a very simple Moose role which provides a \s-1CGI\s0 like \*(C`param\*(C' method.
I found that I had written this code over and over and over and over again, and each time it was the same. So I thought, why not put it in a role?
This role provides a \*(C`params\*(C' attribute which has a read-only accessor, and a HashRef type constraint. It also adds a builder method (see \*(C`init_params\*(C' method below) to properly initialize it.
Return the \s-1HASH\s0 ref in which the parameters are stored.
This is your standard \s-1CGI\s0 style \*(C`param\*(C' method. If passed no arguments, it will return a list of param names. If passed a single name argument it will return the param associated with that name. If passed a key value pair (or set of key value pairs) it will assign them into the params.
This is the params attribute \*(C`builder\*(C' option, so it is called the params are initialized. \s-1NOTE:\s0 You can override this by defining your own version in your class, because local class methods beat role methods in composition.
Returns the role metaclass.
The \*(C`param\*(C' method can be found in several other modules, such as \s-1CGI\s0, CGI::Application and HTML::Template to name a few. This is such a common Perl idiom that I felt it really deserved it's own role (if for nothing more than I was sick of re-writing and copy-pasting it all the time).
There are also a few modules which attempt to solve the same problem as this module. Those are:
This module is much more ambitious than mine, and provides much deeper functionality. For most of my purposes, this module would have been overkill, but if you need really sophisticated param handling and the ability to provide several different APIs (tied, etc), this module is probably the way to go.
This module is very similar to mine, but for a different framework. It works with the Mixin::ExtraFields framework.
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Stevan Little <[email protected]>
Copyright 2007 by Infinity Interactive, Inc.
<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.