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