CINXE.COM

autodie - Replace functions with ones that succeed or die with lexical scope - 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 - Replace functions with ones that succeed or die with lexical scope - 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"> <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.40.1</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/autodie">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item active" href="/5.40.1/autodie">5.40.1</a> <a class="dropdown-item" href="/5.40.0/autodie">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/autodie">5.38.3</a> <a class="dropdown-item" href="/5.38.2/autodie">5.38.2</a> <a class="dropdown-item" href="/5.38.1/autodie">5.38.1</a> <a class="dropdown-item" href="/5.38.0/autodie">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/autodie">5.36.3</a> <a class="dropdown-item" href="/5.36.2/autodie">5.36.2</a> <a class="dropdown-item" href="/5.36.1/autodie">5.36.1</a> <a class="dropdown-item" href="/5.36.0/autodie">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/autodie">5.34.3</a> <a class="dropdown-item" href="/5.34.2/autodie">5.34.2</a> <a class="dropdown-item" href="/5.34.1/autodie">5.34.1</a> <a class="dropdown-item" href="/5.34.0/autodie">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/autodie">5.32.1</a> <a class="dropdown-item" href="/5.32.0/autodie">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/autodie">5.30.3</a> <a class="dropdown-item" href="/5.30.2/autodie">5.30.2</a> <a class="dropdown-item" href="/5.30.1/autodie">5.30.1</a> <a class="dropdown-item" href="/5.30.0/autodie">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/autodie">5.28.3</a> <a class="dropdown-item" href="/5.28.2/autodie">5.28.2</a> <a class="dropdown-item" href="/5.28.1/autodie">5.28.1</a> <a class="dropdown-item" href="/5.28.0/autodie">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/autodie">5.26.3</a> <a class="dropdown-item" href="/5.26.2/autodie">5.26.2</a> <a class="dropdown-item" href="/5.26.1/autodie">5.26.1</a> <a class="dropdown-item" href="/5.26.0/autodie">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/autodie">5.24.4</a> <a class="dropdown-item" href="/5.24.3/autodie">5.24.3</a> <a class="dropdown-item" href="/5.24.2/autodie">5.24.2</a> <a class="dropdown-item" href="/5.24.1/autodie">5.24.1</a> <a class="dropdown-item" href="/5.24.0/autodie">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/autodie">5.22.4</a> <a class="dropdown-item" href="/5.22.3/autodie">5.22.3</a> <a class="dropdown-item" href="/5.22.2/autodie">5.22.2</a> <a class="dropdown-item" href="/5.22.1/autodie">5.22.1</a> <a class="dropdown-item" href="/5.22.0/autodie">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/autodie">5.20.3</a> <a class="dropdown-item" href="/5.20.2/autodie">5.20.2</a> <a class="dropdown-item" href="/5.20.1/autodie">5.20.1</a> <a class="dropdown-item" href="/5.20.0/autodie">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/autodie">5.18.4</a> <a class="dropdown-item" href="/5.18.3/autodie">5.18.3</a> <a class="dropdown-item" href="/5.18.2/autodie">5.18.2</a> <a class="dropdown-item" href="/5.18.1/autodie">5.18.1</a> <a class="dropdown-item" href="/5.18.0/autodie">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/autodie">5.16.3</a> <a class="dropdown-item" href="/5.16.2/autodie">5.16.2</a> <a class="dropdown-item" href="/5.16.1/autodie">5.16.1</a> <a class="dropdown-item" href="/5.16.0/autodie">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/autodie">5.14.4</a> <a class="dropdown-item" href="/5.14.3/autodie">5.14.3</a> <a class="dropdown-item" href="/5.14.2/autodie">5.14.2</a> <a class="dropdown-item" href="/5.14.1/autodie">5.14.1</a> <a class="dropdown-item" href="/5.14.0/autodie">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/autodie">5.12.5</a> <a class="dropdown-item" href="/5.12.4/autodie">5.12.4</a> <a class="dropdown-item" href="/5.12.3/autodie">5.12.3</a> <a class="dropdown-item" href="/5.12.2/autodie">5.12.2</a> <a class="dropdown-item" href="/5.12.1/autodie">5.12.1</a> <a class="dropdown-item" href="/5.12.0/autodie">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/autodie">5.10.1</a> <a class="dropdown-item" href="/5.10.0/autodie">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/autodie">5.8.9</a> <a class="dropdown-item" href="/5.8.8/autodie">5.8.8</a> <a class="dropdown-item" href="/5.8.7/autodie">5.8.7</a> <a class="dropdown-item" href="/5.8.6/autodie">5.8.6</a> <a class="dropdown-item" href="/5.8.5/autodie">5.8.5</a> <a class="dropdown-item" href="/5.8.4/autodie">5.8.4</a> <a class="dropdown-item" href="/5.8.3/autodie">5.8.3</a> <a class="dropdown-item" href="/5.8.2/autodie">5.8.2</a> <a class="dropdown-item" href="/5.8.1/autodie">5.8.1</a> <a class="dropdown-item" href="/5.8.0/autodie">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/autodie">5.6.2</a> <a class="dropdown-item" href="/5.6.1/autodie">5.6.1</a> <a class="dropdown-item" href="/5.6.0/autodie">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/autodie">5.005_04</a> <a class="dropdown-item" href="/5.005_03/autodie">5.005_03</a> <a class="dropdown-item" href="/5.005_02/autodie">5.005_02</a> <a class="dropdown-item" href="/5.005_01/autodie">5.005_01</a> <a class="dropdown-item" href="/5.005/autodie">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">blead</a> <a class="dropdown-item" href="/5.41.10/autodie">5.41.10</a> <a class="dropdown-item" href="/5.41.9/autodie">5.41.9</a> <a class="dropdown-item" href="/5.41.8/autodie">5.41.8</a> <a class="dropdown-item" href="/5.41.7/autodie">5.41.7</a> <a class="dropdown-item" href="/5.41.6/autodie">5.41.6</a> <a class="dropdown-item" href="/5.41.5/autodie">5.41.5</a> <a class="dropdown-item" href="/5.41.4/autodie">5.41.4</a> <a class="dropdown-item" href="/5.41.3/autodie">5.41.3</a> <a class="dropdown-item" href="/5.41.2/autodie">5.41.2</a> <a class="dropdown-item" href="/5.41.1/autodie">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/autodie">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/autodie">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/autodie">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/autodie">5.39.10</a> <a class="dropdown-item" href="/5.39.9/autodie">5.39.9</a> <a class="dropdown-item" href="/5.39.8/autodie">5.39.8</a> <a class="dropdown-item" href="/5.39.7/autodie">5.39.7</a> <a class="dropdown-item" href="/5.39.6/autodie">5.39.6</a> <a class="dropdown-item" href="/5.39.5/autodie">5.39.5</a> <a class="dropdown-item" href="/5.39.4/autodie">5.39.4</a> <a class="dropdown-item" href="/5.39.3/autodie">5.39.3</a> <a class="dropdown-item" href="/5.39.2/autodie">5.39.2</a> <a class="dropdown-item" href="/5.39.1/autodie">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/autodie">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="/perl">Perl</a> <a class="dropdown-item" href="/perlintro">Intro</a> <a class="dropdown-item" href="/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/perlfaq">FAQs</a> <a class="dropdown-item" href="/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/perlop">Operators</a> <a class="dropdown-item" href="/functions">Functions</a> <a class="dropdown-item" href="/variables">Variables</a> <a class="dropdown-item" href="/modules">Modules</a> <a class="dropdown-item" href="/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/perldelta">Release Notes</a> <a class="dropdown-item" href="/perlcommunity">Community</a> <a class="dropdown-item" href="/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="/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="/autodie">autodie</a> <div id="more"> (<a href="/autodie.txt">source</a>, <a href="https://metacpan.org/pod/autodie">CPAN</a>) </div> <div id="moduleversion">version 2.37</div> </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> </li> <li> <a class="text-decoration-none" href="#EXCEPTIONS">EXCEPTIONS</a> </li> <li> <a class="text-decoration-none" href="#CATEGORIES">CATEGORIES</a> </li> <li> <a class="text-decoration-none" href="#FUNCTION-SPECIFIC-NOTES">FUNCTION SPECIFIC NOTES</a> <ul> <li> <a class="text-decoration-none" href="#print">print</a> </li> <li> <a class="text-decoration-none" href="#flock">flock</a> </li> <li> <a class="text-decoration-none" href="#system/exec">system/exec</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#GOTCHAS">GOTCHAS</a> </li> <li> <a class="text-decoration-none" href="#DIAGNOSTICS">DIAGNOSTICS</a> </li> <li> <a class="text-decoration-none" href="#Tips-and-Tricks">Tips and Tricks</a> <ul> <li> <a class="text-decoration-none" href="#Importing-autodie-into-another-namespace-than-%22caller%22">Importing autodie into another namespace than &quot;caller&quot;</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#BUGS">BUGS</a> <ul> <li> <a class="text-decoration-none" href="#autodie-and-string-eval">autodie and string eval</a> </li> <li> <a class="text-decoration-none" href="#REPORTING-BUGS">REPORTING BUGS</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#FEEDBACK">FEEDBACK</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> <li> <a class="text-decoration-none" href="#ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>autodie - Replace functions with ones that succeed or die with lexical scope</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use autodie; # Recommended: implies &#39;use autodie qw(:default)&#39; use autodie qw(:all); # Recommended more: defaults and system/exec. use autodie qw(open close); # open/close succeed or die open(my $fh, &quot;&lt;&quot;, $filename); # No need to check! { no autodie qw(open); # open failures won&#39;t die open(my $fh, &quot;&lt;&quot;, $filename); # Could fail silently! no autodie; # disable all autodies } print &quot;Hello World&quot; or die $!; # autodie DOESN&#39;T check print!</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <pre><code class="plaintext">bIlujDI&#39; yIchegh()Qo&#39;; yIHegh()! It is better to die() than to return() in failure. -- Klingon programming proverb.</code></pre> <p>The <code>autodie</code> pragma provides a convenient way to replace functions that normally return false on failure with equivalents that throw an exception on failure.</p> <p>The <code>autodie</code> pragma has <i>lexical scope</i>, meaning that functions and subroutines altered with <code>autodie</code> will only change their behaviour until the end of the enclosing block, file, or <code>eval</code>.</p> <p>If <code>system</code> is specified as an argument to <code>autodie</code>, then it uses <a href="/IPC::System::Simple">IPC::System::Simple</a> to do the heavy lifting. See the description of that module for more information.</p> <h1 id="EXCEPTIONS"><a class="permalink" href="#EXCEPTIONS">#</a>EXCEPTIONS</h1> <p>Exceptions produced by the <code>autodie</code> pragma are members of the <a href="/autodie::exception">autodie::exception</a> class. The preferred way to work with these exceptions under Perl 5.10 is as follows:</p> <pre><code>eval { use autodie; open(my $fh, &#39;&lt;&#39;, $some_file); my @records = &lt;$fh&gt;; # Do things with @records... close($fh); }; if ($@ and $@-&gt;isa(&#39;autodie::exception&#39;)) { if ($@-&gt;matches(&#39;open&#39;)) { print &quot;Error from open\n&quot;; } if ($@-&gt;matches(&#39;:io&#39; )) { print &quot;Non-open, IO error.&quot;; } } elsif ($@) { # A non-autodie exception. }</code></pre> <p>See <a href="/autodie::exception">autodie::exception</a> for further information on interrogating exceptions.</p> <h1 id="CATEGORIES"><a class="permalink" href="#CATEGORIES">#</a>CATEGORIES</h1> <p>Autodie uses a simple set of categories to group together similar built-ins. Requesting a category type (starting with a colon) will enable autodie for all built-ins beneath that category. For example, requesting <code>:file</code> will enable autodie for <code>close</code>, <code>fcntl</code>, <code>open</code> and <code>sysopen</code>.</p> <p>The categories are currently:</p> <pre><code class="plaintext">:all :default :io read seek sysread sysseek syswrite :dbm dbmclose dbmopen :file binmode close chmod chown fcntl flock ioctl open sysopen truncate :filesys chdir closedir opendir link mkdir readlink rename rmdir symlink unlink :ipc kill pipe :msg msgctl msgget msgrcv msgsnd :semaphore semctl semget semop :shm shmctl shmget shmread :socket accept bind connect getsockopt listen recv send setsockopt shutdown socketpair :threads fork :system system exec</code></pre> <p>Note that while the above category system is presently a strict hierarchy, this should not be assumed.</p> <p>A plain <code>use autodie</code> implies <code>use autodie qw(:default)</code>. Note that <code>system</code> and <code>exec</code> are not enabled by default. <code>system</code> requires the optional <a href="/IPC::System::Simple">IPC::System::Simple</a> module to be installed, and enabling <code>system</code> or <code>exec</code> will invalidate their exotic forms. See <a href="#BUGS">&quot;BUGS&quot;</a> below for more details.</p> <p>The syntax:</p> <pre><code>use autodie qw(:1.994);</code></pre> <p>allows the <code>:default</code> list from a particular version to be used. This provides the convenience of using the default methods, but the surety that no behavioral changes will occur if the <code>autodie</code> module is upgraded.</p> <p><code>autodie</code> can be enabled for all of Perl&#39;s built-ins, including <code>system</code> and <code>exec</code> with:</p> <pre><code>use autodie qw(:all);</code></pre> <h1 id="FUNCTION-SPECIFIC-NOTES"><a class="permalink" href="#FUNCTION-SPECIFIC-NOTES">#</a><a id="FUNCTION"></a>FUNCTION SPECIFIC NOTES</h1> <h2 id="print"><a class="permalink" href="#print">#</a>print</h2> <p>The autodie pragma <b>does not check calls to <code>print</code></b>.</p> <h2 id="flock"><a class="permalink" href="#flock">#</a>flock</h2> <p>It is not considered an error for <code>flock</code> to return false if it fails due to an <code>EWOULDBLOCK</code> (or equivalent) condition. This means one can still use the common convention of testing the return value of <code>flock</code> when called with the <code>LOCK_NB</code> option:</p> <pre><code>use autodie; if ( flock($fh, LOCK_EX | LOCK_NB) ) { # We have a lock }</code></pre> <p>Autodying <code>flock</code> will generate an exception if <code>flock</code> returns false with any other error.</p> <h2 id="system/exec"><a class="permalink" href="#system/exec">#</a><a id="system"></a><a id="system-exec"></a>system/exec</h2> <p>The <code>system</code> built-in is considered to have failed in the following circumstances:</p> <ul> <li><p>The command does not start.</p> </li> <li><p>The command is killed by a signal.</p> </li> <li><p>The command returns a non-zero exit value (but see below).</p> </li> </ul> <p>On success, the autodying form of <code>system</code> returns the <i>exit value</i> rather than the contents of <code>$?</code>.</p> <p>Additional allowable exit values can be supplied as an optional first argument to autodying <code>system</code>:</p> <pre><code>system( [ 0, 1, 2 ], $cmd, @args); # 0,1,2 are good exit values</code></pre> <p><code>autodie</code> uses the <a href="/IPC::System::Simple">IPC::System::Simple</a> module to change <code>system</code>. See its documentation for further information.</p> <p>Applying <code>autodie</code> to <code>system</code> or <code>exec</code> causes the exotic forms <code>system { $cmd } @args </code> or <code>exec { $cmd } @args</code> to be considered a syntax error until the end of the lexical scope. If you really need to use the exotic form, you can call <code>CORE::system</code> or <code>CORE::exec</code> instead, or use <code>no autodie qw(system exec)</code> before calling the exotic form.</p> <h1 id="GOTCHAS"><a class="permalink" href="#GOTCHAS">#</a>GOTCHAS</h1> <p>Functions called in list context are assumed to have failed if they return an empty list, or a list consisting only of a single undef element.</p> <p>Some builtins (e.g. <code>chdir</code> or <code>truncate</code>) has a call signature that cannot completely be represented with a Perl prototype. This means that some valid Perl code will be invalid under autodie. As an example:</p> <pre><code>chdir(BAREWORD);</code></pre> <p>Without autodie (and assuming BAREWORD is an open filehandle/dirhandle) this is a valid call to chdir. But under autodie, <code>chdir</code> will behave like it had the prototype &quot;;$&quot; and thus BAREWORD will be a syntax error (under &quot;use strict&quot;. Without strict, it will interpreted as a filename).</p> <h1 id="DIAGNOSTICS"><a class="permalink" href="#DIAGNOSTICS">#</a>DIAGNOSTICS</h1> <dl> <dt id=":void-cannot-be-used-with-lexical-scope"><a class="permalink" href="#:void-cannot-be-used-with-lexical-scope">#</a><a id="void-cannot-be-used-with-lexical-scope"></a>:void cannot be used with lexical scope</dt> <dd> <p>The <code>:void</code> option is supported in <a href="/Fatal">Fatal</a>, but not <code>autodie</code>. To workaround this, <code>autodie</code> may be explicitly disabled until the end of the current block with <code>no autodie</code>. To disable autodie for only a single function (eg, open) use <code>no autodie qw(open)</code>.</p> <p><code>autodie</code> performs no checking of called context to determine whether to throw an exception; the explicitness of error handling with <code>autodie</code> is a deliberate feature.</p> </dd> <dt id="No-user-hints-defined-for-%s"><a class="permalink" href="#No-user-hints-defined-for-%25s">#</a><a id="No"></a><a id="No-user-hints-defined-for-s"></a>No user hints defined for %s</dt> <dd> <p>You&#39;ve insisted on hints for user-subroutines, either by pre-pending a <code>!</code> to the subroutine name itself, or earlier in the list of arguments to <code>autodie</code>. However the subroutine in question does not have any hints available.</p> </dd> </dl> <p>See also <a href="/Fatal#DIAGNOSTICS">&quot;DIAGNOSTICS&quot; in Fatal</a>.</p> <h1 id="Tips-and-Tricks"><a class="permalink" href="#Tips-and-Tricks">#</a><a id="Tips"></a>Tips and Tricks</h1> <h2 id="Importing-autodie-into-another-namespace-than-&quot;caller&quot;"><a class="permalink" href="#Importing-autodie-into-another-namespace-than-%22caller%22">#</a><a id="Importing"></a><a id="Importing-autodie-into-another-namespace-than-caller"></a>Importing autodie into another namespace than &quot;caller&quot;</h2> <p>It is possible to import autodie into a different namespace by using <a href="/Import::Into">Import::Into</a>. However, you have to pass a &quot;caller depth&quot; (rather than a package name) for this to work correctly.</p> <h1 id="BUGS"><a class="permalink" href="#BUGS">#</a>BUGS</h1> <p>&quot;Used only once&quot; warnings can be generated when <code>autodie</code> or <code>Fatal</code> is used with package filehandles (eg, <code>FILE</code>). Scalar filehandles are strongly recommended instead.</p> <p>When using <code>autodie</code> or <code>Fatal</code> with user subroutines, the declaration of those subroutines must appear before the first use of <code>Fatal</code> or <code>autodie</code>, or have been exported from a module. Attempting to use <code>Fatal</code> or <code>autodie</code> on other user subroutines will result in a compile-time error.</p> <p>Due to a bug in Perl, <code>autodie</code> may &quot;lose&quot; any format which has the same name as an autodying built-in or function.</p> <p><code>autodie</code> may not work correctly if used inside a file with a name that looks like a string eval, such as <i>eval (3)</i>.</p> <h2 id="autodie-and-string-eval"><a class="permalink" href="#autodie-and-string-eval">#</a><a id="autodie"></a>autodie and string eval</h2> <p>Due to the current implementation of <code>autodie</code>, unexpected results may be seen when used near or with the string version of eval. <i>None of these bugs exist when using block eval</i>.</p> <p>Under Perl 5.8 only, <code>autodie</code> <i>does not</i> propagate into string <code>eval</code> statements, although it can be explicitly enabled inside a string <code>eval</code>.</p> <p>Under Perl 5.10 only, using a string eval when <code>autodie</code> is in effect can cause the autodie behaviour to leak into the surrounding scope. This can be worked around by using a <code>no autodie</code> at the end of the scope to explicitly remove autodie&#39;s effects, or by avoiding the use of string eval.</p> <p><i>None of these bugs exist when using block eval</i>. The use of <code>autodie</code> with block eval is considered good practice.</p> <h2 id="REPORTING-BUGS"><a class="permalink" href="#REPORTING-BUGS">#</a><a id="REPORTING"></a>REPORTING BUGS</h2> <p>Please report bugs via the GitHub Issue Tracker at <a href="https://github.com/pjf/autodie/issues">https://github.com/pjf/autodie/issues</a>.</p> <h1 id="FEEDBACK"><a class="permalink" href="#FEEDBACK">#</a>FEEDBACK</h1> <p>If you find this module useful, please consider rating it on metacpan at <a href="https://metacpan.org/pod/autodie">https://metacpan.org/pod/autodie</a> .</p> <p>The module author loves to hear how <code>autodie</code> has made your life better (or worse). Feedback can be sent to &lt;pjf@perltraining.com.au&gt;.</p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Copyright 2008-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="/Fatal">Fatal</a>, <a href="/autodie::exception">autodie::exception</a>, <a href="/autodie::hints">autodie::hints</a>, <a href="/IPC::System::Simple">IPC::System::Simple</a></p> <p><i>Perl tips, autodie</i> at <a href="http://perltraining.com.au/tips/2008-08-20.html">http://perltraining.com.au/tips/2008-08-20.html</a></p> <h1 id="ACKNOWLEDGEMENTS"><a class="permalink" href="#ACKNOWLEDGEMENTS">#</a>ACKNOWLEDGEMENTS</h1> <p>Mark Reed and Roland Giersig -- Klingon translators.</p> <p>See the <i>AUTHORS</i> file for full credits. The latest version of this file can be found at <a href="https://github.com/pjf/autodie/tree/master/AUTHORS">https://github.com/pjf/autodie/tree/master/AUTHORS</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