Commit | Line | Data |
c466e58f |
1 | package Moose::Meta::Attribute::Native::Trait::String; |
e3c07b19 |
2 | use Moose::Role; |
3 | |
45160c4b |
4 | our $VERSION = '0.99'; |
e3c07b19 |
5 | $VERSION = eval $VERSION; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
c466e58f |
8 | use Moose::Meta::Attribute::Native::MethodProvider::String; |
e3c07b19 |
9 | |
c466e58f |
10 | with 'Moose::Meta::Attribute::Native::Trait'; |
e3c07b19 |
11 | |
12 | has 'method_provider' => ( |
13 | is => 'ro', |
14 | isa => 'ClassName', |
15 | predicate => 'has_method_provider', |
c466e58f |
16 | default => 'Moose::Meta::Attribute::Native::MethodProvider::String', |
e3c07b19 |
17 | ); |
18 | |
2edb73d9 |
19 | sub _default_default { q{} } |
20 | sub _default_is { 'rw' } |
2e069f5a |
21 | sub _helper_type { 'Str' } |
e3c07b19 |
22 | |
e3c07b19 |
23 | no Moose::Role; |
24 | |
e3c07b19 |
25 | 1; |
26 | |
27 | __END__ |
28 | |
29 | =pod |
30 | |
31 | =head1 NAME |
32 | |
2420461c |
33 | Moose::Meta::Attribute::Native::Trait::String - Helper trait for Str attributes |
e3c07b19 |
34 | |
35 | =head1 SYNOPSIS |
36 | |
37 | package MyHomePage; |
38 | use Moose; |
e3c07b19 |
39 | |
40 | has 'text' => ( |
9610c1d2 |
41 | traits => ['String'], |
e3c07b19 |
42 | is => 'rw', |
43 | isa => 'Str', |
2edb73d9 |
44 | default => q{}, |
9610c1d2 |
45 | handles => { |
5f3663b2 |
46 | add_text => 'append', |
47 | replace_text => 'replace', |
9610c1d2 |
48 | }, |
e3c07b19 |
49 | ); |
50 | |
51 | my $page = MyHomePage->new(); |
52 | $page->add_text("foo"); # same as $page->text($page->text . "foo"); |
53 | |
54 | =head1 DESCRIPTION |
55 | |
56 | This module provides a simple string attribute, to which mutating string |
57 | operations can be applied more easily (no need to make an lvalue attribute |
58 | metaclass or use temporary variables). Additional methods are provided for |
59 | completion. |
60 | |
61 | If your attribute definition does not include any of I<is>, I<isa>, |
5f3663b2 |
62 | I<default> or I<handles> but does use the C<String> metaclass, |
e3c07b19 |
63 | then this module applies defaults as in the L</SYNOPSIS> |
2420461c |
64 | above. This allows for a very basic string definition: |
e3c07b19 |
65 | |
2420461c |
66 | has 'foo' => (traits => ['String']); |
e3c07b19 |
67 | $obj->append_foo; |
68 | |
e3c07b19 |
69 | =head1 PROVIDED METHODS |
70 | |
79b647c6 |
71 | These methods are implemented in |
2420461c |
72 | L<Moose::Meta::Attribute::Native::MethodProvider::String>. It is important to |
79b647c6 |
73 | note that all those methods do in place modification of the value stored in |
74 | the attribute. |
e3c07b19 |
75 | |
76 | =over 4 |
77 | |
157e0475 |
78 | =item B<inc> |
e3c07b19 |
79 | |
80 | Increments the value stored in this slot using the magical string autoincrement |
a6dd8c94 |
81 | operator. Note that Perl doesn't provide analogous behavior in C<-->, so |
e3c07b19 |
82 | C<dec> is not available. |
83 | |
157e0475 |
84 | =item B<append($string)> |
e3c07b19 |
85 | |
86 | Append a string, like C<.=>. |
87 | |
157e0475 |
88 | =item B<prepend($string)> |
e3c07b19 |
89 | |
90 | Prepend a string. |
91 | |
157e0475 |
92 | =item B<replace($pattern, $replacement)> |
e3c07b19 |
93 | |
94 | Performs a regexp substitution (L<perlop/s>). There is no way to provide the |
95 | C<g> flag, but code references will be accepted for the replacement, causing |
96 | the regex to be modified with a single C<e>. C</smxi> can be applied using the |
97 | C<qr> operator. |
98 | |
157e0475 |
99 | =item B<match($pattern)> |
e3c07b19 |
100 | |
157e0475 |
101 | Like C<replace> but without the replacement. Provided mostly for completeness. |
e3c07b19 |
102 | |
157e0475 |
103 | =item B<chop> |
e3c07b19 |
104 | |
105 | L<perlfunc/chop> |
106 | |
157e0475 |
107 | =item B<chomp> |
e3c07b19 |
108 | |
109 | L<perlfunc/chomp> |
110 | |
157e0475 |
111 | =item B<clear> |
e3c07b19 |
112 | |
113 | Sets the string to the empty string (not the value passed to C<default>). |
114 | |
eb95da0e |
115 | =item B<length> |
116 | |
117 | L<perlfunc/length> |
118 | |
119 | =item B<substr> |
120 | |
121 | L<perlfunc/substr>. We go to some lengths to match the different functionality |
122 | based on C<substr>'s arity. |
123 | |
e3c07b19 |
124 | =back |
125 | |
79b647c6 |
126 | =head1 METHODS |
127 | |
128 | =over 4 |
129 | |
130 | =item B<meta> |
131 | |
132 | =item B<method_provider> |
133 | |
134 | =item B<has_method_provider> |
135 | |
136 | =back |
137 | |
e3c07b19 |
138 | =head1 BUGS |
139 | |
d4048ef3 |
140 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
141 | |
142 | =head1 AUTHOR |
143 | |
144 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
145 | |
146 | =head1 COPYRIGHT AND LICENSE |
147 | |
148 | Copyright 2007-2009 by Infinity Interactive, Inc. |
149 | |
150 | L<http://www.iinteractive.com> |
151 | |
152 | This library is free software; you can redistribute it and/or modify |
153 | it under the same terms as Perl itself. |
154 | |
155 | =cut |