Integrate mainline
[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
49=item $class->FDOPEN($fd)
50
51Not yet in use.
52
53=item $class->SYSOPEN($path,$imode,$perm,$fh)
54
55Not yet in use.
56
57=item $obj->FILENO($fh)
58
47bfe92f 59Returns a numeric value for Unix-like file descriptor. Return -1 if
60there isn't one. Optional. Default is fileno($fh).
e7a1fdd7 61
62=item $obj->READ($buffer,$len,$fh)
63
6391fff2 64Returns the number of octets placed in $buffer (must be less than or
65equal to $len). Optional. Default is to use FILL instead.
e7a1fdd7 66
67=item $obj->WRITE($buffer,$fh)
68
69Returns the number of octets from buffer that have been sucessfully written.
70
71=item $obj->FILL($fh)
72
47bfe92f 73Should return a string to be placed in the buffer. Optional. If not
74provided must provide READ or reject handles open for reading in
75PUSHED.
e7a1fdd7 76
77=item $obj->CLOSE($fh)
78
79Should return 0 on success, -1 on error.
80Optional.
81
82=item $obj->SEEK($posn,$whence,$fh)
83
84Should return 0 on success, -1 on error.
f74ea477 85Optional. Default is to fail, but that is likely to be changed
86in future.
e7a1fdd7 87
88=item $obj->TELL($fh)
89
90Returns file postion.
f74ea477 91Optional. Default to be determined.
e7a1fdd7 92
93=item $obj->UNREAD($buffer,$fh)
94
47bfe92f 95Returns the number of octets from buffer that have been sucessfully
f74ea477 96saved to be returned on future FILL/READ calls. Optional. Default is
47bfe92f 97to push data into a temporary layer above this one.
e7a1fdd7 98
99=item $obj->FLUSH($fh)
100
47bfe92f 101Flush any buffered write data. May possibly be called on readable
102handles too. Should return 0 on success, -1 on error.
e7a1fdd7 103
104=item $obj->SETLINEBUF($fh)
105
106Optional. No return.
107
108=item $obj->CLEARERR($fh)
109
110Optional. No return.
111
112=item $obj->ERROR($fh)
113
114Optional. Returns error state. Default is no error until a mechanism
115to signal error (die?) is worked out.
116
117=item $obj->EOF($fh)
118
47bfe92f 119Optional. Returns end-of-file state. Default is function of return
120value of FILL or READ.
e7a1fdd7 121
122=back
123
a2ae6f84 124=head2 Example - a Hexadecimal Handle
125
126Given the following module, Hex.pm:
127
128 package Hex;
129
130 sub PUSHED
131 {
f74ea477 132 my ($class,$mode,$fh) = @_;
a2ae6f84 133 # When writing we buffer the data
f74ea477 134 my $buf = '';
135 return bless \$buf,$class;
a2ae6f84 136 }
137
138 sub FILL
139 {
140 my ($obj,$fh) = @_;
141 my $line = <$fh>;
142 return (defined $line) ? pack("H*", $line) : undef;
a2ae6f84 143 }
144
145 sub WRITE
146 {
147 my ($obj,$buf,$fh) = @_;
148 $$obj .= unpack("H*", $buf);
149 return length($buf);
150 }
151
152 sub FLUSH
153 {
154 my ($obj,$fh) = @_;
155 print $fh $$obj or return -1;
156 $$obj = '';
157 return 0;
158 }
159
160 1;
161
162the following code opens up an output handle that will convert any
163output to hexadecimal dump of the output bytes: for example "A" will
164be converted to "41" (on ASCII-based machines, on EBCDIC platforms
165the "A" will become "c1")
166
167 use Hex;
168 open(my $fh, ">:Via(Hex)", "foo.hex");
169
170and the following code will read the hexdump in and convert it
171on the fly back into bytes:
172
173 open(my $fh, "<:Via(Hex)", "foo.hex");
174
e7a1fdd7 175=cut
176
177