← Index
NYTProf Performance Profile   « block view • line view • sub view »
For bin/dpath
  Run on Tue Jun 5 15:31:33 2012
Reported on Tue Jun 5 15:31:38 2012

Filename/home/ss5/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/Exception/Class/Base.pm
StatementsExecuted 58 statements in 7.01ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1115.98ms20.0msException::Class::Base::::BEGIN@10Exception::Class::Base::BEGIN@10
1111.10ms1.37msException::Class::Base::::BEGIN@9Exception::Class::Base::BEGIN@9
111257µs257µsException::Class::Base::::BEGIN@37Exception::Class::Base::BEGIN@37
111151µs400µsException::Class::Base::::BEGIN@15Exception::Class::Base::BEGIN@15
11150µs74µsException::Class::Base::::BEGIN@7Exception::Class::Base::BEGIN@7
11138µs180µsException::Class::Base::::BEGIN@11Exception::Class::Base::BEGIN@11
11137µs88µsException::Class::Base::::BEGIN@43Exception::Class::Base::BEGIN@43
11135µs311µsException::Class::Base::::BEGIN@13Exception::Class::Base::BEGIN@13
11135µs233µsException::Class::Base::::BEGIN@34Exception::Class::Base::BEGIN@34
11132µs32µsException::Class::Base::::BEGIN@2Exception::Class::Base::BEGIN@2
11127µs38µsException::Class::Base::::BEGIN@6Exception::Class::Base::BEGIN@6
11126µs63µsException::Class::Base::::BEGIN@63Exception::Class::Base::BEGIN@63
0000s0sException::Class::Base::::ClassesException::Class::Base::Classes
0000s0sException::Class::Base::::FieldsException::Class::Base::Fields
0000s0sException::Class::Base::::__ANON__[:34]Exception::Class::Base::__ANON__[:34]
0000s0sException::Class::Base::::__ANON__[:41]Exception::Class::Base::__ANON__[:41]
0000s0sException::Class::Base::::__ANON__[:62]Exception::Class::Base::__ANON__[:62]
0000s0sException::Class::Base::::_initializeException::Class::Base::_initialize
0000s0sException::Class::Base::::as_stringException::Class::Base::as_string
0000s0sException::Class::Base::::caughtException::Class::Base::caught
0000s0sException::Class::Base::::descriptionException::Class::Base::description
0000s0sException::Class::Base::::full_messageException::Class::Base::full_message
0000s0sException::Class::Base::::newException::Class::Base::new
0000s0sException::Class::Base::::rethrowException::Class::Base::rethrow
0000s0sException::Class::Base::::show_traceException::Class::Base::show_trace
0000s0sException::Class::Base::::throwException::Class::Base::throw
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Exception::Class::Base;
2
# spent 32µs within Exception::Class::Base::BEGIN@2 which was called: # once (32µs+0s) by Exception::Class::BEGIN@10 at line 4
BEGIN {
3128µs $Exception::Class::Base::VERSION = '1.32';
4188µs132µs}
# spent 32µs making 1 call to Exception::Class::Base::BEGIN@2
5
6288µs249µs
# spent 38µs (27+11) within Exception::Class::Base::BEGIN@6 which was called: # once (27µs+11µs) by Exception::Class::BEGIN@10 at line 6
use strict;
# spent 38µs making 1 call to Exception::Class::Base::BEGIN@6 # spent 11µs making 1 call to strict::import
7295µs298µs
# spent 74µs (50+24) within Exception::Class::Base::BEGIN@7 which was called: # once (50µs+24µs) by Exception::Class::BEGIN@10 at line 7
use warnings;
# spent 74µs making 1 call to Exception::Class::Base::BEGIN@7 # spent 24µs making 1 call to warnings::import
8
92409µs11.37ms
# spent 1.37ms (1.10+274µs) within Exception::Class::Base::BEGIN@9 which was called: # once (1.10ms+274µs) by Exception::Class::BEGIN@10 at line 9
use Class::Data::Inheritable;
# spent 1.37ms making 1 call to Exception::Class::Base::BEGIN@9
103545µs220.1ms
# spent 20.0ms (5.98+14.0) within Exception::Class::Base::BEGIN@10 which was called: # once (5.98ms+14.0ms) by Exception::Class::BEGIN@10 at line 10
use Devel::StackTrace 1.20;
# spent 20.0ms making 1 call to Exception::Class::Base::BEGIN@10 # spent 76µs making 1 call to UNIVERSAL::VERSION
112112µs2322µs
# spent 180µs (38+142) within Exception::Class::Base::BEGIN@11 which was called: # once (38µs+142µs) by Exception::Class::BEGIN@10 at line 11
use Scalar::Util qw( blessed );
# spent 180µs making 1 call to Exception::Class::Base::BEGIN@11 # spent 142µs making 1 call to Exporter::import
12
132362µs2588µs
# spent 311µs (35+277) within Exception::Class::Base::BEGIN@13 which was called: # once (35µs+277µs) by Exception::Class::BEGIN@10 at line 13
use base qw(Class::Data::Inheritable);
# spent 311µs making 1 call to Exception::Class::Base::BEGIN@13 # spent 277µs making 1 call to base::import
14
15
# spent 400µs (151+250) within Exception::Class::Base::BEGIN@15 which was called: # once (151µs+250µs) by Exception::Class::BEGIN@10 at line 30
BEGIN {
16976µs152µs __PACKAGE__->mk_classdata('Trace');
# spent 52µs making 1 call to Class::Data::Inheritable::mk_classdata
17137µs __PACKAGE__->mk_classdata('NoRefs');
# spent 37µs making 1 call to Class::Data::Inheritable::mk_classdata
18115µs __PACKAGE__->NoRefs(1);
19
20140µs __PACKAGE__->mk_classdata('NoContextInfo');
# spent 40µs making 1 call to Class::Data::Inheritable::mk_classdata
21111µs __PACKAGE__->NoContextInfo(0);
22
23135µs __PACKAGE__->mk_classdata('RespectOverload');
# spent 35µs making 1 call to Class::Data::Inheritable::mk_classdata
24110µs __PACKAGE__->RespectOverload(0);
25
26138µs __PACKAGE__->mk_classdata('MaxArgLength');
# spent 38µs making 1 call to Class::Data::Inheritable::mk_classdata
27111µs __PACKAGE__->MaxArgLength(0);
28
29 sub Fields { () }
301196µs1400µs}
# spent 400µs making 1 call to Exception::Class::Base::BEGIN@15
31
32use overload
33 # an exception is always true
342382µs2432µs
# spent 233µs (35+199) within Exception::Class::Base::BEGIN@34 which was called: # once (35µs+199µs) by Exception::Class::BEGIN@10 at line 34
bool => sub {1}, '""' => 'as_string', fallback => 1;
# spent 233µs making 1 call to Exception::Class::Base::BEGIN@34 # spent 199µs making 1 call to overload::import
35
36# Create accessor routines
37
# spent 257µs within Exception::Class::Base::BEGIN@37 which was called: # once (257µs+0s) by Exception::Class::BEGIN@10 at line 66
BEGIN {
3827258µs my @fields = qw( message pid uid euid gid egid time trace );
39
40 foreach my $f (@fields) {
41 my $sub = sub { my $s = shift; return $s->{$f}; };
42
432566µs2140µs
# spent 88µs (37+52) within Exception::Class::Base::BEGIN@43 which was called: # once (37µs+52µs) by Exception::Class::BEGIN@10 at line 43
no strict 'refs';
# spent 88µs making 1 call to Exception::Class::Base::BEGIN@43 # spent 52µs making 1 call to strict::unimport
44 *{$f} = $sub;
45 }
46 *error = \&message;
47
48 my %trace_fields = (
49 package => 'package',
50 file => 'filename',
51 line => 'line',
52 );
53
54 while ( my ( $f, $m ) = each %trace_fields ) {
55 my $sub = sub {
56 my $s = shift;
57 return $s->{$f} if exists $s->{$f};
58
59 my $frame = $s->trace->frame(0);
60
61 return $s->{$f} = $frame ? $frame->$m() : undef;
62 };
632130µs299µs
# spent 63µs (26+37) within Exception::Class::Base::BEGIN@63 which was called: # once (26µs+37µs) by Exception::Class::BEGIN@10 at line 63
no strict 'refs';
# spent 63µs making 1 call to Exception::Class::Base::BEGIN@63 # spent 37µs making 1 call to strict::unimport
64 *{$f} = $sub;
65 }
6613.66ms1257µs}
# spent 257µs making 1 call to Exception::Class::Base::BEGIN@37
67
681;
69
70sub Classes { Exception::Class::Classes() }
71
72sub throw {
73 my $proto = shift;
74
75 $proto->rethrow if ref $proto;
76
77 die $proto->new(@_);
78}
79
80sub rethrow {
81 my $self = shift;
82
83 die $self;
84}
85
86sub new {
87 my $proto = shift;
88 my $class = ref $proto || $proto;
89
90 my $self = bless {}, $class;
91
92 $self->_initialize(@_);
93
94 return $self;
95}
96
97sub _initialize {
98 my $self = shift;
99 my %p = @_ == 1 ? ( error => $_[0] ) : @_;
100
101 $self->{message} = $p{message} || $p{error} || '';
102
103 $self->{show_trace} = $p{show_trace} if exists $p{show_trace};
104
105 if ( $self->NoContextInfo() ) {
106 $self->{show_trace} = 0;
107 $self->{package} = $self->{file} = $self->{line} = undef;
108 }
109 else {
110 # CORE::time is important to fix an error with some versions of
111 # Perl
112 $self->{time} = CORE::time();
113 $self->{pid} = $$;
114 $self->{uid} = $<;
115 $self->{euid} = $>;
116 $self->{gid} = $(;
117 $self->{egid} = $);
118
119 my @ignore_class = (__PACKAGE__);
120 my @ignore_package = 'Exception::Class';
121
122 if ( my $i = delete $p{ignore_class} ) {
123 push @ignore_class, ( ref($i) eq 'ARRAY' ? @$i : $i );
124 }
125
126 if ( my $i = delete $p{ignore_package} ) {
127 push @ignore_package, ( ref($i) eq 'ARRAY' ? @$i : $i );
128 }
129
130 $self->{trace} = Devel::StackTrace->new(
131 ignore_class => \@ignore_class,
132 ignore_package => \@ignore_package,
133 no_refs => $self->NoRefs,
134 respect_overload => $self->RespectOverload,
135 max_arg_length => $self->MaxArgLength,
136 );
137 }
138
139 my %fields = map { $_ => 1 } $self->Fields;
140 while ( my ( $key, $value ) = each %p ) {
141 next if $key =~ /^(?:error|message|show_trace)$/;
142
143 if ( $fields{$key} ) {
144 $self->{$key} = $value;
145 }
146 else {
147 Exception::Class::Base->throw(
148 error => "unknown field $key passed to constructor for class "
149 . ref $self );
150 }
151 }
152}
153
154sub description {
155 return 'Generic exception';
156}
157
158sub show_trace {
159 my $self = shift;
160
161 return 0 unless $self->{trace};
162
163 if (@_) {
164 $self->{show_trace} = shift;
165 }
166
167 return exists $self->{show_trace} ? $self->{show_trace} : $self->Trace;
168}
169
170sub as_string {
171 my $self = shift;
172
173 my $str = $self->full_message;
174 $str .= "\n\n" . $self->trace->as_string
175 if $self->show_trace;
176
177 return $str;
178}
179
180sub full_message { $_[0]->{message} }
181
182#
183# The %seen bit protects against circular inheritance.
184#
18515µseval <<'EOF' if $] == 5.006;
186sub isa {
187 my ( $inheritor, $base ) = @_;
188 $inheritor = ref($inheritor) if ref($inheritor);
189
190 my %seen;
191
192 no strict 'refs';
193 my @parents = ( $inheritor, @{"$inheritor\::ISA"} );
194 while ( my $class = shift @parents ) {
195 return 1 if $class eq $base;
196
197 push @parents, grep { !$seen{$_}++ } @{"$class\::ISA"};
198 }
199 return 0;
200}
201EOF
202
203sub caught {
204 my $class = shift;
205
206 my $e = $@;
207
208 return unless defined $e && blessed($e) && $e->isa($class);
209 return $e;
210}
211
212114µs1;
213
214# ABSTRACT: A base class for exception objects
215
- -
218=pod
219
220=head1 NAME
221
222Exception::Class::Base - A base class for exception objects
223
224=head1 VERSION
225
226version 1.32
227
228=head1 SYNOPSIS
229
230 use Exception::Class 'MyException';
231
232 eval { MyException->throw( error => 'I feel funny.' ) };
233
234 print $@->error();
235
236=head1 DESCRIPTION
237
238This class is the base class for all exceptions created by
239L<Exception::Class>. It provides a number of methods for getting
240information about the exception.
241
242=head1 METHODS
243
244=head2 MyException->Trace($boolean)
245
246Each C<Exception::Class::Base> subclass can be set individually to
247include a stacktrace when the C<as_string> method is called. The
248default is to not include a stacktrace. Calling this method with a
249value changes this behavior. It always returns the current value
250(after any change is applied).
251
252This value is inherited by any subclasses. However, if this value is
253set for a subclass, it will thereafter be independent of the value in
254C<Exception::Class::Base>.
255
256Do not call this on the C<Exception::Class::Base> class directly or
257you'll change it for all exception classes that use
258L<Exception::Class>, including ones created in modules you don't
259control.
260
261This is a class method, not an object method.
262
263=head2 MyException->NoRefs($boolean)
264
265When a C<Devel::StackTrace> object is created, it walks through the
266stack and stores the arguments which were passed to each subroutine on
267the stack. If any of these arguments are references, then that means
268that the C<Devel::StackTrace> ends up increasing the refcount of these
269references, delaying their destruction.
270
271Since C<Exception::Class::Base> uses C<Devel::StackTrace> internally,
272this method provides a way to tell C<Devel::StackTrace> not to store
273these references. Instead, C<Devel::StackTrace> replaces references
274with their stringified representation.
275
276This method defaults to true. As with C<Trace()>, it is inherited by
277subclasses but setting it in a subclass makes it independent
278thereafter.
279
280Do not call this on the C<Exception::Class::Base> class directly or
281you'll change it for all exception classes that use
282L<Exception::Class>, including ones created in modules you don't
283control.
284
285=head2 MyException->RespectOverload($boolean)
286
287When a C<Devel::StackTrace> object stringifies, by default it ignores
288stringification overloading on any objects being dealt with.
289
290Since C<Exception::Class::Base> uses C<Devel::StackTrace> internally,
291this method provides a way to tell C<Devel::StackTrace> to respect
292overloading.
293
294This method defaults to false. As with C<Trace()>, it is inherited by
295subclasses but setting it in a subclass makes it independent
296thereafter.
297
298Do not call this on the C<Exception::Class::Base> class directly or
299you'll change it for all exception classes that use
300L<Exception::Class>, including ones created in modules you don't
301control.
302
303=head2 MyException->MaxArgLength($boolean)
304
305When a C<Devel::StackTrace> object stringifies, by default it displays
306the full argument for each function. This parameter can be used to
307limit the maximum length of each argument.
308
309Since C<Exception::Class::Base> uses C<Devel::StackTrace> internally,
310this method provides a way to tell C<Devel::StackTrace> to limit the
311length of arguments.
312
313This method defaults to 0. As with C<Trace()>, it is inherited by
314subclasses but setting it in a subclass makes it independent
315thereafter.
316
317Do not call this on the C<Exception::Class::Base> class directly or
318you'll change it for all exception classes that use
319L<Exception::Class>, including ones created in modules you don't
320control.
321
322=head2 MyException->Fields
323
324This method returns the extra fields defined for the given class, as
325an array.
326
327Do not call this on the C<Exception::Class::Base> class directly or
328you'll change it for all exception classes that use
329L<Exception::Class>, including ones created in modules you don't
330control.
331
332=head2 MyException->throw( $message )
333
334=head2 MyException->throw( message => $message )
335
336=head2 MyException->throw( error => $error )
337
338This method creates a new object with the given error message. If no
339error message is given, this will be an empty string. It then dies
340with this object as its argument.
341
342This method also takes a C<show_trace> parameter which indicates
343whether or not the particular exception object being created should
344show a stacktrace when its C<as_string()> method is called. This
345overrides the value of C<Trace()> for this class if it is given.
346
347The frames included in the trace can be controlled by the C<ignore_class>
348and C<ignore_package> parameters. These are passed directly to
349Devel::Stacktrace's constructor. See C<Devel::Stacktrace> for more details.
350
351If only a single value is given to the constructor it is assumed to be
352the message parameter.
353
354Additional keys corresponding to the fields defined for the particular
355exception subclass will also be accepted.
356
357=head2 MyException->new(...)
358
359This method takes the same parameters as C<throw()>, but instead of
360dying simply returns a new exception object.
361
362This method is always called when constructing a new exception object
363via the C<throw()> method.
364
365=head2 MyException->description()
366
367Returns the description for the given C<Exception::Class::Base>
368subclass. The C<Exception::Class::Base> class's description is
369"Generic exception" (this may change in the future). This is also an
370object method.
371
372=head2 $exception->rethrow()
373
374Simply dies with the object as its sole argument. It's just syntactic
375sugar. This does not change any of the object's attribute values.
376However, it will cause C<caller()> to report the die as coming from
377within the C<Exception::Class::Base> class rather than where rethrow
378was called.
379
380Of course, you always have access to the original stacktrace for the
381exception object.
382
383=head2 $exception->message()
384
385=head2 $exception->error()
386
387Returns the error/message associated with the exception.
388
389=head2 $exception->pid()
390
391Returns the pid at the time the exception was thrown.
392
393=head2 $exception->uid()
394
395Returns the real user id at the time the exception was thrown.
396
397=head2 $exception->gid()
398
399Returns the real group id at the time the exception was thrown.
400
401=head2 $exception->euid()
402
403Returns the effective user id at the time the exception was thrown.
404
405=head2 $exception->egid()
406
407Returns the effective group id at the time the exception was thrown.
408
409=head2 $exception->time()
410
411Returns the time in seconds since the epoch at the time the exception
412was thrown.
413
414=head2 $exception->package()
415
416Returns the package from which the exception was thrown.
417
418=head2 $exception->file()
419
420Returns the file within which the exception was thrown.
421
422=head2 $exception->line()
423
424Returns the line where the exception was thrown.
425
426=head2 $exception->trace()
427
428Returns the trace object associated with the object.
429
430=head2 $exception->show_trace($boolean)
431
432This method can be used to set whether or not a stack trace is
433included when the as_string method is called or the object is
434stringified.
435
436=head2 $exception->as_string()
437
438Returns a string form of the error message (something like what you'd
439expect from die). If the class or object is set to show traces then
440then the full trace is also included. The result looks like
441C<Carp::confess()>.
442
443=head2 $exception->full_message()
444
445Called by the C<as_string()> method to get the message. By default,
446this is the same as calling the C<message()> method, but may be
447overridden by a subclass. See below for details.
448
449=head1 LIGHTWEIGHT EXCEPTIONS
450
451A lightweight exception is one which records no information about its context
452when it is created. This can be achieved by setting C<<
453$class->NoContextInfo() >> to a true value.
454
455You can make this the default for a class of exceptions by setting it after
456creating the class:
457
458 use Exception::Class (
459 'LightWeight',
460 'HeavyWeight',
461 );
462
463 LightWeight->NoContextInfo(1);
464
465A lightweight exception does have a stack trace object, nor does it record the
466time, pid, uid, euid, gid, or egid. It only has a message.
467
468=head1 OVERLOADING
469
470C<Exception::Class::Base> objects are overloaded so that
471stringification produces a normal error message. This just calls the
472C<< $exception->as_string() >> method described above. This means
473that you can just C<print $@> after an C<eval> and not worry about
474whether or not its an actual object. It also means an application or
475module could do this:
476
477 $SIG{__DIE__} = sub { Exception::Class::Base->throw( error => join '', @_ ); };
478
479and this would probably not break anything (unless someone was
480expecting a different type of exception object from C<die()>).
481
482=head1 OVERRIDING THE as_string METHOD
483
484By default, the C<as_string()> method simply returns the value
485C<message> or C<error> param plus a stack trace, if the class's
486C<Trace()> method returns a true value or C<show_trace> was set when
487creating the exception.
488
489However, once you add new fields to a subclass, you may want to
490include those fields in the stringified error.
491
492Inside the C<as_string()> method, the message (non-stack trace)
493portion of the error is generated by calling the C<full_message()>
494method. This can be easily overridden. For example:
495
496 sub full_message {
497 my $self = shift;
498
499 my $msg = $self->message;
500
501 $msg .= " and foo was " . $self->foo;
502
503 return $msg;
504 }
505
506=head1 AUTHOR
507
508 Dave Rolsky <autarch@urth.org>
509
510=head1 COPYRIGHT AND LICENSE
511
512This software is Copyright (c) 2010 by Dave Rolsky.
513
514This is free software, licensed under:
515
516 The Artistic License 2.0
517
518=cut
519
520
521__END__