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