| Filename | /2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/Any/Moose.pm |
| Statements | Executed 90 statements in 1.25ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 979µs | 7.03ms | Any::Moose::_install_module |
| 1 | 1 | 1 | 30µs | 34µs | Any::Moose::_backer_of |
| 1 | 1 | 1 | 30µs | 7.15ms | Any::Moose::import |
| 1 | 1 | 1 | 18µs | 18µs | Any::Moose::BEGIN@7 |
| 1 | 1 | 1 | 16µs | 73µs | Any::Moose::any_moose |
| 1 | 1 | 1 | 16µs | 21µs | Any::Moose::_canonicalize_fragment |
| 1 | 1 | 1 | 11µs | 84µs | Any::Moose::_canonicalize_options |
| 1 | 1 | 1 | 9µs | 25µs | Any::Moose::BEGIN@9 |
| 1 | 1 | 1 | 7µs | 12µs | Any::Moose::BEGIN@8 |
| 5 | 5 | 1 | 7µs | 7µs | Any::Moose::CORE:subst (opcode) |
| 1 | 1 | 1 | 7µs | 22µs | Any::Moose::BEGIN@69 |
| 1 | 1 | 1 | 7µs | 15µs | Any::Moose::BEGIN@191 |
| 7 | 1 | 1 | 6µs | 6µs | Any::Moose::moose_is_preferred |
| 2 | 2 | 1 | 3µs | 3µs | Any::Moose::_is_moose_loaded |
| 1 | 1 | 1 | 1µs | 1µs | Any::Moose::mouse_is_preferred |
| 2 | 2 | 1 | 800ns | 800ns | Any::Moose::CORE:match (opcode) |
| 0 | 0 | 0 | 0s | 0s | Any::Moose::is_moose_loaded |
| 0 | 0 | 0 | 0s | 0s | Any::Moose::unimport |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Any::Moose; | ||||
| 2 | { | ||||
| 3 | 2 | 800ns | $Any::Moose::VERSION = '0.18'; | ||
| 4 | } | ||||
| 5 | # ABSTRACT: use Moose or Mouse modules | ||||
| 6 | |||||
| 7 | 3 | 30µs | 1 | 18µs | # spent 18µs within Any::Moose::BEGIN@7 which was called:
# once (18µs+0s) by MooseX::Log::Log4perl::BEGIN@4 at line 7 # spent 18µs making 1 call to Any::Moose::BEGIN@7 |
| 8 | 3 | 17µs | 2 | 16µs | # spent 12µs (7+4) within Any::Moose::BEGIN@8 which was called:
# once (7µs+4µs) by MooseX::Log::Log4perl::BEGIN@4 at line 8 # spent 12µs making 1 call to Any::Moose::BEGIN@8
# spent 4µs making 1 call to strict::import |
| 9 | 3 | 223µs | 2 | 41µs | # spent 25µs (9+16) within Any::Moose::BEGIN@9 which was called:
# once (9µs+16µs) by MooseX::Log::Log4perl::BEGIN@4 at line 9 # spent 25µs making 1 call to Any::Moose::BEGIN@9
# spent 16µs making 1 call to warnings::import |
| 10 | |||||
| 11 | # decide which backend to use | ||||
| 12 | 1 | 100ns | our $PREFERRED; | ||
| 13 | 1 | 400ns | do { | ||
| 14 | 1 | 300ns | local $@; | ||
| 15 | 1 | 3µs | 1 | 2µs | if ($ENV{ANY_MOOSE}) { # spent 2µs making 1 call to Any::Moose::_is_moose_loaded |
| 16 | $PREFERRED = $ENV{'ANY_MOOSE'}; | ||||
| 17 | warn "ANY_MOOSE is not set to Moose or Mouse" | ||||
| 18 | unless $PREFERRED eq 'Moose' | ||||
| 19 | || $PREFERRED eq 'Mouse'; | ||||
| 20 | |||||
| 21 | # if we die here, then perl gives "unknown error" which doesn't tell | ||||
| 22 | # you what the problem is at all. argh. | ||||
| 23 | if ($PREFERRED eq 'Moose' && !eval { require Moose }) { | ||||
| 24 | warn "\$ANY_MOOSE is set to Moose but we cannot load it"; | ||||
| 25 | } | ||||
| 26 | elsif ($PREFERRED eq 'Mouse' && !eval { require Mouse }) { | ||||
| 27 | warn "\$ANY_MOOSE is set to Mouse but we cannot load it"; | ||||
| 28 | } | ||||
| 29 | } | ||||
| 30 | elsif (_is_moose_loaded()) { | ||||
| 31 | 1 | 600ns | $PREFERRED = 'Moose'; | ||
| 32 | } | ||||
| 33 | elsif (eval { require Mouse }) { | ||||
| 34 | $PREFERRED = 'Mouse'; | ||||
| 35 | } | ||||
| 36 | elsif (eval { require Moose }) { | ||||
| 37 | $PREFERRED = 'Moose'; | ||||
| 38 | } | ||||
| 39 | else { | ||||
| 40 | require Carp; | ||||
| 41 | warn "Unable to locate Mouse or Moose in INC"; | ||||
| 42 | } | ||||
| 43 | }; | ||||
| 44 | |||||
| 45 | # spent 7.15ms (30µs+7.12) within Any::Moose::import which was called:
# once (30µs+7.12ms) by MooseX::Log::Log4perl::BEGIN@4 at line 4 of MooseX/Log/Log4perl.pm | ||||
| 46 | 1 | 500ns | my $self = shift; | ||
| 47 | 1 | 1µs | my $pkg = caller; | ||
| 48 | |||||
| 49 | # Any::Moose gives you strict and warnings | ||||
| 50 | 1 | 1µs | 1 | 2µs | strict->import; # spent 2µs making 1 call to strict::import |
| 51 | 1 | 1µs | 1 | 8µs | warnings->import; # spent 8µs making 1 call to warnings::import |
| 52 | |||||
| 53 | # first options are for Mo*se | ||||
| 54 | 1 | 600ns | unshift @_, 'Moose' if !@_ || ref($_[0]); | ||
| 55 | |||||
| 56 | 1 | 2µs | while (my $module = shift) { | ||
| 57 | 1 | 1µs | my $options = @_ && ref($_[0]) ? shift : []; | ||
| 58 | |||||
| 59 | 1 | 2µs | 1 | 84µs | $options = $self->_canonicalize_options( # spent 84µs making 1 call to Any::Moose::_canonicalize_options |
| 60 | module => $module, | ||||
| 61 | options => $options, | ||||
| 62 | package => $pkg, | ||||
| 63 | ); | ||||
| 64 | |||||
| 65 | 1 | 4µs | 1 | 7.03ms | $self->_install_module($options); # spent 7.03ms making 1 call to Any::Moose::_install_module |
| 66 | } | ||||
| 67 | |||||
| 68 | # give them any_moose too | ||||
| 69 | 3 | 446µs | 2 | 36µs | # spent 22µs (7+15) within Any::Moose::BEGIN@69 which was called:
# once (7µs+15µs) by MooseX::Log::Log4perl::BEGIN@4 at line 69 # spent 22µs making 1 call to Any::Moose::BEGIN@69
# spent 15µs making 1 call to strict::unimport |
| 70 | 1 | 10µs | *{$pkg.'::any_moose'} = \&any_moose; | ||
| 71 | } | ||||
| 72 | |||||
| 73 | sub unimport { | ||||
| 74 | my $sel = shift; | ||||
| 75 | my $pkg = caller; | ||||
| 76 | my $module; | ||||
| 77 | |||||
| 78 | if(@_){ | ||||
| 79 | $module = any_moose(shift, $pkg); | ||||
| 80 | } | ||||
| 81 | else { | ||||
| 82 | $module = _backer_of($pkg); | ||||
| 83 | } | ||||
| 84 | my $e = do{ | ||||
| 85 | local $@; | ||||
| 86 | eval "package $pkg;\n" | ||||
| 87 | . '$module->unimport();'; | ||||
| 88 | $@; | ||||
| 89 | }; | ||||
| 90 | |||||
| 91 | if ($e) { | ||||
| 92 | require Carp; | ||||
| 93 | Carp::croak("Cannot unimport Any::Moose: $e"); | ||||
| 94 | } | ||||
| 95 | |||||
| 96 | return; | ||||
| 97 | } | ||||
| 98 | |||||
| 99 | # spent 34µs (30+4) within Any::Moose::_backer_of which was called:
# once (30µs+4µs) by Any::Moose::any_moose at line 169 | ||||
| 100 | 1 | 300ns | my $pkg = shift; | ||
| 101 | |||||
| 102 | 1 | 600ns | if(exists $INC{'Mouse.pm'}){ | ||
| 103 | my $meta = Mouse::Util::get_metaclass_by_name($pkg); | ||||
| 104 | if ($meta) { | ||||
| 105 | return 'Mouse::Role' if $meta->isa('Mouse::Meta::Role'); | ||||
| 106 | return 'Mouse' if $meta->isa('Mouse::Meta::Class'); | ||||
| 107 | } | ||||
| 108 | } | ||||
| 109 | |||||
| 110 | 1 | 1µs | 1 | 1µs | if (_is_moose_loaded()) { # spent 1µs making 1 call to Any::Moose::_is_moose_loaded |
| 111 | 1 | 2µs | 1 | 3µs | my $meta = Class::MOP::get_metaclass_by_name($pkg); # spent 3µs making 1 call to Class::MOP::get_metaclass_by_name |
| 112 | 1 | 400ns | if ($meta) { | ||
| 113 | return 'Moose::Role' if $meta->isa('Moose::Meta::Role'); | ||||
| 114 | return 'Moose' if $meta->isa('Moose::Meta::Class'); | ||||
| 115 | } | ||||
| 116 | } | ||||
| 117 | |||||
| 118 | 1 | 3µs | return undef; | ||
| 119 | } | ||||
| 120 | |||||
| 121 | # spent 84µs (11+73) within Any::Moose::_canonicalize_options which was called:
# once (11µs+73µs) by Any::Moose::import at line 59 | ||||
| 122 | 1 | 300ns | my $self = shift; | ||
| 123 | 1 | 3µs | my %args = @_; | ||
| 124 | |||||
| 125 | 1 | 200ns | my %options; | ||
| 126 | 1 | 800ns | if (ref($args{options}) eq 'HASH') { | ||
| 127 | %options = %{ $args{options} }; | ||||
| 128 | } | ||||
| 129 | else { | ||||
| 130 | 1 | 1µs | %options = ( | ||
| 131 | imports => $args{options}, | ||||
| 132 | ); | ||||
| 133 | } | ||||
| 134 | |||||
| 135 | 1 | 600ns | $options{package} = $args{package}; | ||
| 136 | 1 | 2µs | 1 | 73µs | $options{module} = any_moose($args{module}, $args{package}); # spent 73µs making 1 call to Any::Moose::any_moose |
| 137 | |||||
| 138 | 1 | 4µs | return \%options; | ||
| 139 | } | ||||
| 140 | |||||
| 141 | # spent 7.03ms (979µs+6.05) within Any::Moose::_install_module which was called:
# once (979µs+6.05ms) by Any::Moose::import at line 65 | ||||
| 142 | 1 | 400ns | my $self = shift; | ||
| 143 | 1 | 300ns | my $options = shift; | ||
| 144 | |||||
| 145 | 1 | 500ns | my $module = $options->{module}; | ||
| 146 | 1 | 5µs | 1 | 2µs | (my $file = $module . '.pm') =~ s{::}{/}g; # spent 2µs making 1 call to Any::Moose::CORE:subst |
| 147 | |||||
| 148 | 1 | 68µs | require $file; | ||
| 149 | |||||
| 150 | 1 | 800ns | my $e = do { | ||
| 151 | 1 | 600ns | local $@; | ||
| 152 | 1 | 44µs | eval "package $options->{package};\n" # spent 10µs executing statements in string eval | ||
| 153 | . '$module->import(@{ $options->{imports} });'; | ||||
| 154 | 1 | 900ns | $@; | ||
| 155 | }; | ||||
| 156 | 1 | 400ns | if ($e) { | ||
| 157 | require Carp; | ||||
| 158 | Carp::croak("Cannot import Any::Moose: $e"); | ||||
| 159 | } | ||||
| 160 | 1 | 5µs | return; | ||
| 161 | } | ||||
| 162 | |||||
| 163 | # spent 73µs (16+57) within Any::Moose::any_moose which was called:
# once (16µs+57µs) by Any::Moose::_canonicalize_options at line 136 | ||||
| 164 | 1 | 1µs | 1 | 21µs | my $fragment = _canonicalize_fragment(shift); # spent 21µs making 1 call to Any::Moose::_canonicalize_fragment |
| 165 | 1 | 400ns | my $package = shift || caller; | ||
| 166 | |||||
| 167 | # Mouse gets first dibs because it doesn't introspect existing classes | ||||
| 168 | |||||
| 169 | 1 | 1µs | 1 | 34µs | my $backer = _backer_of($package) || ''; # spent 34µs making 1 call to Any::Moose::_backer_of |
| 170 | |||||
| 171 | 1 | 4µs | 1 | 600ns | if ($backer =~ /^Mouse/) { # spent 600ns making 1 call to Any::Moose::CORE:match |
| 172 | $fragment =~ s/^Moose/Mouse/; | ||||
| 173 | return $fragment; | ||||
| 174 | } | ||||
| 175 | |||||
| 176 | 1 | 2µs | 1 | 200ns | return $fragment if $backer =~ /^Moose/; # spent 200ns making 1 call to Any::Moose::CORE:match |
| 177 | |||||
| 178 | 1 | 1µs | 1 | 1µs | $fragment =~ s/^Moose/Mouse/ if mouse_is_preferred(); # spent 1µs making 1 call to Any::Moose::mouse_is_preferred |
| 179 | 1 | 3µs | return $fragment; | ||
| 180 | } | ||||
| 181 | |||||
| 182 | 1 | 1µs | for my $name (qw/ | ||
| 183 | load_class | ||||
| 184 | is_class_loaded | ||||
| 185 | class_of | ||||
| 186 | get_metaclass_by_name | ||||
| 187 | get_all_metaclass_instances | ||||
| 188 | get_all_metaclass_names | ||||
| 189 | load_first_existing_class | ||||
| 190 | /) { | ||||
| 191 | 3 | 225µs | 2 | 24µs | # spent 15µs (7+8) within Any::Moose::BEGIN@191 which was called:
# once (7µs+8µs) by MooseX::Log::Log4perl::BEGIN@4 at line 191 # spent 15µs making 1 call to Any::Moose::BEGIN@191
# spent 8µs making 1 call to strict::unimport |
| 192 | *{__PACKAGE__."::$name"} = moose_is_preferred() | ||||
| 193 | ? *{"Class::MOP::$name"} | ||||
| 194 | 7 | 34µs | 7 | 6µs | : *{"Mouse::Util::$name"}; # spent 6µs making 7 calls to Any::Moose::moose_is_preferred, avg 929ns/call |
| 195 | } | ||||
| 196 | |||||
| 197 | 7 | 14µs | # spent 6µs within Any::Moose::moose_is_preferred which was called 7 times, avg 929ns/call:
# 7 times (6µs+0s) by MooseX::Log::Log4perl::BEGIN@4 at line 194, avg 929ns/call | ||
| 198 | 1 | 3µs | # spent 1µs within Any::Moose::mouse_is_preferred which was called:
# once (1µs+0s) by Any::Moose::any_moose at line 178 | ||
| 199 | |||||
| 200 | 2 | 31µs | sub _is_moose_loaded { exists $INC{'Moose.pm'} } | ||
| 201 | |||||
| 202 | sub is_moose_loaded { | ||||
| 203 | require Carp; | ||||
| 204 | Carp::carp("Any::Moose::is_moose_loaded is deprecated. Please use Any::Moose::moose_is_preferred instead"); | ||||
| 205 | goto \&_is_moose_loaded; | ||||
| 206 | } | ||||
| 207 | |||||
| 208 | # spent 21µs (16+5) within Any::Moose::_canonicalize_fragment which was called:
# once (16µs+5µs) by Any::Moose::any_moose at line 164 | ||||
| 209 | 1 | 300ns | my $fragment = shift; | ||
| 210 | |||||
| 211 | 1 | 200ns | return 'Moose' if !$fragment; | ||
| 212 | |||||
| 213 | # any_moose("X::Types") -> any_moose("MooseX::Types") | ||||
| 214 | 1 | 5µs | 1 | 800ns | $fragment =~ s/^X::/MooseX::/; # spent 800ns making 1 call to Any::Moose::CORE:subst |
| 215 | |||||
| 216 | # any_moose("::Util") -> any_moose("Moose::Util") | ||||
| 217 | 1 | 2µs | 1 | 300ns | $fragment =~ s/^::/Moose::/; # spent 300ns making 1 call to Any::Moose::CORE:subst |
| 218 | |||||
| 219 | # any_moose("Mouse::Util") -> any_moose("Moose::Util") | ||||
| 220 | 1 | 7µs | 1 | 300ns | $fragment =~ s/^Mouse(X?)\b/Moose$1/; # spent 300ns making 1 call to Any::Moose::CORE:subst |
| 221 | |||||
| 222 | # any_moose("Util") -> any_moose("Moose::Util") | ||||
| 223 | 1 | 5µs | 1 | 4µs | $fragment =~ s/^(?!Moose)/Moose::/; # spent 4µs making 1 call to Any::Moose::CORE:subst |
| 224 | |||||
| 225 | 1 | 3µs | return $fragment; | ||
| 226 | } | ||||
| 227 | |||||
| 228 | 1 | 14µs | 1; | ||
| 229 | |||||
| 230 | |||||
| 231 | =pod | ||||
| 232 | |||||
| 233 | =head1 NAME | ||||
| 234 | |||||
| 235 | Any::Moose - use Moose or Mouse modules | ||||
| 236 | |||||
| 237 | =head1 VERSION | ||||
| 238 | |||||
| 239 | version 0.18 | ||||
| 240 | |||||
| 241 | =head1 SYNOPSIS | ||||
| 242 | |||||
| 243 | =head2 BASIC | ||||
| 244 | |||||
| 245 | package Class; | ||||
| 246 | |||||
| 247 | # uses Moose if it's loaded or demanded, Mouse otherwise | ||||
| 248 | use Any::Moose; | ||||
| 249 | |||||
| 250 | # cleans the namespace up | ||||
| 251 | no Any::Moose; | ||||
| 252 | |||||
| 253 | =head2 OTHER MODULES | ||||
| 254 | |||||
| 255 | package Other::Class; | ||||
| 256 | use Any::Moose; | ||||
| 257 | |||||
| 258 | # uses Moose::Util::TypeConstraints if the class has loaded Moose, | ||||
| 259 | # Mouse::Util::TypeConstraints otherwise. | ||||
| 260 | use Any::Moose '::Util::TypeConstraints'; | ||||
| 261 | |||||
| 262 | =head2 ROLES | ||||
| 263 | |||||
| 264 | package My::Sorter; | ||||
| 265 | use Any::Moose 'Role'; | ||||
| 266 | |||||
| 267 | requires 'cmp'; | ||||
| 268 | |||||
| 269 | =head2 COMPLEX USAGE | ||||
| 270 | |||||
| 271 | package My::Meta::Class; | ||||
| 272 | use Any::Moose; | ||||
| 273 | |||||
| 274 | # uses subtype from Moose::Util::TypeConstraints if the class loaded Moose, | ||||
| 275 | # subtype from Mouse::Util::TypeConstraints otherwise. | ||||
| 276 | # similarly for Mo*se::Util's does_role | ||||
| 277 | use Any::Moose ( | ||||
| 278 | '::Util::TypeConstraints' => ['subtype'], | ||||
| 279 | '::Util' => ['does_role'], | ||||
| 280 | ); | ||||
| 281 | |||||
| 282 | # uses MouseX::Types or MooseX::Types | ||||
| 283 | use Any::Moose 'X::Types'; | ||||
| 284 | |||||
| 285 | # gives you the right class name depending on which Mo*se was loaded | ||||
| 286 | extends any_moose('::Meta::Class'); | ||||
| 287 | |||||
| 288 | =head1 DESCRIPTION | ||||
| 289 | |||||
| 290 | Though we recommend that people generally use L<Moose>, we accept that Moose | ||||
| 291 | cannot yet be used for everything everywhere. People generally like the Moose | ||||
| 292 | sugar, so many people use L<Mouse>, a lightweight replacement for parts of | ||||
| 293 | Moose. | ||||
| 294 | |||||
| 295 | Because Mouse strives for compatibility with Moose, it's easy to substitute one | ||||
| 296 | for the other. This module facilitates that substitution. By default, Mouse | ||||
| 297 | will be provided to libraries, unless Moose is already loaded -or- | ||||
| 298 | explicitly requested by the end-user. The end-user can force the decision | ||||
| 299 | of which backend to use by setting the environment variable C<ANY_MOOSE> to | ||||
| 300 | be C<Moose> or C<Mouse>. | ||||
| 301 | |||||
| 302 | Note that the decision of which backend to use is made only once, so that if | ||||
| 303 | Any-Moose picks Mouse, then a third-party library loads Moose, anything else | ||||
| 304 | that uses Any-Moose will continue to pick Mouse. | ||||
| 305 | |||||
| 306 | So, if you have to use L<Mouse>, please be considerate to the Moose fanboys | ||||
| 307 | (like myself!) and use L<Any-Moose> instead. C<:)> | ||||
| 308 | |||||
| 309 | =head1 SEE ALSO | ||||
| 310 | |||||
| 311 | L<Moose> | ||||
| 312 | |||||
| 313 | L<Mouse> | ||||
| 314 | |||||
| 315 | L<Squirrel> - a deprecated first-stab at Any-Moose-like logic. Its biggest | ||||
| 316 | fault was in making the decision of which backend to use every time it was | ||||
| 317 | used, rather than just once. | ||||
| 318 | |||||
| 319 | =head1 AUTHORS | ||||
| 320 | |||||
| 321 | =over 4 | ||||
| 322 | |||||
| 323 | =item * | ||||
| 324 | |||||
| 325 | Shawn M Moore <sartak@gmail.com> | ||||
| 326 | |||||
| 327 | =item * | ||||
| 328 | |||||
| 329 | Florian Ragwitz <rafl@debian.org> | ||||
| 330 | |||||
| 331 | =item * | ||||
| 332 | |||||
| 333 | Stevan Little <stevan@iinteractive.com> | ||||
| 334 | |||||
| 335 | =item * | ||||
| 336 | |||||
| 337 | Tokuhiro Matsuno <tokuhirom@gmail.com> | ||||
| 338 | |||||
| 339 | =item * | ||||
| 340 | |||||
| 341 | Goro Fuji <gfuji@cpan.org> | ||||
| 342 | |||||
| 343 | =item * | ||||
| 344 | |||||
| 345 | Ricardo Signes <rjbs@cpan.org> | ||||
| 346 | |||||
| 347 | =back | ||||
| 348 | |||||
| 349 | =head1 COPYRIGHT AND LICENSE | ||||
| 350 | |||||
| 351 | This software is copyright (c) 2011 by Best Practical Solutions. | ||||
| 352 | |||||
| 353 | This is free software; you can redistribute it and/or modify it under | ||||
| 354 | the same terms as the Perl 5 programming language system itself. | ||||
| 355 | |||||
| 356 | =cut | ||||
| 357 | |||||
| 358 | |||||
| 359 | __END__ | ||||
sub Any::Moose::CORE:match; # opcode | |||||
# spent 7µs within Any::Moose::CORE:subst which was called 5 times, avg 1µs/call:
# once (4µs+0s) by Any::Moose::_canonicalize_fragment at line 223
# once (2µs+0s) by Any::Moose::_install_module at line 146
# once (800ns+0s) by Any::Moose::_canonicalize_fragment at line 214
# once (300ns+0s) by Any::Moose::_canonicalize_fragment at line 217
# once (300ns+0s) by Any::Moose::_canonicalize_fragment at line 220 |