Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "PerlIO::via 3" | |
132 | .TH PerlIO::via 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | PerlIO::via \- Helper class for PerlIO layers implemented in perl | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 2 | |
138 | \& use PerlIO::via::Layer; | |
139 | \& open($fh,"<:via(Layer)",...); | |
140 | .Ve | |
141 | .PP | |
142 | .Vb 2 | |
143 | \& use Some::Other::Package; | |
144 | \& open($fh,">:via(Some::Other::Package)",...); | |
145 | .Ve | |
146 | .SH "DESCRIPTION" | |
147 | .IX Header "DESCRIPTION" | |
148 | The PerlIO::via module allows you to develop PerlIO layers in Perl, without | |
149 | having to go into the nitty gritty of programming C with \s-1XS\s0 as the interface | |
150 | to Perl. | |
151 | .PP | |
152 | One example module, PerlIO::via::QuotedPrint, is included with Perl | |
153 | 5.8.0, and more example modules are available from \s-1CPAN\s0, such as | |
154 | PerlIO::via::StripHTML and PerlIO::via::Base64. The | |
155 | PerlIO::via::StripHTML module for instance, allows you to say: | |
156 | .PP | |
157 | .Vb 3 | |
158 | \& use PerlIO::via::StripHTML; | |
159 | \& open( my $fh, "<:via(StripHTML)", "index.html" ); | |
160 | \& my @line = <$fh>; | |
161 | .Ve | |
162 | .PP | |
163 | to obtain the text of an HTML-file in an array with all the HTML-tags | |
164 | automagically removed. | |
165 | .PP | |
166 | Please note that if the layer is created in the PerlIO::via:: namespace, it | |
167 | does \fBnot\fR have to be fully qualified. The PerlIO::via module will prefix | |
168 | the PerlIO::via:: namespace if the specified modulename does not exist as a | |
169 | fully qualified module name. | |
170 | .SH "EXPECTED METHODS" | |
171 | .IX Header "EXPECTED METHODS" | |
172 | To create a Perl module that implements a PerlIO layer in Perl (as opposed to | |
173 | in C using \s-1XS\s0 as the interface to Perl), you need to supply some of the | |
174 | following subroutines. It is recommended to create these Perl modules in the | |
175 | PerlIO::via:: namespace, so that they can easily be located on \s-1CPAN\s0 and use | |
176 | the default namespace feature of the PerlIO::via module itself. | |
177 | .PP | |
178 | Please note that this is an area of recent development in Perl and that the | |
179 | interface described here is therefore still subject to change (and hopefully | |
180 | will have better documentation and more examples). | |
181 | .PP | |
182 | In the method descriptions below \fI$fh\fR will be | |
183 | a reference to a glob which can be treated as a perl file handle. | |
184 | It refers to the layer below. \fI$fh\fR is not passed if the layer | |
185 | is at the bottom of the stack, for this reason and to maintain | |
186 | some level of \*(L"compatibility\*(R" with \s-1TIEHANDLE\s0 classes it is passed last. | |
187 | .IP "$class\->\s-1PUSHED\s0([$mode[,$fh]])" 4 | |
188 | .IX Item "$class->PUSHED([$mode[,$fh]])" | |
189 | Should return an object or the class, or \-1 on failure. (Compare | |
190 | \&\s-1TIEHANDLE\s0.) The arguments are an optional mode string (\*(L"r\*(R", \*(L"w\*(R", | |
191 | \&\*(L"w+\*(R", ...) and a filehandle for the PerlIO layer below. Mandatory. | |
192 | .Sp | |
193 | When layer is pushed as part of an \f(CW\*(C`open\*(C'\fR call, \f(CW\*(C`PUSHED\*(C'\fR will be called | |
194 | \&\fIbefore\fR the actual open occurs whether than be via \f(CW\*(C`OPEN\*(C'\fR, \f(CW\*(C`SYSOPEN\*(C'\fR, | |
195 | \&\f(CW\*(C`FDOPEN\*(C'\fR or by letting lower layer do the open. | |
196 | .IP "$obj\->\s-1POPPED\s0([$fh])" 4 | |
197 | .IX Item "$obj->POPPED([$fh])" | |
198 | Optional \- layer is about to be removed. | |
199 | .IP "$obj\->\s-1UTF8\s0($bellowFlag,[$fh])" 4 | |
200 | .IX Item "$obj->UTF8($bellowFlag,[$fh])" | |
201 | Optional \- if present it will be called immediately after \s-1PUSHED\s0 has | |
202 | returned. It should return true value if the layer expects data to be | |
203 | \&\s-1UTF\-8\s0 encoded. If it returns true result is as if caller had done | |
204 | .Sp | |
205 | .Vb 1 | |
206 | \& ":via(YourClass):utf8" | |
207 | .Ve | |
208 | .Sp | |
209 | If not present of it it returns false, then stream is left with | |
210 | flag clear. | |
211 | The \fI$bellowFlag\fR argument will be true if there is a layer below | |
212 | and that layer was expecting \s-1UTF\-8\s0. | |
213 | .IP "$obj\->\s-1OPEN\s0($path,$mode[,$fh])" 4 | |
214 | .IX Item "$obj->OPEN($path,$mode[,$fh])" | |
215 | Optional \- if not present lower layer does open. | |
216 | If present called for normal opens after layer is pushed. | |
217 | This function is subject to change as there is no easy way | |
218 | to get lower layer to do open and then regain control. | |
219 | .IP "$obj\->\s-1BINMODE\s0([,$fh])" 4 | |
220 | .IX Item "$obj->BINMODE([,$fh])" | |
221 | Optional \- if not available layer is popped on binmode($fh) or when \f(CW\*(C`:raw\*(C'\fR | |
222 | is pushed. If present it should return 0 on success \-1 on error and undef | |
223 | to pop the layer. | |
224 | .IP "$obj\->\s-1FDOPEN\s0($fd[,$fh])" 4 | |
225 | .IX Item "$obj->FDOPEN($fd[,$fh])" | |
226 | Optional \- if not present lower layer does open. | |
227 | If present called for opens which pass a numeric file | |
228 | descriptor after layer is pushed. | |
229 | This function is subject to change as there is no easy way | |
230 | to get lower layer to do open and then regain control. | |
231 | .IP "$obj\->\s-1SYSOPEN\s0($path,$imode,$perm,[,$fh])" 4 | |
232 | .IX Item "$obj->SYSOPEN($path,$imode,$perm,[,$fh])" | |
233 | Optional \- if not present lower layer does open. | |
234 | If present called for sysopen style opens which pass a numeric mode | |
235 | and permissions after layer is pushed. | |
236 | This function is subject to change as there is no easy way | |
237 | to get lower layer to do open and then regain control. | |
238 | .IP "$obj\->\s-1FILENO\s0($fh)" 4 | |
239 | .IX Item "$obj->FILENO($fh)" | |
240 | Returns a numeric value for Unix-like file descriptor. Return \-1 if | |
241 | there isn't one. Optional. Default is fileno($fh). | |
242 | .IP "$obj\->\s-1READ\s0($buffer,$len,$fh)" 4 | |
243 | .IX Item "$obj->READ($buffer,$len,$fh)" | |
244 | Returns the number of octets placed in \f(CW$buffer\fR (must be less than or | |
245 | equal to \f(CW$len\fR). Optional. Default is to use \s-1FILL\s0 instead. | |
246 | .IP "$obj\->\s-1WRITE\s0($buffer,$fh)" 4 | |
247 | .IX Item "$obj->WRITE($buffer,$fh)" | |
248 | Returns the number of octets from buffer that have been successfully written. | |
249 | .IP "$obj\->\s-1FILL\s0($fh)" 4 | |
250 | .IX Item "$obj->FILL($fh)" | |
251 | Should return a string to be placed in the buffer. Optional. If not | |
252 | provided must provide \s-1READ\s0 or reject handles open for reading in | |
253 | \&\s-1PUSHED\s0. | |
254 | .IP "$obj\->\s-1CLOSE\s0($fh)" 4 | |
255 | .IX Item "$obj->CLOSE($fh)" | |
256 | Should return 0 on success, \-1 on error. | |
257 | Optional. | |
258 | .IP "$obj\->\s-1SEEK\s0($posn,$whence,$fh)" 4 | |
259 | .IX Item "$obj->SEEK($posn,$whence,$fh)" | |
260 | Should return 0 on success, \-1 on error. | |
261 | Optional. Default is to fail, but that is likely to be changed | |
262 | in future. | |
263 | .IP "$obj\->\s-1TELL\s0($fh)" 4 | |
264 | .IX Item "$obj->TELL($fh)" | |
265 | Returns file postion. | |
266 | Optional. Default to be determined. | |
267 | .IP "$obj\->\s-1UNREAD\s0($buffer,$fh)" 4 | |
268 | .IX Item "$obj->UNREAD($buffer,$fh)" | |
269 | Returns the number of octets from buffer that have been successfully | |
270 | saved to be returned on future \s-1FILL/READ\s0 calls. Optional. Default is | |
271 | to push data into a temporary layer above this one. | |
272 | .IP "$obj\->\s-1FLUSH\s0($fh)" 4 | |
273 | .IX Item "$obj->FLUSH($fh)" | |
274 | Flush any buffered write data. May possibly be called on readable | |
275 | handles too. Should return 0 on success, \-1 on error. | |
276 | .IP "$obj\->\s-1SETLINEBUF\s0($fh)" 4 | |
277 | .IX Item "$obj->SETLINEBUF($fh)" | |
278 | Optional. No return. | |
279 | .IP "$obj\->\s-1CLEARERR\s0($fh)" 4 | |
280 | .IX Item "$obj->CLEARERR($fh)" | |
281 | Optional. No return. | |
282 | .IP "$obj\->\s-1ERROR\s0($fh)" 4 | |
283 | .IX Item "$obj->ERROR($fh)" | |
284 | Optional. Returns error state. Default is no error until a mechanism | |
285 | to signal error (die?) is worked out. | |
286 | .IP "$obj\->\s-1EOF\s0($fh)" 4 | |
287 | .IX Item "$obj->EOF($fh)" | |
288 | Optional. Returns end-of-file state. Default is function of return | |
289 | value of \s-1FILL\s0 or \s-1READ\s0. | |
290 | .SH "EXAMPLES" | |
291 | .IX Header "EXAMPLES" | |
292 | Check the PerlIO::via:: namespace on \s-1CPAN\s0 for examples of PerlIO layers | |
293 | implemented in Perl. To give you an idea how simple the implementation of | |
294 | a PerlIO layer can look, as simple example is included here. | |
295 | .Sh "Example \- a Hexadecimal Handle" | |
296 | .IX Subsection "Example - a Hexadecimal Handle" | |
297 | Given the following module, PerlIO::via::Hex : | |
298 | .PP | |
299 | .Vb 1 | |
300 | \& package PerlIO::via::Hex; | |
301 | .Ve | |
302 | .PP | |
303 | .Vb 7 | |
304 | \& sub PUSHED | |
305 | \& { | |
306 | \& my ($class,$mode,$fh) = @_; | |
307 | \& # When writing we buffer the data | |
308 | \& my $buf = ''; | |
309 | \& return bless \e$buf,$class; | |
310 | \& } | |
311 | .Ve | |
312 | .PP | |
313 | .Vb 6 | |
314 | \& sub FILL | |
315 | \& { | |
316 | \& my ($obj,$fh) = @_; | |
317 | \& my $line = <$fh>; | |
318 | \& return (defined $line) ? pack("H*", $line) : undef; | |
319 | \& } | |
320 | .Ve | |
321 | .PP | |
322 | .Vb 6 | |
323 | \& sub WRITE | |
324 | \& { | |
325 | \& my ($obj,$buf,$fh) = @_; | |
326 | \& $$obj .= unpack("H*", $buf); | |
327 | \& return length($buf); | |
328 | \& } | |
329 | .Ve | |
330 | .PP | |
331 | .Vb 7 | |
332 | \& sub FLUSH | |
333 | \& { | |
334 | \& my ($obj,$fh) = @_; | |
335 | \& print $fh $$obj or return -1; | |
336 | \& $$obj = ''; | |
337 | \& return 0; | |
338 | \& } | |
339 | .Ve | |
340 | .PP | |
341 | .Vb 1 | |
342 | \& 1; | |
343 | .Ve | |
344 | .PP | |
345 | the following code opens up an output handle that will convert any | |
346 | output to hexadecimal dump of the output bytes: for example \*(L"A\*(R" will | |
347 | be converted to \*(L"41\*(R" (on ASCII-based machines, on \s-1EBCDIC\s0 platforms | |
348 | the \*(L"A\*(R" will become \*(L"c1\*(R") | |
349 | .PP | |
350 | .Vb 2 | |
351 | \& use PerlIO::via::Hex; | |
352 | \& open(my $fh, ">:via(Hex)", "foo.hex"); | |
353 | .Ve | |
354 | .PP | |
355 | and the following code will read the hexdump in and convert it | |
356 | on the fly back into bytes: | |
357 | .PP | |
358 | .Vb 1 | |
359 | \& open(my $fh, "<:via(Hex)", "foo.hex"); | |
360 | .Ve |