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