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