"Clean" implementation of binmode(FH)/":raw" identity.
[p5sagit/p5-mst-13.2.git] / ext / PerlIO / Via / Via.pm
CommitLineData
e7a1fdd7 1package PerlIO::Via;
2our $VERSION = '0.01';
3use XSLoader ();
4XSLoader::load 'PerlIO::Via';
51;
6__END__
7
8=head1 NAME
9
10PerlIO::Via - Helper class for PerlIO layers implemented in perl
11
12=head1 SYNOPSIS
13
14 use Some::Package;
15
16 open($fh,"<:Via(Some::Package)",...);
17
18=head1 DESCRIPTION
19
20The package to be used as a layer should implement at least some of the
21following methods. In the method descriptions below I<$fh> will be
22a reference to a glob which can be treated as a perl file handle.
23It refers to the layer below. I<$fh> is not passed if the layer
24is at the bottom of the stack, for this reason and to maintain
2b8740d8 25some level of "compatibility" with TIEHANDLE classes it is passed last.
26
f74ea477 27As an example, in Perl release 5.8.0 the included MIME::QuotedPrint
28module defines the required TIEHANDLE methods so that you can say
2b8740d8 29
30 use MIME::QuotedPrint;
31 open(my $fh, ">Via(MIME::QuotedPrint)", "qp");
e7a1fdd7 32
33=over 4
34
7d61c391 35=item $class->PUSHED([$mode[,$fh]])
e7a1fdd7 36
7d61c391 37Should return an object or the class, or -1 on failure. (Compare
38TIEHANDLE.) The arguments are an optional mode string ("r", "w",
39"w+", ...) and a filehandle for the PerlIO layer below. Mandatory.
e7a1fdd7 40
41=item $obj->POPPED([$fh])
42
43Optional - layer is about to be removed.
44
45=item $class->OPEN($path,$mode[,$fh])
46
47Not yet in use.
48
86e05cf2 49=item $obj->BINMODE([,$fh])
50
51Optional - if not available layer is popped on binmode($fh) or when C<:raw>
52is pushed. If present it should return 0 on success -1 on error and undef
53to pop the layer.
54
e7a1fdd7 55=item $class->FDOPEN($fd)
56
57Not yet in use.
58
59=item $class->SYSOPEN($path,$imode,$perm,$fh)
60
61Not yet in use.
62
63=item $obj->FILENO($fh)
64
47bfe92f 65Returns a numeric value for Unix-like file descriptor. Return -1 if
66there isn't one. Optional. Default is fileno($fh).
e7a1fdd7 67
68=item $obj->READ($buffer,$len,$fh)
69
6391fff2 70Returns the number of octets placed in $buffer (must be less than or
71equal to $len). Optional. Default is to use FILL instead.
e7a1fdd7 72
73=item $obj->WRITE($buffer,$fh)
74
75Returns the number of octets from buffer that have been sucessfully written.
76
77=item $obj->FILL($fh)
78
47bfe92f 79Should return a string to be placed in the buffer. Optional. If not
80provided must provide READ or reject handles open for reading in
81PUSHED.
e7a1fdd7 82
83=item $obj->CLOSE($fh)
84
85Should return 0 on success, -1 on error.
86Optional.
87
88=item $obj->SEEK($posn,$whence,$fh)
89
90Should return 0 on success, -1 on error.
f74ea477 91Optional. Default is to fail, but that is likely to be changed
92in future.
e7a1fdd7 93
94=item $obj->TELL($fh)
95
96Returns file postion.
f74ea477 97Optional. Default to be determined.
e7a1fdd7 98
99=item $obj->UNREAD($buffer,$fh)
100
47bfe92f 101Returns the number of octets from buffer that have been sucessfully
f74ea477 102saved to be returned on future FILL/READ calls. Optional. Default is
47bfe92f 103to push data into a temporary layer above this one.
e7a1fdd7 104
105=item $obj->FLUSH($fh)
106
47bfe92f 107Flush any buffered write data. May possibly be called on readable
108handles too. Should return 0 on success, -1 on error.
e7a1fdd7 109
110=item $obj->SETLINEBUF($fh)
111
112Optional. No return.
113
114=item $obj->CLEARERR($fh)
115
116Optional. No return.
117
118=item $obj->ERROR($fh)
119
120Optional. Returns error state. Default is no error until a mechanism
121to signal error (die?) is worked out.
122
123=item $obj->EOF($fh)
124
47bfe92f 125Optional. Returns end-of-file state. Default is function of return
126value of FILL or READ.
e7a1fdd7 127
128=back
129
a2ae6f84 130=head2 Example - a Hexadecimal Handle
131
132Given the following module, Hex.pm:
133
134 package Hex;
135
136 sub PUSHED
137 {
f74ea477 138 my ($class,$mode,$fh) = @_;
a2ae6f84 139 # When writing we buffer the data
f74ea477 140 my $buf = '';
141 return bless \$buf,$class;
a2ae6f84 142 }
143
144 sub FILL
145 {
146 my ($obj,$fh) = @_;
147 my $line = <$fh>;
148 return (defined $line) ? pack("H*", $line) : undef;
a2ae6f84 149 }
150
151 sub WRITE
152 {
153 my ($obj,$buf,$fh) = @_;
154 $$obj .= unpack("H*", $buf);
155 return length($buf);
156 }
157
158 sub FLUSH
159 {
160 my ($obj,$fh) = @_;
161 print $fh $$obj or return -1;
162 $$obj = '';
163 return 0;
164 }
165
166 1;
167
168the following code opens up an output handle that will convert any
169output to hexadecimal dump of the output bytes: for example "A" will
170be converted to "41" (on ASCII-based machines, on EBCDIC platforms
171the "A" will become "c1")
172
173 use Hex;
174 open(my $fh, ">:Via(Hex)", "foo.hex");
175
176and the following code will read the hexdump in and convert it
177on the fly back into bytes:
178
179 open(my $fh, "<:Via(Hex)", "foo.hex");
180
e7a1fdd7 181=cut
182
183