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 ':encoding(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
34 layers of output streams. With the "IO" subpragma you can control
35 both 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
40 environment 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 ':encoding(utf8)';
55 use open IO => ':encoding(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 When open() is given an explicit list of layers (with the three-arg
72 syntax), they override the list declared using this pragma. open() can
73 also be given a single colon (:) for a layer name, to override this
74 pragma and use the default (":raw" on Unix, ":crlf" on Windows).
75
76 The ":std" subpragma on its own has no effect, but if combined with the
77 ":utf8" or ":encoding" subpragmas, it converts the standard filehandles
78 (STDIN, STDOUT, STDERR) to comply with encoding selected for
79 input/output handles. For example, if both input and out are chosen to
80 be ":encoding(utf8)", a ":std" will mean that STDIN, STDOUT, and STDERR
81 are also in ":encoding(utf8)". On the other hand, if only output is
82 chosen to be in ":encoding(koi8r)", a ":std" will cause only the STDOUT
83 and STDERR to be in "koi8r". The ":locale" subpragma implicitly turns
84 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,
99 including Windows). These two layers are no-ops on platforms where
100 binmode() is a no-op, but perform their functions everywhere if PerlIO
101 is 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
110 "PerlIO::Layer" which is created by the C code in perlio.c. As yet
111 there is nothing useful you can do with the object at the perl level.
112
114 "binmode" in perlfunc, "open" in perlfunc, perlunicode, PerlIO,
115 encoding
116
117
118
119perl v5.16.3 2013-03-04 open(3pm)