1MooseX::Param(3) User Contributed Perl Documentation MooseX::Param(3)
2
3
4
6 MooseX::Param - Simple role to provide a standard param method
7
9 package My::Template::System;
10 use Moose;
11
12 with 'MooseX::Param';
13
14 # ...
15
16 my $template = My::Template::System->new(
17 params => {
18 foo => 10,
19 bar => 20,
20 baz => 30,
21 }
22 );
23
24 # fetching params
25 $template->param('foo'); # 10
26
27 # getting list of params
28 $template->param(); # foo, bar, baz
29
30 # setting params
31 $template->param(foo => 30, bar => 100);
32
34 This is a very simple Moose role which provides a CGI like "param"
35 method.
36
37 I found that I had written this code over and over and over and over
38 again, and each time it was the same. So I thought, why not put it in a
39 role?
40
42 params
43 This role provides a "params" attribute which has a read-only
44 accessor, and a HashRef type constraint. It also adds a builder
45 method (see "init_params" method below) to properly initalize it.
46
48 params
49 Return the HASH ref in which the parameters are stored.
50
51 param
52 This is your standard CGI style "param" method. If passed no
53 arguments, it will return a list of param names. If passed a single
54 name argument it will return the param associated with that name.
55 If passed a key value pair (or set of key value pairs) it will
56 assign them into the params.
57
58 init_params
59 This is the params attribute "builder" option, so it is called the
60 params are initialized.
61
62 NOTE: You can override this by defining your own version in your
63 class, because local class methods beat role methods in
64 composition.
65
66 meta
67 Returns the role metaclass.
68
70 The "param" method can be found in several other modules, such as CGI,
71 CGI::Application and HTML::Template to name a few. This is such a
72 common Perl idiom that I felt it really deserved it's own role (if for
73 nothing more than I was sick of re-writing and copy-pasting it all the
74 time).
75
76 There are also a few modules which attempt to solve the same problem as
77 this module. Those are:
78
79 Class::Param
80 This module is much more ambitious than mine, and provides much
81 deeper functionality. For most of my purposes, this module would
82 have been overkill, but if you need really sophisticated param
83 handling and the ability to provide several different APIs (tied,
84 etc), this module is probably the way to go.
85
86 Mixin::ExtraFields::Param
87 This module is very similar to mine, but for a different framework.
88 It works with the Mixin::ExtraFields framework.
89
91 All complex software has bugs lurking in it, and this module is no
92 exception. If you find a bug please either email me, or add the bug to
93 cpan-RT.
94
96 Stevan Little <stevan@iinteractive.com>
97
99 Copyright 2007 by Infinity Interactive, Inc.
100
101 <http://www.iinteractive.com>
102
103 This library is free software; you can redistribute it and/or modify it
104 under the same terms as Perl itself.
105
106
107
108perl v5.12.0 2010-05-03 MooseX::Param(3)