1open(3pm) Perl Programmers Reference Guide open(3pm)
2
3
4
6 open - perl pragma to set default PerlIO layers for input and output
7
9 use open IN => ":crlf", OUT => ":bytes";
10 use open OUT => ':utf8';
11 use open IO => ":encoding(iso-8859-7)";
12
13 use open IO => ':locale';
14
15 use open ':utf8';
16 use open ':locale';
17 use open ':encoding(iso-8859-7)';
18
19 use open ':std';
20
22 Full-fledged support for I/O layers is now implemented provided Perl is
23 configured to use PerlIO as its IO system (which is now the default).
24
25 The "open" pragma serves as one of the interfaces to declare default
26 "layers" (also known as "disciplines") for all I/O. Any two-argument
27 open(), readpipe() (aka qx//) and similar operators found within the
28 lexical scope of this pragma will use the declared defaults. Even
29 three-argument opens may be affected by this pragma when they don't
30 specify IO layers in MODE.
31
32 With the "IN" subpragma you can declare the default layers of input
33 streams, and with the "OUT" subpragma you can declare the default lay‐
34 ers of output streams. With the "IO" subpragma you can control both
35 input and output streams simultaneously.
36
37 If you have a legacy encoding, you can use the ":encoding(...)" tag.
38
39 If you want to set your encoding layers based on your locale environ‐
40 ment variables, you can use the ":locale" tag. For example:
41
42 $ENV{LANG} = 'ru_RU.KOI8-R';
43 # the :locale will probe the locale environment variables like LANG
44 use open OUT => ':locale';
45 open(O, ">koi8");
46 print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
47 close O;
48 open(I, "<koi8");
49 printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
50 close I;
51
52 These are equivalent
53
54 use open ':utf8';
55 use open IO => ':utf8';
56
57 as are these
58
59 use open ':locale';
60 use open IO => ':locale';
61
62 and these
63
64 use open ':encoding(iso-8859-7)';
65 use open IO => ':encoding(iso-8859-7)';
66
67 The matching of encoding names is loose: case does not matter, and many
68 encodings have several aliases. See Encode::Supported for details and
69 the list of supported locales.
70
71 Note that ":utf8" PerlIO layer must always be specified exactly like
72 that, it is not subject to the loose matching of encoding names.
73
74 When open() is given an explicit list of layers (with the three-arg
75 syntax), they override the list declared using this pragma.
76
77 The ":std" subpragma on its own has no effect, but if combined with the
78 ":utf8" or ":encoding" subpragmas, it converts the standard filehandles
79 (STDIN, STDOUT, STDERR) to comply with encoding selected for input/out‐
80 put handles. For example, if both input and out are chosen to be
81 ":utf8", a ":std" will mean that STDIN, STDOUT, and STDERR are also in
82 ":utf8". On the other hand, if only output is chosen to be in ":encod‐
83 ing(koi8r)", a ":std" will cause only the STDOUT and STDERR to be in
84 "koi8r". The ":locale" subpragma implicitly turns on ":std".
85
86 The logic of ":locale" is described in full in encoding, but in short
87 it is first trying nl_langinfo(CODESET) and then guessing from the
88 LC_ALL and LANG locale environment variables.
89
90 Directory handles may also support PerlIO layers in the future.
91
93 If Perl is not built to use PerlIO as its IO system then only the two
94 pseudo-layers ":bytes" and ":crlf" are available.
95
96 The ":bytes" layer corresponds to "binary mode" and the ":crlf" layer
97 corresponds to "text mode" on platforms that distinguish between the
98 two modes when opening files (which is many DOS-like platforms, includ‐
99 ing Windows). These two layers are no-ops on platforms where binmode()
100 is a no-op, but perform their functions everywhere if PerlIO is
101 enabled.
102
104 There is a class method in "PerlIO::Layer" "find" which is implemented
105 as XS code. It is called by "import" to validate the layers:
106
107 PerlIO::Layer::->find("perlio")
108
109 The return value (if defined) is a Perl object, of class "Per‐
110 lIO::Layer" which is created by the C code in perlio.c. As yet there
111 is nothing useful you can do with the object at the perl level.
112
114 "binmode" in perlfunc, "open" in perlfunc, perlunicode, PerlIO, encod‐
115 ing
116
117
118
119perl v5.8.8 2001-09-21 open(3pm)