← Index
NYTProf Performance Profile   « line view »
For index.cgi
  Run on Sat May 9 17:18:47 2020
Reported on Sat May 9 17:19:07 2020

Filename/usr/local/share/perl/5.18.2/Try/Tiny.pm
StatementsExecuted 72 statements in 1.30ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1111.23ms1.59msTry::Tiny::ScopeGuard::::BEGIN@144Try::Tiny::ScopeGuard::BEGIN@144
11152µs786µsTry::Tiny::::BEGIN@18 Try::Tiny::BEGIN@18
21151µs4.19msTry::Tiny::::try Try::Tiny::try
11116µs40µsTry::Tiny::::BEGIN@12 Try::Tiny::BEGIN@12
11113µs59µsTry::Tiny::::BEGIN@15 Try::Tiny::BEGIN@15
21112µs12µsTry::Tiny::::catch Try::Tiny::catch
11111µs11µsTry::Tiny::::BEGIN@6 Try::Tiny::BEGIN@6
11111µs16µsTry::Tiny::::BEGIN@10 Try::Tiny::BEGIN@10
1119µs9µsTry::Tiny::::BEGIN@2 Try::Tiny::BEGIN@2
1118µs23µsTry::Tiny::::BEGIN@9 Try::Tiny::BEGIN@9
0000s0sTry::Tiny::ScopeGuard::::DESTROYTry::Tiny::ScopeGuard::DESTROY
0000s0sTry::Tiny::ScopeGuard::::_newTry::Tiny::ScopeGuard::_new
0000s0sTry::Tiny::::__ANON__[:18] Try::Tiny::__ANON__[:18]
0000s0sTry::Tiny::::finally Try::Tiny::finally
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Try::Tiny;
2
# spent 9µs within Try::Tiny::BEGIN@2 which was called: # once (9µs+0s) by Module::Implementation::BEGIN@9 at line 4
BEGIN {
316µs $Try::Tiny::AUTHORITY = 'cpan:NUFFIN';
4130µs19µs}
# spent 9µs making 1 call to Try::Tiny::BEGIN@2
51700ns$Try::Tiny::VERSION = '0.22';
6257µs111µs
# spent 11µs within Try::Tiny::BEGIN@6 which was called: # once (11µs+0s) by Module::Implementation::BEGIN@9 at line 6
use 5.006;
# spent 11µs making 1 call to Try::Tiny::BEGIN@6
7# ABSTRACT: minimal try/catch with proper preservation of $@
8
9227µs239µs
# spent 23µs (8+16) within Try::Tiny::BEGIN@9 which was called: # once (8µs+16µs) by Module::Implementation::BEGIN@9 at line 9
use strict;
# spent 23µs making 1 call to Try::Tiny::BEGIN@9 # spent 16µs making 1 call to strict::import
10233µs221µs
# spent 16µs (11+5) within Try::Tiny::BEGIN@10 which was called: # once (11µs+5µs) by Module::Implementation::BEGIN@9 at line 10
use warnings;
# spent 16µs making 1 call to Try::Tiny::BEGIN@10 # spent 5µs making 1 call to warnings::import
11
12380µs364µs
# spent 40µs (16+24) within Try::Tiny::BEGIN@12 which was called: # once (16µs+24µs) by Module::Implementation::BEGIN@9 at line 12
use Exporter 5.57 'import';
# spent 40µs making 1 call to Try::Tiny::BEGIN@12 # spent 15µs making 1 call to UNIVERSAL::VERSION # spent 9µs making 1 call to Exporter::import
1312µsour @EXPORT = our @EXPORT_OK = qw(try catch finally);
14
15270µs2106µs
# spent 59µs (13+47) within Try::Tiny::BEGIN@15 which was called: # once (13µs+47µs) by Module::Implementation::BEGIN@9 at line 15
use Carp;
# spent 59µs making 1 call to Try::Tiny::BEGIN@15 # spent 47µs making 1 call to Exporter::import
1611µs$Carp::Internal{+__PACKAGE__}++;
17
181610µs1786µs
# spent 786µs (52+734) within Try::Tiny::BEGIN@18 which was called: # once (52µs+734µs) by Module::Implementation::BEGIN@9 at line 18
BEGIN { eval "use Sub::Name; 1" or *{subname} = sub {1} }
# spent 786µs making 1 call to Try::Tiny::BEGIN@18
# spent 160µs executing statements in string eval
# includes 409µ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 4.19ms (51µs+4.14) within Try::Tiny::try which was called 2 times, avg 2.10ms/call: # 2 times (51µs+4.14ms) by Module::Implementation::_load_implementation at line 98 of Module/Implementation.pm, avg 2.10ms/call
sub try (&;@) {
2521µ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
292800ns 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)
332800ns 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.
3722µs foreach my $code_ref (@code_refs) {
38
3924µs if ( ref($code_ref) eq 'Try::Tiny::Catch' ) {
402400ns croak 'A try() may not be followed by multiple catch() blocks'
41 if $catch;
4221µ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
5921µs my $caller = caller;
60233µs225µs subname("${caller}::try {...} " => $try);
# spent 25µs making 2 calls to Sub::Name::subname, avg 12µs/call
61222µs216µs subname("${caller}::catch {...} " => $catch) if $catch;
# spent 16µs making 2 calls to Sub::Name::subname, avg 8µs/call
6222µ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
6621µs my $prev_error = $@;
67
682400ns my ( @ret, $error );
69
70 # failed will be true if the eval dies, because 1 will not be returned
71 # from the eval body
7222µs my $failed = not eval {
732400ns $@ = $prev_error;
74
75 # evaluate the try block in the correct context
7621µs if ( $wantarray ) {
77 @ret = $try->();
78 } elsif ( defined $wantarray ) {
79 $ret[0] = $try->();
80 } else {
8123µs24.10ms $try->();
# spent 4.10ms making 2 calls to Module::Implementation::try {...} , avg 2.05ms/call
82 };
83
8421µs return 1; # properly set $fail to false
85 };
86
87 # preserve the current error and reset the original value of $@
882800ns $error = $@;
892800ns $@ = $prev_error;
90
91 # set up a scope guard to invoke the finally block at the end
92 my @guards =
9322µ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.
982900ns 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
11428µs return $wantarray ? @ret : $ret[0];
115 }
116}
117
118
# spent 12µs within Try::Tiny::catch which was called 2 times, avg 6µs/call: # 2 times (12µs+0s) by Module::Implementation::_load_implementation at line 98 of Module/Implementation.pm, avg 6µs/call
sub catch (&;@) {
11922µs my ( $block, @rest ) = @_;
120
1212600ns croak 'Useless bare catch()' unless wantarray;
122
123 return (
124214µs bless(\$block, 'Try::Tiny::Catch'),
125 @rest,
126 );
127}
128
129sub 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{
1411800ns package # hide from PAUSE
142 Try::Tiny::ScopeGuard;
143
1442272µs21.65ms
# spent 1.59ms (1.23+359µs) within Try::Tiny::ScopeGuard::BEGIN@144 which was called: # once (1.23ms+359µs) by Module::Implementation::BEGIN@9 at line 144
use constant UNSTABLE_DOLLARAT => ($] < '5.013002') ? 1 : 0;
# spent 1.59ms making 1 call to Try::Tiny::ScopeGuard::BEGIN@144 # spent 57µ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
17315µs__END__