| Filename | /2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/x86_64-linux/Moose/Meta/Attribute/Native.pm |
| Statements | Executed 37 statements in 322µs |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 10µs | 10µs | Moose::Meta::Attribute::Native::BEGIN@2 |
| 1 | 1 | 1 | 10µs | 45µs | Moose::Meta::Attribute::Native::BEGIN@9 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::Attribute::Native::__ANON__[:30] |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Moose::Meta::Attribute::Native; | ||||
| 2 | # spent 10µs within Moose::Meta::Attribute::Native::BEGIN@2 which was called:
# once (10µs+0s) by Moose::BEGIN@47 at line 4 | ||||
| 3 | 1 | 4µs | $Moose::Meta::Attribute::Native::AUTHORITY = 'cpan:STEVAN'; | ||
| 4 | 1 | 26µs | 1 | 10µs | } # spent 10µs making 1 call to Moose::Meta::Attribute::Native::BEGIN@2 |
| 5 | { | ||||
| 6 | 2 | 1µs | $Moose::Meta::Attribute::Native::VERSION = '2.0602'; | ||
| 7 | } | ||||
| 8 | |||||
| 9 | 3 | 230µs | 2 | 80µs | # spent 45µs (10+35) within Moose::Meta::Attribute::Native::BEGIN@9 which was called:
# once (10µs+35µs) by Moose::BEGIN@47 at line 9 # spent 45µs making 1 call to Moose::Meta::Attribute::Native::BEGIN@9
# spent 35µs making 1 call to Exporter::import |
| 10 | |||||
| 11 | 1 | 2µs | my @trait_names = qw(Bool Counter Number String Array Hash Code); | ||
| 12 | |||||
| 13 | 1 | 700ns | for my $trait_name (@trait_names) { | ||
| 14 | 28 | 47µs | my $trait_class = "Moose::Meta::Attribute::Native::Trait::$trait_name"; | ||
| 15 | 7 | 530µs | my $meta = Class::MOP::Class->initialize( # spent 530µs making 7 calls to Class::MOP::Class::initialize, avg 76µs/call | ||
| 16 | "Moose::Meta::Attribute::Custom::Trait::$trait_name" | ||||
| 17 | ); | ||||
| 18 | 7 | 504µs | if ($meta->find_method_by_name('register_implementation')) { # spent 504µs making 7 calls to Class::MOP::Class::find_method_by_name, avg 72µs/call | ||
| 19 | my $class = $meta->name->register_implementation; | ||||
| 20 | Moose->throw_error( | ||||
| 21 | "An implementation for $trait_name already exists " . | ||||
| 22 | "(found '$class' when trying to register '$trait_class')" | ||||
| 23 | ); | ||||
| 24 | } | ||||
| 25 | $meta->add_method(register_implementation => sub { | ||||
| 26 | # resolve_metatrait_alias will load classes anyway, but throws away | ||||
| 27 | # their error message; we WANT to die if there's a problem | ||||
| 28 | load_class($trait_class); | ||||
| 29 | return $trait_class; | ||||
| 30 | 7 | 335µs | }); # spent 335µs making 7 calls to Class::MOP::Mixin::HasMethods::add_method, avg 48µs/call | ||
| 31 | } | ||||
| 32 | |||||
| 33 | 1 | 11µs | 1; | ||
| 34 | |||||
| 35 | # ABSTRACT: Delegate to native Perl types | ||||
| 36 | |||||
| - - | |||||
| 39 | =pod | ||||
| 40 | |||||
| 41 | =head1 NAME | ||||
| 42 | |||||
| 43 | Moose::Meta::Attribute::Native - Delegate to native Perl types | ||||
| 44 | |||||
| 45 | =head1 VERSION | ||||
| 46 | |||||
| 47 | version 2.0602 | ||||
| 48 | |||||
| 49 | =head1 SYNOPSIS | ||||
| 50 | |||||
| 51 | package MyClass; | ||||
| 52 | use Moose; | ||||
| 53 | |||||
| 54 | has 'mapping' => ( | ||||
| 55 | traits => ['Hash'], | ||||
| 56 | is => 'rw', | ||||
| 57 | isa => 'HashRef[Str]', | ||||
| 58 | default => sub { {} }, | ||||
| 59 | handles => { | ||||
| 60 | exists_in_mapping => 'exists', | ||||
| 61 | ids_in_mapping => 'keys', | ||||
| 62 | get_mapping => 'get', | ||||
| 63 | set_mapping => 'set', | ||||
| 64 | set_quantity => [ set => 'quantity' ], | ||||
| 65 | }, | ||||
| 66 | ); | ||||
| 67 | |||||
| 68 | my $obj = MyClass->new; | ||||
| 69 | $obj->set_quantity(10); # quantity => 10 | ||||
| 70 | $obj->set_mapping('foo', 4); # foo => 4 | ||||
| 71 | $obj->set_mapping('bar', 5); # bar => 5 | ||||
| 72 | $obj->set_mapping('baz', 6); # baz => 6 | ||||
| 73 | |||||
| 74 | # prints 5 | ||||
| 75 | print $obj->get_mapping('bar') if $obj->exists_in_mapping('bar'); | ||||
| 76 | |||||
| 77 | # prints 'quantity, foo, bar, baz' | ||||
| 78 | print join ', ', $obj->ids_in_mapping; | ||||
| 79 | |||||
| 80 | =head1 DESCRIPTION | ||||
| 81 | |||||
| 82 | Native delegations allow you to delegate to native Perl data | ||||
| 83 | structures as if they were objects. For example, in the L</SYNOPSIS> you can | ||||
| 84 | see a hash reference being treated as if it has methods named C<exists()>, | ||||
| 85 | C<keys()>, C<get()>, and C<set()>. | ||||
| 86 | |||||
| 87 | The delegation methods (mostly) map to Perl builtins and operators. The return | ||||
| 88 | values of these delegations should be the same as the corresponding Perl | ||||
| 89 | operation. Any deviations will be explicitly documented. | ||||
| 90 | |||||
| 91 | =head1 API | ||||
| 92 | |||||
| 93 | Native delegations are enabled by passing certain options to C<has> when | ||||
| 94 | creating an attribute. | ||||
| 95 | |||||
| 96 | =head2 traits | ||||
| 97 | |||||
| 98 | To enable this feature, pass the appropriate name in the C<traits> array | ||||
| 99 | reference for the attribute. For example, to enable this feature for hash | ||||
| 100 | reference, we include C<'Hash'> in the list of traits. | ||||
| 101 | |||||
| 102 | =head2 isa | ||||
| 103 | |||||
| 104 | You will need to make sure that the attribute has an appropriate type. For | ||||
| 105 | example, to use this with a Hash you must specify that your attribute is some | ||||
| 106 | sort of C<HashRef>. | ||||
| 107 | |||||
| 108 | =head2 handles | ||||
| 109 | |||||
| 110 | This is just like any other delegation, but only a hash reference is allowed | ||||
| 111 | when defining native delegations. The keys are the methods to be created in | ||||
| 112 | the class which contains the attribute. The values are the methods provided by | ||||
| 113 | the associated trait. Currying works the same way as it does with any other | ||||
| 114 | delegation. | ||||
| 115 | |||||
| 116 | See the docs for each native trait for details on what methods are available. | ||||
| 117 | |||||
| 118 | =head2 is | ||||
| 119 | |||||
| 120 | Some traits provide a default C<is> for historical reasons. This behavior is | ||||
| 121 | deprecated, and you are strongly encouraged to provide a value. If you don't | ||||
| 122 | plan to read and write the attribute value directly, not passing the C<is> | ||||
| 123 | option will prevent standard accessor generation. | ||||
| 124 | |||||
| 125 | =head2 default or builder | ||||
| 126 | |||||
| 127 | Some traits provide a default C<default> for historical reasons. This behavior | ||||
| 128 | is deprecated, and you are strongly encouraged to provide a default value or | ||||
| 129 | make the attribute required. | ||||
| 130 | |||||
| 131 | =head1 TRAITS FOR NATIVE DELEGATIONS | ||||
| 132 | |||||
| 133 | =over | ||||
| 134 | |||||
| 135 | =item L<Array|Moose::Meta::Attribute::Native::Trait::Array> | ||||
| 136 | |||||
| 137 | has 'queue' => ( | ||||
| 138 | traits => ['Array'], | ||||
| 139 | is => 'ro', | ||||
| 140 | isa => 'ArrayRef[Str]', | ||||
| 141 | default => sub { [] }, | ||||
| 142 | handles => { | ||||
| 143 | add_item => 'push', | ||||
| 144 | next_item => 'shift', | ||||
| 145 | # ... | ||||
| 146 | } | ||||
| 147 | ); | ||||
| 148 | |||||
| 149 | =item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool> | ||||
| 150 | |||||
| 151 | has 'is_lit' => ( | ||||
| 152 | traits => ['Bool'], | ||||
| 153 | is => 'ro', | ||||
| 154 | isa => 'Bool', | ||||
| 155 | default => 0, | ||||
| 156 | handles => { | ||||
| 157 | illuminate => 'set', | ||||
| 158 | darken => 'unset', | ||||
| 159 | flip_switch => 'toggle', | ||||
| 160 | is_dark => 'not', | ||||
| 161 | # ... | ||||
| 162 | } | ||||
| 163 | ); | ||||
| 164 | |||||
| 165 | =item L<Code|Moose::Meta::Attribute::Native::Trait::Code> | ||||
| 166 | |||||
| 167 | has 'callback' => ( | ||||
| 168 | traits => ['Code'], | ||||
| 169 | is => 'ro', | ||||
| 170 | isa => 'CodeRef', | ||||
| 171 | default => sub { | ||||
| 172 | sub {'called'} | ||||
| 173 | }, | ||||
| 174 | handles => { | ||||
| 175 | call => 'execute', | ||||
| 176 | # ... | ||||
| 177 | } | ||||
| 178 | ); | ||||
| 179 | |||||
| 180 | =item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter> | ||||
| 181 | |||||
| 182 | has 'counter' => ( | ||||
| 183 | traits => ['Counter'], | ||||
| 184 | is => 'ro', | ||||
| 185 | isa => 'Num', | ||||
| 186 | default => 0, | ||||
| 187 | handles => { | ||||
| 188 | inc_counter => 'inc', | ||||
| 189 | dec_counter => 'dec', | ||||
| 190 | reset_counter => 'reset', | ||||
| 191 | # ... | ||||
| 192 | } | ||||
| 193 | ); | ||||
| 194 | |||||
| 195 | =item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash> | ||||
| 196 | |||||
| 197 | has 'options' => ( | ||||
| 198 | traits => ['Hash'], | ||||
| 199 | is => 'ro', | ||||
| 200 | isa => 'HashRef[Str]', | ||||
| 201 | default => sub { {} }, | ||||
| 202 | handles => { | ||||
| 203 | set_option => 'set', | ||||
| 204 | get_option => 'get', | ||||
| 205 | has_option => 'exists', | ||||
| 206 | # ... | ||||
| 207 | } | ||||
| 208 | ); | ||||
| 209 | |||||
| 210 | =item L<Number|Moose::Meta::Attribute::Native::Trait::Number> | ||||
| 211 | |||||
| 212 | has 'integer' => ( | ||||
| 213 | traits => ['Number'], | ||||
| 214 | is => 'ro', | ||||
| 215 | isa => 'Int', | ||||
| 216 | default => 5, | ||||
| 217 | handles => { | ||||
| 218 | set => 'set', | ||||
| 219 | add => 'add', | ||||
| 220 | sub => 'sub', | ||||
| 221 | mul => 'mul', | ||||
| 222 | div => 'div', | ||||
| 223 | mod => 'mod', | ||||
| 224 | abs => 'abs', | ||||
| 225 | # ... | ||||
| 226 | } | ||||
| 227 | ); | ||||
| 228 | |||||
| 229 | =item L<String|Moose::Meta::Attribute::Native::Trait::String> | ||||
| 230 | |||||
| 231 | has 'text' => ( | ||||
| 232 | traits => ['String'], | ||||
| 233 | is => 'ro', | ||||
| 234 | isa => 'Str', | ||||
| 235 | default => q{}, | ||||
| 236 | handles => { | ||||
| 237 | add_text => 'append', | ||||
| 238 | replace_text => 'replace', | ||||
| 239 | # ... | ||||
| 240 | } | ||||
| 241 | ); | ||||
| 242 | |||||
| 243 | =back | ||||
| 244 | |||||
| 245 | =head1 COMPATIBILITY WITH MooseX::AttributeHelpers | ||||
| 246 | |||||
| 247 | This feature used to be a separated CPAN distribution called | ||||
| 248 | L<MooseX::AttributeHelpers>. | ||||
| 249 | |||||
| 250 | When the feature was incorporated into the Moose core, some of the API details | ||||
| 251 | were changed. The underlying capabilities are the same, but some details of | ||||
| 252 | the API were changed. | ||||
| 253 | |||||
| 254 | =head1 BUGS | ||||
| 255 | |||||
| 256 | See L<Moose/BUGS> for details on reporting bugs. | ||||
| 257 | |||||
| 258 | =head1 AUTHOR | ||||
| 259 | |||||
| 260 | Moose is maintained by the Moose Cabal, along with the help of many contributors. See L<Moose/CABAL> and L<Moose/CONTRIBUTORS> for details. | ||||
| 261 | |||||
| 262 | =head1 COPYRIGHT AND LICENSE | ||||
| 263 | |||||
| 264 | This software is copyright (c) 2012 by Infinity Interactive, Inc.. | ||||
| 265 | |||||
| 266 | This is free software; you can redistribute it and/or modify it under | ||||
| 267 | the same terms as the Perl 5 programming language system itself. | ||||
| 268 | |||||
| 269 | =cut | ||||
| 270 | |||||
| 271 | |||||
| 272 | __END__ |