CINXE.COM
PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space - 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>PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/PerlIO"> <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.20.0</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/PerlIO">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/PerlIO">5.40.1</a> <a class="dropdown-item" href="/5.40.0/PerlIO">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/PerlIO">5.38.3</a> <a class="dropdown-item" href="/5.38.2/PerlIO">5.38.2</a> <a class="dropdown-item" href="/5.38.1/PerlIO">5.38.1</a> <a class="dropdown-item" href="/5.38.0/PerlIO">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/PerlIO">5.36.3</a> <a class="dropdown-item" href="/5.36.2/PerlIO">5.36.2</a> <a class="dropdown-item" href="/5.36.1/PerlIO">5.36.1</a> <a class="dropdown-item" href="/5.36.0/PerlIO">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/PerlIO">5.34.3</a> <a class="dropdown-item" href="/5.34.2/PerlIO">5.34.2</a> <a class="dropdown-item" href="/5.34.1/PerlIO">5.34.1</a> <a class="dropdown-item" href="/5.34.0/PerlIO">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/PerlIO">5.32.1</a> <a class="dropdown-item" href="/5.32.0/PerlIO">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/PerlIO">5.30.3</a> <a class="dropdown-item" href="/5.30.2/PerlIO">5.30.2</a> <a class="dropdown-item" href="/5.30.1/PerlIO">5.30.1</a> <a class="dropdown-item" href="/5.30.0/PerlIO">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/PerlIO">5.28.3</a> <a class="dropdown-item" href="/5.28.2/PerlIO">5.28.2</a> <a class="dropdown-item" href="/5.28.1/PerlIO">5.28.1</a> <a class="dropdown-item" href="/5.28.0/PerlIO">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/PerlIO">5.26.3</a> <a class="dropdown-item" href="/5.26.2/PerlIO">5.26.2</a> <a class="dropdown-item" href="/5.26.1/PerlIO">5.26.1</a> <a class="dropdown-item" href="/5.26.0/PerlIO">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/PerlIO">5.24.4</a> <a class="dropdown-item" href="/5.24.3/PerlIO">5.24.3</a> <a class="dropdown-item" href="/5.24.2/PerlIO">5.24.2</a> <a class="dropdown-item" href="/5.24.1/PerlIO">5.24.1</a> <a class="dropdown-item" href="/5.24.0/PerlIO">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/PerlIO">5.22.4</a> <a class="dropdown-item" href="/5.22.3/PerlIO">5.22.3</a> <a class="dropdown-item" href="/5.22.2/PerlIO">5.22.2</a> <a class="dropdown-item" href="/5.22.1/PerlIO">5.22.1</a> <a class="dropdown-item" href="/5.22.0/PerlIO">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/PerlIO">5.20.3</a> <a class="dropdown-item" href="/5.20.2/PerlIO">5.20.2</a> <a class="dropdown-item" href="/5.20.1/PerlIO">5.20.1</a> <a class="dropdown-item active" href="/5.20.0/PerlIO">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/PerlIO">5.18.4</a> <a class="dropdown-item" href="/5.18.3/PerlIO">5.18.3</a> <a class="dropdown-item" href="/5.18.2/PerlIO">5.18.2</a> <a class="dropdown-item" href="/5.18.1/PerlIO">5.18.1</a> <a class="dropdown-item" href="/5.18.0/PerlIO">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/PerlIO">5.16.3</a> <a class="dropdown-item" href="/5.16.2/PerlIO">5.16.2</a> <a class="dropdown-item" href="/5.16.1/PerlIO">5.16.1</a> <a class="dropdown-item" href="/5.16.0/PerlIO">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/PerlIO">5.14.4</a> <a class="dropdown-item" href="/5.14.3/PerlIO">5.14.3</a> <a class="dropdown-item" href="/5.14.2/PerlIO">5.14.2</a> <a class="dropdown-item" href="/5.14.1/PerlIO">5.14.1</a> <a class="dropdown-item" href="/5.14.0/PerlIO">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/PerlIO">5.12.5</a> <a class="dropdown-item" href="/5.12.4/PerlIO">5.12.4</a> <a class="dropdown-item" href="/5.12.3/PerlIO">5.12.3</a> <a class="dropdown-item" href="/5.12.2/PerlIO">5.12.2</a> <a class="dropdown-item" href="/5.12.1/PerlIO">5.12.1</a> <a class="dropdown-item" href="/5.12.0/PerlIO">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/PerlIO">5.10.1</a> <a class="dropdown-item" href="/5.10.0/PerlIO">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/PerlIO">5.8.9</a> <a class="dropdown-item" href="/5.8.8/PerlIO">5.8.8</a> <a class="dropdown-item" href="/5.8.7/PerlIO">5.8.7</a> <a class="dropdown-item" href="/5.8.6/PerlIO">5.8.6</a> <a class="dropdown-item" href="/5.8.5/PerlIO">5.8.5</a> <a class="dropdown-item" href="/5.8.4/PerlIO">5.8.4</a> <a class="dropdown-item" href="/5.8.3/PerlIO">5.8.3</a> <a class="dropdown-item" href="/5.8.2/PerlIO">5.8.2</a> <a class="dropdown-item" href="/5.8.1/PerlIO">5.8.1</a> <a class="dropdown-item" href="/5.8.0/PerlIO">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/PerlIO">5.6.2</a> <a class="dropdown-item" href="/5.6.1/PerlIO">5.6.1</a> <a class="dropdown-item" href="/5.6.0/PerlIO">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/PerlIO">5.005_04</a> <a class="dropdown-item" href="/5.005_03/PerlIO">5.005_03</a> <a class="dropdown-item" href="/5.005_02/PerlIO">5.005_02</a> <a class="dropdown-item" href="/5.005_01/PerlIO">5.005_01</a> <a class="dropdown-item" href="/5.005/PerlIO">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/PerlIO">blead</a> <a class="dropdown-item" href="/5.41.9/PerlIO">5.41.9</a> <a class="dropdown-item" href="/5.41.8/PerlIO">5.41.8</a> <a class="dropdown-item" href="/5.41.7/PerlIO">5.41.7</a> <a class="dropdown-item" href="/5.41.6/PerlIO">5.41.6</a> <a class="dropdown-item" href="/5.41.5/PerlIO">5.41.5</a> <a class="dropdown-item" href="/5.41.4/PerlIO">5.41.4</a> <a class="dropdown-item" href="/5.41.3/PerlIO">5.41.3</a> <a class="dropdown-item" href="/5.41.2/PerlIO">5.41.2</a> <a class="dropdown-item" href="/5.41.1/PerlIO">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/PerlIO">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/PerlIO">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/PerlIO">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/PerlIO">5.39.10</a> <a class="dropdown-item" href="/5.39.9/PerlIO">5.39.9</a> <a class="dropdown-item" href="/5.39.8/PerlIO">5.39.8</a> <a class="dropdown-item" href="/5.39.7/PerlIO">5.39.7</a> <a class="dropdown-item" href="/5.39.6/PerlIO">5.39.6</a> <a class="dropdown-item" href="/5.39.5/PerlIO">5.39.5</a> <a class="dropdown-item" href="/5.39.4/PerlIO">5.39.4</a> <a class="dropdown-item" href="/5.39.3/PerlIO">5.39.3</a> <a class="dropdown-item" href="/5.39.2/PerlIO">5.39.2</a> <a class="dropdown-item" href="/5.39.1/PerlIO">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/PerlIO">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.20.0/perl">Perl</a> <a class="dropdown-item" href="/5.20.0/perlintro">Intro</a> <a class="dropdown-item" href="/5.20.0/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.20.0/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.20.0/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.0/perlop">Operators</a> <a class="dropdown-item" href="/5.20.0/functions">Functions</a> <a class="dropdown-item" href="/5.20.0/variables">Variables</a> <a class="dropdown-item" href="/5.20.0/modules">Modules</a> <a class="dropdown-item" href="/5.20.0/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.0/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.20.0/perlcommunity">Community</a> <a class="dropdown-item" href="/5.20.0/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.20.0/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.20.0/PerlIO">PerlIO</a> <div id="more"> (<a href="/5.20.0/PerlIO.txt">source</a>, <a href="https://metacpan.org/pod/PerlIO">CPAN</a>) </div> <div id="moduleversion">version 1.09</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.20.0. <a href="/PerlIO">View the latest version</a> </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="#Custom-Layers">Custom Layers</a> </li> <li> <a class="text-decoration-none" href="#Alternatives-to-raw">Alternatives to raw</a> </li> <li> <a class="text-decoration-none" href="#Defaults-and-how-to-override-them">Defaults and how to override them</a> </li> <li> <a class="text-decoration-none" href="#Querying-the-layers-of-filehandles">Querying the layers of filehandles</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</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>PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>open($fh, "<:crlf", "my.txt"); # support platform-native and # CRLF text files open($fh, "<", "his.jpg"); # portably open a binary file for reading binmode($fh); Shell: PERLIO=perlio perl ....</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>When an undefined layer 'foo' is encountered in an <code>open</code> or <code>binmode</code> layer specification then C code performs the equivalent of:</p> <pre><code>use PerlIO 'foo';</code></pre> <p>The perl code in PerlIO.pm then attempts to locate a layer by doing</p> <pre><code>require PerlIO::foo;</code></pre> <p>Otherwise the <code>PerlIO</code> package is a place holder for additional PerlIO related functions.</p> <p>The following layers are currently defined:</p> <dl> <dt id=":unix"><a class="permalink" href="#:unix">#</a><a id="unix"></a>:unix</dt> <dd> <p>Lowest level layer which provides basic PerlIO operations in terms of UNIX/POSIX numeric file descriptor calls (open(), read(), write(), lseek(), close()).</p> </dd> <dt id=":stdio"><a class="permalink" href="#:stdio">#</a><a id="stdio"></a>:stdio</dt> <dd> <p>Layer which calls <code>fread</code>, <code>fwrite</code> and <code>fseek</code>/<code>ftell</code> etc. Note that as this is "real" stdio it will ignore any layers beneath it and go straight to the operating system via the C library as usual.</p> </dd> <dt id=":perlio"><a class="permalink" href="#:perlio">#</a><a id="perlio"></a>:perlio</dt> <dd> <p>A from scratch implementation of buffering for PerlIO. Provides fast access to the buffer for <code>sv_gets</code> which implements perl's readline/<> and in general attempts to minimize data copying.</p> <p><code>:perlio</code> will insert a <code>:unix</code> layer below itself to do low level IO.</p> </dd> <dt id=":crlf"><a class="permalink" href="#:crlf">#</a><a id="crlf"></a>:crlf</dt> <dd> <p>A layer that implements DOS/Windows like CRLF line endings. On read converts pairs of CR,LF to a single "\n" newline character. On write converts each "\n" to a CR,LF pair. Note that this layer will silently refuse to be pushed on top of itself.</p> <p>It currently does <i>not</i> mimic MS-DOS as far as treating of Control-Z as being an end-of-file marker.</p> <p>Based on the <code>:perlio</code> layer.</p> </dd> <dt id=":utf8"><a class="permalink" href="#:utf8">#</a><a id="utf8"></a>:utf8</dt> <dd> <p>Declares that the stream accepts perl's <i>internal</i> encoding of characters. (Which really is UTF-8 on ASCII machines, but is UTF-EBCDIC on EBCDIC machines.) This allows any character perl can represent to be read from or written to the stream. The UTF-X encoding is chosen to render simple text parts (i.e. non-accented letters, digits and common punctuation) human readable in the encoded file.</p> <p>(<b>CAUTION</b>: This layer does not validate byte sequences. For reading input, you should instead use <code>:encoding(utf8)</code> instead of bare <code>:utf8</code>.)</p> <p>Here is how to write your native data out using UTF-8 (or UTF-EBCDIC) and then read it back in.</p> <pre><code>open(F, ">:utf8", "data.utf"); print F $out; close(F); open(F, "<:utf8", "data.utf"); $in = <F>; close(F);</code></pre> </dd> <dt id=":bytes"><a class="permalink" href="#:bytes">#</a><a id="bytes"></a>:bytes</dt> <dd> <p>This is the inverse of the <code>:utf8</code> layer. It turns off the flag on the layer below so that data read from it is considered to be "octets" i.e. characters in the range 0..255 only. Likewise on output perl will warn if a "wide" character is written to a such a stream.</p> </dd> <dt id=":raw"><a class="permalink" href="#:raw">#</a><a id="raw"></a>:raw</dt> <dd> <p>The <code>:raw</code> layer is <i>defined</i> as being identical to calling <code>binmode($fh)</code> - the stream is made suitable for passing binary data, i.e. each byte is passed as-is. The stream will still be buffered.</p> <p>In Perl 5.6 and some books the <code>:raw</code> layer (previously sometimes also referred to as a "discipline") is documented as the inverse of the <code>:crlf</code> layer. That is no longer the case - other layers which would alter the binary nature of the stream are also disabled. If you want UNIX line endings on a platform that normally does CRLF translation, but still want UTF-8 or encoding defaults, the appropriate thing to do is to add <code>:perlio</code> to the PERLIO environment variable.</p> <p>The implementation of <code>:raw</code> is as a pseudo-layer which when "pushed" pops itself and then any layers which do not declare themselves as suitable for binary data. (Undoing :utf8 and :crlf are implemented by clearing flags rather than popping layers but that is an implementation detail.)</p> <p>As a consequence of the fact that <code>:raw</code> normally pops layers, it usually only makes sense to have it as the only or first element in a layer specification. When used as the first element it provides a known base on which to build e.g.</p> <pre><code>open($fh,":raw:utf8",...)</code></pre> <p>will construct a "binary" stream, but then enable UTF-8 translation.</p> </dd> <dt id=":pop"><a class="permalink" href="#:pop">#</a><a id="pop"></a>:pop</dt> <dd> <p>A pseudo layer that removes the top-most layer. Gives perl code a way to manipulate the layer stack. Note that <code>:pop</code> only works on real layers and will not undo the effects of pseudo layers like <code>:utf8</code>. An example of a possible use might be:</p> <pre><code>open($fh,...) ... binmode($fh,":encoding(...)"); # next chunk is encoded ... binmode($fh,":pop"); # back to un-encoded</code></pre> <p>A more elegant (and safer) interface is needed.</p> </dd> <dt id=":win32"><a class="permalink" href="#:win32">#</a><a id="win32"></a>:win32</dt> <dd> <p>On Win32 platforms this <i>experimental</i> layer uses the native "handle" IO rather than the unix-like numeric file descriptor layer. Known to be buggy as of perl 5.8.2.</p> </dd> </dl> <h2 id="Custom-Layers"><a class="permalink" href="#Custom-Layers">#</a><a id="Custom"></a>Custom Layers</h2> <p>It is possible to write custom layers in addition to the above builtin ones, both in C/XS and Perl. Two such layers (and one example written in Perl using the latter) come with the Perl distribution.</p> <dl> <dt id=":encoding"><a class="permalink" href="#:encoding">#</a><a id="encoding"></a>:encoding</dt> <dd> <p>Use <code>:encoding(ENCODING)</code> either in open() or binmode() to install a layer that transparently does character set and encoding transformations, for example from Shift-JIS to Unicode. Note that under <code>stdio</code> an <code>:encoding</code> also enables <code>:utf8</code>. See <a href="/5.20.0/PerlIO::encoding">PerlIO::encoding</a> for more information.</p> </dd> <dt id=":mmap"><a class="permalink" href="#:mmap">#</a><a id="mmap"></a>:mmap</dt> <dd> <p>A layer which implements "reading" of files by using <code>mmap()</code> to make a (whole) file appear in the process's address space, and then using that as PerlIO's "buffer". This <i>may</i> be faster in certain circumstances for large files, and may result in less physical memory use when multiple processes are reading the same file.</p> <p>Files which are not <code>mmap()</code>-able revert to behaving like the <code>:perlio</code> layer. Writes also behave like the <code>:perlio</code> layer, as <code>mmap()</code> for write needs extra house-keeping (to extend the file) which negates any advantage.</p> <p>The <code>:mmap</code> layer will not exist if the platform does not support <code>mmap()</code>.</p> </dd> <dt id=":via"><a class="permalink" href="#:via">#</a><a id="via"></a>:via</dt> <dd> <p>Use <code>:via(MODULE)</code> either in open() or binmode() to install a layer that does whatever transformation (for example compression / decompression, encryption / decryption) to the filehandle. See <a href="/5.20.0/PerlIO::via">PerlIO::via</a> for more information.</p> </dd> </dl> <h2 id="Alternatives-to-raw"><a class="permalink" href="#Alternatives-to-raw">#</a><a id="Alternatives"></a>Alternatives to raw</h2> <p>To get a binary stream an alternate method is to use:</p> <pre><code>open($fh,"whatever") binmode($fh);</code></pre> <p>this has the advantage of being backward compatible with how such things have had to be coded on some platforms for years.</p> <p>To get an unbuffered stream specify an unbuffered layer (e.g. <code>:unix</code>) in the open call:</p> <pre><code>open($fh,"<:unix",$path)</code></pre> <h2 id="Defaults-and-how-to-override-them"><a class="permalink" href="#Defaults-and-how-to-override-them">#</a><a id="Defaults"></a>Defaults and how to override them</h2> <p>If the platform is MS-DOS like and normally does CRLF to "\n" translation for text files then the default layers are :</p> <pre><code class="plaintext">unix crlf</code></pre> <p>(The low level "unix" layer may be replaced by a platform specific low level layer.)</p> <p>Otherwise if <code>Configure</code> found out how to do "fast" IO using the system's stdio, then the default layers are:</p> <pre><code class="plaintext">unix stdio</code></pre> <p>Otherwise the default layers are</p> <pre><code class="plaintext">unix perlio</code></pre> <p>These defaults may change once perlio has been better tested and tuned.</p> <p>The default can be overridden by setting the environment variable PERLIO to a space separated list of layers (<code>unix</code> or platform low level layer is always pushed first).</p> <p>This can be used to see the effect of/bugs in the various layers e.g.</p> <pre><code class="plaintext">cd .../perl/t PERLIO=stdio ./perl harness PERLIO=perlio ./perl harness</code></pre> <p>For the various values of PERLIO see <a href="/5.20.0/perlrun#PERLIO">"PERLIO" in perlrun</a>.</p> <h2 id="Querying-the-layers-of-filehandles"><a class="permalink" href="#Querying-the-layers-of-filehandles">#</a><a id="Querying"></a>Querying the layers of filehandles</h2> <p>The following returns the <b>names</b> of the PerlIO layers on a filehandle.</p> <pre><code>my @layers = PerlIO::get_layers($fh); # Or FH, *FH, "FH".</code></pre> <p>The layers are returned in the order an open() or binmode() call would use them. Note that the "default stack" depends on the operating system and on the Perl version, and both the compile-time and runtime configurations of Perl.</p> <p>The following table summarizes the default layers on UNIX-like and DOS-like platforms and depending on the setting of <code>$ENV{PERLIO}</code>:</p> <pre><code class="plaintext">PERLIO UNIX-like DOS-like ------ --------- -------- unset / "" unix perlio / stdio [1] unix crlf stdio unix perlio / stdio [1] stdio perlio unix perlio unix perlio # [1] "stdio" if Configure found out how to do "fast stdio" (depends # on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"</code></pre> <p>By default the layers from the input side of the filehandle are returned; to get the output side, use the optional <code>output</code> argument:</p> <pre><code>my @layers = PerlIO::get_layers($fh, output => 1);</code></pre> <p>(Usually the layers are identical on either side of a filehandle but for example with sockets there may be differences, or if you have been using the <code>open</code> pragma.)</p> <p>There is no set_layers(), nor does get_layers() return a tied array mirroring the stack, or anything fancy like that. This is not accidental or unintentional. The PerlIO layer stack is a bit more complicated than just a stack (see for example the behaviour of <code>:raw</code>). You are supposed to use open() and binmode() to manipulate the stack.</p> <p><b>Implementation details follow, please close your eyes.</b></p> <p>The arguments to layers are by default returned in parentheses after the name of the layer, and certain layers (like <code>utf8</code>) are not real layers but instead flags on real layers; to get all of these returned separately, use the optional <code>details</code> argument:</p> <pre><code>my @layer_and_args_and_flags = PerlIO::get_layers($fh, details => 1);</code></pre> <p>The result will be up to be three times the number of layers: the first element will be a name, the second element the arguments (unspecified arguments will be <code>undef</code>), the third element the flags, the fourth element a name again, and so forth.</p> <p><b>You may open your eyes now.</b></p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Nick Ing-Simmons <nick@ing-simmons.net></p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.20.0/perlfunc#binmode">"binmode" in perlfunc</a>, <a href="/5.20.0/perlfunc#open">"open" in perlfunc</a>, <a href="/5.20.0/perlunicode">perlunicode</a>, <a href="/5.20.0/perliol">perliol</a>, <a href="/5.20.0/Encode">Encode</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>