Add a pseudolayer ":locale" to the open pragma which
[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         if (not $locale_encoding && in_locale()) {
18             if ($ENV{LC_ALL} =~ /^[^.]+\.([^.]+)$/) {
19                 $locale_encoding = $1;
20             } elsif ($ENV{LANG} =~ /^[^.]+\.([^.]+)$/) {
21                 $locale_encoding = $1;
22             }
23         } else {
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!)
29             # --jhi
30         }
31     }
32 }
33
34 sub import {
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);
41     while (@args) {
42         my $type = shift(@args);
43         my $discp = shift(@args);
44         my @val;
45         foreach my $layer (split(/\s+/,$discp)) {
46             $layer =~ s/^://;
47             if ($layer eq 'locale') {
48                 use Encode;
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)";
54             }
55             unless(PerlIO::Layer::->find($layer)) {
56                 carp("Unknown discipline layer '$layer'");
57             }
58             push(@val,":$layer");
59             if ($layer =~ /^(crlf|raw)$/) {
60                 $^H{"open_$type"} = $layer;
61             }
62         }
63         if ($type eq 'IN') {
64             $in  = join(' ',@val);
65         }
66         elsif ($type eq 'OUT') {
67             $out = join(' ',@val);
68         }
69         else {
70             croak "Unknown discipline class '$type'";
71         }
72     }
73     ${^OPEN} = join('\0',$in,$out);
74 }
75
76 1;
77 __END__
78
79 =head1 NAME
80
81 open - perl pragma to set default disciplines for input and output
82
83 =head1 SYNOPSIS
84
85     use open IN => ":crlf", OUT => ":raw";
86
87 =head1 DESCRIPTION
88
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
91 default).
92
93 The C<open> pragma serves as one of the interfaces to declare default
94 "layers" (aka disciplines) for all I/O.
95
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
99 declared defaults.
100
101 When open() is given an explicit list of layers they are appended to
102 the list declared using this pragma.
103
104 Directory handles may also support disciplines in future.
105
106 =head1 NONPERLIO FUNCTIONALITY
107
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.
110
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.
117
118 =head1 IMPLEMENTATION DETAILS
119
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:
122
123    PerlIO::Layer::->find("perlio")
124
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.
128
129 =head1 SEE ALSO
130
131 L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<PerlIO>
132
133 =cut