Seth Woolley's Man Viewer

Manual for open - man 3 open

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

open(2,3,n)(3)                Perl Programmers Reference Guide                open(2,3,n)(3)



NAME
       open(2,3,n) - perl pragma to set(7,n,1 builtins) default PerlIO layers for input and output

SYNOPSIS
           use open(2,3,n) IN  => ":crlf", OUT => ":bytes";
           use open(2,3,n) OUT => ':utf8';
           use open(2,3,n) IO  => ":encoding(iso-8859-7)";

           use open(2,3,n) IO  => ':locale';

           use open(2,3,n) ':utf8';
           use open(2,3,n) ':locale';
           use open(2,3,n) ':encoding(iso-8859-7)';

           use open(2,3,n) ':std';

DESCRIPTION
       Full-fledged support for I/O layers is now implemented provided Perl is
       configured to use PerlIO as its IO system (which is now the default).

       The "open(2,3,n)" pragma serves as one of the interfaces to declare default
       "layers" (also known as "disciplines") for all I/O. Any two-argument
       open(2,3,n)(), readpipe() (aka qx//) and similar operators found within the
       lexical scope of this pragma will use the declared defaults.  Three-
       argument opens are not affected by this pragma since there you (can)
       explicitly specify the layers and are supposed to know what you are
       doing.

       With the "IN" subpragma you can declare the default layers of input
       streams, and with the "OUT" subpragma you can declare the default lay-
       ers of output streams.  With the "IO"  subpragma you can control both
       input and output streams simultaneously.

       If you have a legacy encoding(3,n), you can use the ":encoding(...)" tag.

       if(3,n) you want to set(7,n,1 builtins) your encoding(3,n) layers based on your locale(3,5,7) environ-
       ment variables, you can use the ":locale" tag.  For example:

           $ENV{LANG} = 'ru_RU.KOI8-R';
           # the :locale will probe the locale(3,5,7) environment variables like LANG
           use open(2,3,n) OUT => ':locale';
           open(2,3,n)(O, ">koi8");
           print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
           close(2,7,n) O;
           open(2,3,n)(I, "<koi8");
           printf(1,3,1 builtins) "%#x\n", ord(<I>), "\n"; # this should print 0xc1
           close(2,7,n) I;

       These are equivalent

           use open(2,3,n) ':utf8';
           use open(2,3,n) IO => ':utf8';

       as are these

           use open(2,3,n) ':locale';
           use open(2,3,n) IO => ':locale';

       and these

           use open(2,3,n) ':encoding(iso-8859-7)';
           use open(2,3,n) IO => ':encoding(iso-8859-7)';

       The matching of encoding(3,n) names is loose: case does not matter, and many
       encodings have several aliases.  See Encode::Supported for details and
       the list of supported locales.

       Note that ":utf8" PerlIO layer must always be specified exactly like
       that, it is not subject to the loose matching of encoding(3,n) names.

       When open(2,3,n)() is given an explicit list of layers they are appended to
       the list declared using this pragma.

       The ":std" subpragma on its own has no effect, but if(3,n) combined with the
       ":utf8" or ":encoding" subpragmas, it converts the standard filehandles
       (STDIN, STDOUT, STDERR) to comply with encoding(3,n) selected for input/out-
       put handles.  For example, if(3,n) both input and out are chosen to be
       ":utf8", a ":std" will mean that STDIN, STDOUT, and STDERR are also in(1,8)
       ":utf8".  On the other hand, if(3,n) only output is chosen to be in(1,8) ":encod-
       ing(koi8r)", a ":std" will cause only the STDOUT and STDERR to be in(1,8)
       "koi8r".  The ":locale" subpragma implicitly turns on ":std".

       The logic of ":locale" is as follows:

       1.  If the platform supports the langinfo(CODESET) interface, the code-
           set(7,n,1 builtins) returned is used as the default encoding(3,n) for the open(2,3,n) pragma.

       2.  If 1. didn't work but we are under the locale(3,5,7) pragma, the environ-
           ment variables LC_ALL and LANG (in(1,8) that order) are matched for
           encodings (the part after ".", if(3,n) any), and if(3,n) any found, that is
           used as the default encoding(3,n) for the open(2,3,n) pragma.

       3.  If 1. and 2. didn't work, the environment variables LC_ALL and LANG
           (in(1,8) that order) are matched for anything looking like UTF-8, and if(3,n)
           any found, ":utf8" is used as the default encoding(3,n) for the open(2,3,n)
           pragma.

       If your locale(3,5,7) environment variables (LC_ALL, LC_CTYPE, LANG) contain
       the strings 'UTF-8' or 'UTF8' (case-insensitive matching), the default
       encoding(3,n) of your STDIN, STDOUT, and STDERR, and of any subsequent file(1,n)
       open(2,3,n), is UTF-8.

       Directory handles may also support PerlIO layers in(1,8) the future.

NONPERLIO FUNCTIONALITY
       If Perl is not built to use PerlIO as its IO system then only the two
       pseudo-layers ":bytes" and ":crlf" are available.

       The ":bytes" layer corresponds to "binary mode" and the ":crlf" layer
       corresponds to "text mode" on platforms that distinguish between the
       two modes when opening files (which is many DOS-like platforms, includ-
       ing Windows).  These two layers are no-ops on platforms where binmode()
       is a no-op, but perform their functions everywhere if(3,n) PerlIO is
       enabled.

IMPLEMENTATION DETAILS
       There is a class method in(1,8) "PerlIO::Layer" "find" which is implemented
       as XS code.  It is called by "import" to validate the layers:

          PerlIO::Layer::->find("perlio")

       The return value (if(3,n) defined) is a Perl object, of class "Per-
       lIO::Layer" which is created by the C code in(1,8) perlio.c.  As yet there
       is nothing useful you can do with the object at the perl level.

SEE ALSO
       "binmode" in(1,8) perlfunc, "open(2,3,n)" in(1,8) perlfunc, perlunicode, PerlIO, encod-
       ing(3,n)



perl v5.8.5                       2001-09-21                           open(2,3,n)(3)

References for this manual (incoming links)