CINXE.COM

open - 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>open - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/functions/open"> <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.8.9</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/functions/open">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/functions/open">5.40.1</a> <a class="dropdown-item" href="/5.40.0/functions/open">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/functions/open">5.38.3</a> <a class="dropdown-item" href="/5.38.2/functions/open">5.38.2</a> <a class="dropdown-item" href="/5.38.1/functions/open">5.38.1</a> <a class="dropdown-item" href="/5.38.0/functions/open">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/functions/open">5.36.3</a> <a class="dropdown-item" href="/5.36.2/functions/open">5.36.2</a> <a class="dropdown-item" href="/5.36.1/functions/open">5.36.1</a> <a class="dropdown-item" href="/5.36.0/functions/open">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/functions/open">5.34.3</a> <a class="dropdown-item" href="/5.34.2/functions/open">5.34.2</a> <a class="dropdown-item" href="/5.34.1/functions/open">5.34.1</a> <a class="dropdown-item" href="/5.34.0/functions/open">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/functions/open">5.32.1</a> <a class="dropdown-item" href="/5.32.0/functions/open">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/functions/open">5.30.3</a> <a class="dropdown-item" href="/5.30.2/functions/open">5.30.2</a> <a class="dropdown-item" href="/5.30.1/functions/open">5.30.1</a> <a class="dropdown-item" href="/5.30.0/functions/open">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/functions/open">5.28.3</a> <a class="dropdown-item" href="/5.28.2/functions/open">5.28.2</a> <a class="dropdown-item" href="/5.28.1/functions/open">5.28.1</a> <a class="dropdown-item" href="/5.28.0/functions/open">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/functions/open">5.26.3</a> <a class="dropdown-item" href="/5.26.2/functions/open">5.26.2</a> <a class="dropdown-item" href="/5.26.1/functions/open">5.26.1</a> <a class="dropdown-item" href="/5.26.0/functions/open">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/functions/open">5.24.4</a> <a class="dropdown-item" href="/5.24.3/functions/open">5.24.3</a> <a class="dropdown-item" href="/5.24.2/functions/open">5.24.2</a> <a class="dropdown-item" href="/5.24.1/functions/open">5.24.1</a> <a class="dropdown-item" href="/5.24.0/functions/open">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/functions/open">5.22.4</a> <a class="dropdown-item" href="/5.22.3/functions/open">5.22.3</a> <a class="dropdown-item" href="/5.22.2/functions/open">5.22.2</a> <a class="dropdown-item" href="/5.22.1/functions/open">5.22.1</a> <a class="dropdown-item" href="/5.22.0/functions/open">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/functions/open">5.20.3</a> <a class="dropdown-item" href="/5.20.2/functions/open">5.20.2</a> <a class="dropdown-item" href="/5.20.1/functions/open">5.20.1</a> <a class="dropdown-item" href="/5.20.0/functions/open">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/functions/open">5.18.4</a> <a class="dropdown-item" href="/5.18.3/functions/open">5.18.3</a> <a class="dropdown-item" href="/5.18.2/functions/open">5.18.2</a> <a class="dropdown-item" href="/5.18.1/functions/open">5.18.1</a> <a class="dropdown-item" href="/5.18.0/functions/open">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/functions/open">5.16.3</a> <a class="dropdown-item" href="/5.16.2/functions/open">5.16.2</a> <a class="dropdown-item" href="/5.16.1/functions/open">5.16.1</a> <a class="dropdown-item" href="/5.16.0/functions/open">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/functions/open">5.14.4</a> <a class="dropdown-item" href="/5.14.3/functions/open">5.14.3</a> <a class="dropdown-item" href="/5.14.2/functions/open">5.14.2</a> <a class="dropdown-item" href="/5.14.1/functions/open">5.14.1</a> <a class="dropdown-item" href="/5.14.0/functions/open">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/functions/open">5.12.5</a> <a class="dropdown-item" href="/5.12.4/functions/open">5.12.4</a> <a class="dropdown-item" href="/5.12.3/functions/open">5.12.3</a> <a class="dropdown-item" href="/5.12.2/functions/open">5.12.2</a> <a class="dropdown-item" href="/5.12.1/functions/open">5.12.1</a> <a class="dropdown-item" href="/5.12.0/functions/open">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/functions/open">5.10.1</a> <a class="dropdown-item" href="/5.10.0/functions/open">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item active" href="/5.8.9/functions/open">5.8.9</a> <a class="dropdown-item" href="/5.8.8/functions/open">5.8.8</a> <a class="dropdown-item" href="/5.8.7/functions/open">5.8.7</a> <a class="dropdown-item" href="/5.8.6/functions/open">5.8.6</a> <a class="dropdown-item" href="/5.8.5/functions/open">5.8.5</a> <a class="dropdown-item" href="/5.8.4/functions/open">5.8.4</a> <a class="dropdown-item" href="/5.8.3/functions/open">5.8.3</a> <a class="dropdown-item" href="/5.8.2/functions/open">5.8.2</a> <a class="dropdown-item" href="/5.8.1/functions/open">5.8.1</a> <a class="dropdown-item" href="/5.8.0/functions/open">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/functions/open">5.6.2</a> <a class="dropdown-item" href="/5.6.1/functions/open">5.6.1</a> <a class="dropdown-item" href="/5.6.0/functions/open">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/functions/open">5.005_04</a> <a class="dropdown-item" href="/5.005_03/functions/open">5.005_03</a> <a class="dropdown-item" href="/5.005_02/functions/open">5.005_02</a> <a class="dropdown-item" href="/5.005_01/functions/open">5.005_01</a> <a class="dropdown-item" href="/5.005/functions/open">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/functions/open">blead</a> <a class="dropdown-item" href="/5.41.10/functions/open">5.41.10</a> <a class="dropdown-item" href="/5.41.9/functions/open">5.41.9</a> <a class="dropdown-item" href="/5.41.8/functions/open">5.41.8</a> <a class="dropdown-item" href="/5.41.7/functions/open">5.41.7</a> <a class="dropdown-item" href="/5.41.6/functions/open">5.41.6</a> <a class="dropdown-item" href="/5.41.5/functions/open">5.41.5</a> <a class="dropdown-item" href="/5.41.4/functions/open">5.41.4</a> <a class="dropdown-item" href="/5.41.3/functions/open">5.41.3</a> <a class="dropdown-item" href="/5.41.2/functions/open">5.41.2</a> <a class="dropdown-item" href="/5.41.1/functions/open">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/functions/open">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/functions/open">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/functions/open">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/functions/open">5.39.10</a> <a class="dropdown-item" href="/5.39.9/functions/open">5.39.9</a> <a class="dropdown-item" href="/5.39.8/functions/open">5.39.8</a> <a class="dropdown-item" href="/5.39.7/functions/open">5.39.7</a> <a class="dropdown-item" href="/5.39.6/functions/open">5.39.6</a> <a class="dropdown-item" href="/5.39.5/functions/open">5.39.5</a> <a class="dropdown-item" href="/5.39.4/functions/open">5.39.4</a> <a class="dropdown-item" href="/5.39.3/functions/open">5.39.3</a> <a class="dropdown-item" href="/5.39.2/functions/open">5.39.2</a> <a class="dropdown-item" href="/5.39.1/functions/open">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/functions/open">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.8.9/perl">Perl</a> <a class="dropdown-item" href="/5.8.9/perlintro">Intro</a> <a class="dropdown-item" href="/5.8.9/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.8.9/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.8.9/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/perlop">Operators</a> <a class="dropdown-item" href="/5.8.9/functions">Functions</a> <a class="dropdown-item" href="/5.8.9/variables">Variables</a> <a class="dropdown-item" href="/5.8.9/modules">Modules</a> <a class="dropdown-item" href="/5.8.9/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.8.9/perlcommunity">Community</a> <a class="dropdown-item" href="/5.8.9/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.8.9/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.8.9/functions">functions</a> / <a href="/5.8.9/functions/open">open</a> <div id="more"> (<a href="/5.8.9/functions/open.txt">source</a>, <a href="https://metacpan.org/pod/perlfunc#open-FILEHANDLE,EXPR">CPAN</a>) </div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.8.9. <a href="/functions/open">View the latest version</a> </div> <div class="leading-notice"> <strong>open</strong> may also refer to the module: <a href="/5.8.9/open">open</a> </div> <dl> <dt id="open-FILEHANDLE,EXPR"><a class="permalink" href="#open-FILEHANDLE,EXPR">#</a><a id="open"></a><a id="open-FILEHANDLE-EXPR"></a>open FILEHANDLE,EXPR </dt> <dd> </dd> <dt id="open-FILEHANDLE,MODE,EXPR"><a class="permalink" href="#open-FILEHANDLE,MODE,EXPR">#</a><a id="open1"></a><a id="open-FILEHANDLE-MODE-EXPR"></a>open FILEHANDLE,MODE,EXPR</dt> <dd> </dd> <dt id="open-FILEHANDLE,MODE,EXPR,LIST"><a class="permalink" href="#open-FILEHANDLE,MODE,EXPR,LIST">#</a><a id="open2"></a><a id="open-FILEHANDLE-MODE-EXPR-LIST"></a>open FILEHANDLE,MODE,EXPR,LIST</dt> <dd> </dd> <dt id="open-FILEHANDLE,MODE,REFERENCE"><a class="permalink" href="#open-FILEHANDLE,MODE,REFERENCE">#</a><a id="open3"></a><a id="open-FILEHANDLE-MODE-REFERENCE"></a>open FILEHANDLE,MODE,REFERENCE</dt> <dd> </dd> <dt id="open-FILEHANDLE"><a class="permalink" href="#open-FILEHANDLE">#</a><a id="open4"></a>open FILEHANDLE</dt> <dd> <p>Opens the file whose filename is given by EXPR, and associates it with FILEHANDLE.</p> <p>Simple examples to open a file for reading:</p> <pre><code>open(my $fh, &#39;&lt;&#39;, &quot;input.txt&quot;) or die $!;</code></pre> <p>and for writing:</p> <pre><code>open(my $fh, &#39;&gt;&#39;, &quot;output.txt&quot;) or die $!;</code></pre> <p>(The following is a comprehensive reference to open(): for a gentler introduction you may consider <a href="/5.8.9/perlopentut">perlopentut</a>.)</p> <p>If FILEHANDLE is an undefined scalar variable (or array or hash element) the variable is assigned a reference to a new anonymous filehandle, otherwise if FILEHANDLE is an expression, its value is used as the name of the real filehandle wanted. (This is considered a symbolic reference, so <code>use strict &#39;refs&#39;</code> should <i>not</i> be in effect.)</p> <p>If EXPR is omitted, the scalar variable of the same name as the FILEHANDLE contains the filename. (Note that lexical variables--those declared with <code>my</code>--will not work for this purpose; so if you&#39;re using <code>my</code>, specify EXPR in your call to open.)</p> <p>If three or more arguments are specified then the mode of opening and the file name are separate. If MODE is <code>&#39;&lt;&#39;</code> or nothing, the file is opened for input. If MODE is <code>&#39;&gt;&#39;</code>, the file is truncated and opened for output, being created if necessary. If MODE is <code>&#39;&gt;&gt;&#39;</code>, the file is opened for appending, again being created if necessary.</p> <p>You can put a <code>&#39;+&#39;</code> in front of the <code>&#39;&gt;&#39;</code> or <code>&#39;&lt;&#39;</code> to indicate that you want both read and write access to the file; thus <code>&#39;+&lt;&#39;</code> is almost always preferred for read/write updates--the <code>&#39;+&gt;&#39;</code> mode would clobber the file first. You can&#39;t usually use either read-write mode for updating textfiles, since they have variable length records. See the <b>-i</b> switch in <a href="/5.8.9/perlrun">perlrun</a> for a better approach. The file is created with permissions of <code>0666</code> modified by the process&#39; <code>umask</code> value.</p> <p>These various prefixes correspond to the fopen(3) modes of <code>&#39;r&#39;</code>, <code>&#39;r+&#39;</code>, <code>&#39;w&#39;</code>, <code>&#39;w+&#39;</code>, <code>&#39;a&#39;</code>, and <code>&#39;a+&#39;</code>.</p> <p>In the 2-arguments (and 1-argument) form of the call the mode and filename should be concatenated (in this order), possibly separated by spaces. It is possible to omit the mode in these forms if the mode is <code>&#39;&lt;&#39;</code>.</p> <p>If the filename begins with <code>&#39;|&#39;</code>, the filename is interpreted as a command to which output is to be piped, and if the filename ends with a <code>&#39;|&#39;</code>, the filename is interpreted as a command which pipes output to us. See <a href="/5.8.9/perlipc#Using-open()-for-IPC">&quot;Using open() for IPC&quot; in perlipc</a> for more examples of this. (You are not allowed to <code>open</code> to a command that pipes both in <i>and</i> out, but see <a href="/5.8.9/IPC::Open2">IPC::Open2</a>, <a href="/5.8.9/IPC::Open3">IPC::Open3</a>, and <a href="/5.8.9/perlipc#Bidirectional-Communication-with-Another-Process">&quot;Bidirectional Communication with Another Process&quot; in perlipc</a> for alternatives.)</p> <p>For three or more arguments if MODE is <code>&#39;|-&#39;</code>, the filename is interpreted as a command to which output is to be piped, and if MODE is <code>&#39;-|&#39;</code>, the filename is interpreted as a command which pipes output to us. In the 2-arguments (and 1-argument) form one should replace dash (<code>&#39;-&#39;</code>) with the command. See <a href="/5.8.9/perlipc#Using-open()-for-IPC">&quot;Using open() for IPC&quot; in perlipc</a> for more examples of this. (You are not allowed to <code>open</code> to a command that pipes both in <i>and</i> out, but see <a href="/5.8.9/IPC::Open2">IPC::Open2</a>, <a href="/5.8.9/IPC::Open3">IPC::Open3</a>, and <a href="/5.8.9/perlipc#Bidirectional-Communication">&quot;Bidirectional Communication&quot; in perlipc</a> for alternatives.)</p> <p>In the three-or-more argument form of pipe opens, if LIST is specified (extra arguments after the command name) then LIST becomes arguments to the command invoked if the platform supports it. The meaning of <code>open</code> with more than three arguments for non-pipe modes is not yet specified. Experimental &quot;layers&quot; may give extra LIST arguments meaning.</p> <p>In the 2-arguments (and 1-argument) form opening <code>&#39;-&#39;</code> opens STDIN and opening <code>&#39;&gt;-&#39;</code> opens STDOUT.</p> <p>You may use the three-argument form of open to specify IO &quot;layers&quot; (sometimes also referred to as &quot;disciplines&quot;) to be applied to the handle that affect how the input and output are processed (see <a href="/5.8.9/open">open</a> and <a href="/5.8.9/PerlIO">PerlIO</a> for more details). For example</p> <pre><code>open(my $fh, &quot;&lt;:encoding(UTF-8)&quot;, &quot;file&quot;)</code></pre> <p>will open the UTF-8 encoded file containing Unicode characters, see <a href="/5.8.9/perluniintro">perluniintro</a>. Note that if layers are specified in the three-arg form then default layers stored in ${^OPEN} (see <a href="/5.8.9/perlvar">perlvar</a>; usually set by the <b>open</b> pragma or the switch <b>-CioD</b>) are ignored.</p> <p>Open returns nonzero upon success, the undefined value otherwise. If the <code>open</code> involved a pipe, the return value happens to be the pid of the subprocess.</p> <p>If you&#39;re running Perl on a system that distinguishes between text files and binary files, then you should check out <a href="/5.8.9/functions/binmode">&quot;binmode&quot;</a> for tips for dealing with this. The key distinction between systems that need <code>binmode</code> and those that don&#39;t is their text file formats. Systems like Unix, Mac OS, and Plan 9, which delimit lines with a single character, and which encode that character in C as <code>&quot;\n&quot;</code>, do not need <code>binmode</code>. The rest need it.</p> <p>When opening a file, it&#39;s usually a bad idea to continue normal execution if the request failed, so <code>open</code> is frequently used in connection with <code>die</code>. Even if <code>die</code> won&#39;t do what you want (say, in a CGI script, where you want to make a nicely formatted error message (but there are modules that can help with that problem)) you should always check the return value from opening a file. The infrequent exception is when working with an unopened filehandle is actually what you want to do.</p> <p>As a special case the 3-arg form with a read/write mode and the third argument being <code>undef</code>:</p> <pre><code>open(my $tmp, &quot;+&gt;&quot;, undef) or die ...</code></pre> <p>opens a filehandle to an anonymous temporary file. Also using &quot;+&lt;&quot; works for symmetry, but you really should consider writing something to the temporary file first. You will need to seek() to do the reading.</p> <p>Since v5.8.0, perl has built using PerlIO by default. Unless you&#39;ve changed this (i.e. Configure -Uuseperlio), you can open file handles to &quot;in memory&quot; files held in Perl scalars via:</p> <pre><code>open($fh, &#39;&gt;&#39;, \$variable) || ..</code></pre> <p>Though if you try to re-open <code>STDOUT</code> or <code>STDERR</code> as an &quot;in memory&quot; file, you have to close it first:</p> <pre><code>close STDOUT; open STDOUT, &#39;&gt;&#39;, \$variable or die &quot;Can&#39;t open STDOUT: $!&quot;;</code></pre> <p>Examples:</p> <pre><code> $ARTICLE = 100; open ARTICLE or die &quot;Can&#39;t find article $ARTICLE: $!\n&quot;; while (&lt;ARTICLE&gt;) {... open(LOG, &#39;&gt;&gt;/usr/spool/news/twitlog&#39;); # (log is reserved) # if the open fails, output is discarded open(my $dbase, &#39;+&lt;&#39;, &#39;dbase.mine&#39;) # open for update or die &quot;Can&#39;t open &#39;dbase.mine&#39; for update: $!&quot;; open(my $dbase, &#39;+&lt;dbase.mine&#39;) # ditto or die &quot;Can&#39;t open &#39;dbase.mine&#39; for update: $!&quot;; open(ARTICLE, &#39;-|&#39;, &quot;caesar &lt;$article&quot;) # decrypt article or die &quot;Can&#39;t start caesar: $!&quot;; open(ARTICLE, &quot;caesar &lt;$article |&quot;) # ditto or die &quot;Can&#39;t start caesar: $!&quot;; open(EXTRACT, &quot;|sort &gt;Tmp$$&quot;) # $$ is our process id or die &quot;Can&#39;t start sort: $!&quot;; # in memory files open(MEMORY,&#39;&gt;&#39;, \$var) or die &quot;Can&#39;t open memory file: $!&quot;; print MEMORY &quot;foo!\n&quot;; # output will end up in $var # process argument list of files along with any includes foreach $file (@ARGV) { process($file, &#39;fh00&#39;); } sub process { my($filename, $input) = @_; $input++; # this is a string increment unless (open($input, $filename)) { print STDERR &quot;Can&#39;t open $filename: $!\n&quot;; return; } local $_; while (&lt;$input&gt;) { # note use of indirection if (/^#include &quot;(.*)&quot;/) { process($1, $input); next; } #... # whatever } }</code></pre> <p>See <a href="/5.8.9/perliol">perliol</a> for detailed info on PerlIO.</p> <p>You may also, in the Bourne shell tradition, specify an EXPR beginning with <code>&#39;&gt;&amp;&#39;</code>, in which case the rest of the string is interpreted as the name of a filehandle (or file descriptor, if numeric) to be duped (as <a href="http://man.he.net/man2/dup">dup(2)</a>) and opened. You may use <code>&amp;</code> after <code>&gt;</code>, <code>&gt;&gt;</code>, <code>&lt;</code>, <code>+&gt;</code>, <code>+&gt;&gt;</code>, and <code>+&lt;</code>. The mode you specify should match the mode of the original filehandle. (Duping a filehandle does not take into account any existing contents of IO buffers.) If you use the 3-arg form then you can pass either a number, the name of a filehandle or the normal &quot;reference to a glob&quot;.</p> <p>Here is a script that saves, redirects, and restores <code>STDOUT</code> and <code>STDERR</code> using various methods:</p> <pre><code>#!/usr/bin/perl open my $oldout, &quot;&gt;&amp;STDOUT&quot; or die &quot;Can&#39;t dup STDOUT: $!&quot;; open OLDERR, &quot;&gt;&amp;&quot;, \*STDERR or die &quot;Can&#39;t dup STDERR: $!&quot;; open STDOUT, &#39;&gt;&#39;, &quot;foo.out&quot; or die &quot;Can&#39;t redirect STDOUT: $!&quot;; open STDERR, &quot;&gt;&amp;STDOUT&quot; or die &quot;Can&#39;t dup STDOUT: $!&quot;; select STDERR; $| = 1; # make unbuffered select STDOUT; $| = 1; # make unbuffered print STDOUT &quot;stdout 1\n&quot;; # this works for print STDERR &quot;stderr 1\n&quot;; # subprocesses too open STDOUT, &quot;&gt;&amp;&quot;, $oldout or die &quot;Can&#39;t dup \$oldout: $!&quot;; open STDERR, &quot;&gt;&amp;OLDERR&quot; or die &quot;Can&#39;t dup OLDERR: $!&quot;; print STDOUT &quot;stdout 2\n&quot;; print STDERR &quot;stderr 2\n&quot;;</code></pre> <p>If you specify <code>&#39;&lt;&amp;=X&#39;</code>, where <code>X</code> is a file descriptor number or a filehandle, then Perl will do an equivalent of C&#39;s <code>fdopen</code> of that file descriptor (and not call <a href="http://man.he.net/man2/dup">dup(2)</a>); this is more parsimonious of file descriptors. For example:</p> <pre><code># open for input, reusing the fileno of $fd open(FILEHANDLE, &quot;&lt;&amp;=$fd&quot;)</code></pre> <p>or</p> <pre><code>open(FILEHANDLE, &quot;&lt;&amp;=&quot;, $fd)</code></pre> <p>or</p> <pre><code class="plaintext"># open for append, using the fileno of OLDFH open(FH, &quot;&gt;&gt;&amp;=&quot;, OLDFH)</code></pre> <p>or</p> <pre><code class="plaintext">open(FH, &quot;&gt;&gt;&amp;=OLDFH&quot;)</code></pre> <p>Being parsimonious on filehandles is also useful (besides being parsimonious) for example when something is dependent on file descriptors, like for example locking using flock(). If you do just <code>open(A, &#39;&gt;&gt;&amp;B&#39;)</code>, the filehandle A will not have the same file descriptor as B, and therefore flock(A) will not flock(B), and vice versa. But with <code>open(A, &#39;&gt;&gt;&amp;=B&#39;)</code> the filehandles will share the same file descriptor.</p> <p>Note that if you are using Perls older than 5.8.0, Perl will be using the standard C libraries&#39; fdopen() to implement the &quot;=&quot; functionality. On many UNIX systems fdopen() fails when file descriptors exceed a certain value, typically 255. For Perls 5.8.0 and later, PerlIO is most often the default.</p> <p>You can see whether Perl has been compiled with PerlIO or not by running <code>perl -V</code> and looking for <code>useperlio=</code> line. If <code>useperlio</code> is <code>define</code>, you have PerlIO, otherwise you don&#39;t.</p> <p>If you open a pipe on the command <code>&#39;-&#39;</code>, i.e., either <code>&#39;|-&#39;</code> or <code>&#39;-|&#39;</code> with 2-arguments (or 1-argument) form of open(), then there is an implicit fork done, and the return value of open is the pid of the child within the parent process, and <code>0</code> within the child process. (Use <code>defined($pid)</code> to determine whether the open was successful.) The filehandle behaves normally for the parent, but i/o to that filehandle is piped from/to the STDOUT/STDIN of the child process. In the child process the filehandle isn&#39;t opened--i/o happens from/to the new STDOUT or STDIN. Typically this is used like the normal piped open when you want to exercise more control over just how the pipe command gets executed, such as when you are running setuid, and don&#39;t want to have to scan shell commands for metacharacters. The following triples are more or less equivalent:</p> <pre><code>open(FOO, &quot;|tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;); open(FOO, &#39;|-&#39;, &quot;tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;); open(FOO, &#39;|-&#39;) || exec &#39;tr&#39;, &#39;[a-z]&#39;, &#39;[A-Z]&#39;; open(FOO, &#39;|-&#39;, &quot;tr&quot;, &#39;[a-z]&#39;, &#39;[A-Z]&#39;); open(FOO, &quot;cat -n &#39;$file&#39;|&quot;); open(FOO, &#39;-|&#39;, &quot;cat -n &#39;$file&#39;&quot;); open(FOO, &#39;-|&#39;) || exec &#39;cat&#39;, &#39;-n&#39;, $file; open(FOO, &#39;-|&#39;, &quot;cat&quot;, &#39;-n&#39;, $file);</code></pre> <p>The last example in each block shows the pipe as &quot;list form&quot;, which is not yet supported on all platforms. A good rule of thumb is that if your platform has true <code>fork()</code> (in other words, if your platform is UNIX) you can use the list form.</p> <p>See <a href="/5.8.9/perlipc#Safe-Pipe-Opens">&quot;Safe Pipe Opens&quot; in perlipc</a> for more examples of this.</p> <p>Beginning with v5.6.0, Perl will attempt to flush all files opened for output before any operation that may do a fork, but this may not be supported on some platforms (see <a href="/5.8.9/perlport">perlport</a>). To be safe, you may need to set <code>$|</code> ($AUTOFLUSH in English) or call the <code>autoflush()</code> method of <code>IO::Handle</code> on any open handles.</p> <p>On systems that support a close-on-exec flag on files, the flag will be set for the newly opened file descriptor as determined by the value of $^F. See <a href="/5.8.9/perlvar#%24%5EF">&quot;$^F&quot; in perlvar</a>.</p> <p>Closing any piped filehandle causes the parent process to wait for the child to finish, and returns the status value in <code>$?</code> and <code>${^CHILD_ERROR_NATIVE}</code>.</p> <p>The filename passed to 2-argument (or 1-argument) form of open() will have leading and trailing whitespace deleted, and the normal redirection characters honored. This property, known as &quot;magic open&quot;, can often be used to good effect. A user could specify a filename of <i>&quot;rsh cat file |&quot;</i>, or you could change certain filenames as needed:</p> <pre><code>$filename =~ s/(.*\.gz)\s*$/gzip -dc &lt; $1|/; open(FH, $filename) or die &quot;Can&#39;t open $filename: $!&quot;;</code></pre> <p>Use 3-argument form to open a file with arbitrary weird characters in it,</p> <pre><code>open(FOO, &#39;&lt;&#39;, $file);</code></pre> <p>otherwise it&#39;s necessary to protect any leading and trailing whitespace:</p> <pre><code>$file =~ s#^(\s)#./$1#; open(FOO, &quot;&lt; $file\0&quot;);</code></pre> <p>(this may not work on some bizarre filesystems). One should conscientiously choose between the <i>magic</i> and 3-arguments form of open():</p> <pre><code>open IN, $ARGV[0];</code></pre> <p>will allow the user to specify an argument of the form <code>&quot;rsh cat file |&quot;</code>, but will not work on a filename which happens to have a trailing space, while</p> <pre><code>open IN, &#39;&lt;&#39;, $ARGV[0];</code></pre> <p>will have exactly the opposite restrictions.</p> <p>If you want a &quot;real&quot; C <code>open</code> (see <a href="http://man.he.net/man2/open">open(2)</a> on your system), then you should use the <code>sysopen</code> function, which involves no such magic (but may use subtly different filemodes than Perl open(), which is mapped to C fopen()). This is another way to protect your filenames from interpretation. For example:</p> <pre><code> use IO::Handle; sysopen(HANDLE, $path, O_RDWR|O_CREAT|O_EXCL) or die &quot;sysopen $path: $!&quot;; $oldfh = select(HANDLE); $| = 1; select($oldfh); print HANDLE &quot;stuff $$\n&quot;; seek(HANDLE, 0, 0); print &quot;File contains: &quot;, &lt;HANDLE&gt;;</code></pre> <p>Using the constructor from the <code>IO::Handle</code> package (or one of its subclasses, such as <code>IO::File</code> or <code>IO::Socket</code>), you can generate anonymous filehandles that have the scope of whatever variables hold references to them, and automatically close whenever and however you leave that scope:</p> <pre><code> use IO::File; #... sub read_myfile_munged { my $ALL = shift; my $handle = IO::File-&gt;new; open($handle, &quot;myfile&quot;) or die &quot;myfile: $!&quot;; $first = &lt;$handle&gt; or return (); # Automatically closed here. mung $first or die &quot;mung failed&quot;; # Or here. return $first, &lt;$handle&gt; if $ALL; # Or here. $first; # Or here. }</code></pre> <p>See <a href="/5.8.9/functions/seek">&quot;seek&quot;</a> for some details about mixing reading and writing.</p> </dd> </dl> </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