CINXE.COM
IO::Handle - supply object methods for I/O handles - 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>IO::Handle - supply object methods for I/O handles - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/IO::Handle"> <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="/IO::Handle">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item active" href="/5.40.1/IO::Handle">5.40.1</a> <a class="dropdown-item" href="/5.40.0/IO::Handle">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/IO::Handle">5.38.3</a> <a class="dropdown-item" href="/5.38.2/IO::Handle">5.38.2</a> <a class="dropdown-item" href="/5.38.1/IO::Handle">5.38.1</a> <a class="dropdown-item" href="/5.38.0/IO::Handle">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/IO::Handle">5.36.3</a> <a class="dropdown-item" href="/5.36.2/IO::Handle">5.36.2</a> <a class="dropdown-item" href="/5.36.1/IO::Handle">5.36.1</a> <a class="dropdown-item" href="/5.36.0/IO::Handle">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/IO::Handle">5.34.3</a> <a class="dropdown-item" href="/5.34.2/IO::Handle">5.34.2</a> <a class="dropdown-item" href="/5.34.1/IO::Handle">5.34.1</a> <a class="dropdown-item" href="/5.34.0/IO::Handle">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/IO::Handle">5.32.1</a> <a class="dropdown-item" href="/5.32.0/IO::Handle">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/IO::Handle">5.30.3</a> <a class="dropdown-item" href="/5.30.2/IO::Handle">5.30.2</a> <a class="dropdown-item" href="/5.30.1/IO::Handle">5.30.1</a> <a class="dropdown-item" href="/5.30.0/IO::Handle">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/IO::Handle">5.28.3</a> <a class="dropdown-item" href="/5.28.2/IO::Handle">5.28.2</a> <a class="dropdown-item" href="/5.28.1/IO::Handle">5.28.1</a> <a class="dropdown-item" href="/5.28.0/IO::Handle">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/IO::Handle">5.26.3</a> <a class="dropdown-item" href="/5.26.2/IO::Handle">5.26.2</a> <a class="dropdown-item" href="/5.26.1/IO::Handle">5.26.1</a> <a class="dropdown-item" href="/5.26.0/IO::Handle">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/IO::Handle">5.24.4</a> <a class="dropdown-item" href="/5.24.3/IO::Handle">5.24.3</a> <a class="dropdown-item" href="/5.24.2/IO::Handle">5.24.2</a> <a class="dropdown-item" href="/5.24.1/IO::Handle">5.24.1</a> <a class="dropdown-item" href="/5.24.0/IO::Handle">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/IO::Handle">5.22.4</a> <a class="dropdown-item" href="/5.22.3/IO::Handle">5.22.3</a> <a class="dropdown-item" href="/5.22.2/IO::Handle">5.22.2</a> <a class="dropdown-item" href="/5.22.1/IO::Handle">5.22.1</a> <a class="dropdown-item" href="/5.22.0/IO::Handle">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/IO::Handle">5.20.3</a> <a class="dropdown-item" href="/5.20.2/IO::Handle">5.20.2</a> <a class="dropdown-item" href="/5.20.1/IO::Handle">5.20.1</a> <a class="dropdown-item" href="/5.20.0/IO::Handle">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/IO::Handle">5.18.4</a> <a class="dropdown-item" href="/5.18.3/IO::Handle">5.18.3</a> <a class="dropdown-item" href="/5.18.2/IO::Handle">5.18.2</a> <a class="dropdown-item" href="/5.18.1/IO::Handle">5.18.1</a> <a class="dropdown-item" href="/5.18.0/IO::Handle">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/IO::Handle">5.16.3</a> <a class="dropdown-item" href="/5.16.2/IO::Handle">5.16.2</a> <a class="dropdown-item" href="/5.16.1/IO::Handle">5.16.1</a> <a class="dropdown-item" href="/5.16.0/IO::Handle">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/IO::Handle">5.14.4</a> <a class="dropdown-item" href="/5.14.3/IO::Handle">5.14.3</a> <a class="dropdown-item" href="/5.14.2/IO::Handle">5.14.2</a> <a class="dropdown-item" href="/5.14.1/IO::Handle">5.14.1</a> <a class="dropdown-item" href="/5.14.0/IO::Handle">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/IO::Handle">5.12.5</a> <a class="dropdown-item" href="/5.12.4/IO::Handle">5.12.4</a> <a class="dropdown-item" href="/5.12.3/IO::Handle">5.12.3</a> <a class="dropdown-item" href="/5.12.2/IO::Handle">5.12.2</a> <a class="dropdown-item" href="/5.12.1/IO::Handle">5.12.1</a> <a class="dropdown-item" href="/5.12.0/IO::Handle">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/IO::Handle">5.10.1</a> <a class="dropdown-item" href="/5.10.0/IO::Handle">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/IO::Handle">5.8.9</a> <a class="dropdown-item" href="/5.8.8/IO::Handle">5.8.8</a> <a class="dropdown-item" href="/5.8.7/IO::Handle">5.8.7</a> <a class="dropdown-item" href="/5.8.6/IO::Handle">5.8.6</a> <a class="dropdown-item" href="/5.8.5/IO::Handle">5.8.5</a> <a class="dropdown-item" href="/5.8.4/IO::Handle">5.8.4</a> <a class="dropdown-item" href="/5.8.3/IO::Handle">5.8.3</a> <a class="dropdown-item" href="/5.8.2/IO::Handle">5.8.2</a> <a class="dropdown-item" href="/5.8.1/IO::Handle">5.8.1</a> <a class="dropdown-item" href="/5.8.0/IO::Handle">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/IO::Handle">5.6.2</a> <a class="dropdown-item" href="/5.6.1/IO::Handle">5.6.1</a> <a class="dropdown-item" href="/5.6.0/IO::Handle">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/IO::Handle">5.005_04</a> <a class="dropdown-item" href="/5.005_03/IO::Handle">5.005_03</a> <a class="dropdown-item" href="/5.005_02/IO::Handle">5.005_02</a> <a class="dropdown-item" href="/5.005_01/IO::Handle">5.005_01</a> <a class="dropdown-item" href="/5.005/IO::Handle">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/IO::Handle">blead</a> <a class="dropdown-item" href="/5.41.10/IO::Handle">5.41.10</a> <a class="dropdown-item" href="/5.41.9/IO::Handle">5.41.9</a> <a class="dropdown-item" href="/5.41.8/IO::Handle">5.41.8</a> <a class="dropdown-item" href="/5.41.7/IO::Handle">5.41.7</a> <a class="dropdown-item" href="/5.41.6/IO::Handle">5.41.6</a> <a class="dropdown-item" href="/5.41.5/IO::Handle">5.41.5</a> <a class="dropdown-item" href="/5.41.4/IO::Handle">5.41.4</a> <a class="dropdown-item" href="/5.41.3/IO::Handle">5.41.3</a> <a class="dropdown-item" href="/5.41.2/IO::Handle">5.41.2</a> <a class="dropdown-item" href="/5.41.1/IO::Handle">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/IO::Handle">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/IO::Handle">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/IO::Handle">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/IO::Handle">5.39.10</a> <a class="dropdown-item" href="/5.39.9/IO::Handle">5.39.9</a> <a class="dropdown-item" href="/5.39.8/IO::Handle">5.39.8</a> <a class="dropdown-item" href="/5.39.7/IO::Handle">5.39.7</a> <a class="dropdown-item" href="/5.39.6/IO::Handle">5.39.6</a> <a class="dropdown-item" href="/5.39.5/IO::Handle">5.39.5</a> <a class="dropdown-item" href="/5.39.4/IO::Handle">5.39.4</a> <a class="dropdown-item" href="/5.39.3/IO::Handle">5.39.3</a> <a class="dropdown-item" href="/5.39.2/IO::Handle">5.39.2</a> <a class="dropdown-item" href="/5.39.1/IO::Handle">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/IO::Handle">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="/IO::Handle">IO::Handle</a> <div id="more"> (<a href="/IO::Handle.txt">source</a>, <a href="https://metacpan.org/pod/IO::Handle">CPAN</a>) </div> <div id="moduleversion">version 1.55</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="#CONSTRUCTOR">CONSTRUCTOR</a> </li> <li> <a class="text-decoration-none" href="#METHODS">METHODS</a> </li> <li> <a class="text-decoration-none" href="#NOTE">NOTE</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> <li> <a class="text-decoration-none" href="#BUGS">BUGS</a> </li> <li> <a class="text-decoration-none" href="#HISTORY">HISTORY</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>IO::Handle - supply object methods for I/O handles</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use IO::Handle; my $io = IO::Handle->new(); if ($io->fdopen(fileno(STDIN),"r")) { print $io->getline; $io->close; } my $io = IO::Handle->new(); if ($io->fdopen(fileno(STDOUT),"w")) { $io->print("Some text\n"); } # setvbuf is not available by default on Perls 5.8.0 and later. use IO::Handle '_IOLBF'; $io->setvbuf(my $buffer_var, _IOLBF, 1024); undef $io; # automatically closes the file if it's open autoflush STDOUT 1;</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p><code>IO::Handle</code> is the base class for all other IO handle classes. It is not intended that objects of <code>IO::Handle</code> would be created directly, but instead <code>IO::Handle</code> is inherited from by several other classes in the IO hierarchy.</p> <p>If you are reading this documentation, looking for a replacement for the <code>FileHandle</code> package, then I suggest you read the documentation for <code>IO::File</code> too.</p> <h1 id="CONSTRUCTOR"><a class="permalink" href="#CONSTRUCTOR">#</a>CONSTRUCTOR</h1> <dl> <dt id="new-()"><a class="permalink" href="#new-()">#</a><a id="new"></a>new ()</dt> <dd> <p>Creates a new <code>IO::Handle</code> object.</p> </dd> <dt id="new_from_fd-(-FD,-MODE-)"><a class="permalink" href="#new_from_fd-(-FD,-MODE-)">#</a><a id="new_from_fd"></a><a id="new_from_fd-FD-MODE"></a>new_from_fd ( FD, MODE )</dt> <dd> <p>Creates an <code>IO::Handle</code> like <code>new</code> does. It requires two parameters, which are passed to the method <code>fdopen</code>; if the fdopen fails, the object is destroyed. Otherwise, it is returned to the caller.</p> </dd> </dl> <h1 id="METHODS"><a class="permalink" href="#METHODS">#</a>METHODS</h1> <p>See <a href="/perlfunc">perlfunc</a> for complete descriptions of each of the following supported <code>IO::Handle</code> methods, which are just front ends for the corresponding built-in functions:</p> <pre><code>$io->close $io->eof $io->fcntl( FUNCTION, SCALAR ) $io->fileno $io->format_write( [FORMAT_NAME] ) $io->getc $io->ioctl( FUNCTION, SCALAR ) $io->read ( BUF, LEN, [OFFSET] ) $io->print ( ARGS ) $io->printf ( FMT, [ARGS] ) $io->say ( ARGS ) $io->stat $io->sysread ( BUF, LEN, [OFFSET] ) $io->syswrite ( BUF, [LEN, [OFFSET]] ) $io->truncate ( LEN )</code></pre> <p>See <a href="/perlvar">perlvar</a> for complete descriptions of each of the following supported <code>IO::Handle</code> methods. All of them return the previous value of the attribute and takes an optional single argument that when given will set the value. If no argument is given the previous value is unchanged (except for $io->autoflush will actually turn ON autoflush by default).</p> <pre><code>$io->autoflush ( [BOOL] ) $| $io->format_page_number( [NUM] ) $% $io->format_lines_per_page( [NUM] ) $= $io->format_lines_left( [NUM] ) $- $io->format_name( [STR] ) $~ $io->format_top_name( [STR] ) $^ $io->input_line_number( [NUM]) $.</code></pre> <p>The following methods are not supported on a per-filehandle basis.</p> <pre><code>IO::Handle->format_line_break_characters( [STR] ) $: IO::Handle->format_formfeed( [STR]) $^L IO::Handle->output_field_separator( [STR] ) $, IO::Handle->output_record_separator( [STR] ) $\ IO::Handle->input_record_separator( [STR] ) $/</code></pre> <p>Furthermore, for doing normal I/O you might need these:</p> <dl> <dt id="$io->fdopen-(-FD,-MODE-)"><a class="permalink" href="#$io-%3Efdopen-(-FD,-MODE-)">#</a><a id="fdopen"></a><a id="io-fdopen-FD-MODE"></a>$io->fdopen ( FD, MODE )</dt> <dd> <p><code>fdopen</code> is like an ordinary <code>open</code> except that its first parameter is not a filename but rather a file handle name, an IO::Handle object, or a file descriptor number. (For the documentation of the <code>open</code> method, see <a href="/IO::File">IO::File</a>.)</p> </dd> <dt id="$io->opened"><a class="permalink" href="#$io-%3Eopened">#</a><a id="opened"></a><a id="io-opened"></a>$io->opened</dt> <dd> <p>Returns true if the object is currently a valid file descriptor, false otherwise.</p> </dd> <dt id="$io->getline"><a class="permalink" href="#$io-%3Egetline">#</a><a id="getline"></a><a id="io-getline"></a>$io->getline</dt> <dd> <p>This works like <$io> described in <a href="/perlop#I%2FO-Operators">"I/O Operators" in perlop</a> except that it's more readable and can be safely called in a list context but still returns just one line. If used as the conditional within a <code>while</code> or C-style <code>for</code> loop, however, you will need to emulate the functionality of <$io> with <code>defined($_ = $io->getline)</code>.</p> </dd> <dt id="$io->getlines"><a class="permalink" href="#$io-%3Egetlines">#</a><a id="getlines"></a><a id="io-getlines"></a>$io->getlines</dt> <dd> <p>This works like <$io> when called in a list context to read all the remaining lines in a file, except that it's more readable. It will also croak() if accidentally called in a scalar context.</p> </dd> <dt id="$io->ungetc-(-ORD-)"><a class="permalink" href="#$io-%3Eungetc-(-ORD-)">#</a><a id="ungetc"></a><a id="io-ungetc-ORD"></a>$io->ungetc ( ORD )</dt> <dd> <p>Pushes a character with the given ordinal value back onto the given handle's input stream. Only one character of pushback per handle is guaranteed.</p> </dd> <dt id="$io->write-(-BUF,-LEN-[,-OFFSET-]-)"><a class="permalink" href="#$io-%3Ewrite-(-BUF,-LEN-%5B,-OFFSET-%5D-)">#</a><a id="write"></a><a id="io-write-BUF-LEN-OFFSET"></a>$io->write ( BUF, LEN [, OFFSET ] )</dt> <dd> <p>This <code>write</code> is somewhat like <code>write</code> found in C, in that it is the opposite of read. The wrapper for the perl <code>write</code> function is called <code>format_write</code>. However, whilst the C <code>write</code> function returns the number of bytes written, this <code>write</code> function simply returns true if successful (like <code>print</code>). A more C-like <code>write</code> is <code>syswrite</code> (see above).</p> </dd> <dt id="$io->error"><a class="permalink" href="#$io-%3Eerror">#</a><a id="error"></a><a id="io-error"></a>$io->error</dt> <dd> <p>Returns a true value if the given handle has experienced any errors since it was opened or since the last call to <code>clearerr</code>, or if the handle is invalid. It only returns false for a valid handle with no outstanding errors.</p> </dd> <dt id="$io->clearerr"><a class="permalink" href="#$io-%3Eclearerr">#</a><a id="clearerr"></a><a id="io-clearerr"></a>$io->clearerr</dt> <dd> <p>Clear the given handle's error indicator. Returns -1 if the handle is invalid, 0 otherwise.</p> </dd> <dt id="$io->sync"><a class="permalink" href="#$io-%3Esync">#</a><a id="sync"></a><a id="io-sync"></a>$io->sync</dt> <dd> <p><code>sync</code> synchronizes a file's in-memory state with that on the physical medium. <code>sync</code> does not operate at the perlio api level, but operates on the file descriptor (similar to sysread, sysseek and systell). This means that any data held at the perlio api level will not be synchronized. To synchronize data that is buffered at the perlio api level you must use the flush method. <code>sync</code> is not implemented on all platforms. Returns "0 but true" on success, <code>undef</code> on error, <code>undef</code> for an invalid handle. See <a>fsync(3c)</a>.</p> </dd> <dt id="$io->flush"><a class="permalink" href="#$io-%3Eflush">#</a><a id="flush"></a><a id="io-flush"></a>$io->flush</dt> <dd> <p><code>flush</code> causes perl to flush any buffered data at the perlio api level. Any unread data in the buffer will be discarded, and any unwritten data will be written to the underlying file descriptor. Returns "0 but true" on success, <code>undef</code> on error.</p> </dd> <dt id="$io->printflush-(-ARGS-)"><a class="permalink" href="#$io-%3Eprintflush-(-ARGS-)">#</a><a id="printflush"></a><a id="io-printflush-ARGS"></a>$io->printflush ( ARGS )</dt> <dd> <p>Turns on autoflush, print ARGS and then restores the autoflush status of the <code>IO::Handle</code> object. Returns the return value from print.</p> </dd> <dt id="$io->blocking-(-[-BOOL-]-)"><a class="permalink" href="#$io-%3Eblocking-(-%5B-BOOL-%5D-)">#</a><a id="blocking"></a><a id="io-blocking-BOOL"></a>$io->blocking ( [ BOOL ] )</dt> <dd> <p>If called with an argument <code>blocking</code> will turn on non-blocking IO if <code>BOOL</code> is false, and turn it off if <code>BOOL</code> is true.</p> <p><code>blocking</code> will return the value of the previous setting, or the current setting if <code>BOOL</code> is not given.</p> <p>If an error occurs <code>blocking</code> will return undef and <code>$!</code> will be set.</p> </dd> <dt id="binmode(-[LAYER]-)"><a class="permalink" href="#binmode(-%5BLAYER%5D-)">#</a><a id="binmode"></a><a id="binmode-LAYER"></a>binmode( [LAYER] )</dt> <dd> <p><code>binmode</code> sets <code>binmode</code> on the underlying <code>IO</code> object, as documented in <code>perldoc -f binmode</code>.</p> <p><code>binmode</code> accepts one optional parameter, which is the layer to be passed on to the <code>binmode</code> call.</p> </dd> </dl> <p>If the C functions setbuf() and/or setvbuf() are available, then <code>IO::Handle::setbuf</code> and <code>IO::Handle::setvbuf</code> set the buffering policy for an IO::Handle. The calling sequences for the Perl functions are the same as their C counterparts--including the constants <code>_IOFBF</code>, <code>_IOLBF</code>, and <code>_IONBF</code> for setvbuf()--except that the buffer parameter specifies a scalar variable to use as a buffer. You should only change the buffer before any I/O, or immediately after calling flush.</p> <p>WARNING: The IO::Handle::setvbuf() is not available by default on Perls 5.8.0 and later because setvbuf() is rather specific to using the stdio library, while Perl prefers the new perlio subsystem instead.</p> <p>WARNING: A variable used as a buffer by <code>setbuf</code> or <code>setvbuf</code> <b>must not be modified</b> in any way until the IO::Handle is closed or <code>setbuf</code> or <code>setvbuf</code> is called again, or memory corruption may result! Remember that the order of global destruction is undefined, so even if your buffer variable remains in scope until program termination, it may be undefined before the file IO::Handle is closed. Note that you need to import the constants <code>_IOFBF</code>, <code>_IOLBF</code>, and <code>_IONBF</code> explicitly. Like C, setbuf returns nothing. setvbuf returns "0 but true", on success, <code>undef</code> on failure.</p> <p>Lastly, there is a special method for working under <b>-T</b> and setuid/gid scripts:</p> <dl> <dt id="$io->untaint"><a class="permalink" href="#$io-%3Euntaint">#</a><a id="untaint"></a><a id="io-untaint"></a>$io->untaint</dt> <dd> <p>Marks the object as taint-clean, and as such data read from it will also be considered taint-clean. Note that this is a very trusting action to take, and appropriate consideration for the data source and potential vulnerability should be kept in mind. Returns 0 on success, -1 if setting the taint-clean flag failed. (eg invalid handle)</p> </dd> </dl> <h1 id="NOTE"><a class="permalink" href="#NOTE">#</a>NOTE</h1> <p>An <code>IO::Handle</code> object is a reference to a symbol/GLOB reference (see the <a href="/Symbol">Symbol</a> package). Some modules that inherit from <code>IO::Handle</code> may want to keep object related variables in the hash table part of the GLOB. In an attempt to prevent modules trampling on each other I propose the that any such module should prefix its variables with its own name separated by _'s. For example the IO::Socket module keeps a <code>timeout</code> variable in 'io_socket_timeout'.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/perlfunc">perlfunc</a>, <a href="/perlop#I%2FO-Operators">"I/O Operators" in perlop</a>, <a href="/IO::File">IO::File</a></p> <h1 id="BUGS"><a class="permalink" href="#BUGS">#</a>BUGS</h1> <p>Due to backwards compatibility, all filehandles resemble objects of class <code>IO::Handle</code>, or actually classes derived from that class. They actually aren't. Which means you can't derive your own class from <code>IO::Handle</code> and inherit those methods.</p> <h1 id="HISTORY"><a class="permalink" href="#HISTORY">#</a>HISTORY</h1> <p>Derived from FileHandle.pm by Graham Barr <<i>gbarr@pobox.com</i>></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>