Debian Codegraph @

upstream/opensuse-tumbleweed/man3/autodie::hints.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "autodie::hints 3pm"
61 .TH autodie::hints 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 autodie::hints \- Provide hints about user subroutines to autodie
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& package Your::Module;
72 \&
73 \& our %DOES = ( \*(Aqautodie::hints::provider\*(Aq => 1 );
74 \&
75 \& sub AUTODIE_HINTS {
76 \& return {
77 \& foo => { scalar => HINTS, list => SOME_HINTS },
78 \& bar => { scalar => HINTS, list => MORE_HINTS },
79 \& }
80 \& }
81 \&
82 \& # Later, in your main program...
83 \&
84 \& use Your::Module qw(foo bar);
85 \& use autodie qw(:default foo bar);
86 \&
87 \& foo(); # succeeds or dies based on scalar hints
88 \&
89 \& # Alternatively, hints can be set on subroutines we\*(Aqve
90 \& # imported.
91 \&
92 \& use autodie::hints;
93 \& use Some::Module qw(think_positive);
94 \&
95 \& BEGIN {
96 \& autodie::hints\->set_hints_for(
97 \& \e&think_positive,
98 \& {
99 \& fail => sub { $_[0] <= 0 }
100 \& }
101 \& )
102 \& }
103 \& use autodie qw(think_positive);
104 \&
105 \& think_positive(...); # Returns positive or dies.
106 .Ve
107 .SH DESCRIPTION
108 .IX Header "DESCRIPTION"
109 .SS Introduction
110 .IX Subsection "Introduction"
111 The autodie pragma is very smart when it comes to working with
112 Perl\*(Aqs built\-in functions. The behaviour for these functions are
113 fixed, and \f(CW\*(C`autodie\*(C'\fR knows exactly how they try to signal failure.
114 .PP
115 But what about user\-defined subroutines from modules? If you use
116 \&\f(CW\*(C`autodie\*(C'\fR on a user\-defined subroutine then it assumes the following
117 behaviour to demonstrate failure:
118 .IP \(bu 4
119 A false value, in scalar context
120 .IP \(bu 4
121 An empty list, in list context
122 .IP \(bu 4
123 A list containing a single undef, in list context
124 .PP
125 All other return values (including the list of the single zero, and the
126 list containing a single empty string) are considered successful. However,
127 real\-world code isn\*(Aqt always that easy. Perhaps the code you\*(Aqre working
128 with returns a string containing the word "FAIL" upon failure, or a
129 two element list containing \f(CW\*(C`(undef, "human error message")\*(C'\fR. To make
130 autodie work with these sorts of subroutines, we have
131 the \fIhinting interface\fR.
132 .PP
133 The hinting interface allows \fIhints\fR to be provided to \f(CW\*(C`autodie\*(C'\fR
134 on how it should detect failure from user\-defined subroutines. While
135 these \fIcan\fR be provided by the end\-user of \f(CW\*(C`autodie\*(C'\fR, they are ideally
136 written into the module itself, or into a helper module or sub\-class
137 of \f(CW\*(C`autodie\*(C'\fR itself.
138 .SS "What are hints?"
139 .IX Subsection "What are hints?"
140 A \fIhint\fR is a subroutine or value that is checked against the
141 return value of an autodying subroutine. If the match returns true,
142 \&\f(CW\*(C`autodie\*(C'\fR considers the subroutine to have failed.
143 .PP
144 If the hint provided is a subroutine, then \f(CW\*(C`autodie\*(C'\fR will pass
145 the complete return value to that subroutine. If the hint is a regexp object,
146 then \f(CW\*(C`autodie\*(C'\fR will match it against the return value. If the hint is undef,
147 the return value must be undef. On Perl versions 5.10 and newer, any other
148 value can be provided and it will be smart matched against the value provided.
149 However, smart matched values like this are deprecated.
150 .PP
151 Hints can be provided for both scalar and list contexts. Note
152 that an autodying subroutine will never see a void context, as
153 \&\f(CW\*(C`autodie\*(C'\fR always needs to capture the return value for examination.
154 Autodying subroutines called in void context act as if they\*(Aqre called
155 in a scalar context, but their return value is discarded after it
156 has been checked.
157 .SS "Example hints"
158 .IX Subsection "Example hints"
159 Hints may consist of subroutine references, objects overloading
160 smart\-match, regular expressions, and depending on Perl version possibly
161 other things. You can specify different hints for how
162 failure should be identified in scalar and list contexts.
163 .PP
164 These examples apply for use in the \f(CW\*(C`AUTODIE_HINTS\*(C'\fR subroutine and when
165 calling \f(CW\*(C`autodie::hints\->set_hints_for()\*(C'\fR.
166 .PP
167 The most common context\-specific hints are:
168 .PP
169 .Vb 2
170 \& # Scalar failures always return undef:
171 \& { scalar => sub { !defined($_[0]) } }
172 \&
173 \& # Scalar failures return any false value [default expectation]:
174 \& { scalar => sub { ! $_[0] } }
175 \&
176 \& # Scalar failures always return zero explicitly:
177 \& { scalar => sub { defined($_[0]) && $_[0] eq \*(Aq0\*(Aq } }
178 \&
179 \& # List failures always return an empty list:
180 \& { list => sub { !@_ } }
181 \&
182 \& # List failures return () or (undef) [default expectation]:
183 \& { list => sub { ! @_ || @_ == 1 && !defined $_[0] } }
184 \&
185 \& # List failures return () or a single false value:
186 \& { list => sub { ! @_ || @_ == 1 && !$_[0] } }
187 \&
188 \& # List failures return (undef, "some string")
189 \& { list => sub { @_ == 2 && !defined $_[0] } }
190 \&
191 \& # Unsuccessful foo() returns \*(AqFAIL\*(Aq or \*(Aq_FAIL\*(Aq in scalar context,
192 \& # returns (\-1) in list context...
193 \& autodie::hints\->set_hints_for(
194 \& \e&foo,
195 \& {
196 \& scalar => qr/^ _? FAIL $/xms,
197 \& list => sub { @_ == 1 && $_[0] eq \-1 },
198 \& }
199 \& );
200 \&
201 \& # Unsuccessful foo() returns 0 in all contexts...
202 \& autodie::hints\->set_hints_for(
203 \& \e&foo,
204 \& {
205 \& scalar => sub { defined($_[0]) && $_[0] == 0 },
206 \& list => sub { @_ == 1 && defined($_[0]) && $_[0] == 0 },
207 \& }
208 \& );
209 .Ve
210 .PP
211 This "in all contexts" construction is very common, and can be
212 abbreviated, using the \*(Aqfail\*(Aq key. This sets both the \f(CW\*(C`scalar\*(C'\fR
213 and \f(CW\*(C`list\*(C'\fR hints to the same value:
214 .PP
215 .Vb 7
216 \& # Unsuccessful foo() returns 0 in all contexts...
217 \& autodie::hints\->set_hints_for(
218 \& \e&foo,
219 \& {
220 \& fail => sub { @_ == 1 and defined $_[0] and $_[0] == 0 }
221 \& }
222 \& );
223 \&
224 \& # Unsuccessful think_positive() returns negative number on failure...
225 \& autodie::hints\->set_hints_for(
226 \& \e&think_positive,
227 \& {
228 \& fail => sub { $_[0] < 0 }
229 \& }
230 \& );
231 \&
232 \& # Unsuccessful my_system() returns non\-zero on failure...
233 \& autodie::hints\->set_hints_for(
234 \& \e&my_system,
235 \& {
236 \& fail => sub { $_[0] != 0 }
237 \& }
238 \& );
239 .Ve
240 .SH "Manually setting hints from within your program"
241 .IX Header "Manually setting hints from within your program"
242 If you are using a module which returns something special on failure, then
243 you can manually create hints for each of the desired subroutines. Once
244 the hints are specified, they are available for all files and modules loaded
245 thereafter, thus you can move this work into a module and it will still
246 work.
247 .PP
248 .Vb 2
249 \& use Some::Module qw(foo bar);
250 \& use autodie::hints;
251 \&
252 \& autodie::hints\->set_hints_for(
253 \& \e&foo,
254 \& {
255 \& scalar => SCALAR_HINT,
256 \& list => LIST_HINT,
257 \& }
258 \& );
259 \& autodie::hints\->set_hints_for(
260 \& \e&bar,
261 \& { fail => SOME_HINT, }
262 \& );
263 .Ve
264 .PP
265 It is possible to pass either a subroutine reference (recommended) or a fully
266 qualified subroutine name as the first argument. This means you can set hints
267 on modules that \fImight\fR get loaded:
268 .PP
269 .Vb 4
270 \& use autodie::hints;
271 \& autodie::hints\->set_hints_for(
272 \& \*(AqSome::Module:bar\*(Aq, { fail => SCALAR_HINT, }
273 \& );
274 .Ve
275 .PP
276 This technique is most useful when you have a project that uses a
277 lot of third\-party modules. You can define all your possible hints
278 in one\-place. This can even be in a sub\-class of autodie. For
279 example:
280 .PP
281 .Vb 1
282 \& package my::autodie;
283 \&
284 \& use parent qw(autodie);
285 \& use autodie::hints;
286 \&
287 \& autodie::hints\->set_hints_for(...);
288 \&
289 \& 1;
290 .Ve
291 .PP
292 You can now \f(CW\*(C`use my::autodie\*(C'\fR, which will work just like the standard
293 \&\f(CW\*(C`autodie\*(C'\fR, but is now aware of any hints that you\*(Aqve set.
294 .SH "Adding hints to your module"
295 .IX Header "Adding hints to your module"
296 \&\f(CW\*(C`autodie\*(C'\fR provides a passive interface to allow you to declare hints for
297 your module. These hints will be found and used by \f(CW\*(C`autodie\*(C'\fR if it
298 is loaded, but otherwise have no effect (or dependencies) without autodie.
299 To set these, your module needs to declare that it \fIdoes\fR the
300 \&\f(CW\*(C`autodie::hints::provider\*(C'\fR role. This can be done by writing your
301 own \f(CW\*(C`DOES\*(C'\fR method, using a system such as \f(CW\*(C`Class::DOES\*(C'\fR to handle
302 the heavy\-lifting for you, or declaring a \f(CW%DOES\fR package variable
303 with a \f(CW\*(C`autodie::hints::provider\*(C'\fR key and a corresponding true value.
304 .PP
305 Note that checking for a \f(CW%DOES\fR hash is an \f(CW\*(C`autodie\*(C'\fR\-only
306 short\-cut. Other modules do not use this mechanism for checking
307 roles, although you can use the \f(CW\*(C`Class::DOES\*(C'\fR module from the
308 CPAN to allow it.
309 .PP
310 In addition, you must define a \f(CW\*(C`AUTODIE_HINTS\*(C'\fR subroutine that returns
311 a hash\-reference containing the hints for your subroutines:
312 .PP
313 .Vb 1
314 \& package Your::Module;
315 \&
316 \& # We can use the Class::DOES from the CPAN to declare adherence
317 \& # to a role.
318 \&
319 \& use Class::DOES \*(Aqautodie::hints::provider\*(Aq => 1;
320 \&
321 \& # Alternatively, we can declare the role in %DOES. Note that
322 \& # this is an autodie specific optimisation, although Class::DOES
323 \& # can be used to promote this to a true role declaration.
324 \&
325 \& our %DOES = ( \*(Aqautodie::hints::provider\*(Aq => 1 );
326 \&
327 \& # Finally, we must define the hints themselves.
328 \&
329 \& sub AUTODIE_HINTS {
330 \& return {
331 \& foo => { scalar => HINTS, list => SOME_HINTS },
332 \& bar => { scalar => HINTS, list => MORE_HINTS },
333 \& baz => { fail => HINTS },
334 \& }
335 \& }
336 .Ve
337 .PP
338 This allows your code to set hints without relying on \f(CW\*(C`autodie\*(C'\fR and
339 \&\f(CW\*(C`autodie::hints\*(C'\fR being loaded, or even installed. In this way your
340 code can do the right thing when \f(CW\*(C`autodie\*(C'\fR is installed, but does not
341 need to depend upon it to function.
342 .SH "Insisting on hints"
343 .IX Header "Insisting on hints"
344 When a user\-defined subroutine is wrapped by \f(CW\*(C`autodie\*(C'\fR, it will
345 use hints if they are available, and otherwise reverts to the
346 \&\fIdefault behaviour\fR described in the introduction of this document.
347 This can be problematic if we expect a hint to exist, but (for
348 whatever reason) it has not been loaded.
349 .PP
350 We can ask autodie to \fIinsist\fR that a hint be used by prefixing
351 an exclamation mark to the start of the subroutine name. A lone
352 exclamation mark indicates that \fIall\fR subroutines after it must
353 have hints declared.
354 .PP
355 .Vb 2
356 \& # foo() and bar() must have their hints defined
357 \& use autodie qw( !foo !bar baz );
358 \&
359 \& # Everything must have hints (recommended).
360 \& use autodie qw( ! foo bar baz );
361 \&
362 \& # bar() and baz() must have their hints defined
363 \& use autodie qw( foo ! bar baz );
364 \&
365 \& # Enable autodie for all of Perl\*(Aqs supported built\-ins,
366 \& # as well as for foo(), bar() and baz(). Everything must
367 \& # have hints.
368 \& use autodie qw( ! :all foo bar baz );
369 .Ve
370 .PP
371 If hints are not available for the specified subroutines, this will cause a
372 compile\-time error. Insisting on hints for Perl\*(Aqs built\-in functions
373 (eg, \f(CW\*(C`open\*(C'\fR and \f(CW\*(C`close\*(C'\fR) is always successful.
374 .PP
375 Insisting on hints is \fIstrongly\fR recommended.
376 .SH Diagnostics
377 .IX Header "Diagnostics"
378 .IP "Attempts to set_hints_for unidentifiable subroutine" 4
379 .IX Item "Attempts to set_hints_for unidentifiable subroutine"
380 You\*(Aqve called \f(CW\*(C`autodie::hints\->set_hints_for()\*(C'\fR using a subroutine
381 reference, but that reference could not be resolved back to a
382 subroutine name. It may be an anonymous subroutine (which can\*(Aqt
383 be made autodying), or may lack a name for other reasons.
384 .Sp
385 If you receive this error with a subroutine that has a real name,
386 then you may have found a bug in autodie. See "BUGS" in autodie
387 for how to report this.
388 .ie n .IP "fail hints cannot be provided with either scalar or list hints for %s" 4
389 .el .IP "fail hints cannot be provided with either scalar or list hints for \f(CW%s\fR" 4
390 .IX Item "fail hints cannot be provided with either scalar or list hints for %s"
391 When defining hints, you can either supply both \f(CW\*(C`list\*(C'\fR and
392 \&\f(CW\*(C`scalar\*(C'\fR keywords, \fIor\fR you can provide a single \f(CW\*(C`fail\*(C'\fR keyword.
393 You can\*(Aqt mix and match them.
394 .ie n .IP "%s hint missing for %s" 4
395 .el .IP "\f(CW%s\fR hint missing for \f(CW%s\fR" 4
396 .IX Item "%s hint missing for %s"
397 You\*(Aqve provided either a \f(CW\*(C`scalar\*(C'\fR hint without supplying
398 a \f(CW\*(C`list\*(C'\fR hint, or vice\-versa. You \fImust\fR supply both \f(CW\*(C`scalar\*(C'\fR
399 and \f(CW\*(C`list\*(C'\fR hints, \fIor\fR a single \f(CW\*(C`fail\*(C'\fR hint.
400 .SH ACKNOWLEDGEMENTS
401 .IX Header "ACKNOWLEDGEMENTS"
402 .IP \(bu 4
403 Dr Damian Conway for suggesting the hinting interface and providing the
404 example usage.
405 .IP \(bu 4
406 Jacinta Richardson for translating much of my ideas into this
407 documentation.
408 .SH AUTHOR
409 .IX Header "AUTHOR"
410 Copyright 2009, Paul Fenwick <[email protected]>
411 .SH LICENSE
412 .IX Header "LICENSE"
413 This module is free software. You may distribute it under the
414 same terms as Perl itself.
415 .SH "SEE ALSO"
416 .IX Header "SEE ALSO"
417 autodie, Class::DOES