← Index
NYTProf Performance Profile   « line view »
For -e
  Run on Thu Jun 30 16:34:56 2016
Reported on Thu Jun 30 16:35:09 2016

Filename/home/s1/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Try/Tiny.pm
StatementsExecuted 78 statements in 1.27ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
111431µs483µsTry::Tiny::::BEGIN@16 Try::Tiny::BEGIN@16
22236µs1.05msTry::Tiny::::try Try::Tiny::try
22227µs47µsTry::Tiny::::catch Try::Tiny::catch
11125µs25µsTry::Tiny::::BEGIN@2 Try::Tiny::BEGIN@2
11113µs28µsTry::Tiny::::BEGIN@10 Try::Tiny::BEGIN@10
11110µs59µsTry::Tiny::ScopeGuard::::BEGIN@161Try::Tiny::ScopeGuard::BEGIN@161
1119µs12µsTry::Tiny::::BEGIN@7 Try::Tiny::BEGIN@7
1118µs53µsTry::Tiny::::BEGIN@13 Try::Tiny::BEGIN@13
1118µs13µsTry::Tiny::::BEGIN@8 Try::Tiny::BEGIN@8
0000s0sTry::Tiny::ScopeGuard::::DESTROYTry::Tiny::ScopeGuard::DESTROY
0000s0sTry::Tiny::ScopeGuard::::_newTry::Tiny::ScopeGuard::_new
0000s0sTry::Tiny::::__ANON__[:28] Try::Tiny::__ANON__[:28]
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; # git description: v0.23-3-g5ee27f1
2263µs125µs
# spent 25µs within Try::Tiny::BEGIN@2 which was called: # once (25µs+0s) by Module::Implementation::BEGIN@9 at line 2
use 5.006;
# spent 25µs making 1 call to Try::Tiny::BEGIN@2
3# ABSTRACT: minimal try/catch with proper preservation of $@
4
51500nsour $VERSION = '0.24';
6
7225µs216µs
# spent 12µs (9+3) within Try::Tiny::BEGIN@7 which was called: # once (9µs+3µs) by Module::Implementation::BEGIN@9 at line 7
use strict;
# spent 12µs making 1 call to Try::Tiny::BEGIN@7 # spent 3µs making 1 call to strict::import
8231µs218µs
# spent 13µs (8+5) within Try::Tiny::BEGIN@8 which was called: # once (8µs+5µs) by Module::Implementation::BEGIN@9 at line 8
use warnings;
# spent 13µs making 1 call to Try::Tiny::BEGIN@8 # spent 5µs making 1 call to warnings::import
9
10358µs344µs
# spent 28µs (13+15) within Try::Tiny::BEGIN@10 which was called: # once (13µs+15µs) by Module::Implementation::BEGIN@9 at line 10
use Exporter 5.57 'import';
# spent 28µs making 1 call to Try::Tiny::BEGIN@10 # spent 8µs making 1 call to Exporter::import # spent 8µs making 1 call to UNIVERSAL::VERSION
1112µsour @EXPORT = our @EXPORT_OK = qw(try catch finally);
12
132188µs299µs
# spent 53µs (8+45) within Try::Tiny::BEGIN@13 which was called: # once (8µs+45µs) by Module::Implementation::BEGIN@9 at line 13
use Carp;
# spent 53µs making 1 call to Try::Tiny::BEGIN@13 # spent 45µs making 1 call to Exporter::import
141800ns$Carp::Internal{+__PACKAGE__}++;
15
16
# spent 483µs (431+52) within Try::Tiny::BEGIN@16 which was called: # once (431µs+52µs) by Module::Implementation::BEGIN@9 at line 30
BEGIN {
171600ns my $su = $INC{'Sub/Util.pm'} && defined &Sub::Util::set_subname;
181300ns my $sn = $INC{'Sub/Name.pm'} && eval { Sub::Name->VERSION(0.08) };
191300ns unless ($su || $sn) {
202213µs $su = eval { require Sub::Util; } && defined &Sub::Util::set_subname;
211400ns unless ($su) {
22 $sn = eval { require Sub::Name; Sub::Name->VERSION(0.08) };
23 }
24 }
25
26 *_subname = $su ? \&Sub::Util::set_subname
27 : $sn ? \&Sub::Name::subname
2811µs : sub { $_[1] };
2914µs *_HAS_SUBNAME = ($su || $sn) ? sub(){1} : sub(){0};
301440µs1483µs}
# spent 483µs making 1 call to Try::Tiny::BEGIN@16
31
32# Need to prototype as @ not $$ because of the way Perl evaluates the prototype.
33# Keeping it at $$ means you only ever get 1 sub because we need to eval in a list
34# context & not a scalar one
35
36
# spent 1.05ms (36µs+1.02) within Try::Tiny::try which was called 2 times, avg 526µs/call: # once (16µs+803µs) by Module::Implementation::_load_implementation at line 98 of Module/Implementation.pm # once (20µs+214µs) by DateTime::Format::Alami::parse_datetime at line 39 of DateTime.pm
sub try (&;@) {
3721µs my ( $try, @code_refs ) = @_;
38
39 # we need to save this here, the eval block will be in scalar context due
40 # to $failed
412600ns my $wantarray = wantarray;
42
43 # work around perl bug by explicitly initializing these, due to the likelyhood
44 # this will be used in global destruction (perl rt#119311)
452800ns my ( $catch, @finally ) = ();
46
47 # find labeled blocks in the argument list.
48 # catch and finally tag the blocks by blessing a scalar reference to them.
4922µs foreach my $code_ref (@code_refs) {
50
5123µs if ( ref($code_ref) eq 'Try::Tiny::Catch' ) {
522300ns croak 'A try() may not be followed by multiple catch() blocks'
53 if $catch;
5421µs $catch = ${$code_ref};
55 } elsif ( ref($code_ref) eq 'Try::Tiny::Finally' ) {
56 push @finally, ${$code_ref};
57 } else {
58 croak(
59 'try() encountered an unexpected argument ('
60 . ( defined $code_ref ? $code_ref : 'undef' )
61 . ') - perhaps a missing semi-colon before or'
62 );
63 }
64 }
65
66 # FIXME consider using local $SIG{__DIE__} to accumulate all errors. It's
67 # not perfect, but we could provide a list of additional errors for
68 # $catch->();
69
70 # name the blocks if we have Sub::Name installed
7121µs my $caller = caller;
72213µs28µs _subname("${caller}::try {...} " => $try)
# spent 8µs making 2 calls to Sub::Util::set_subname, avg 4µs/call
73 if _HAS_SUBNAME;
74
75 # save the value of $@ so we can set $@ back to it in the beginning of the eval
76 # and restore $@ after the eval finishes
7721µs my $prev_error = $@;
78
792300ns my ( @ret, $error );
80
81 # failed will be true if the eval dies, because 1 will not be returned
82 # from the eval body
8322µs my $failed = not eval {
842600ns $@ = $prev_error;
85
86 # evaluate the try block in the correct context
8721µs if ( $wantarray ) {
88 @ret = $try->();
89 } elsif ( defined $wantarray ) {
90 $ret[0] = $try->();
91 } else {
9222µs21.01ms $try->();
# spent 799µs making 1 call to Module::Implementation::try {...} # spent 211µs making 1 call to DateTime::try {...}
93 };
94
9521µs return 1; # properly set $failed to false
96 };
97
98 # preserve the current error and reset the original value of $@
992400ns $error = $@;
1002600ns $@ = $prev_error;
101
102 # set up a scope guard to invoke the finally block at the end
103 my @guards =
10422µs map { Try::Tiny::ScopeGuard->_new($_, $failed ? $error : ()) }
105 @finally;
106
107 # at this point $failed contains a true value if the eval died, even if some
108 # destructor overwrote $@ as the eval was unwinding.
1092700ns if ( $failed ) {
110 # if we got an error, invoke the catch block.
111 if ( $catch ) {
112 # This works like given($error), but is backwards compatible and
113 # sets $_ in the dynamic scope for the body of C<$catch>
114 for ($error) {
115 return $catch->($error);
116 }
117
118 # in case when() was used without an explicit return, the C<for>
119 # loop will be aborted and there's no useful return value
120 }
121
122 return;
123 } else {
124 # no failure, $@ is back to what it was, everything is fine
12528µs return $wantarray ? @ret : $ret[0];
126 }
127}
128
129
# spent 47µs (27+20) within Try::Tiny::catch which was called 2 times, avg 23µs/call: # once (14µs+13µs) by Module::Implementation::_load_implementation at line 98 of Module/Implementation.pm # once (13µs+7µs) by DateTime::Format::Alami::parse_datetime at line 39 of DateTime.pm
sub catch (&;@) {
13022µs my ( $block, @rest ) = @_;
131
1322700ns croak 'Useless bare catch()' unless wantarray;
133
13421µs my $caller = caller;
135233µs220µs _subname("${caller}::catch {...} " => $block)
# spent 20µs making 2 calls to Sub::Util::set_subname, avg 10µs/call
136 if _HAS_SUBNAME;
137 return (
138213µs bless(\$block, 'Try::Tiny::Catch'),
139 @rest,
140 );
141}
142
143sub finally (&;@) {
144 my ( $block, @rest ) = @_;
145
146 croak 'Useless bare finally()' unless wantarray;
147
148 my $caller = caller;
149 _subname("${caller}::finally {...} " => $block)
150 if _HAS_SUBNAME;
151 return (
152 bless(\$block, 'Try::Tiny::Finally'),
153 @rest,
154 );
155}
156
157{
158 package # hide from PAUSE
159 Try::Tiny::ScopeGuard;
160
1612149µs2108µs
# spent 59µs (10+49) within Try::Tiny::ScopeGuard::BEGIN@161 which was called: # once (10µs+49µs) by Module::Implementation::BEGIN@9 at line 161
use constant UNSTABLE_DOLLARAT => ($] < '5.013002') ? 1 : 0;
# spent 59µs making 1 call to Try::Tiny::ScopeGuard::BEGIN@161 # spent 49µs making 1 call to constant::import
162
163 sub _new {
164 shift;
165 bless [ @_ ];
166 }
167
168 sub DESTROY {
169 my ($code, @args) = @{ $_[0] };
170
171 local $@ if UNSTABLE_DOLLARAT;
172 eval {
173 $code->(@args);
174 1;
175 } or do {
176 warn
177 "Execution of finally() block $code resulted in an exception, which "
178 . '*CAN NOT BE PROPAGATED* due to fundamental limitations of Perl. '
179 . 'Your program will continue as if this event never took place. '
180 . "Original exception text follows:\n\n"
181 . (defined $@ ? $@ : '$@ left undefined...')
182 . "\n"
183 ;
184 }
185 }
186}
187
1881400ns__PACKAGE__
189__END__
19014µs