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.
73
74 The ":std" subpragma on its own has no effect, but if combined with the
75 ":utf8" or ":encoding" subpragmas, it converts the standard filehandles
76 (STDIN, STDOUT, STDERR) to comply with encoding selected for
77 input/output handles. For example, if both input and out are chosen to
78 be ":encoding(utf8)", a ":std" will mean that STDIN, STDOUT, and STDERR
79 are also in ":encoding(utf8)". On the other hand, if only output is
80 chosen to be in ":encoding(koi8r)", a ":std" will cause only the STDOUT
81 and STDERR to be in "koi8r". The ":locale" subpragma implicitly turns
82 on ":std".
83
84 The logic of ":locale" is described in full in encoding, but in short
85 it is first trying nl_langinfo(CODESET) and then guessing from the
86 LC_ALL and LANG locale environment variables.
87
88 Directory handles may also support PerlIO layers in the future.
89
91 If Perl is not built to use PerlIO as its IO system then only the two
92 pseudo-layers ":bytes" and ":crlf" are available.
93
94 The ":bytes" layer corresponds to "binary mode" and the ":crlf" layer
95 corresponds to "text mode" on platforms that distinguish between the
96 two modes when opening files (which is many DOS-like platforms,
97 including Windows). These two layers are no-ops on platforms where
98 binmode() is a no-op, but perform their functions everywhere if PerlIO
99 is enabled.
100
102 There is a class method in "PerlIO::Layer" "find" which is implemented
103 as XS code. It is called by "import" to validate the layers:
104
105 PerlIO::Layer::->find("perlio")
106
107 The return value (if defined) is a Perl object, of class
108 "PerlIO::Layer" which is created by the C code in perlio.c. As yet
109 there is nothing useful you can do with the object at the perl level.
110
112 "binmode" in perlfunc, "open" in perlfunc, perlunicode, PerlIO,
113 encoding
114
115
116
117perl v5.10.1 2009-07-03 open(3pm)