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