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