| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "open 3perl" |
| 58 |
.TH open 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
open \- perl pragma to set default PerlIO layers for input and output |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 7 |
| 68 |
\& use open IN => \*(Aq:crlf\*(Aq, OUT => \*(Aq:raw\*(Aq; |
| 69 |
\& open my $in, \*(Aq<\*(Aq, \*(Aqfoo.txt\*(Aq or die "open failed: $!"; |
| 70 |
\& my $line = <$in>; # CRLF translated |
| 71 |
\& close $in; |
| 72 |
\& open my $out, \*(Aq>\*(Aq, \*(Aqbar.txt\*(Aq or die "open failed: $!"; |
| 73 |
\& print $out $line; # no translation of bytes |
| 74 |
\& close $out; |
| 75 |
\& |
| 76 |
\& use open OUT => \*(Aq:encoding(UTF\-8)\*(Aq; |
| 77 |
\& use open IN => \*(Aq:encoding(iso\-8859\-7)\*(Aq; |
| 78 |
\& |
| 79 |
\& use open IO => \*(Aq:locale\*(Aq; |
| 80 |
\& |
| 81 |
\& # IO implicit only for :utf8, :encoding, :locale |
| 82 |
\& use open \*(Aq:encoding(UTF\-8)\*(Aq; |
| 83 |
\& use open \*(Aq:encoding(iso\-8859\-7)\*(Aq; |
| 84 |
\& use open \*(Aq:locale\*(Aq; |
| 85 |
\& |
| 86 |
\& # with :std, also affect global standard handles |
| 87 |
\& use open \*(Aq:std\*(Aq, \*(Aq:encoding(UTF\-8)\*(Aq; |
| 88 |
\& use open \*(Aq:std\*(Aq, OUT => \*(Aq:encoding(cp1252)\*(Aq; |
| 89 |
\& use open \*(Aq:std\*(Aq, IO => \*(Aq:raw :encoding(UTF\-16LE)\*(Aq; |
| 90 |
.Ve |
| 91 |
.SH DESCRIPTION |
| 92 |
.IX Header "DESCRIPTION" |
| 93 |
Full-fledged support for I/O layers is now implemented provided |
| 94 |
Perl is configured to use PerlIO as its IO system (which has been the |
| 95 |
default since 5.8, and the only supported configuration since 5.16). |
| 96 |
.PP |
| 97 |
The \f(CW\*(C`open\*(C'\fR pragma serves as one of the interfaces to declare default |
| 98 |
"layers" (previously known as "disciplines") for all I/O. Any \fBopen()\fR, |
| 99 |
\&\fBreadpipe()\fR (aka qx//) and similar operators found within the |
| 100 |
lexical scope of this pragma will use the declared defaults via the |
| 101 |
\&\f(CW\*(C`${^OPEN}\*(C'\fR variable. |
| 102 |
.PP |
| 103 |
Layers are specified with a leading colon by convention. You can |
| 104 |
specify a stack of multiple layers as a space-separated string. |
| 105 |
See PerlIO for more information on the available layers. |
| 106 |
.PP |
| 107 |
With the \f(CW\*(C`IN\*(C'\fR subpragma you can declare the default layers |
| 108 |
of input streams, and with the \f(CW\*(C`OUT\*(C'\fR subpragma you can declare |
| 109 |
the default layers of output streams. With the \f(CW\*(C`IO\*(C'\fR subpragma |
| 110 |
(may be omitted for \f(CW\*(C`:utf8\*(C'\fR, \f(CW\*(C`:locale\*(C'\fR, or \f(CW\*(C`:encoding\*(C'\fR) you |
| 111 |
can control both input and output streams simultaneously. |
| 112 |
.PP |
| 113 |
When \fBopen()\fR is given an explicit list of layers (with the three-arg |
| 114 |
syntax), they override the list declared using this pragma. \fBopen()\fR can |
| 115 |
also be given a single colon (:) for a layer name, to override this pragma |
| 116 |
and use the default as detailed in |
| 117 |
"Defaults and how to override them" in PerlIO. |
| 118 |
.PP |
| 119 |
To translate from and to an arbitrary text encoding, use the \f(CW\*(C`:encoding\*(C'\fR |
| 120 |
layer. The matching of encoding names in \f(CW\*(C`:encoding\*(C'\fR is loose: case does |
| 121 |
not matter, and many encodings have several aliases. See |
| 122 |
Encode::Supported for details and the list of supported locales. |
| 123 |
.PP |
| 124 |
If you want to set your encoding layers based on your |
| 125 |
locale environment variables, you can use the \f(CW\*(C`:locale\*(C'\fR pseudo-layer. |
| 126 |
For example: |
| 127 |
.PP |
| 128 |
.Vb 9 |
| 129 |
\& $ENV{LANG} = \*(Aqru_RU.KOI8\-R\*(Aq; |
| 130 |
\& # the :locale will probe the locale environment variables like LANG |
| 131 |
\& use open OUT => \*(Aq:locale\*(Aq; |
| 132 |
\& open(my $out, \*(Aq>\*(Aq, \*(Aqkoi8\*(Aq) or die "open failed: $!"; |
| 133 |
\& print $out chr(0x430); # CYRILLIC SMALL LETTER A = KOI8\-R 0xc1 |
| 134 |
\& close $out; |
| 135 |
\& open(my $in, \*(Aq<\*(Aq, \*(Aqkoi8\*(Aq) or die "open failed: $!"; |
| 136 |
\& printf "%#x\en", ord(<$in>); # this should print 0xc1 |
| 137 |
\& close $in; |
| 138 |
.Ve |
| 139 |
.PP |
| 140 |
The logic of \f(CW\*(C`:locale\*(C'\fR is described in full in |
| 141 |
"The \f(CW\*(C`:locale\*(C'\fR sub-pragma" in encoding, |
| 142 |
but in short it is first trying nl_langinfo(CODESET) and then |
| 143 |
guessing from the LC_ALL and LANG locale environment variables. |
| 144 |
\&\f(CW\*(C`:locale\*(C'\fR also implicitly turns on \f(CW\*(C`:std\*(C'\fR. |
| 145 |
.PP |
| 146 |
\&\f(CW\*(C`:std\*(C'\fR is not a layer but an additional subpragma. When specified in the |
| 147 |
import list, it activates an additional functionality of pushing the |
| 148 |
layers selected for input/output handles to the standard filehandles |
| 149 |
(STDIN, STDOUT, STDERR). If the new layers and existing layer stack both |
| 150 |
end with an \f(CW\*(C`:encoding\*(C'\fR layer, the existing \f(CW\*(C`:encoding\*(C'\fR layer will also |
| 151 |
be removed. |
| 152 |
.PP |
| 153 |
For example, if both input and out are chosen to be \f(CW:encoding(UTF\-8)\fR, a |
| 154 |
\&\f(CW\*(C`:std\*(C'\fR will mean that STDIN, STDOUT, and STDERR will also have |
| 155 |
\&\f(CW:encoding(UTF\-8)\fR set. On the other hand, if only output is chosen to |
| 156 |
be in \f(CW:encoding(koi8r)\fR, a \f(CW\*(C`:std\*(C'\fR will cause only the STDOUT and STDERR |
| 157 |
to be in \f(CW\*(C`koi8r\*(C'\fR. |
| 158 |
.PP |
| 159 |
The effect of \f(CW\*(C`:std\*(C'\fR is not lexical as it modifies the layer stack of the |
| 160 |
global handles. If you wish to apply only this global effect and not the |
| 161 |
effect on handles that are opened in that scope, you can isolate the call |
| 162 |
to this pragma in its own lexical scope. |
| 163 |
.PP |
| 164 |
.Vb 1 |
| 165 |
\& { use open \*(Aq:std\*(Aq, IO => \*(Aq:encoding(UTF\-8)\*(Aq } |
| 166 |
.Ve |
| 167 |
.PP |
| 168 |
Before Perl 5.34, \f(CW\*(C`:std\*(C'\fR would only apply the first layer provided that is |
| 169 |
either \f(CW\*(C`:utf8\*(C'\fR or has a layer argument, e.g. \f(CW:encoding(UTF\-8)\fR. Since |
| 170 |
Perl 5.34 it will apply the same layer stack it provides to \f(CW\*(C`${^OPEN}\*(C'\fR. |
| 171 |
.SH "IMPLEMENTATION DETAILS" |
| 172 |
.IX Header "IMPLEMENTATION DETAILS" |
| 173 |
There is a class method in \f(CW\*(C`PerlIO::Layer\*(C'\fR \f(CW\*(C`find\*(C'\fR which is |
| 174 |
implemented as XS code. It is called by \f(CW\*(C`import\*(C'\fR to validate the |
| 175 |
layers: |
| 176 |
.PP |
| 177 |
.Vb 1 |
| 178 |
\& PerlIO::Layer::\->find("perlio") |
| 179 |
.Ve |
| 180 |
.PP |
| 181 |
The return value (if defined) is a Perl object, of class |
| 182 |
\&\f(CW\*(C`PerlIO::Layer\*(C'\fR which is created by the C code in \fIperlio.c\fR. As |
| 183 |
yet there is nothing useful you can do with the object at the perl |
| 184 |
level. |
| 185 |
.SH "SEE ALSO" |
| 186 |
.IX Header "SEE ALSO" |
| 187 |
"binmode" in perlfunc, "open" in perlfunc, perlunicode, PerlIO, |
| 188 |
encoding |