This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Lexical use open ... support:
[perl5.git] / lib / open.pm
CommitLineData
d1edabcf 1package open;
ac27b0f5 2use Carp;
16fe6d59
GS
3$open::hint_bits = 0x20000;
4
ac27b0f5
NIS
5use vars qw(%layers @layers);
6
7# Populate hash in non-PerlIO case
8%layers = (crlf => 1, raw => 0) unless (@layers);
9
16fe6d59
GS
10sub import {
11 shift;
12 die "`use open' needs explicit list of disciplines" unless @_;
13 $^H |= $open::hint_bits;
ac27b0f5
NIS
14 my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
15 my @in = split(/\s+/,$in);
16 my @out = split(/\s+/,$out);
16fe6d59
GS
17 while (@_) {
18 my $type = shift;
ac27b0f5
NIS
19 my $discp = shift;
20 my @val;
21 foreach my $layer (split(/\s+:?/,$discp)) {
22 unless(exists $layers{$layer}) {
23 croak "Unknown discipline layer '$layer'";
24 }
25 push(@val,":$layer");
26 if ($layer =~ /^(crlf|raw)$/) {
27 $^H{"open_$type"} = $layer;
16fe6d59 28 }
ac27b0f5
NIS
29 }
30 if ($type eq 'IN') {
31 $in = join(' ',@val);
32 }
33 elsif ($type eq 'OUT') {
34 $out = join(' ',@val);
16fe6d59
GS
35 }
36 else {
ac27b0f5 37 croak "Unknown discipline class '$type'";
16fe6d59
GS
38 }
39 }
ac27b0f5 40 ${^OPEN} = join('\0',$in,$out);
16fe6d59
GS
41}
42
431;
44__END__
d1edabcf
GS
45
46=head1 NAME
47
48open - perl pragma to set default disciplines for input and output
49
50=head1 SYNOPSIS
51
16fe6d59 52 use open IN => ":crlf", OUT => ":raw";
d1edabcf
GS
53
54=head1 DESCRIPTION
55
d1edabcf 56The open pragma is used to declare one or more default disciplines for
16fe6d59
GS
57I/O operations. Any open() and readpipe() (aka qx//) operators found
58within the lexical scope of this pragma will use the declared defaults.
59Neither open() with an explicit set of disciplines, nor sysopen() are
642f9deb 60influenced by this pragma.
16fe6d59
GS
61
62Only the two pseudo-disciplines ":raw" and ":crlf" are currently
63available.
64
65The ":raw" discipline corresponds to "binary mode" and the ":crlf"
66discipline corresponds to "text mode" on platforms that distinguish
67between the two modes when opening files (which is many DOS-like
68platforms, including Windows). These two disciplines are currently
69no-ops on platforms where binmode() is a no-op, but will be
70supported everywhere in future.
d1edabcf 71
16fe6d59 72=head1 UNIMPLEMENTED FUNCTIONALITY
d1edabcf 73
16fe6d59
GS
74Full-fledged support for I/O disciplines is currently unimplemented.
75When they are eventually supported, this pragma will serve as one of
76the interfaces to declare default disciplines for all I/O.
77
78In future, any default disciplines declared by this pragma will be
9bafe919 79available by the special discipline name ":DEFAULT", and could be used
16fe6d59
GS
80within handle constructors that allow disciplines to be specified.
81This would make it possible to stack new disciplines over the default
82ones.
d1edabcf 83
9bafe919 84 open FH, "<:para :DEFAULT", $file or die "can't open $file: $!";
d1edabcf 85
16fe6d59
GS
86Socket and directory handles will also support disciplines in
87future.
88
89Full support for I/O disciplines will enable all of the supported
90disciplines to work on all platforms.
91
d1edabcf
GS
92=head1 SEE ALSO
93
16fe6d59 94L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>
d1edabcf
GS
95
96=cut