3 $open::hint_bits = 0x20000;
9 sub in_locale { $^H & $locale::hint_bits }
11 sub _get_locale_encoding {
12 unless (defined $locale_encoding) {
13 eval { use I18N::Langinfo qw(langinfo CODESET) };
15 $locale_encoding = langinfo(CODESET);
17 if (not $locale_encoding && in_locale()) {
18 if ($ENV{LC_ALL} =~ /^[^.]+\.([^.]+)$/) {
19 $locale_encoding = $1;
20 } elsif ($ENV{LANG} =~ /^[^.]+\.([^.]+)$/) {
21 $locale_encoding = $1;
24 # Could do heuristics based on the country and language
25 # parts of LC_ALL and LANG (the parts before the dot (if any)),
26 # since we have Locale::Country and Locale::Language available.
27 # TODO: get a database of Language -> Encoding mappings
28 # (the Estonian database would be excellent!)
35 my ($class,@args) = @_;
36 croak("`use open' needs explicit list of disciplines") unless @args;
37 $^H |= $open::hint_bits;
38 my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
39 my @in = split(/\s+/,$in);
40 my @out = split(/\s+/,$out);
42 my $type = shift(@args);
43 my $discp = shift(@args);
45 foreach my $layer (split(/\s+/,$discp)) {
47 if ($layer eq 'locale') {
49 _get_locale_encoding()
50 unless defined $locale_encoding;
51 croak "Cannot figure out an encoding to use"
52 unless defined $locale_encoding;
53 $layer = "encoding($locale_encoding)";
55 unless(PerlIO::Layer::->find($layer)) {
56 carp("Unknown discipline layer '$layer'");
59 if ($layer =~ /^(crlf|raw)$/) {
60 $^H{"open_$type"} = $layer;
66 elsif ($type eq 'OUT') {
67 $out = join(' ',@val);
70 croak "Unknown discipline class '$type'";
73 ${^OPEN} = join('\0',$in,$out);
81 open - perl pragma to set default disciplines for input and output
85 use open IN => ":crlf", OUT => ":raw";
89 Full-fledged support for I/O disciplines is now implemented provided
90 Perl is configured to use PerlIO as its IO system (which is now the
93 The C<open> pragma serves as one of the interfaces to declare default
94 "layers" (aka disciplines) for all I/O.
96 The C<open> pragma is used to declare one or more default layers for
97 I/O operations. Any open(), readpipe() (aka qx//) and similar
98 operators found within the lexical scope of this pragma will use the
101 When open() is given an explicit list of layers they are appended to
102 the list declared using this pragma.
104 Directory handles may also support disciplines in future.
106 =head1 NONPERLIO FUNCTIONALITY
108 If Perl is not built to use PerlIO as its IO system then only the two
109 pseudo-disciplines ":raw" and ":crlf" are available.
111 The ":raw" discipline corresponds to "binary mode" and the ":crlf"
112 discipline corresponds to "text mode" on platforms that distinguish
113 between the two modes when opening files (which is many DOS-like
114 platforms, including Windows). These two disciplines are no-ops on
115 platforms where binmode() is a no-op, but perform their functions
116 everywhere if PerlIO is enabled.
118 =head1 IMPLEMENTATION DETAILS
120 There is a class method in C<PerlIO::Layer> C<find> which is implemented as XS code.
121 It is called by C<import> to validate the layers:
123 PerlIO::Layer::->find("perlio")
125 The return value (if defined) is a Perl object, of class C<PerlIO::Layer> which is
126 created by the C code in F<perlio.c>. As yet there is nothing useful you can do with the
127 object at the perl level.
131 L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<PerlIO>