Add INOUT to control both ways at the same time.
[p5sagit/p5-mst-13.2.git] / lib / open.pm
1 package open;
2 use Carp;
3 $open::hint_bits = 0x20000;
4
5 our $VERSION = '1.01';
6
7 my $locale_encoding;
8
9 sub in_locale { $^H & $locale::hint_bits }
10
11 sub _get_locale_encoding {
12     unless (defined $locale_encoding) {
13         eval { use I18N::Langinfo qw(langinfo CODESET) };
14         unless ($@) {
15             $locale_encoding = langinfo(CODESET);
16         }
17         my $country_language;
18         if (not $locale_encoding && in_locale()) {
19             if ($ENV{LC_ALL} =~ /^([^.]+)\.([^.]+)$/) {
20                 ($country_language, $locale_encoding) = ($1, $2);
21             } elsif ($ENV{LANG} =~ /^([^.]+)\.([^.]+)$/) {
22                 ($country_language, $locale_encoding) = ($1, $2);
23             }
24         } else {
25             # Could do heuristics based on the country and language
26             # parts of LC_ALL and LANG (the parts before the dot (if any)),
27             # since we have Locale::Country and Locale::Language available.
28             # TODO: get a database of Language -> Encoding mappings
29             # (the Estonian database would be excellent!)
30             # --jhi
31         }
32         if (defined $locale_encoding &&
33             $locale_encoding eq 'euc' &&
34             defined $country_language) {
35             if ($country_language =~ /^ja_JP|japan(?:ese)$/i) {
36                 $locale_encoding = 'eucjp';
37             } elsif ($country_language =~ /^ko_KR|korea(?:n)$/i) {
38                 $locale_encoding = 'euckr';
39             } elsif ($country_language =~ /^zh_TW|taiwan(?:ese)$/i) {
40                 $locale_encoding = 'euctw';
41             }
42             croak "Locale encoding 'euc' too ambiguous"
43                 if $locale_encoding eq 'euc';
44         }
45     }
46 }
47
48 sub import {
49     my ($class,@args) = @_;
50     croak("`use open' needs explicit list of disciplines") unless @args;
51     $^H |= $open::hint_bits;
52     my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
53     my @in  = split(/\s+/,$in);
54     my @out = split(/\s+/,$out);
55     while (@args) {
56         my $type = shift(@args);
57         my $discp = shift(@args);
58         my @val;
59         foreach my $layer (split(/\s+/,$discp)) {
60             $layer =~ s/^://;
61             if ($layer eq 'locale') {
62                 use Encode;
63                 _get_locale_encoding()
64                     unless defined $locale_encoding;
65                 croak "Cannot figure out an encoding to use"
66                     unless defined $locale_encoding;
67                 if ($locale_encoding =~ /^utf-?8$/i) {
68                     $layer = "utf8";
69                 } else {
70                     $layer = "encoding($locale_encoding)";
71                 }
72             }
73             unless(PerlIO::Layer::->find($layer)) {
74                 carp("Unknown discipline layer '$layer'");
75             }
76             push(@val,":$layer");
77             if ($layer =~ /^(crlf|raw)$/) {
78                 $^H{"open_$type"} = $layer;
79             }
80         }
81         if ($type eq 'IN') {
82             $in  = join(' ',@val);
83         }
84         elsif ($type eq 'OUT') {
85             $out = join(' ',@val);
86         }
87         elsif ($type eq 'INOUT') {
88             $in = $out = join(' ',@val);
89         }
90         else {
91             croak "Unknown discipline class '$type'";
92         }
93     }
94     ${^OPEN} = join('\0',$in,$out);
95 }
96
97 1;
98 __END__
99
100 =head1 NAME
101
102 open - perl pragma to set default disciplines for input and output
103
104 =head1 SYNOPSIS
105
106     use open IN => ":crlf", OUT => ":raw";
107     use open INOUT => ":utf8";
108
109 =head1 DESCRIPTION
110
111 Full-fledged support for I/O disciplines is now implemented provided
112 Perl is configured to use PerlIO as its IO system (which is now the
113 default).
114
115 The C<open> pragma serves as one of the interfaces to declare default
116 "layers" (aka disciplines) for all I/O.
117
118 The C<open> pragma is used to declare one or more default layers for
119 I/O operations.  Any open(), readpipe() (aka qx//) and similar
120 operators found within the lexical scope of this pragma will use the
121 declared defaults.
122
123 When open() is given an explicit list of layers they are appended to
124 the list declared using this pragma.
125
126 Directory handles may also support disciplines in future.
127
128 =head1 NONPERLIO FUNCTIONALITY
129
130 If Perl is not built to use PerlIO as its IO system then only the two
131 pseudo-disciplines ":raw" and ":crlf" are available.
132
133 The ":raw" discipline corresponds to "binary mode" and the ":crlf"
134 discipline corresponds to "text mode" on platforms that distinguish
135 between the two modes when opening files (which is many DOS-like
136 platforms, including Windows).  These two disciplines are no-ops on
137 platforms where binmode() is a no-op, but perform their functions
138 everywhere if PerlIO is enabled.
139
140 =head1 IMPLEMENTATION DETAILS
141
142 There is a class method in C<PerlIO::Layer> C<find> which is
143 implemented as XS code.  It is called by C<import> to validate the
144 layers:
145
146    PerlIO::Layer::->find("perlio")
147
148 The return value (if defined) is a Perl object, of class
149 C<PerlIO::Layer> which is created by the C code in F<perlio.c>.  As
150 yet there is nothing useful you can do with the object at the perl
151 level.
152
153 =head1 SEE ALSO
154
155 L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<PerlIO>
156
157 =cut