CINXE.COM

autodie::hints - Provide hints about user subroutines to autodie - Perldoc Browser

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>autodie::hints - Provide hints about user subroutines to autodie - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/autodie::hints"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.41.9</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/autodie::hints">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/autodie::hints">5.40.1</a> <a class="dropdown-item" href="/5.40.0/autodie::hints">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/autodie::hints">5.38.3</a> <a class="dropdown-item" href="/5.38.2/autodie::hints">5.38.2</a> <a class="dropdown-item" href="/5.38.1/autodie::hints">5.38.1</a> <a class="dropdown-item" href="/5.38.0/autodie::hints">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/autodie::hints">5.36.3</a> <a class="dropdown-item" href="/5.36.2/autodie::hints">5.36.2</a> <a class="dropdown-item" href="/5.36.1/autodie::hints">5.36.1</a> <a class="dropdown-item" href="/5.36.0/autodie::hints">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/autodie::hints">5.34.3</a> <a class="dropdown-item" href="/5.34.2/autodie::hints">5.34.2</a> <a class="dropdown-item" href="/5.34.1/autodie::hints">5.34.1</a> <a class="dropdown-item" href="/5.34.0/autodie::hints">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/autodie::hints">5.32.1</a> <a class="dropdown-item" href="/5.32.0/autodie::hints">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/autodie::hints">5.30.3</a> <a class="dropdown-item" href="/5.30.2/autodie::hints">5.30.2</a> <a class="dropdown-item" href="/5.30.1/autodie::hints">5.30.1</a> <a class="dropdown-item" href="/5.30.0/autodie::hints">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/autodie::hints">5.28.3</a> <a class="dropdown-item" href="/5.28.2/autodie::hints">5.28.2</a> <a class="dropdown-item" href="/5.28.1/autodie::hints">5.28.1</a> <a class="dropdown-item" href="/5.28.0/autodie::hints">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/autodie::hints">5.26.3</a> <a class="dropdown-item" href="/5.26.2/autodie::hints">5.26.2</a> <a class="dropdown-item" href="/5.26.1/autodie::hints">5.26.1</a> <a class="dropdown-item" href="/5.26.0/autodie::hints">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/autodie::hints">5.24.4</a> <a class="dropdown-item" href="/5.24.3/autodie::hints">5.24.3</a> <a class="dropdown-item" href="/5.24.2/autodie::hints">5.24.2</a> <a class="dropdown-item" href="/5.24.1/autodie::hints">5.24.1</a> <a class="dropdown-item" href="/5.24.0/autodie::hints">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/autodie::hints">5.22.4</a> <a class="dropdown-item" href="/5.22.3/autodie::hints">5.22.3</a> <a class="dropdown-item" href="/5.22.2/autodie::hints">5.22.2</a> <a class="dropdown-item" href="/5.22.1/autodie::hints">5.22.1</a> <a class="dropdown-item" href="/5.22.0/autodie::hints">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/autodie::hints">5.20.3</a> <a class="dropdown-item" href="/5.20.2/autodie::hints">5.20.2</a> <a class="dropdown-item" href="/5.20.1/autodie::hints">5.20.1</a> <a class="dropdown-item" href="/5.20.0/autodie::hints">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/autodie::hints">5.18.4</a> <a class="dropdown-item" href="/5.18.3/autodie::hints">5.18.3</a> <a class="dropdown-item" href="/5.18.2/autodie::hints">5.18.2</a> <a class="dropdown-item" href="/5.18.1/autodie::hints">5.18.1</a> <a class="dropdown-item" href="/5.18.0/autodie::hints">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/autodie::hints">5.16.3</a> <a class="dropdown-item" href="/5.16.2/autodie::hints">5.16.2</a> <a class="dropdown-item" href="/5.16.1/autodie::hints">5.16.1</a> <a class="dropdown-item" href="/5.16.0/autodie::hints">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/autodie::hints">5.14.4</a> <a class="dropdown-item" href="/5.14.3/autodie::hints">5.14.3</a> <a class="dropdown-item" href="/5.14.2/autodie::hints">5.14.2</a> <a class="dropdown-item" href="/5.14.1/autodie::hints">5.14.1</a> <a class="dropdown-item" href="/5.14.0/autodie::hints">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/autodie::hints">5.12.5</a> <a class="dropdown-item" href="/5.12.4/autodie::hints">5.12.4</a> <a class="dropdown-item" href="/5.12.3/autodie::hints">5.12.3</a> <a class="dropdown-item" href="/5.12.2/autodie::hints">5.12.2</a> <a class="dropdown-item" href="/5.12.1/autodie::hints">5.12.1</a> <a class="dropdown-item" href="/5.12.0/autodie::hints">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/autodie::hints">5.10.1</a> <a class="dropdown-item" href="/5.10.0/autodie::hints">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/autodie::hints">5.8.9</a> <a class="dropdown-item" href="/5.8.8/autodie::hints">5.8.8</a> <a class="dropdown-item" href="/5.8.7/autodie::hints">5.8.7</a> <a class="dropdown-item" href="/5.8.6/autodie::hints">5.8.6</a> <a class="dropdown-item" href="/5.8.5/autodie::hints">5.8.5</a> <a class="dropdown-item" href="/5.8.4/autodie::hints">5.8.4</a> <a class="dropdown-item" href="/5.8.3/autodie::hints">5.8.3</a> <a class="dropdown-item" href="/5.8.2/autodie::hints">5.8.2</a> <a class="dropdown-item" href="/5.8.1/autodie::hints">5.8.1</a> <a class="dropdown-item" href="/5.8.0/autodie::hints">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/autodie::hints">5.6.2</a> <a class="dropdown-item" href="/5.6.1/autodie::hints">5.6.1</a> <a class="dropdown-item" href="/5.6.0/autodie::hints">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/autodie::hints">5.005_04</a> <a class="dropdown-item" href="/5.005_03/autodie::hints">5.005_03</a> <a class="dropdown-item" href="/5.005_02/autodie::hints">5.005_02</a> <a class="dropdown-item" href="/5.005_01/autodie::hints">5.005_01</a> <a class="dropdown-item" href="/5.005/autodie::hints">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/autodie::hints">blead</a> <a class="dropdown-item" href="/5.41.10/autodie::hints">5.41.10</a> <a class="dropdown-item active" href="/5.41.9/autodie::hints">5.41.9</a> <a class="dropdown-item" href="/5.41.8/autodie::hints">5.41.8</a> <a class="dropdown-item" href="/5.41.7/autodie::hints">5.41.7</a> <a class="dropdown-item" href="/5.41.6/autodie::hints">5.41.6</a> <a class="dropdown-item" href="/5.41.5/autodie::hints">5.41.5</a> <a class="dropdown-item" href="/5.41.4/autodie::hints">5.41.4</a> <a class="dropdown-item" href="/5.41.3/autodie::hints">5.41.3</a> <a class="dropdown-item" href="/5.41.2/autodie::hints">5.41.2</a> <a class="dropdown-item" href="/5.41.1/autodie::hints">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/autodie::hints">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/autodie::hints">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/autodie::hints">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/autodie::hints">5.39.10</a> <a class="dropdown-item" href="/5.39.9/autodie::hints">5.39.9</a> <a class="dropdown-item" href="/5.39.8/autodie::hints">5.39.8</a> <a class="dropdown-item" href="/5.39.7/autodie::hints">5.39.7</a> <a class="dropdown-item" href="/5.39.6/autodie::hints">5.39.6</a> <a class="dropdown-item" href="/5.39.5/autodie::hints">5.39.5</a> <a class="dropdown-item" href="/5.39.4/autodie::hints">5.39.4</a> <a class="dropdown-item" href="/5.39.3/autodie::hints">5.39.3</a> <a class="dropdown-item" href="/5.39.2/autodie::hints">5.39.2</a> <a class="dropdown-item" href="/5.39.1/autodie::hints">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/autodie::hints">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.41.9/perl">Perl</a> <a class="dropdown-item" href="/5.41.9/perlintro">Intro</a> <a class="dropdown-item" href="/5.41.9/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.41.9/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.41.9/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.41.9/perlop">Operators</a> <a class="dropdown-item" href="/5.41.9/functions">Functions</a> <a class="dropdown-item" href="/5.41.9/variables">Variables</a> <a class="dropdown-item" href="/5.41.9/modules">Modules</a> <a class="dropdown-item" href="/5.41.9/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.41.9/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.41.9/perlcommunity">Community</a> <a class="dropdown-item" href="/5.41.9/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.41.9/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.41.9/autodie::hints">autodie::hints</a> <div id="more"> (<a href="/5.41.9/autodie::hints.txt">source</a>, <a href="https://metacpan.org/pod/autodie::hints">CPAN</a>) </div> <div id="moduleversion">version 2.37</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.41.9. This is a development version of Perl. </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> <ul> <li> <a class="text-decoration-none" href="#Introduction">Introduction</a> </li> <li> <a class="text-decoration-none" href="#What-are-hints?">What are hints?</a> </li> <li> <a class="text-decoration-none" href="#Example-hints">Example hints</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#Manually-setting-hints-from-within-your-program">Manually setting hints from within your program</a> </li> <li> <a class="text-decoration-none" href="#Adding-hints-to-your-module">Adding hints to your module</a> </li> <li> <a class="text-decoration-none" href="#Insisting-on-hints">Insisting on hints</a> </li> <li> <a class="text-decoration-none" href="#Diagnostics">Diagnostics</a> </li> <li> <a class="text-decoration-none" href="#ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</a> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</a> </li> <li> <a class="text-decoration-none" href="#LICENSE">LICENSE</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>autodie::hints - Provide hints about user subroutines to autodie</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>package Your::Module; our %DOES = ( &#39;autodie::hints::provider&#39; =&gt; 1 ); sub AUTODIE_HINTS { return { foo =&gt; { scalar =&gt; HINTS, list =&gt; SOME_HINTS }, bar =&gt; { scalar =&gt; HINTS, list =&gt; MORE_HINTS }, } } # Later, in your main program... use Your::Module qw(foo bar); use autodie qw(:default foo bar); foo(); # succeeds or dies based on scalar hints # Alternatively, hints can be set on subroutines we&#39;ve # imported. use autodie::hints; use Some::Module qw(think_positive); BEGIN { autodie::hints-&gt;set_hints_for( \&amp;think_positive, { fail =&gt; sub { $_[0] &lt;= 0 } } ) } use autodie qw(think_positive); think_positive(...); # Returns positive or dies.</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <h2 id="Introduction"><a class="permalink" href="#Introduction">#</a>Introduction</h2> <p>The <a href="/5.41.9/autodie">autodie</a> pragma is very smart when it comes to working with Perl&#39;s built-in functions. The behaviour for these functions are fixed, and <code>autodie</code> knows exactly how they try to signal failure.</p> <p>But what about user-defined subroutines from modules? If you use <code>autodie</code> on a user-defined subroutine then it assumes the following behaviour to demonstrate failure:</p> <ul> <li><p>A false value, in scalar context</p> </li> <li><p>An empty list, in list context</p> </li> <li><p>A list containing a single undef, in list context</p> </li> </ul> <p>All other return values (including the list of the single zero, and the list containing a single empty string) are considered successful. However, real-world code isn&#39;t always that easy. Perhaps the code you&#39;re working with returns a string containing the word &quot;FAIL&quot; upon failure, or a two element list containing <code>(undef, &quot;human error message&quot;)</code>. To make autodie work with these sorts of subroutines, we have the <i>hinting interface</i>.</p> <p>The hinting interface allows <i>hints</i> to be provided to <code>autodie</code> on how it should detect failure from user-defined subroutines. While these <i>can</i> be provided by the end-user of <code>autodie</code>, they are ideally written into the module itself, or into a helper module or sub-class of <code>autodie</code> itself.</p> <h2 id="What-are-hints?"><a class="permalink" href="#What-are-hints?">#</a><a id="What"></a><a id="What-are-hints"></a>What are hints?</h2> <p>A <i>hint</i> is a subroutine or value that is checked against the return value of an autodying subroutine. If the match returns true, <code>autodie</code> considers the subroutine to have failed.</p> <p>If the hint provided is a subroutine, then <code>autodie</code> will pass the complete return value to that subroutine. If the hint is a regexp object, then <code>autodie</code> will match it against the return value. If the hint is undef, the return value must be undef. On Perl versions 5.10 and newer, any other value can be provided and it will be smart matched against the value provided. However, smart matched values like this are deprecated.</p> <p>Hints can be provided for both scalar and list contexts. Note that an autodying subroutine will never see a void context, as <code>autodie</code> always needs to capture the return value for examination. Autodying subroutines called in void context act as if they&#39;re called in a scalar context, but their return value is discarded after it has been checked.</p> <h2 id="Example-hints"><a class="permalink" href="#Example-hints">#</a><a id="Example"></a>Example hints</h2> <p>Hints may consist of subroutine references, objects overloading smart-match, regular expressions, and depending on Perl version possibly other things. You can specify different hints for how failure should be identified in scalar and list contexts.</p> <p>These examples apply for use in the <code>AUTODIE_HINTS</code> subroutine and when calling <code>autodie::hints-&gt;set_hints_for()</code>.</p> <p>The most common context-specific hints are:</p> <pre><code># Scalar failures always return undef: { scalar =&gt; sub { !defined($_[0]) } } # Scalar failures return any false value [default expectation]: { scalar =&gt; sub { ! $_[0] } } # Scalar failures always return zero explicitly: { scalar =&gt; sub { defined($_[0]) &amp;&amp; $_[0] eq &#39;0&#39; } } # List failures always return an empty list: { list =&gt; sub { !@_ } } # List failures return () or (undef) [default expectation]: { list =&gt; sub { ! @_ || @_ == 1 &amp;&amp; !defined $_[0] } } # List failures return () or a single false value: { list =&gt; sub { ! @_ || @_ == 1 &amp;&amp; !$_[0] } } # List failures return (undef, &quot;some string&quot;) { list =&gt; sub { @_ == 2 &amp;&amp; !defined $_[0] } } # Unsuccessful foo() returns &#39;FAIL&#39; or &#39;_FAIL&#39; in scalar context, # returns (-1) in list context... autodie::hints-&gt;set_hints_for( \&amp;foo, { scalar =&gt; qr/^ _? FAIL $/xms, list =&gt; sub { @_ == 1 &amp;&amp; $_[0] eq -1 }, } ); # Unsuccessful foo() returns 0 in all contexts... autodie::hints-&gt;set_hints_for( \&amp;foo, { scalar =&gt; sub { defined($_[0]) &amp;&amp; $_[0] == 0 }, list =&gt; sub { @_ == 1 &amp;&amp; defined($_[0]) &amp;&amp; $_[0] == 0 }, } );</code></pre> <p>This &quot;in all contexts&quot; construction is very common, and can be abbreviated, using the &#39;fail&#39; key. This sets both the <code>scalar</code> and <code>list</code> hints to the same value:</p> <pre><code> # Unsuccessful foo() returns 0 in all contexts... autodie::hints-&gt;set_hints_for( \&amp;foo, { fail =&gt; sub { @_ == 1 and defined $_[0] and $_[0] == 0 } } ); # Unsuccessful think_positive() returns negative number on failure... autodie::hints-&gt;set_hints_for( \&amp;think_positive, { fail =&gt; sub { $_[0] &lt; 0 } } ); # Unsuccessful my_system() returns non-zero on failure... autodie::hints-&gt;set_hints_for( \&amp;my_system, { fail =&gt; sub { $_[0] != 0 } } );</code></pre> <h1 id="Manually-setting-hints-from-within-your-program"><a class="permalink" href="#Manually-setting-hints-from-within-your-program">#</a><a id="Manually"></a>Manually setting hints from within your program</h1> <p>If you are using a module which returns something special on failure, then you can manually create hints for each of the desired subroutines. Once the hints are specified, they are available for all files and modules loaded thereafter, thus you can move this work into a module and it will still work.</p> <pre><code> use Some::Module qw(foo bar); use autodie::hints; autodie::hints-&gt;set_hints_for( \&amp;foo, { scalar =&gt; SCALAR_HINT, list =&gt; LIST_HINT, } ); autodie::hints-&gt;set_hints_for( \&amp;bar, { fail =&gt; SOME_HINT, } );</code></pre> <p>It is possible to pass either a subroutine reference (recommended) or a fully qualified subroutine name as the first argument. This means you can set hints on modules that <i>might</i> get loaded:</p> <pre><code>use autodie::hints; autodie::hints-&gt;set_hints_for( &#39;Some::Module:bar&#39;, { fail =&gt; SCALAR_HINT, } );</code></pre> <p>This technique is most useful when you have a project that uses a lot of third-party modules. You can define all your possible hints in one-place. This can even be in a sub-class of autodie. For example:</p> <pre><code>package my::autodie; use parent qw(autodie); use autodie::hints; autodie::hints-&gt;set_hints_for(...); 1;</code></pre> <p>You can now <code>use my::autodie</code>, which will work just like the standard <code>autodie</code>, but is now aware of any hints that you&#39;ve set.</p> <h1 id="Adding-hints-to-your-module"><a class="permalink" href="#Adding-hints-to-your-module">#</a><a id="Adding"></a>Adding hints to your module</h1> <p><code>autodie</code> provides a passive interface to allow you to declare hints for your module. These hints will be found and used by <code>autodie</code> if it is loaded, but otherwise have no effect (or dependencies) without autodie. To set these, your module needs to declare that it <i>does</i> the <code>autodie::hints::provider</code> role. This can be done by writing your own <code>DOES</code> method, using a system such as <code>Class::DOES</code> to handle the heavy-lifting for you, or declaring a <code>%DOES</code> package variable with a <code>autodie::hints::provider</code> key and a corresponding true value.</p> <p>Note that checking for a <code>%DOES</code> hash is an <code>autodie</code>-only short-cut. Other modules do not use this mechanism for checking roles, although you can use the <code>Class::DOES</code> module from the CPAN to allow it.</p> <p>In addition, you must define a <code>AUTODIE_HINTS</code> subroutine that returns a hash-reference containing the hints for your subroutines:</p> <pre><code> package Your::Module; # We can use the Class::DOES from the CPAN to declare adherence # to a role. use Class::DOES &#39;autodie::hints::provider&#39; =&gt; 1; # Alternatively, we can declare the role in %DOES. Note that # this is an autodie specific optimisation, although Class::DOES # can be used to promote this to a true role declaration. our %DOES = ( &#39;autodie::hints::provider&#39; =&gt; 1 ); # Finally, we must define the hints themselves. sub AUTODIE_HINTS { return { foo =&gt; { scalar =&gt; HINTS, list =&gt; SOME_HINTS }, bar =&gt; { scalar =&gt; HINTS, list =&gt; MORE_HINTS }, baz =&gt; { fail =&gt; HINTS }, } }</code></pre> <p>This allows your code to set hints without relying on <code>autodie</code> and <code>autodie::hints</code> being loaded, or even installed. In this way your code can do the right thing when <code>autodie</code> is installed, but does not need to depend upon it to function.</p> <h1 id="Insisting-on-hints"><a class="permalink" href="#Insisting-on-hints">#</a><a id="Insisting"></a>Insisting on hints</h1> <p>When a user-defined subroutine is wrapped by <code>autodie</code>, it will use hints if they are available, and otherwise reverts to the <i>default behaviour</i> described in the introduction of this document. This can be problematic if we expect a hint to exist, but (for whatever reason) it has not been loaded.</p> <p>We can ask autodie to <i>insist</i> that a hint be used by prefixing an exclamation mark to the start of the subroutine name. A lone exclamation mark indicates that <i>all</i> subroutines after it must have hints declared.</p> <pre><code> # foo() and bar() must have their hints defined use autodie qw( !foo !bar baz ); # Everything must have hints (recommended). use autodie qw( ! foo bar baz ); # bar() and baz() must have their hints defined use autodie qw( foo ! bar baz ); # Enable autodie for all of Perl&#39;s supported built-ins, # as well as for foo(), bar() and baz(). Everything must # have hints. use autodie qw( ! :all foo bar baz );</code></pre> <p>If hints are not available for the specified subroutines, this will cause a compile-time error. Insisting on hints for Perl&#39;s built-in functions (eg, <code>open</code> and <code>close</code>) is always successful.</p> <p>Insisting on hints is <i>strongly</i> recommended.</p> <h1 id="Diagnostics"><a class="permalink" href="#Diagnostics">#</a>Diagnostics</h1> <dl> <dt id="Attempts-to-set_hints_for-unidentifiable-subroutine"><a class="permalink" href="#Attempts-to-set_hints_for-unidentifiable-subroutine">#</a><a id="Attempts"></a>Attempts to set_hints_for unidentifiable subroutine</dt> <dd> <p>You&#39;ve called <code>autodie::hints-&gt;set_hints_for()</code> using a subroutine reference, but that reference could not be resolved back to a subroutine name. It may be an anonymous subroutine (which can&#39;t be made autodying), or may lack a name for other reasons.</p> <p>If you receive this error with a subroutine that has a real name, then you may have found a bug in autodie. See <a href="/5.41.9/autodie#BUGS">&quot;BUGS&quot; in autodie</a> for how to report this.</p> </dd> <dt id="fail-hints-cannot-be-provided-with-either-scalar-or-list-hints-for-%s"><a class="permalink" href="#fail-hints-cannot-be-provided-with-either-scalar-or-list-hints-for-%25s">#</a><a id="fail"></a><a id="fail-hints-cannot-be-provided-with-either-scalar-or-list-hints-for-s"></a>fail hints cannot be provided with either scalar or list hints for %s</dt> <dd> <p>When defining hints, you can either supply both <code>list</code> and <code>scalar</code> keywords, <i>or</i> you can provide a single <code>fail</code> keyword. You can&#39;t mix and match them.</p> </dd> <dt id="%s-hint-missing-for-%s"><a class="permalink" href="#%25s-hint-missing-for-%25s">#</a><a id="s-hint-missing-for-s"></a>%s hint missing for %s</dt> <dd> <p>You&#39;ve provided either a <code>scalar</code> hint without supplying a <code>list</code> hint, or vice-versa. You <i>must</i> supply both <code>scalar</code> and <code>list</code> hints, <i>or</i> a single <code>fail</code> hint.</p> </dd> </dl> <h1 id="ACKNOWLEDGEMENTS"><a class="permalink" href="#ACKNOWLEDGEMENTS">#</a>ACKNOWLEDGEMENTS</h1> <ul> <li><p>Dr Damian Conway for suggesting the hinting interface and providing the example usage.</p> </li> <li><p>Jacinta Richardson for translating much of my ideas into this documentation.</p> </li> </ul> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Copyright 2009, Paul Fenwick &lt;pjf@perltraining.com.au&gt;</p> <h1 id="LICENSE"><a class="permalink" href="#LICENSE">#</a>LICENSE</h1> <p>This module is free software. You may distribute it under the same terms as Perl itself.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.41.9/autodie">autodie</a>, <a href="/5.41.9/Class::DOES">Class::DOES</a></p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10