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