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