Filename | /usr/local/share/perl/5.14.2/Try/Tiny.pm |
Statements | Executed 19083 statements in 18.7ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
706 | 14 | 10 | 10.7ms | 188ms | try (recurses: max depth 3, inclusive time 161ms) | Try::Tiny::
706 | 14 | 10 | 2.19ms | 2.19ms | catch | Try::Tiny::
1 | 1 | 1 | 2.07ms | 2.28ms | BEGIN@14 | Try::Tiny::
1 | 1 | 1 | 76µs | 1.32ms | BEGIN@20 | Try::Tiny::
1 | 1 | 1 | 27µs | 27µs | BEGIN@8 | Try::Tiny::
1 | 1 | 1 | 20µs | 94µs | BEGIN@146 | Try::Tiny::ScopeGuard::
1 | 1 | 1 | 19µs | 104µs | BEGIN@17 | Try::Tiny::
1 | 1 | 1 | 17µs | 17µs | BEGIN@2 | Try::Tiny::
1 | 1 | 1 | 15µs | 21µs | BEGIN@11 | Try::Tiny::
1 | 1 | 1 | 14µs | 26µs | BEGIN@12 | 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 17µs within Try::Tiny::BEGIN@2 which was called:
# once (17µs+0s) by Module::Implementation::BEGIN@13 at line 4 | ||||
3 | 1 | 10µs | $Try::Tiny::AUTHORITY = 'cpan:NUFFIN'; | ||
4 | 1 | 63µs | 1 | 17µs | } # spent 17µs making 1 call to Try::Tiny::BEGIN@2 |
5 | { | ||||
6 | 2 | 3µs | $Try::Tiny::VERSION = '0.18'; | ||
7 | } | ||||
8 | 2 | 89µs | 1 | 27µs | # spent 27µs within Try::Tiny::BEGIN@8 which was called:
# once (27µs+0s) by Module::Implementation::BEGIN@13 at line 8 # spent 27µs making 1 call to Try::Tiny::BEGIN@8 |
9 | # ABSTRACT: minimal try/catch with proper preservation of $@ | ||||
10 | |||||
11 | 2 | 45µs | 2 | 28µs | # spent 21µs (15+7) within Try::Tiny::BEGIN@11 which was called:
# once (15µs+7µs) by Module::Implementation::BEGIN@13 at line 11 # spent 21µs making 1 call to Try::Tiny::BEGIN@11
# spent 7µs making 1 call to strict::import |
12 | 2 | 66µs | 2 | 37µs | # spent 26µs (14+12) within Try::Tiny::BEGIN@12 which was called:
# once (14µs+12µs) by Module::Implementation::BEGIN@13 at line 12 # spent 26µs making 1 call to Try::Tiny::BEGIN@12
# spent 12µs making 1 call to warnings::import |
13 | |||||
14 | 2 | 288µs | 2 | 2.39ms | # spent 2.28ms (2.07+211µs) within Try::Tiny::BEGIN@14 which was called:
# once (2.07ms+211µs) by Module::Implementation::BEGIN@13 at line 14 # spent 2.28ms making 1 call to Try::Tiny::BEGIN@14
# spent 110µs making 1 call to base::import |
15 | 1 | 4µs | our @EXPORT = our @EXPORT_OK = qw(try catch finally); | ||
16 | |||||
17 | 2 | 108µs | 2 | 189µs | # spent 104µs (19+85) within Try::Tiny::BEGIN@17 which was called:
# once (19µs+85µs) by Module::Implementation::BEGIN@13 at line 17 # spent 104µs making 1 call to Try::Tiny::BEGIN@17
# spent 85µs making 1 call to Exporter::import |
18 | 1 | 2µs | $Carp::Internal{+__PACKAGE__}++; | ||
19 | |||||
20 | 1 | 913µs | 1 | 1.32ms | # spent 1.32ms (76µs+1.24) within Try::Tiny::BEGIN@20 which was called:
# once (76µs+1.24ms) by Module::Implementation::BEGIN@13 at line 20 # spent 1.32ms making 1 call to Try::Tiny::BEGIN@20 # spent 226µs executing statements in string eval # includes 564µ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 188ms (10.7+177) within Try::Tiny::try which was called 706 times, avg 266µs/call:
# 252 times (3.50ms+27.1ms) by Class::MOP::Attribute::_process_accessors at line 401 of Class/MOP/Attribute.pm, avg 122µs/call
# 181 times (2.62ms+113ms) by Class::MOP::Class::_post_add_attribute at line 896 of Class/MOP/Class.pm, avg 639µs/call
# 83 times (1.38ms+-1.38ms) by Class::MOP::Method::Accessor::_generate_reader_method_inline at line 157 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 64 times (999µs+-999µs) by Moose::Meta::Method::Accessor::_compile_code at line 47 of Moose/Meta/Method/Accessor.pm, avg 0s/call
# 52 times (913µs+-913µs) by Class::MOP::Method::Accessor::_generate_accessor_method_inline at line 123 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 35 times (560µs+25.4ms) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 115 of Class/MOP/Method/Constructor.pm, avg 742µs/call
# 18 times (271µs+-271µs) by Class::MOP::Method::Accessor::_generate_predicate_method_inline at line 212 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 5 times (85µs+96µs) by Moose::Object::DESTROY at line 102 of Moose/Object.pm, avg 36µs/call
# 4 times (135µs+11.6ms) by Class::Load::try_load_class at line 183 of Class/Load.pm, avg 2.93ms/call
# 3 times (102µs+2.66ms) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 919µs/call
# 3 times (48µs+498µs) by Moose::Meta::Method::Destructor::_initialize_body at line 106 of Moose/Meta/Method/Destructor.pm, avg 182µs/call
# 3 times (43µs+-43µs) by Class::MOP::Method::Accessor::_generate_writer_method_inline at line 187 of Class/MOP/Method/Accessor.pm, avg 0s/call
# 2 times (36µs+-36µs) by Class::MOP::Method::Accessor::_generate_clearer_method_inline at line 237 of Class/MOP/Method/Accessor.pm, avg 0s/call
# once (38µs+175µs) by DateTime::set_time_zone at line 2017 of DateTime.pm | ||||
27 | 16945 | 14.0ms | 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 | 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 | 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 | foreach my $code_ref (@code_refs) { | ||||
40 | |||||
41 | if ( ref($code_ref) eq 'Try::Tiny::Catch' ) { | ||||
42 | croak 'A try() may not be followed by multiple catch() blocks' | ||||
43 | if $catch; | ||||
44 | $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 | my $caller = caller; | ||||
62 | 706 | 1.96ms | subname("${caller}::try {...} " => $try); # spent 1.96ms making 706 calls to Sub::Name::subname, avg 3µs/call | ||
63 | 706 | 1.26ms | subname("${caller}::catch {...} " => $catch) if $catch; # spent 1.26ms making 706 calls to Sub::Name::subname, avg 2µs/call | ||
64 | 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 | my $prev_error = $@; | ||||
69 | |||||
70 | 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 | my $failed = not eval { | ||||
75 | $@ = $prev_error; | ||||
76 | |||||
77 | # evaluate the try block in the correct context | ||||
78 | if ( $wantarray ) { | ||||
79 | 3 | 11.4ms | @ret = $try->(); # spent 11.4ms making 3 calls to Class::Load::try {...} , avg 3.79ms/call | ||
80 | } elsif ( defined $wantarray ) { | ||||
81 | 261 | 110ms | $ret[0] = $try->(); # spent 64.0ms making 158 calls to Class::MOP::Method::Accessor::try {...} , avg 405µs/call
# spent 25.2ms making 35 calls to Class::MOP::Method::Constructor::try {...} , avg 721µs/call
# spent 20.0ms making 64 calls to Moose::Meta::Method::Accessor::try {...} , avg 312µs/call
# spent 482µs making 3 calls to Moose::Meta::Method::Destructor::try {...} , avg 161µs/call
# spent 143µs making 1 call to Class::Load::try {...} | ||
82 | } else { | ||||
83 | 442 | 214ms | $try->(); # spent 112ms making 181 calls to Class::MOP::Class::try {...} , avg 620µs/call
# spent 98.9ms making 252 calls to Class::MOP::Attribute::try {...} , avg 393µs/call
# spent 2.62ms making 3 calls to Module::Implementation::try {...} , avg 874µs/call
# spent 166µs making 1 call to DateTime::try {...}
# spent 72µs making 5 calls to Moose::Object::try {...} , avg 14µs/call | ||
84 | }; | ||||
85 | |||||
86 | return 1; # properly set $fail to false | ||||
87 | }; | ||||
88 | |||||
89 | # preserve the current error and reset the original value of $@ | ||||
90 | $error = $@; | ||||
91 | $@ = $prev_error; | ||||
92 | |||||
93 | # set up a scope guard to invoke the finally block at the end | ||||
94 | my @guards = | ||||
95 | 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 | if ( $failed ) { | ||||
101 | # if we got an error, invoke the catch block. | ||||
102 | 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 | 34µs | return $catch->($error); # spent 34µs making 1 call to Class::Load::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 | return $wantarray ? @ret : $ret[0]; | ||||
117 | } | ||||
118 | } | ||||
119 | |||||
120 | # spent 2.19ms within Try::Tiny::catch which was called 706 times, avg 3µs/call:
# 252 times (747µs+0s) by Class::MOP::Attribute::_process_accessors at line 401 of Class/MOP/Attribute.pm, avg 3µs/call
# 181 times (587µs+0s) by Class::MOP::Class::_post_add_attribute at line 896 of Class/MOP/Class.pm, avg 3µs/call
# 83 times (227µs+0s) by Class::MOP::Method::Accessor::_generate_reader_method_inline at line 157 of Class/MOP/Method/Accessor.pm, avg 3µs/call
# 64 times (182µs+0s) by Moose::Meta::Method::Accessor::_compile_code at line 47 of Moose/Meta/Method/Accessor.pm, avg 3µs/call
# 52 times (149µs+0s) by Class::MOP::Method::Accessor::_generate_accessor_method_inline at line 123 of Class/MOP/Method/Accessor.pm, avg 3µs/call
# 35 times (113µs+0s) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 115 of Class/MOP/Method/Constructor.pm, avg 3µs/call
# 18 times (46µs+0s) by Class::MOP::Method::Accessor::_generate_predicate_method_inline at line 212 of Class/MOP/Method/Accessor.pm, avg 3µs/call
# 5 times (21µs+0s) by Moose::Object::DESTROY at line 102 of Moose/Object.pm, avg 4µs/call
# 4 times (28µs+0s) by Class::Load::try_load_class at line 183 of Class/Load.pm, avg 7µs/call
# 3 times (54µs+0s) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 18µs/call
# 3 times (15µs+0s) by Moose::Meta::Method::Destructor::_initialize_body at line 106 of Moose/Meta/Method/Destructor.pm, avg 5µs/call
# 3 times (8µ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 (6µs+0s) by Class::MOP::Method::Accessor::_generate_clearer_method_inline at line 237 of Class/MOP/Method/Accessor.pm, avg 3µs/call
# once (8µs+0s) by DateTime::set_time_zone at line 2017 of DateTime.pm | ||||
121 | 2118 | 2.83ms | my ( $block, @rest ) = @_; | ||
122 | |||||
123 | croak 'Useless bare catch()' unless wantarray; | ||||
124 | |||||
125 | return ( | ||||
126 | 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 | 800ns | package # hide from PAUSE | ||
144 | Try::Tiny::ScopeGuard; | ||||
145 | |||||
146 | 2 | 332µs | 2 | 168µs | # spent 94µs (20+74) within Try::Tiny::ScopeGuard::BEGIN@146 which was called:
# once (20µs+74µs) by Module::Implementation::BEGIN@13 at line 146 # spent 94µs making 1 call to Try::Tiny::ScopeGuard::BEGIN@146
# spent 74µ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 | 10µs | __END__ |