Filename | /usr/share/perl5/Try/Tiny.pm |
Statements | Executed 149 statements in 1.10ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
5 | 4 | 4 | 115µs | 9.15ms | try (recurses: max depth 1, inclusive time 1.95ms) | Try::Tiny::
1 | 1 | 1 | 48µs | 3.67ms | BEGIN@18 | Try::Tiny::
5 | 4 | 4 | 23µs | 23µs | catch | Try::Tiny::
1 | 1 | 1 | 14µs | 14µs | BEGIN@6 | Try::Tiny::
1 | 1 | 1 | 14µs | 29µs | BEGIN@12 | Try::Tiny::
1 | 1 | 1 | 10µs | 46µs | BEGIN@144 | Try::Tiny::ScopeGuard::
1 | 1 | 1 | 9µs | 48µs | BEGIN@15 | Try::Tiny::
1 | 1 | 1 | 8µs | 14µs | BEGIN@10 | Try::Tiny::
1 | 1 | 1 | 8µs | 8µs | BEGIN@2 | Try::Tiny::
1 | 1 | 1 | 7µs | 21µs | BEGIN@9 | 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__[:18] | 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 8µs within Try::Tiny::BEGIN@2 which was called:
# once (8µs+0s) by main::BEGIN@11 at line 4 | ||||
3 | 1 | 6µs | $Try::Tiny::AUTHORITY = 'cpan:NUFFIN'; | ||
4 | 1 | 30µs | 1 | 8µs | } # spent 8µs making 1 call to Try::Tiny::BEGIN@2 |
5 | 1 | 700ns | $Try::Tiny::VERSION = '0.19'; | ||
6 | 2 | 56µs | 1 | 14µs | # spent 14µs within Try::Tiny::BEGIN@6 which was called:
# once (14µs+0s) by main::BEGIN@11 at line 6 # spent 14µs making 1 call to Try::Tiny::BEGIN@6 |
7 | # ABSTRACT: minimal try/catch with proper preservation of $@ | ||||
8 | |||||
9 | 2 | 26µs | 2 | 35µs | # spent 21µs (7+14) within Try::Tiny::BEGIN@9 which was called:
# once (7µs+14µs) by main::BEGIN@11 at line 9 # spent 21µs making 1 call to Try::Tiny::BEGIN@9
# spent 14µs making 1 call to strict::import |
10 | 2 | 32µs | 2 | 19µs | # spent 14µs (8+5) within Try::Tiny::BEGIN@10 which was called:
# once (8µs+5µs) by main::BEGIN@11 at line 10 # spent 14µs making 1 call to Try::Tiny::BEGIN@10
# spent 5µs making 1 call to warnings::import |
11 | |||||
12 | 3 | 64µs | 3 | 45µs | # spent 29µs (14+16) within Try::Tiny::BEGIN@12 which was called:
# once (14µs+16µs) by main::BEGIN@11 at line 12 # spent 29µs making 1 call to Try::Tiny::BEGIN@12
# spent 9µs making 1 call to UNIVERSAL::VERSION
# spent 7µs making 1 call to Exporter::import |
13 | 1 | 2µs | our @EXPORT = our @EXPORT_OK = qw(try catch finally); | ||
14 | |||||
15 | 2 | 57µs | 2 | 87µs | # spent 48µs (9+39) within Try::Tiny::BEGIN@15 which was called:
# once (9µs+39µs) by main::BEGIN@11 at line 15 # spent 48µs making 1 call to Try::Tiny::BEGIN@15
# spent 39µs making 1 call to Exporter::import |
16 | 1 | 900ns | $Carp::Internal{+__PACKAGE__}++; | ||
17 | |||||
18 | 1 | 481µs | 1 | 3.67ms | # spent 3.67ms (48µs+3.62) within Try::Tiny::BEGIN@18 which was called:
# once (48µs+3.62ms) by main::BEGIN@11 at line 18 # spent 3.67ms making 1 call to Try::Tiny::BEGIN@18 # spent 122µs executing statements in string eval # includes 317µs spent executing 1 call to 1 sub defined therein. |
19 | |||||
20 | # Need to prototype as @ not $$ because of the way Perl evaluates the prototype. | ||||
21 | # Keeping it at $$ means you only ever get 1 sub because we need to eval in a list | ||||
22 | # context & not a scalar one | ||||
23 | |||||
24 | # spent 9.15ms (115µs+9.03) within Try::Tiny::try which was called 5 times, avg 1.83ms/call:
# 2 times (39µs+2.50ms) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 1.27ms/call
# once (20µs+6.46ms) by Search::Elasticsearch::Transport::perform_request at line 41 of /opt/flows/lib/lib/perl5/Search/Elasticsearch/Transport.pm
# once (29µs+98µs) by Search::Elasticsearch::Role::Client::Direct::parse_request at line 36 of /opt/flows/lib/lib/perl5/Search/Elasticsearch/Role/Client/Direct.pm
# once (28µs+-28µs) by Search::Elasticsearch::Role::Cxn::pings_ok at line 96 of /opt/flows/lib/lib/perl5/Search/Elasticsearch/Role/Cxn.pm | ||||
25 | 5 | 4µs | my ( $try, @code_refs ) = @_; | ||
26 | |||||
27 | # we need to save this here, the eval block will be in scalar context due | ||||
28 | # to $failed | ||||
29 | 5 | 2µs | my $wantarray = wantarray; | ||
30 | |||||
31 | # work around perl bug by explicitly initializing these, due to the likelyhood | ||||
32 | # this will be used in global destruction (perl rt#119311) | ||||
33 | 5 | 2µ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 | 5 | 4µs | foreach my $code_ref (@code_refs) { | ||
38 | |||||
39 | 5 | 8µs | if ( ref($code_ref) eq 'Try::Tiny::Catch' ) { | ||
40 | 5 | 1µs | croak 'A try() may not be followed by multiple catch() blocks' | ||
41 | if $catch; | ||||
42 | 5 | 2µ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 | 5 | 3µs | my $caller = caller; | ||
60 | 5 | 30µs | 5 | 16µs | subname("${caller}::try {...} " => $try); # spent 16µs making 5 calls to Sub::Name::subname, avg 3µs/call |
61 | 5 | 21µs | 5 | 9µs | subname("${caller}::catch {...} " => $catch) if $catch; # spent 9µs making 5 calls to Sub::Name::subname, avg 2µs/call |
62 | 5 | 4µ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 | 5 | 2µs | my $prev_error = $@; | ||
67 | |||||
68 | 5 | 900ns | 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 | 5 | 6µs | my $failed = not eval { | ||
73 | 5 | 1µs | $@ = $prev_error; | ||
74 | |||||
75 | # evaluate the try block in the correct context | ||||
76 | 5 | 5µs | 1 | 1.91ms | if ( $wantarray ) { # spent 1.91ms making 1 call to Search::Elasticsearch::Role::Cxn::try {...} |
77 | @ret = $try->(); | ||||
78 | } elsif ( defined $wantarray ) { | ||||
79 | $ret[0] = $try->(); | ||||
80 | } else { | ||||
81 | 4 | 5µs | 4 | 9.04ms | $try->(); # spent 6.46ms making 1 call to Search::Elasticsearch::Transport::try {...}
# spent 2.49ms making 2 calls to Module::Implementation::try {...} , avg 1.25ms/call
# spent 92µs making 1 call to Search::Elasticsearch::Role::Client::Direct::try {...} |
82 | }; | ||||
83 | |||||
84 | 5 | 3µs | return 1; # properly set $fail to false | ||
85 | }; | ||||
86 | |||||
87 | # preserve the current error and reset the original value of $@ | ||||
88 | 5 | 2µs | $error = $@; | ||
89 | 5 | 1µs | $@ = $prev_error; | ||
90 | |||||
91 | # set up a scope guard to invoke the finally block at the end | ||||
92 | my @guards = | ||||
93 | 5 | 5µ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 | 5 | 2µs | if ( $failed ) { | ||
99 | # if we got an error, invoke the catch block. | ||||
100 | 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 | return $catch->($error); | ||||
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 | 5 | 29µs | return $wantarray ? @ret : $ret[0]; | ||
115 | } | ||||
116 | } | ||||
117 | |||||
118 | # spent 23µs within Try::Tiny::catch which was called 5 times, avg 5µs/call:
# 2 times (10µs+0s) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 5µs/call
# once (7µs+0s) by Search::Elasticsearch::Role::Client::Direct::parse_request at line 36 of /opt/flows/lib/lib/perl5/Search/Elasticsearch/Role/Client/Direct.pm
# once (3µs+0s) by Search::Elasticsearch::Transport::perform_request at line 41 of /opt/flows/lib/lib/perl5/Search/Elasticsearch/Transport.pm
# once (3µs+0s) by Search::Elasticsearch::Role::Cxn::pings_ok at line 96 of /opt/flows/lib/lib/perl5/Search/Elasticsearch/Role/Cxn.pm | ||||
119 | 5 | 3µs | my ( $block, @rest ) = @_; | ||
120 | |||||
121 | 5 | 1µs | croak 'Useless bare catch()' unless wantarray; | ||
122 | |||||
123 | return ( | ||||
124 | 5 | 28µs | 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 | 600ns | package # hide from PAUSE | ||
142 | Try::Tiny::ScopeGuard; | ||||
143 | |||||
144 | 2 | 170µs | 2 | 81µs | # spent 46µs (10+35) within Try::Tiny::ScopeGuard::BEGIN@144 which was called:
# once (10µs+35µs) by main::BEGIN@11 at line 144 # spent 46µs making 1 call to Try::Tiny::ScopeGuard::BEGIN@144
# spent 35µ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__ |