line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package MooseX::Param; |
3
|
3
|
|
|
3
|
|
94920
|
use Moose::Role; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
6
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:STEVAN'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has 'params' => ( |
9
|
|
|
|
|
|
|
is => 'rw', |
10
|
|
|
|
|
|
|
isa => 'HashRef', |
11
|
|
|
|
|
|
|
lazy => 1, |
12
|
|
|
|
|
|
|
builder => 'init_params', |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub init_params { +{} } |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub param { |
18
|
|
|
|
|
|
|
my $self = shift; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# if they want the list of keys ... |
21
|
|
|
|
|
|
|
return keys %{$self->params} if scalar @_ == 0; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# if they want to fetch a particular key ... |
24
|
|
|
|
|
|
|
return $self->params->{$_[0]} if scalar @_ == 1; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
((scalar @_ % 2) == 0) |
27
|
|
|
|
|
|
|
|| confess "parameter assignment must be an even numbered list"; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my %new = @_; |
30
|
|
|
|
|
|
|
while (my ($key, $value) = each %new) { |
31
|
|
|
|
|
|
|
$self->params->{$key} = $value; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
return; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
1; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
__END__ |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=pod |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 NAME |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
MooseX::Param - Simple role to provide a standard param method |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 SYNOPSIS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
package My::Template::System; |
50
|
|
|
|
|
|
|
use Moose; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
with 'MooseX::Param'; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# ... |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $template = My::Template::System->new( |
57
|
|
|
|
|
|
|
params => { |
58
|
|
|
|
|
|
|
foo => 10, |
59
|
|
|
|
|
|
|
bar => 20, |
60
|
|
|
|
|
|
|
baz => 30, |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# fetching params |
65
|
|
|
|
|
|
|
$template->param('foo'); # 10 |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# getting list of params |
68
|
|
|
|
|
|
|
$template->param(); # foo, bar, baz |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# setting params |
71
|
|
|
|
|
|
|
$template->param(foo => 30, bar => 100); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 DESCRIPTION |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
This is a very simple Moose role which provides a L<CGI> like C<param> method. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
I found that I had written this code over and over and over and over again, |
78
|
|
|
|
|
|
|
and each time it was the same. So I thought, why not put it in a role? |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=over 4 |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item I<params> |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
This role provides a C<params> attribute which has a read-only accessor, |
87
|
|
|
|
|
|
|
and a HashRef type constraint. It also adds a builder method (see |
88
|
|
|
|
|
|
|
C<init_params> method below) to properly initalize it. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=back |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 METHODS |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=over 4 |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item B<params> |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Return the HASH ref in which the parameters are stored. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item B<param> |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
This is your standard L<CGI> style C<param> method. If passed no arguments, |
103
|
|
|
|
|
|
|
it will return a list of param names. If passed a single name argument it will |
104
|
|
|
|
|
|
|
return the param associated with that name. If passed a key value pair (or set |
105
|
|
|
|
|
|
|
of key value pairs) it will assign them into the params. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=item I<init_params> |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
This is the I<params> attribute C<builder> option, so it is called the |
110
|
|
|
|
|
|
|
params are initialized. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
B<NOTE:> You can override this by defining your own version in your class, |
113
|
|
|
|
|
|
|
because local class methods beat role methods in composition. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item B<meta> |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Returns the role metaclass. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=back |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 SIMILAR MODULES |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
The C<param> method can be found in several other modules, such as L<CGI>, |
124
|
|
|
|
|
|
|
L<CGI::Application> and L<HTML::Template> to name a few. This is such a |
125
|
|
|
|
|
|
|
common Perl idiom that I felt it really deserved it's own role (if for |
126
|
|
|
|
|
|
|
nothing more than I was sick of re-writing and copy-pasting it all the |
127
|
|
|
|
|
|
|
time). |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
There are also a few modules which attempt to solve the same problem as |
130
|
|
|
|
|
|
|
this module. Those are: |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=over 4 |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item L<Class::Param> |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
This module is much more ambitious than mine, and provides much deeper |
137
|
|
|
|
|
|
|
functionality. For most of my purposes, this module would have been |
138
|
|
|
|
|
|
|
overkill, but if you need really sophisticated param handling and the |
139
|
|
|
|
|
|
|
ability to provide several different APIs (tied, etc), this module is |
140
|
|
|
|
|
|
|
probably the way to go. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item L<Mixin::ExtraFields::Param> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
This module is very similar to mine, but for a different framework. It |
145
|
|
|
|
|
|
|
works with the L<Mixin::ExtraFields> framework. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=back |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 BUGS |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
All complex software has bugs lurking in it, and this module is no |
152
|
|
|
|
|
|
|
exception. If you find a bug please either email me, or add the bug |
153
|
|
|
|
|
|
|
to cpan-RT. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 AUTHOR |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Stevan Little E<lt>stevan@iinteractive.comE<gt> |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Copyright 2007 by Infinity Interactive, Inc. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
L<http://www.iinteractive.com> |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
166
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |