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
d1edabcf 1package open;
ac27b0f5 2use Carp;
16fe6d59
GS
3$open::hint_bits = 0x20000;
4
0c4f7ff0 5our $VERSION = '1.01';
b75c8c73 6
58d53262
JH
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
16fe6d59 34sub import {
dfebf958
NIS
35 my ($class,@args) = @_;
36 croak("`use open' needs explicit list of disciplines") unless @args;
16fe6d59 37 $^H |= $open::hint_bits;
ac27b0f5
NIS
38 my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
39 my @in = split(/\s+/,$in);
40 my @out = split(/\s+/,$out);
dfebf958
NIS
41 while (@args) {
42 my $type = shift(@args);
43 my $discp = shift(@args);
ac27b0f5 44 my @val;
dfebf958
NIS
45 foreach my $layer (split(/\s+/,$discp)) {
46 $layer =~ s/^://;
58d53262
JH
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 }
0c4f7ff0 55 unless(PerlIO::Layer::->find($layer)) {
dfebf958 56 carp("Unknown discipline layer '$layer'");
ac27b0f5
NIS
57 }
58 push(@val,":$layer");
59 if ($layer =~ /^(crlf|raw)$/) {
60 $^H{"open_$type"} = $layer;
16fe6d59 61 }
ac27b0f5
NIS
62 }
63 if ($type eq 'IN') {
64 $in = join(' ',@val);
65 }
66 elsif ($type eq 'OUT') {
67 $out = join(' ',@val);
16fe6d59
GS
68 }
69 else {
ac27b0f5 70 croak "Unknown discipline class '$type'";
16fe6d59
GS
71 }
72 }
ac27b0f5 73 ${^OPEN} = join('\0',$in,$out);
16fe6d59
GS
74}
75
761;
77__END__
d1edabcf
GS
78
79=head1 NAME
80
81open - perl pragma to set default disciplines for input and output
82
83=head1 SYNOPSIS
84
16fe6d59 85 use open IN => ":crlf", OUT => ":raw";
d1edabcf
GS
86
87=head1 DESCRIPTION
88
d151aa0e
JH
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).
16fe6d59 92
7d3b96bb
NIS
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
d151aa0e
JH
97I/O operations. Any open(), readpipe() (aka qx//) and similar
98operators found within the lexical scope of this pragma will use the
99declared defaults.
7d3b96bb 100
d151aa0e
JH
101When open() is given an explicit list of layers they are appended to
102the list declared using this pragma.
7d3b96bb
NIS
103
104Directory handles may also support disciplines in future.
105
106=head1 NONPERLIO FUNCTIONALITY
107
d151aa0e
JH
108If Perl is not built to use PerlIO as its IO system then only the two
109pseudo-disciplines ":raw" and ":crlf" are available.
16fe6d59
GS
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
d151aa0e
JH
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.
7d3b96bb
NIS
117
118=head1 IMPLEMENTATION DETAILS
d1edabcf 119
0c4f7ff0
NIS
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.
16fe6d59 128
d1edabcf
GS
129=head1 SEE ALSO
130
7d3b96bb 131L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<PerlIO>
d1edabcf
GS
132
133=cut