Filename | /opt/perl-5.18.1/lib/site_perl/5.18.1/Try/Tiny.pm |
Statements | Executed 11638 statements in 18.8ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
452 | 11 | 8 | 14.7ms | 152ms | try (recurses: max depth 3, inclusive time 57.6ms) | Try::Tiny::
452 | 11 | 8 | 1.49ms | 1.49ms | catch | Try::Tiny::
1 | 1 | 1 | 1.25ms | 1.59ms | BEGIN@146 | Try::Tiny::ScopeGuard::
1 | 1 | 1 | 50µs | 924µs | BEGIN@20 | Try::Tiny::
1 | 1 | 1 | 12µs | 12µs | BEGIN@8 | Try::Tiny::
1 | 1 | 1 | 10µs | 10µs | BEGIN@2 | Try::Tiny::
1 | 1 | 1 | 10µs | 52µs | BEGIN@17 | Try::Tiny::
1 | 1 | 1 | 9µs | 25µs | BEGIN@11 | Try::Tiny::
1 | 1 | 1 | 9µs | 13µs | BEGIN@12 | Try::Tiny::
1 | 1 | 1 | 8µs | 86µs | BEGIN@14 | Try::Tiny::
0 | 0 | 0 | 0s | 0s | DESTROY | Try::Tiny::ScopeGuard::
0 | 0 | 0 | 0s | 0s | _new | Try::Tiny::ScopeGuard::
0 | 0 | 0 | 0s | 0s | __ANON__[:20] | Try::Tiny::
0 | 0 | 0 | 0s | 0s | finally | Try::Tiny::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Try::Tiny; | ||||
2 | # spent 10µs within Try::Tiny::BEGIN@2 which was called:
# once (10µs+0s) by Module::Implementation::BEGIN@13 at line 4 | ||||
3 | 1 | 6µs | $Try::Tiny::AUTHORITY = 'cpan:NUFFIN'; | ||
4 | 1 | 36µs | 1 | 10µs | } # spent 10µs making 1 call to Try::Tiny::BEGIN@2 |
5 | { | ||||
6 | 2 | 1µs | $Try::Tiny::VERSION = '0.18'; | ||
7 | } | ||||
8 | 2 | 54µs | 1 | 12µs | # spent 12µs within Try::Tiny::BEGIN@8 which was called:
# once (12µs+0s) by Module::Implementation::BEGIN@13 at line 8 # spent 12µs making 1 call to Try::Tiny::BEGIN@8 |
9 | # ABSTRACT: minimal try/catch with proper preservation of $@ | ||||
10 | |||||
11 | 2 | 30µs | 2 | 41µs | # spent 25µs (9+16) within Try::Tiny::BEGIN@11 which was called:
# once (9µs+16µs) by Module::Implementation::BEGIN@13 at line 11 # spent 25µs making 1 call to Try::Tiny::BEGIN@11
# spent 16µs making 1 call to strict::import |
12 | 2 | 30µs | 2 | 18µs | # spent 13µs (9+5) within Try::Tiny::BEGIN@12 which was called:
# once (9µs+5µs) by Module::Implementation::BEGIN@13 at line 12 # spent 13µs making 1 call to Try::Tiny::BEGIN@12
# spent 5µs making 1 call to warnings::import |
13 | |||||
14 | 2 | 61µs | 2 | 165µs | # spent 86µs (8+78) within Try::Tiny::BEGIN@14 which was called:
# once (8µs+78µs) by Module::Implementation::BEGIN@13 at line 14 # spent 86µs making 1 call to Try::Tiny::BEGIN@14
# spent 78µs making 1 call to base::import |
15 | 1 | 2µs | our @EXPORT = our @EXPORT_OK = qw(try catch finally); | ||
16 | |||||
17 | 2 | 62µs | 2 | 94µs | # spent 52µs (10+42) within Try::Tiny::BEGIN@17 which was called:
# once (10µs+42µs) by Module::Implementation::BEGIN@13 at line 17 # spent 52µs making 1 call to Try::Tiny::BEGIN@17
# spent 42µs making 1 call to Exporter::import |
18 | 1 | 800ns | $Carp::Internal{+__PACKAGE__}++; | ||
19 | |||||
20 | 1 | 581µs | 1 | 924µs | # spent 924µs (50+874) within Try::Tiny::BEGIN@20 which was called:
# once (50µs+874µs) by Module::Implementation::BEGIN@13 at line 20 # spent 924µs making 1 call to Try::Tiny::BEGIN@20 # spent 140µs executing statements in string eval # includes 365µ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 152ms (14.7+138) within Try::Tiny::try which was called 452 times, avg 337µs/call:
# 191 times (3.37ms+36.3ms) by Class::MOP::Attribute::_process_accessors at line 401 of Class/MOP/Attribute.pm, avg 208µs/call
# 124 times (8.52ms+67.1ms) by Class::MOP::Class::_post_add_attribute at line 896 of Class/MOP/Class.pm, avg 610µs/call
# 55 times (1.20ms+-1.20ms) by Class::MOP::Method::Accessor::_generate_reader_method_inline at line 157 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 31 times (599µs+32.1ms) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 115 of Class/MOP/Method/Constructor.pm, avg 1.05ms/call
# 23 times (464µs+-464µs) by Class::MOP::Method::Accessor::_generate_accessor_method_inline at line 123 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 16 times (307µs+-307µs) by Class::MOP::Method::Accessor::_generate_predicate_method_inline at line 212 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 3 times (91µs+2.28ms) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 791µs/call
# 3 times (57µs+-57µs) by Class::MOP::Method::Accessor::_generate_writer_method_inline at line 187 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 3 times (71µs+-71µs) by Moose::Meta::Method::Accessor::_compile_code at line 47 of Moose/Meta/Method/Accessor.pm, avg 0s/call
# 2 times (45µs+1.72ms) by Class::Load::try_load_class at line 183 of Class/Load.pm, avg 884µs/call
# once (22µs+246µs) by Moose::Meta::Method::Destructor::_initialize_body at line 106 of Moose/Meta/Method/Destructor.pm | ||||
27 | 452 | 210µ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 | 452 | 95µs | my $wantarray = wantarray; | ||
32 | |||||
33 | # work around perl bug by explicitly initializing these, due to the likelyhood | ||||
34 | # this will be used in global destruction (perl rt#119311) | ||||
35 | 452 | 103µs | my ( $catch, @finally ) = (); | ||
36 | |||||
37 | # find labeled blocks in the argument list. | ||||
38 | # catch and finally tag the blocks by blessing a scalar reference to them. | ||||
39 | 452 | 279µs | foreach my $code_ref (@code_refs) { | ||
40 | |||||
41 | 452 | 620µs | if ( ref($code_ref) eq 'Try::Tiny::Catch' ) { | ||
42 | 452 | 46µs | croak 'A try() may not be followed by multiple catch() blocks' | ||
43 | if $catch; | ||||
44 | 452 | 152µs | $catch = ${$code_ref}; | ||
45 | } elsif ( ref($code_ref) eq 'Try::Tiny::Finally' ) { | ||||
46 | push @finally, ${$code_ref}; | ||||
47 | } else { | ||||
48 | croak( | ||||
49 | 'try() encountered an unexpected argument (' | ||||
50 | . ( defined $code_ref ? $code_ref : 'undef' ) | ||||
51 | . ') - perhaps a missing semi-colon before or' | ||||
52 | ); | ||||
53 | } | ||||
54 | } | ||||
55 | |||||
56 | # FIXME consider using local $SIG{__DIE__} to accumulate all errors. It's | ||||
57 | # not perfect, but we could provide a list of additional errors for | ||||
58 | # $catch->(); | ||||
59 | |||||
60 | # name the blocks if we have Sub::Name installed | ||||
61 | 452 | 221µs | my $caller = caller; | ||
62 | 452 | 8.65ms | 452 | 1.31ms | subname("${caller}::try {...} " => $try); # spent 1.31ms making 452 calls to Sub::Name::subname, avg 3µs/call |
63 | 452 | 1.70ms | 452 | 871µs | subname("${caller}::catch {...} " => $catch) if $catch; # spent 871µs making 452 calls to Sub::Name::subname, avg 2µs/call |
64 | 452 | 205µs | subname("${caller}::finally {...} " => $_) foreach @finally; | ||
65 | |||||
66 | # save the value of $@ so we can set $@ back to it in the beginning of the eval | ||||
67 | # and restore $@ after the eval finishes | ||||
68 | 452 | 91µs | my $prev_error = $@; | ||
69 | |||||
70 | 452 | 29µs | my ( @ret, $error ); | ||
71 | |||||
72 | # failed will be true if the eval dies, because 1 will not be returned | ||||
73 | # from the eval body | ||||
74 | 452 | 257µs | my $failed = not eval { | ||
75 | 452 | 46µs | $@ = $prev_error; | ||
76 | |||||
77 | # evaluate the try block in the correct context | ||||
78 | 452 | 566µs | 134 | 63.2ms | if ( $wantarray ) { # spent 31.9ms making 31 calls to Class::MOP::Method::Constructor::try {...} , avg 1.03ms/call
# spent 28.1ms making 97 calls to Class::MOP::Method::Accessor::try {...} , avg 289µs/call
# spent 1.71ms making 2 calls to Class::Load::try {...} , avg 857µs/call
# spent 1.28ms making 3 calls to Moose::Meta::Method::Accessor::try {...} , avg 426µs/call
# spent 236µs making 1 call to Moose::Meta::Method::Destructor::try {...} |
79 | @ret = $try->(); | ||||
80 | } elsif ( defined $wantarray ) { | ||||
81 | $ret[0] = $try->(); | ||||
82 | } else { | ||||
83 | 318 | 320µs | 318 | 130ms | $try->(); # spent 66.5ms making 124 calls to Class::MOP::Class::try {...} , avg 536µs/call
# spent 60.4ms making 191 calls to Class::MOP::Attribute::try {...} , avg 316µs/call
# spent 3.00ms making 3 calls to Module::Implementation::try {...} , avg 1.00ms/call |
84 | }; | ||||
85 | |||||
86 | 451 | 173µs | return 1; # properly set $fail to false | ||
87 | }; | ||||
88 | |||||
89 | # preserve the current error and reset the original value of $@ | ||||
90 | 452 | 67µs | $error = $@; | ||
91 | 452 | 50µs | $@ = $prev_error; | ||
92 | |||||
93 | # set up a scope guard to invoke the finally block at the end | ||||
94 | my @guards = | ||||
95 | 452 | 267µs | map { Try::Tiny::ScopeGuard->_new($_, $failed ? $error : ()) } | ||
96 | @finally; | ||||
97 | |||||
98 | # at this point $failed contains a true value if the eval died, even if some | ||||
99 | # destructor overwrote $@ as the eval was unwinding. | ||||
100 | 452 | 82µs | if ( $failed ) { | ||
101 | # if we got an error, invoke the catch block. | ||||
102 | 1 | 700ns | if ( $catch ) { | ||
103 | # This works like given($error), but is backwards compatible and | ||||
104 | # sets $_ in the dynamic scope for the body of C<$catch> | ||||
105 | for ($error) { | ||||
106 | 1 | 7µs | 1 | 3µs | return $catch->($error); # spent 3µs making 1 call to Module::Implementation::catch {...} |
107 | } | ||||
108 | |||||
109 | # in case when() was used without an explicit return, the C<for> | ||||
110 | # loop will be aborted and there's no useful return value | ||||
111 | } | ||||
112 | |||||
113 | return; | ||||
114 | } else { | ||||
115 | # no failure, $@ is back to what it was, everything is fine | ||||
116 | 451 | 1.40ms | return $wantarray ? @ret : $ret[0]; | ||
117 | } | ||||
118 | } | ||||
119 | |||||
120 | # spent 1.49ms within Try::Tiny::catch which was called 452 times, avg 3µs/call:
# 191 times (620µs+0s) by Class::MOP::Attribute::_process_accessors at line 401 of Class/MOP/Attribute.pm, avg 3µs/call
# 124 times (421µs+0s) by Class::MOP::Class::_post_add_attribute at line 896 of Class/MOP/Class.pm, avg 3µs/call
# 55 times (173µs+0s) by Class::MOP::Method::Accessor::_generate_reader_method_inline at line 157 of Class/MOP/Method/Accessor.pm, avg 3µs/call
# 31 times (107µs+0s) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 115 of Class/MOP/Method/Constructor.pm, avg 3µs/call
# 23 times (72µs+0s) by Class::MOP::Method::Accessor::_generate_accessor_method_inline at line 123 of Class/MOP/Method/Accessor.pm, avg 3µs/call
# 16 times (49µs+0s) by Class::MOP::Method::Accessor::_generate_predicate_method_inline at line 212 of Class/MOP/Method/Accessor.pm, avg 3µs/call
# 3 times (12µs+0s) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 4µs/call
# 3 times (10µs+0s) by Moose::Meta::Method::Accessor::_compile_code at line 47 of Moose/Meta/Method/Accessor.pm, avg 3µs/call
# 3 times (9µs+0s) by Class::MOP::Method::Accessor::_generate_writer_method_inline at line 187 of Class/MOP/Method/Accessor.pm, avg 3µs/call
# 2 times (8µs+0s) by Class::Load::try_load_class at line 183 of Class/Load.pm, avg 4µs/call
# once (4µs+0s) by Moose::Meta::Method::Destructor::_initialize_body at line 106 of Moose/Meta/Method/Destructor.pm | ||||
121 | 452 | 193µs | my ( $block, @rest ) = @_; | ||
122 | |||||
123 | 452 | 60µs | croak 'Useless bare catch()' unless wantarray; | ||
124 | |||||
125 | return ( | ||||
126 | 452 | 1.75ms | bless(\$block, 'Try::Tiny::Catch'), | ||
127 | @rest, | ||||
128 | ); | ||||
129 | } | ||||
130 | |||||
131 | sub finally (&;@) { | ||||
132 | my ( $block, @rest ) = @_; | ||||
133 | |||||
134 | croak 'Useless bare finally()' unless wantarray; | ||||
135 | |||||
136 | return ( | ||||
137 | bless(\$block, 'Try::Tiny::Finally'), | ||||
138 | @rest, | ||||
139 | ); | ||||
140 | } | ||||
141 | |||||
142 | { | ||||
143 | 1 | 200ns | package # hide from PAUSE | ||
144 | Try::Tiny::ScopeGuard; | ||||
145 | |||||
146 | 2 | 275µs | 2 | 1.64ms | # spent 1.59ms (1.25+345µs) within Try::Tiny::ScopeGuard::BEGIN@146 which was called:
# once (1.25ms+345µs) by Module::Implementation::BEGIN@13 at line 146 # spent 1.59ms making 1 call to Try::Tiny::ScopeGuard::BEGIN@146
# spent 52µs making 1 call to constant::import |
147 | |||||
148 | sub _new { | ||||
149 | shift; | ||||
150 | bless [ @_ ]; | ||||
151 | } | ||||
152 | |||||
153 | sub DESTROY { | ||||
154 | my ($code, @args) = @{ $_[0] }; | ||||
155 | |||||
156 | local $@ if UNSTABLE_DOLLARAT; | ||||
157 | eval { | ||||
158 | $code->(@args); | ||||
159 | 1; | ||||
160 | } or do { | ||||
161 | warn | ||||
162 | "Execution of finally() block $code resulted in an exception, which " | ||||
163 | . '*CAN NOT BE PROPAGATED* due to fundamental limitations of Perl. ' | ||||
164 | . 'Your program will continue as if this event never took place. ' | ||||
165 | . "Original exception text follows:\n\n" | ||||
166 | . (defined $@ ? $@ : '$@ left undefined...') | ||||
167 | . "\n" | ||||
168 | ; | ||||
169 | } | ||||
170 | } | ||||
171 | } | ||||
172 | |||||
173 | __PACKAGE__ | ||||
174 | |||||
175 | 1 | 6µs | __END__ |