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