1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "autodie::exception 3pm"
.TH autodie::exception 3pm 2023-11-28 "perl v5.38.2" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
autodie::exception \- Exceptions from autodying functions.
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 2
\& eval {
\& use autodie;
\&
\& open(my $fh, \*(Aq<\*(Aq, \*(Aqsome_file.txt\*(Aq);
\&
\& ...
\& };
\&
\& if (my $E = $@) {
\& say "Ooops! ",$E\->caller," had problems: $@";
\& }
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
When an autodie enabled function fails, it generates an
\&\f(CW\*(C`autodie::exception\*(C'\fR object. This can be interrogated to
determine further information about the error that occurred.
.PP
This document is broken into two sections; those methods that
are most useful to the end-developer, and those methods for
anyone wishing to subclass or get very familiar with
\&\f(CW\*(C`autodie::exception\*(C'\fR.
.SS "Common Methods"
.IX Subsection "Common Methods"
These methods are intended to be used in the everyday dealing
of exceptions.
.PP
The following assume that the error has been copied into
a separate scalar:
.PP
.Vb 3
\& if ($E = $@) {
\& ...
\& }
.Ve
.PP
This is not required, but is recommended in case any code
is called which may reset or alter \f(CW$@\fR.
.PP
\fIargs\fR
.IX Subsection "args"
.PP
.Vb 1
\& my $array_ref = $E\->args;
.Ve
.PP
Provides a reference to the arguments passed to the subroutine
that died.
.PP
\fIfunction\fR
.IX Subsection "function"
.PP
.Vb 1
\& my $sub = $E\->function;
.Ve
.PP
The subroutine (including package) that threw the exception.
.PP
\fIfile\fR
.IX Subsection "file"
.PP
.Vb 1
\& my $file = $E\->file;
.Ve
.PP
The file in which the error occurred (eg, \f(CW\*(C`myscript.pl\*(C'\fR or
\&\f(CW\*(C`MyTest.pm\*(C'\fR).
.PP
\fIpackage\fR
.IX Subsection "package"
.PP
.Vb 1
\& my $package = $E\->package;
.Ve
.PP
The package from which the exceptional subroutine was called.
.PP
\fIcaller\fR
.IX Subsection "caller"
.PP
.Vb 1
\& my $caller = $E\->caller;
.Ve
.PP
The subroutine that \fIcalled\fR the exceptional code.
.PP
\fIline\fR
.IX Subsection "line"
.PP
.Vb 1
\& my $line = $E\->line;
.Ve
.PP
The line in \f(CW\*(C`$E\->file\*(C'\fR where the exceptional code was called.
.PP
\fIcontext\fR
.IX Subsection "context"
.PP
.Vb 1
\& my $context = $E\->context;
.Ve
.PP
The context in which the subroutine was called by autodie; usually
the same as the context in which you called the autodying subroutine.
This can be 'list', 'scalar', or undefined (unknown). It will never
be 'void', as \f(CW\*(C`autodie\*(C'\fR always captures the return value in one way
or another.
.PP
For some core functions that always return a scalar value regardless
of their context (eg, \f(CW\*(C`chown\*(C'\fR), this may be 'scalar', even if you
used a list context.
.PP
\fIreturn\fR
.IX Subsection "return"
.PP
.Vb 1
\& my $return_value = $E\->return;
.Ve
.PP
The value(s) returned by the failed subroutine. When the subroutine
was called in a list context, this will always be a reference to an
array containing the results. When the subroutine was called in
a scalar context, this will be the actual scalar returned.
.PP
\fIerrno\fR
.IX Subsection "errno"
.PP
.Vb 1
\& my $errno = $E\->errno;
.Ve
.PP
The value of \f(CW$!\fR at the time when the exception occurred.
.PP
\&\fBNOTE\fR: This method will leave the main \f(CW\*(C`autodie::exception\*(C'\fR class
and become part of a role in the future. You should only call
\&\f(CW\*(C`errno\*(C'\fR for exceptions where \f(CW$!\fR would reasonably have been
set on failure.
.PP
\fIeval_error\fR
.IX Subsection "eval_error"
.PP
.Vb 1
\& my $old_eval_error = $E\->eval_error;
.Ve
.PP
The contents of \f(CW$@\fR immediately after autodie triggered an
exception. This may be useful when dealing with modules such
as Text::Balanced that set (but do not throw) \f(CW$@\fR on error.
.PP
\fImatches\fR
.IX Subsection "matches"
.PP
.Vb 1
\& if ( $e\->matches(\*(Aqopen\*(Aq) ) { ... }
\&
\& if ( \*(Aqopen\*(Aq ~~ $e ) { ... }
.Ve
.PP
\&\f(CW\*(C`matches\*(C'\fR is used to determine whether a
given exception matches a particular role.
.PP
An exception is considered to match a string if:
.IP \(bu 4
For a string not starting with a colon, the string exactly matches the
package and subroutine that threw the exception. For example,
\&\f(CW\*(C`MyModule::log\*(C'\fR. If the string does not contain a package name,
\&\f(CW\*(C`CORE::\*(C'\fR is assumed.
.IP \(bu 4
For a string that does start with a colon, if the subroutine
throwing the exception \fIdoes\fR that behaviour. For example, the
\&\f(CW\*(C`CORE::open\*(C'\fR subroutine does \f(CW\*(C`:file\*(C'\fR, \f(CW\*(C`:io\*(C'\fR and \f(CW\*(C`:all\*(C'\fR.
.Sp
See "CATEGORIES" in autodie for further information.
.Sp
On Perl 5.10 and above, using smart-match (\f(CW\*(C`~~\*(C'\fR) with an
\&\f(CW\*(C`autodie::exception\*(C'\fR object will use \f(CW\*(C`matches\*(C'\fR underneath. This module
used to recommend using smart-match with the exception object on the left
hand side, but in future Perls that is likely to stop working.
The smart-match facility of this class should only be used with the
exception object on the right hand side. Having the exception object on
the right is both future-proof and portable to older Perls, back to 5.10.
Beware that this facility can only
be relied upon when it is certain that the exception object actually is
an \f(CW\*(C`autodie::exception\*(C'\fR object; it is no more capable than an explicit
call to the \f(CW\*(C`matches\*(C'\fR method.
.SS "Advanced methods"
.IX Subsection "Advanced methods"
The following methods, while usable from anywhere, are primarily
intended for developers wishing to subclass \f(CW\*(C`autodie::exception\*(C'\fR,
write code that registers custom error messages, or otherwise
work closely with the \f(CW\*(C`autodie::exception\*(C'\fR model.
.PP
\fIregister\fR
.IX Subsection "register"
.PP
.Vb 1
\& autodie::exception\->register( \*(AqCORE::open\*(Aq => \e&mysub );
.Ve
.PP
The \f(CW\*(C`register\*(C'\fR method allows for the registration of a message
handler for a given subroutine. The full subroutine name including
the package should be used.
.PP
Registered message handlers will receive the \f(CW\*(C`autodie::exception\*(C'\fR
object as the first parameter.
.PP
\fIadd_file_and_line\fR
.IX Subsection "add_file_and_line"
.PP
.Vb 1
\& say "Problem occurred",$@\->add_file_and_line;
.Ve
.PP
Returns the string \f(CW\*(C` at %s line %d\*(C'\fR, where \f(CW%s\fR is replaced with
the filename, and \f(CW%d\fR is replaced with the line number.
.PP
Primarily intended for use by format handlers.
.PP
\fIstringify\fR
.IX Subsection "stringify"
.PP
.Vb 1
\& say "The error was: ",$@\->stringify;
.Ve
.PP
Formats the error as a human readable string. Usually there's no
reason to call this directly, as it is used automatically if an
\&\f(CW\*(C`autodie::exception\*(C'\fR object is ever used as a string.
.PP
Child classes can override this method to change how they're
stringified.
.PP
\fIformat_default\fR
.IX Subsection "format_default"
.PP
.Vb 1
\& my $error_string = $E\->format_default;
.Ve
.PP
This produces the default error string for the given exception,
\&\fIwithout using any registered message handlers\fR. It is primarily
intended to be called from a message handler when they have
been passed an exception they don't want to format.
.PP
Child classes can override this method to change how default
messages are formatted.
.PP
\fInew\fR
.IX Subsection "new"
.PP
.Vb 7
\& my $error = autodie::exception\->new(
\& args => \e@_,
\& function => "CORE::open",
\& errno => $!,
\& context => \*(Aqscalar\*(Aq,
\& return => undef,
\& );
.Ve
.PP
Creates a new \f(CW\*(C`autodie::exception\*(C'\fR object. Normally called
directly from an autodying function. The \f(CW\*(C`function\*(C'\fR argument
is required, its the function we were trying to call that
generated the exception. The \f(CW\*(C`args\*(C'\fR parameter is optional.
.PP
The \f(CW\*(C`errno\*(C'\fR value is optional. In versions of \f(CW\*(C`autodie::exception\*(C'\fR
1.99 and earlier the code would try to automatically use the
current value of \f(CW$!\fR, but this was unreliable and is no longer
supported.
.PP
Atrributes such as package, file, and caller are determined
automatically, and cannot be specified.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
autodie, autodie::exception::system
.SH LICENSE
.IX Header "LICENSE"
Copyright (C)2008 Paul Fenwick
.PP
This is free software. You may modify and/or redistribute this
code under the same terms as Perl 5.10 itself, or, at your option,
any later version of Perl 5.
.SH AUTHOR
.IX Header "AUTHOR"
Paul Fenwick <pjf@perltraining.com.au>
|