This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Changes.
[perl5.git] / lib / open.pm
... / ...
CommitLineData
1package open;
2use Carp;
3$open::hint_bits = 0x20000;
4
5our $VERSION = '1.01';
6
7my $locale_encoding;
8
9sub in_locale { $^H & $locale::hint_bits }
10
11sub _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
34sub 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
761;
77__END__
78
79=head1 NAME
80
81open - 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
89Full-fledged support for I/O disciplines is now implemented provided
90Perl is configured to use PerlIO as its IO system (which is now the
91default).
92
93The C<open> pragma serves as one of the interfaces to declare default
94"layers" (aka disciplines) for all I/O.
95
96The C<open> pragma is used to declare one or more default layers for
97I/O operations. Any open(), readpipe() (aka qx//) and similar
98operators found within the lexical scope of this pragma will use the
99declared defaults.
100
101When open() is given an explicit list of layers they are appended to
102the list declared using this pragma.
103
104Directory handles may also support disciplines in future.
105
106=head1 NONPERLIO FUNCTIONALITY
107
108If Perl is not built to use PerlIO as its IO system then only the two
109pseudo-disciplines ":raw" and ":crlf" are available.
110
111The ":raw" discipline corresponds to "binary mode" and the ":crlf"
112discipline corresponds to "text mode" on platforms that distinguish
113between the two modes when opening files (which is many DOS-like
114platforms, including Windows). These two disciplines are no-ops on
115platforms where binmode() is a no-op, but perform their functions
116everywhere if PerlIO is enabled.
117
118=head1 IMPLEMENTATION DETAILS
119
120There is a class method in C<PerlIO::Layer> C<find> which is implemented as XS code.
121It is called by C<import> to validate the layers:
122
123 PerlIO::Layer::->find("perlio")
124
125The return value (if defined) is a Perl object, of class C<PerlIO::Layer> which is
126created by the C code in F<perlio.c>. As yet there is nothing useful you can do with the
127object at the perl level.
128
129=head1 SEE ALSO
130
131L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<PerlIO>
132
133=cut