{
    "mode": "perldoc",
    "parameter": "open",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/open/json",
    "generated": "2026-06-03T02:31:43Z",
    "synopsis": "use open IN  => ':crlf', OUT => ':raw';\nopen my $in, '<', 'foo.txt' or die \"open failed: $!\";\nmy $line = <$in>; # CRLF translated\nclose $in;\nopen my $out, '>', 'bar.txt' or die \"open failed: $!\";\nprint $out $line; # no translation of bytes\nclose $out;\nuse open OUT => ':encoding(UTF-8)';\nuse open IN  => ':encoding(iso-8859-7)';\nuse open IO  => ':locale';\n# IO implicit only for :utf8, :encoding, :locale\nuse open ':encoding(UTF-8)';\nuse open ':encoding(iso-8859-7)';\nuse open ':locale';\n# with :std, also affect global standard handles\nuse open ':std', ':encoding(UTF-8)';\nuse open ':std', OUT => ':encoding(cp1252)';\nuse open ':std', IO => ':raw :encoding(UTF-16LE)';",
    "sections": {
        "NAME": {
            "content": "open - perl pragma to set default PerlIO layers for input and output\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use open IN  => ':crlf', OUT => ':raw';\nopen my $in, '<', 'foo.txt' or die \"open failed: $!\";\nmy $line = <$in>; # CRLF translated\nclose $in;\nopen my $out, '>', 'bar.txt' or die \"open failed: $!\";\nprint $out $line; # no translation of bytes\nclose $out;\n\nuse open OUT => ':encoding(UTF-8)';\nuse open IN  => ':encoding(iso-8859-7)';\n\nuse open IO  => ':locale';\n\n# IO implicit only for :utf8, :encoding, :locale\nuse open ':encoding(UTF-8)';\nuse open ':encoding(iso-8859-7)';\nuse open ':locale';\n\n# with :std, also affect global standard handles\nuse open ':std', ':encoding(UTF-8)';\nuse open ':std', OUT => ':encoding(cp1252)';\nuse open ':std', IO => ':raw :encoding(UTF-16LE)';\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Full-fledged support for I/O layers is now implemented provided Perl is configured to use PerlIO\nas its IO system (which has been the default since 5.8, and the only supported configuration\nsince 5.16).\n\nThe \"open\" pragma serves as one of the interfaces to declare default \"layers\" (previously known\nas \"disciplines\") for all I/O. Any open(), readpipe() (aka qx//) and similar operators found\nwithin the lexical scope of this pragma will use the declared defaults via the \"${^OPEN}\"\nvariable.\n\nLayers are specified with a leading colon by convention. You can specify a stack of multiple\nlayers as a space-separated string. See PerlIO for more information on the available layers.\n\nWith the \"IN\" subpragma you can declare the default layers of input streams, and with the \"OUT\"\nsubpragma you can declare the default layers of output streams. With the \"IO\" subpragma (may be\nomitted for \":utf8\", \":locale\", or \":encoding\") you can control both input and output streams\nsimultaneously.\n\nWhen open() is given an explicit list of layers (with the three-arg syntax), they override the\nlist declared using this pragma. open() can also be given a single colon (:) for a layer name,\nto override this pragma and use the default as detailed in \"Defaults and how to override them\"\nin PerlIO.\n\nTo translate from and to an arbitrary text encoding, use the \":encoding\" layer. The matching of\nencoding names in \":encoding\" is loose: case does not matter, and many encodings have several\naliases. See Encode::Supported for details and the list of supported locales.\n\nIf you want to set your encoding layers based on your locale environment variables, you can use\nthe \":locale\" pseudo-layer. For example:\n\n$ENV{LANG} = 'ruRU.KOI8-R';\n# the :locale will probe the locale environment variables like LANG\nuse open OUT => ':locale';\nopen(my $out, '>', 'koi8') or die \"open failed: $!\";\nprint $out chr(0x430); # CYRILLIC SMALL LETTER A = KOI8-R 0xc1\nclose $out;\nopen(my $in, '<', 'koi8') or die \"open failed: $!\";\nprintf \"%#x\\n\", ord(<$in>); # this should print 0xc1\nclose $in;\n\nThe logic of \":locale\" is described in full in \"The \":locale\" sub-pragma\" in encoding, but in\nshort it is first trying nllanginfo(CODESET) and then guessing from the LCALL and LANG locale\nenvironment variables. \":locale\" also implicitly turns on \":std\".\n\n\":std\" is not a layer but an additional subpragma. When specified in the import list, it\nactivates an additional functionality of pushing the layers selected for input/output handles to\nthe standard filehandles (STDIN, STDOUT, STDERR). If the new layers and existing layer stack\nboth end with an \":encoding\" layer, the existing \":encoding\" layer will also be removed.\n\nFor example, if both input and out are chosen to be \":encoding(UTF-8)\", a \":std\" will mean that\nSTDIN, STDOUT, and STDERR will also have \":encoding(UTF-8)\" set. On the other hand, if only\noutput is chosen to be in \":encoding(koi8r)\", a \":std\" will cause only the STDOUT and STDERR to\nbe in \"koi8r\".\n\nThe effect of \":std\" is not lexical as it modifies the layer stack of the global handles. If you\nwish to apply only this global effect and not the effect on handles that are opened in that\nscope, you can isolate the call to this pragma in its own lexical scope.\n\n{ use open ':std', IO => ':encoding(UTF-8)' }\n",
            "subsections": []
        },
        "IMPLEMENTATION DETAILS": {
            "content": "There is a class method in \"PerlIO::Layer\" \"find\" which is implemented as XS code. It is called\nby \"import\" to validate the layers:\n\nPerlIO::Layer::->find(\"perlio\")\n\nThe return value (if defined) is a Perl object, of class \"PerlIO::Layer\" which is created by the\nC code in perlio.c. As yet there is nothing useful you can do with the object at the perl level.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "\"binmode\" in perlfunc, \"open\" in perlfunc, perlunicode, PerlIO, encoding\n",
            "subsections": []
        }
    },
    "summary": "open - perl pragma to set default PerlIO layers for input and output",
    "flags": [],
    "examples": [],
    "see_also": [],
    "tldr": {
        "source": "official",
        "description": "`open` can refer to multiple commands with the same name.",
        "examples": [
            {
                "description": "View documentation for the command available in macOS",
                "command": "tldr open {{-p|--platform}} osx"
            },
            {
                "description": "View documentation for the command available through fish",
                "command": "tldr open.fish"
            }
        ]
    }
}