| Filename | /Users/dde/perl5/perlbrew/perls/5.18.0t/lib/site_perl/5.18.0/Try/Tiny.pm |
| Statements | Executed 16473 statements in 11.2ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 650 | 13 | 10 | 8.61ms | 402ms | Try::Tiny::try (recurses: max depth 4, inclusive time 315ms) |
| 621 | 11 | 8 | 1.39ms | 1.39ms | Try::Tiny::catch |
| 1 | 1 | 1 | 826µs | 1.04ms | Try::Tiny::ScopeGuard::BEGIN@144 |
| 1 | 1 | 1 | 31µs | 604µs | Try::Tiny::BEGIN@20 |
| 1 | 1 | 1 | 8µs | 8µs | Try::Tiny::BEGIN@8 |
| 1 | 1 | 1 | 7µs | 7µs | Try::Tiny::BEGIN@2 |
| 1 | 1 | 1 | 6µs | 34µs | Try::Tiny::BEGIN@17 |
| 1 | 1 | 1 | 6µs | 9µs | Try::Tiny::BEGIN@12 |
| 1 | 1 | 1 | 5µs | 53µs | Try::Tiny::BEGIN@14 |
| 1 | 1 | 1 | 5µs | 16µs | Try::Tiny::BEGIN@11 |
| 0 | 0 | 0 | 0s | 0s | Try::Tiny::ScopeGuard::DESTROY |
| 0 | 0 | 0 | 0s | 0s | Try::Tiny::ScopeGuard::_new |
| 0 | 0 | 0 | 0s | 0s | Try::Tiny::__ANON__[:20] |
| 0 | 0 | 0 | 0s | 0s | Try::Tiny::finally |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Try::Tiny; | ||||
| 2 | # spent 7µs within Try::Tiny::BEGIN@2 which was called:
# once (7µs+0s) by Module::Implementation::BEGIN@13 at line 4 | ||||
| 3 | 1 | 4µs | $Try::Tiny::AUTHORITY = 'cpan:NUFFIN'; | ||
| 4 | 1 | 24µs | 1 | 7µs | } # spent 7µs making 1 call to Try::Tiny::BEGIN@2 |
| 5 | { | ||||
| 6 | 2 | 800ns | $Try::Tiny::VERSION = '0.16'; | ||
| 7 | } | ||||
| 8 | 2 | 35µs | 1 | 8µs | # spent 8µs within Try::Tiny::BEGIN@8 which was called:
# once (8µs+0s) by Module::Implementation::BEGIN@13 at line 8 # spent 8µs making 1 call to Try::Tiny::BEGIN@8 |
| 9 | # ABSTRACT: minimal try/catch with proper preservation of $@ | ||||
| 10 | |||||
| 11 | 2 | 18µs | 2 | 27µs | # spent 16µs (5+11) within Try::Tiny::BEGIN@11 which was called:
# once (5µs+11µs) by Module::Implementation::BEGIN@13 at line 11 # spent 16µs making 1 call to Try::Tiny::BEGIN@11
# spent 11µs making 1 call to strict::import |
| 12 | 2 | 70µs | 2 | 12µs | # spent 9µs (6+3) within Try::Tiny::BEGIN@12 which was called:
# once (6µs+3µs) by Module::Implementation::BEGIN@13 at line 12 # spent 9µs making 1 call to Try::Tiny::BEGIN@12
# spent 3µs making 1 call to warnings::import |
| 13 | |||||
| 14 | 2 | 34µs | 2 | 101µs | # spent 53µs (5+48) within Try::Tiny::BEGIN@14 which was called:
# once (5µs+48µs) by Module::Implementation::BEGIN@13 at line 14 # spent 53µs making 1 call to Try::Tiny::BEGIN@14
# spent 48µs making 1 call to base::import |
| 15 | 1 | 1µs | our @EXPORT = our @EXPORT_OK = qw(try catch finally); | ||
| 16 | |||||
| 17 | 2 | 40µs | 2 | 62µs | # spent 34µs (6+28) within Try::Tiny::BEGIN@17 which was called:
# once (6µs+28µs) by Module::Implementation::BEGIN@13 at line 17 # spent 34µs making 1 call to Try::Tiny::BEGIN@17
# spent 28µs making 1 call to Exporter::import |
| 18 | 1 | 500ns | $Carp::Internal{+__PACKAGE__}++; | ||
| 19 | |||||
| 20 | 1 | 373µs | 1 | 604µs | # spent 604µs (31+573) within Try::Tiny::BEGIN@20 which was called:
# once (31µs+573µs) by Module::Implementation::BEGIN@13 at line 20 # spent 604µs making 1 call to Try::Tiny::BEGIN@20 # spent 94µs executing statements in string eval # includes 243µs spent executing 1 call to 1 sub defined therein. |
| 21 | |||||
| 22 | # Need to prototype as @ not $$ because of the way Perl evaluates the prototype. | ||||
| 23 | # Keeping it at $$ means you only ever get 1 sub because we need to eval in a list | ||||
| 24 | # context & not a scalar one | ||||
| 25 | |||||
| 26 | # spent 402ms (8.61+393) within Try::Tiny::try which was called 650 times, avg 618µs/call:
# 223 times (2.74ms+29.9ms) by Class::MOP::Attribute::_process_accessors at line 401 of Class/MOP/Attribute.pm, avg 146µs/call
# 151 times (1.99ms+282ms) by Class::MOP::Class::_post_add_attribute at line 896 of Class/MOP/Class.pm, avg 1.88ms/call
# 61 times (816µs+-816µs) by Moose::Meta::Method::Accessor::_compile_code at line 47 of Moose/Meta/Method/Accessor.pm, avg 0s/call
# 58 times (828µs+-828µs) by Class::MOP::Method::Accessor::_generate_reader_method_inline at line 157 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 44 times (746µs+-746µs) by Class::MOP::Method::Accessor::_generate_accessor_method_inline at line 123 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 31 times (417µs+22.7ms) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 115 of Class/MOP/Method/Constructor.pm, avg 747µs/call
# 25 times (259µs+-184µs) by Moose::Util::does_role at line 54 of Moose/Util.pm, avg 3µs/call
# 24 times (339µs+-339µs) by Class::MOP::Method::Accessor::_generate_predicate_method_inline at line 212 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 22 times (332µs+58.0ms) by Class::Load::try_load_class at line 183 of Class/Load.pm, avg 2.65ms/call
# 4 times (43µs+292µs) by Moose::Meta::Attribute::does at line 45 of Moose/Meta/Attribute.pm, avg 84µs/call
# 3 times (53µs+2.72ms) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 925µs/call
# 3 times (39µs+-39µs) by Class::MOP::Method::Accessor::_generate_writer_method_inline at line 187 of Class/MOP/Method/Accessor.pm, avg 0s/call
# once (15µs+152µs) by Moose::Meta::Method::Destructor::_initialize_body at line 106 of Moose/Meta/Method/Destructor.pm | ||||
| 27 | 650 | 200µs | my ( $try, @code_refs ) = @_; | ||
| 28 | |||||
| 29 | # we need to save this here, the eval block will be in scalar context due | ||||
| 30 | # to $failed | ||||
| 31 | 650 | 89µs | my $wantarray = wantarray; | ||
| 32 | |||||
| 33 | 650 | 7µs | my ( $catch, @finally ); | ||
| 34 | |||||
| 35 | # find labeled blocks in the argument list. | ||||
| 36 | # catch and finally tag the blocks by blessing a scalar reference to them. | ||||
| 37 | 650 | 233µs | foreach my $code_ref (@code_refs) { | ||
| 38 | |||||
| 39 | 621 | 513µs | if ( ref($code_ref) eq 'Try::Tiny::Catch' ) { | ||
| 40 | 621 | 2µs | croak 'A try() may not be followed by multiple catch() blocks' | ||
| 41 | if $catch; | ||||
| 42 | 621 | 127µs | $catch = ${$code_ref}; | ||
| 43 | } elsif ( ref($code_ref) eq 'Try::Tiny::Finally' ) { | ||||
| 44 | push @finally, ${$code_ref}; | ||||
| 45 | } else { | ||||
| 46 | croak( | ||||
| 47 | 'try() encountered an unexpected argument (' | ||||
| 48 | . ( defined $code_ref ? $code_ref : 'undef' ) | ||||
| 49 | . ') - perhaps a missing semi-colon before or' | ||||
| 50 | ); | ||||
| 51 | } | ||||
| 52 | } | ||||
| 53 | |||||
| 54 | # FIXME consider using local $SIG{__DIE__} to accumulate all errors. It's | ||||
| 55 | # not perfect, but we could provide a list of additional errors for | ||||
| 56 | # $catch->(); | ||||
| 57 | |||||
| 58 | # name the blocks if we have Sub::Name installed | ||||
| 59 | 650 | 197µs | my $caller = caller; | ||
| 60 | 650 | 2.27ms | 650 | 1.29ms | subname("${caller}::try {...} " => $try); # spent 1.29ms making 650 calls to Sub::Name::subname, avg 2µs/call |
| 61 | 650 | 1.67ms | 621 | 878µs | subname("${caller}::catch {...} " => $catch) if $catch; # spent 878µs making 621 calls to Sub::Name::subname, avg 1µs/call |
| 62 | 650 | 206µs | subname("${caller}::finally {...} " => $_) foreach @finally; | ||
| 63 | |||||
| 64 | # save the value of $@ so we can set $@ back to it in the beginning of the eval | ||||
| 65 | # and restore $@ after the eval finishes | ||||
| 66 | 650 | 71µs | my $prev_error = $@; | ||
| 67 | |||||
| 68 | 650 | 2µs | my ( @ret, $error ); | ||
| 69 | |||||
| 70 | # failed will be true if the eval dies, because 1 will not be returned | ||||
| 71 | # from the eval body | ||||
| 72 | 650 | 234µs | my $failed = not eval { | ||
| 73 | 650 | 44µs | $@ = $prev_error; | ||
| 74 | |||||
| 75 | # evaluate the try block in the correct context | ||||
| 76 | 650 | 712µs | 273 | 319ms | if ( $wantarray ) { # spent 218ms making 22 calls to Class::Load::try {...} , avg 9.90ms/call
# spent 54.6ms making 129 calls to Class::MOP::Method::Accessor::try {...} , avg 423µs/call
# spent 23.1ms making 61 calls to Moose::Meta::Method::Accessor::try {...} , avg 379µs/call
# spent 22.6ms making 31 calls to Class::MOP::Method::Constructor::try {...} , avg 729µs/call
# spent 284µs making 4 calls to Moose::Meta::Attribute::try {...} , avg 71µs/call
# spent 148µs making 1 call to Moose::Meta::Method::Destructor::try {...}
# spent 89µs making 25 calls to Moose::Util::try {...} , avg 4µs/call |
| 77 | @ret = $try->(); | ||||
| 78 | } elsif ( defined $wantarray ) { | ||||
| 79 | $ret[0] = $try->(); | ||||
| 80 | } else { | ||||
| 81 | 377 | 259µs | 377 | 387ms | $try->(); # spent 291ms making 151 calls to Class::MOP::Class::try {...} , avg 1.93ms/call
# spent 93.6ms making 223 calls to Class::MOP::Attribute::try {...} , avg 420µs/call
# spent 2.70ms making 3 calls to Module::Implementation::try {...} , avg 901µs/call |
| 82 | }; | ||||
| 83 | |||||
| 84 | 649 | 169µs | return 1; # properly set $fail to false | ||
| 85 | }; | ||||
| 86 | |||||
| 87 | # preserve the current error and reset the original value of $@ | ||||
| 88 | 650 | 75µs | $error = $@; | ||
| 89 | 650 | 51µs | $@ = $prev_error; | ||
| 90 | |||||
| 91 | # set up a scope guard to invoke the finally block at the end | ||||
| 92 | my @guards = | ||||
| 93 | 650 | 220µs | map { Try::Tiny::ScopeGuard->_new($_, $failed ? $error : ()) } | ||
| 94 | @finally; | ||||
| 95 | |||||
| 96 | # at this point $failed contains a true value if the eval died, even if some | ||||
| 97 | # destructor overwrote $@ as the eval was unwinding. | ||||
| 98 | 650 | 66µs | if ( $failed ) { | ||
| 99 | # if we got an error, invoke the catch block. | ||||
| 100 | 1 | 600ns | if ( $catch ) { | ||
| 101 | # This works like given($error), but is backwards compatible and | ||||
| 102 | # sets $_ in the dynamic scope for the body of C<$catch> | ||||
| 103 | for ($error) { | ||||
| 104 | 1 | 3µs | 1 | 16µs | return $catch->($error); # spent 16µs making 1 call to Class::Load::catch {...} |
| 105 | } | ||||
| 106 | |||||
| 107 | # in case when() was used without an explicit return, the C<for> | ||||
| 108 | # loop will be aborted and there's no useful return value | ||||
| 109 | } | ||||
| 110 | |||||
| 111 | return; | ||||
| 112 | } else { | ||||
| 113 | # no failure, $@ is back to what it was, everything is fine | ||||
| 114 | 649 | 1.27ms | return $wantarray ? @ret : $ret[0]; | ||
| 115 | } | ||||
| 116 | } | ||||
| 117 | |||||
| 118 | # spent 1.39ms within Try::Tiny::catch which was called 621 times, avg 2µs/call:
# 223 times (484µs+0s) by Class::MOP::Attribute::_process_accessors at line 401 of Class/MOP/Attribute.pm, avg 2µs/call
# 151 times (348µs+0s) by Class::MOP::Class::_post_add_attribute at line 896 of Class/MOP/Class.pm, avg 2µs/call
# 61 times (141µs+0s) by Moose::Meta::Method::Accessor::_compile_code at line 47 of Moose/Meta/Method/Accessor.pm, avg 2µs/call
# 58 times (119µs+0s) by Class::MOP::Method::Accessor::_generate_reader_method_inline at line 157 of Class/MOP/Method/Accessor.pm, avg 2µs/call
# 44 times (92µs+0s) by Class::MOP::Method::Accessor::_generate_accessor_method_inline at line 123 of Class/MOP/Method/Accessor.pm, avg 2µs/call
# 31 times (75µs+0s) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 115 of Class/MOP/Method/Constructor.pm, avg 2µs/call
# 24 times (49µs+0s) by Class::MOP::Method::Accessor::_generate_predicate_method_inline at line 212 of Class/MOP/Method/Accessor.pm, avg 2µs/call
# 22 times (64µs+0s) by Class::Load::try_load_class at line 183 of Class/Load.pm, avg 3µs/call
# 3 times (10µs+0s) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 3µs/call
# 3 times (6µs+0s) by Class::MOP::Method::Accessor::_generate_writer_method_inline at line 187 of Class/MOP/Method/Accessor.pm, avg 2µs/call
# once (2µs+0s) by Moose::Meta::Method::Destructor::_initialize_body at line 106 of Moose/Meta/Method/Destructor.pm | ||||
| 119 | 621 | 174µs | my ( $block, @rest ) = @_; | ||
| 120 | |||||
| 121 | 621 | 60µs | croak 'Useless bare catch()' unless wantarray; | ||
| 122 | |||||
| 123 | return ( | ||||
| 124 | 621 | 1.48ms | bless(\$block, 'Try::Tiny::Catch'), | ||
| 125 | @rest, | ||||
| 126 | ); | ||||
| 127 | } | ||||
| 128 | |||||
| 129 | sub finally (&;@) { | ||||
| 130 | my ( $block, @rest ) = @_; | ||||
| 131 | |||||
| 132 | croak 'Useless bare finally()' unless wantarray; | ||||
| 133 | |||||
| 134 | return ( | ||||
| 135 | bless(\$block, 'Try::Tiny::Finally'), | ||||
| 136 | @rest, | ||||
| 137 | ); | ||||
| 138 | } | ||||
| 139 | |||||
| 140 | { | ||||
| 141 | 1 | 100ns | package # hide from PAUSE | ||
| 142 | Try::Tiny::ScopeGuard; | ||||
| 143 | |||||
| 144 | 2 | 182µs | 2 | 1.08ms | # spent 1.04ms (826µs+217µs) within Try::Tiny::ScopeGuard::BEGIN@144 which was called:
# once (826µs+217µs) by Module::Implementation::BEGIN@13 at line 144 # spent 1.04ms making 1 call to Try::Tiny::ScopeGuard::BEGIN@144
# spent 34µs making 1 call to constant::import |
| 145 | |||||
| 146 | sub _new { | ||||
| 147 | shift; | ||||
| 148 | bless [ @_ ]; | ||||
| 149 | } | ||||
| 150 | |||||
| 151 | sub DESTROY { | ||||
| 152 | my ($code, @args) = @{ $_[0] }; | ||||
| 153 | |||||
| 154 | local $@ if UNSTABLE_DOLLARAT; | ||||
| 155 | eval { | ||||
| 156 | $code->(@args); | ||||
| 157 | 1; | ||||
| 158 | } or do { | ||||
| 159 | warn | ||||
| 160 | "Execution of finally() block $code resulted in an exception, which " | ||||
| 161 | . '*CAN NOT BE PROPAGATED* due to fundamental limitations of Perl. ' | ||||
| 162 | . 'Your program will continue as if this event never took place. ' | ||||
| 163 | . "Original exception text follows:\n\n" | ||||
| 164 | . (defined $@ ? $@ : '$@ left undefined...') | ||||
| 165 | . "\n" | ||||
| 166 | ; | ||||
| 167 | } | ||||
| 168 | } | ||||
| 169 | } | ||||
| 170 | |||||
| 171 | __PACKAGE__ | ||||
| 172 | |||||
| 173 | 1 | 4µs | __END__ |