Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Method Resolution OrderPerl pragma to restrict unsafe ... (Berikutnya)
Pragmas

Perl pragma to set default PerlIO layers for input and output

Daftar Isi

NAME

open - perl pragma to set default PerlIO layers for input and output

SYNOPSIS

  1. use open IN => ":crlf", OUT => ":bytes";
  2. use open OUT => ':utf8';
  3. use open IO => ":encoding(iso-8859-7)";
  4. use open IO => ':locale';
  5. use open ':encoding(utf8)';
  6. use open ':locale';
  7. use open ':encoding(iso-8859-7)';
  8. use open ':std';

DESCRIPTION

Full-fledged support for I/O layers is now implemented providedPerl is configured to use PerlIO as its IO system (which is now thedefault).

The open pragma serves as one of the interfaces to declare default"layers" (also known as "disciplines") for all I/O. Any two-argumentopen(), readpipe() (aka qx//) and similar operators found within thelexical scope of this pragma will use the declared defaults.Even three-argument opens may be affected by this pragmawhen they don't specify IO layers in MODE.

With the IN subpragma you can declare the default layersof input streams, and with the OUT subpragma you can declarethe default layers of output streams. With the IO subpragmayou can control both input and output streams simultaneously.

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

If you want to set your encoding layers based on yourlocale environment variables, you can use the :locale tag.For example:

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

These are equivalent

  1. use open ':encoding(utf8)';
  2. use open IO => ':encoding(utf8)';

as are these

  1. use open ':locale';
  2. use open IO => ':locale';

and these

  1. use open ':encoding(iso-8859-7)';
  2. use open IO => ':encoding(iso-8859-7)';

The matching of encoding names is loose: case does not matter, andmany encodings have several aliases. See Encode::Supported fordetails and the list of supported locales.

When open() is given an explicit list of layers (with the three-argsyntax), they override the list declared using this pragma. open() canalso be given a single colon (:) for a layer name, to override this pragmaand use the default (:raw on Unix, :crlf on Windows).

The :std subpragma on its own has no effect, but if combined withthe :utf8 or :encoding subpragmas, it converts the standardfilehandles (STDIN, STDOUT, STDERR) to comply with encoding selectedfor input/output handles. For example, if both input and out arechosen to be :encoding(utf8), a :std will mean that STDIN, STDOUT,and STDERR are also in :encoding(utf8). On the other hand, if onlyoutput is chosen to be in :encoding(koi8r), a :std will causeonly the STDOUT and STDERR to be in koi8r. The :locale subpragmaimplicitly turns on :std.

The logic of :locale is described in full in encoding,but in short it is first trying nl_langinfo(CODESET) and thenguessing from the LC_ALL and LANG locale environment variables.

Directory handles may also support PerlIO layers in the future.

NONPERLIO FUNCTIONALITY

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

The :bytes layer corresponds to "binary mode" and the :crlflayer corresponds to "text mode" on platforms that distinguishbetween the two modes when opening files (which is many DOS-likeplatforms, including Windows). These two layers are no-ops onplatforms where binmode() is a no-op, but perform their functionseverywhere if PerlIO is enabled.

IMPLEMENTATION DETAILS

There is a class method in PerlIO::Layer find which isimplemented as XS code. It is called by import to validate thelayers:

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

The return value (if defined) is a Perl object, of classPerlIO::Layer which is created by the C code in perlio.c. Asyet there is nothing useful you can do with the object at the perllevel.

SEE ALSO

binmode, open, perlunicode, PerlIO,encoding

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Method Resolution OrderPerl pragma to restrict unsafe ... (Berikutnya)