Debian Codegraph @

upstream/mageia-cauldron/man3pm/PerlIO::via.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "PerlIO::via 3pm"
61 .TH PerlIO::via 3pm 2023-12-22 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 PerlIO::via \- Helper class for PerlIO layers implemented in perl
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 2
71 \& use PerlIO::via::Layer;
72 \& open($fh,"<:via(Layer)",...);
73 \&
74 \& use Some::Other::Package;
75 \& open($fh,">:via(Some::Other::Package)",...);
76 .Ve
77 .SH DESCRIPTION
78 .IX Header "DESCRIPTION"
79 The PerlIO::via module allows you to develop PerlIO layers in Perl, without
80 having to go into the nitty gritty of programming C with XS as the interface
81 to Perl.
82 .PP
83 One example module, PerlIO::via::QuotedPrint, is included with Perl
84 5.8.0, and more example modules are available from CPAN, such as
85 PerlIO::via::StripHTML and PerlIO::via::Base64. The
86 PerlIO::via::StripHTML module for instance, allows you to say:
87 .PP
88 .Vb 3
89 \& use PerlIO::via::StripHTML;
90 \& open( my $fh, "<:via(StripHTML)", "index.html" );
91 \& my @line = <$fh>;
92 .Ve
93 .PP
94 to obtain the text of an HTML\-file in an array with all the HTML\-tags
95 automagically removed.
96 .PP
97 Please note that if the layer is created in the PerlIO::via:: namespace, it
98 does \fBnot\fR have to be fully qualified. The PerlIO::via module will prefix
99 the PerlIO::via:: namespace if the specified modulename does not exist as a
100 fully qualified module name.
101 .SH "EXPECTED METHODS"
102 .IX Header "EXPECTED METHODS"
103 To create a Perl module that implements a PerlIO layer in Perl (as opposed to
104 in C using XS as the interface to Perl), you need to supply some of the
105 following subroutines. It is recommended to create these Perl modules in the
106 PerlIO::via:: namespace, so that they can easily be located on CPAN and use
107 the default namespace feature of the PerlIO::via module itself.
108 .PP
109 Please note that this is an area of recent development in Perl and that the
110 interface described here is therefore still subject to change (and hopefully
111 will have better documentation and more examples).
112 .PP
113 In the method descriptions below \fR\f(CI$fh\fR\fI\fR will be
114 a reference to a glob which can be treated as a perl file handle.
115 It refers to the layer below. \fI\fR\f(CI$fh\fR\fI\fR is not passed if the layer
116 is at the bottom of the stack, for this reason and to maintain
117 some level of "compatibility" with TIEHANDLE classes it is passed last.
118 .ie n .IP $class\->PUSHED([$mode,[$fh]]) 4
119 .el .IP \f(CW$class\fR\->PUSHED([$mode,[$fh]]) 4
120 .IX Item "$class->PUSHED([$mode,[$fh]])"
121 Should return an object or the class, or \-1 on failure. (Compare
122 TIEHANDLE.) The arguments are an optional mode string ("r", "w",
123 "w+", ...) and a filehandle for the PerlIO layer below. Mandatory.
124 .Sp
125 When the layer is pushed as part of an \f(CW\*(C`open\*(C'\fR call, \f(CW\*(C`PUSHED\*(C'\fR will be called
126 \&\fIbefore\fR the actual open occurs, whether that be via \f(CW\*(C`OPEN\*(C'\fR, \f(CW\*(C`SYSOPEN\*(C'\fR,
127 \&\f(CW\*(C`FDOPEN\*(C'\fR or by letting a lower layer do the open.
128 .ie n .IP $obj\->POPPED([$fh]) 4
129 .el .IP \f(CW$obj\fR\->POPPED([$fh]) 4
130 .IX Item "$obj->POPPED([$fh])"
131 Optional \- called when the layer is about to be removed.
132 .ie n .IP $obj\->UTF8($belowFlag,[$fh]) 4
133 .el .IP \f(CW$obj\fR\->UTF8($belowFlag,[$fh]) 4
134 .IX Item "$obj->UTF8($belowFlag,[$fh])"
135 Optional \- if present it will be called immediately after PUSHED has
136 returned. It should return a true value if the layer expects data to be
137 UTF\-8 encoded. If it returns true, the result is as if the caller had done
138 .Sp
139 .Vb 1
140 \& ":via(YourClass):utf8"
141 .Ve
142 .Sp
143 If not present or if it returns false, then the stream is left with
144 the UTF\-8 flag clear.
145 The \fR\f(CI$belowFlag\fR\fI\fR argument will be true if there is a layer below
146 and that layer was expecting UTF\-8.
147 .ie n .IP $obj\->OPEN($path,$mode,[$fh]) 4
148 .el .IP \f(CW$obj\fR\->OPEN($path,$mode,[$fh]) 4
149 .IX Item "$obj->OPEN($path,$mode,[$fh])"
150 Optional \- if not present a lower layer does the open.
151 If present, called for normal opens after the layer is pushed.
152 This function is subject to change as there is no easy way
153 to get a lower layer to do the open and then regain control.
154 .ie n .IP $obj\->BINMODE([$fh]) 4
155 .el .IP \f(CW$obj\fR\->BINMODE([$fh]) 4
156 .IX Item "$obj->BINMODE([$fh])"
157 Optional \- if not present the layer is popped on binmode($fh) or when \f(CW\*(C`:raw\*(C'\fR
158 is pushed. If present it should return 0 on success, \-1 on error, or undef
159 to pop the layer.
160 .ie n .IP $obj\->FDOPEN($fd,[$fh]) 4
161 .el .IP \f(CW$obj\fR\->FDOPEN($fd,[$fh]) 4
162 .IX Item "$obj->FDOPEN($fd,[$fh])"
163 Optional \- if not present a lower layer does the open.
164 If present, called after the layer is pushed for opens which pass
165 a numeric file descriptor.
166 This function is subject to change as there is no easy way
167 to get a lower layer to do the open and then regain control.
168 .ie n .IP $obj\->SYSOPEN($path,$imode,$perm,[$fh]) 4
169 .el .IP \f(CW$obj\fR\->SYSOPEN($path,$imode,$perm,[$fh]) 4
170 .IX Item "$obj->SYSOPEN($path,$imode,$perm,[$fh])"
171 Optional \- if not present a lower layer does the open.
172 If present, called after the layer is pushed for sysopen style opens
173 which pass a numeric mode and permissions.
174 This function is subject to change as there is no easy way
175 to get a lower layer to do the open and then regain control.
176 .ie n .IP $obj\->FILENO($fh) 4
177 .el .IP \f(CW$obj\fR\->FILENO($fh) 4
178 .IX Item "$obj->FILENO($fh)"
179 Returns a numeric value for a Unix\-like file descriptor. Returns \-1 if
180 there isn\*(Aqt one. Optional. Default is fileno($fh).
181 .ie n .IP $obj\->READ($buffer,$len,$fh) 4
182 .el .IP \f(CW$obj\fR\->READ($buffer,$len,$fh) 4
183 .IX Item "$obj->READ($buffer,$len,$fh)"
184 Returns the number of octets placed in \f(CW$buffer\fR (must be undef to
185 indicate an error or a non\-negative integer less than or equal to the
186 minimum of \f(CW$len\fR and the length of the updated \f(CW$buffer\fR). Optional.
187 Default is to use FILL instead.
188 .ie n .IP $obj\->WRITE($buffer,$fh) 4
189 .el .IP \f(CW$obj\fR\->WRITE($buffer,$fh) 4
190 .IX Item "$obj->WRITE($buffer,$fh)"
191 Returns the number of octets from \f(CW$buffer\fR that have been successfully written.
192 .ie n .IP $obj\->FILL($fh) 4
193 .el .IP \f(CW$obj\fR\->FILL($fh) 4
194 .IX Item "$obj->FILL($fh)"
195 Should return a string to be placed in the buffer. Optional. If not
196 provided, must provide READ or reject handles open for reading in
197 PUSHED.
198 .ie n .IP $obj\->CLOSE($fh) 4
199 .el .IP \f(CW$obj\fR\->CLOSE($fh) 4
200 .IX Item "$obj->CLOSE($fh)"
201 Should return 0 on success, \-1 on error.
202 Optional.
203 .ie n .IP $obj\->SEEK($posn,$whence,$fh) 4
204 .el .IP \f(CW$obj\fR\->SEEK($posn,$whence,$fh) 4
205 .IX Item "$obj->SEEK($posn,$whence,$fh)"
206 Should return 0 on success, \-1 on error.
207 Optional. Default is to fail, but that is likely to be changed
208 in future.
209 .ie n .IP $obj\->TELL($fh) 4
210 .el .IP \f(CW$obj\fR\->TELL($fh) 4
211 .IX Item "$obj->TELL($fh)"
212 Returns file position.
213 Optional. Default to be determined.
214 .ie n .IP $obj\->UNREAD($buffer,$fh) 4
215 .el .IP \f(CW$obj\fR\->UNREAD($buffer,$fh) 4
216 .IX Item "$obj->UNREAD($buffer,$fh)"
217 Returns the number of octets from \f(CW$buffer\fR that have been successfully
218 saved to be returned on future FILL/READ calls. Optional. Default is
219 to push data into a temporary layer above this one.
220 .ie n .IP $obj\->FLUSH($fh) 4
221 .el .IP \f(CW$obj\fR\->FLUSH($fh) 4
222 .IX Item "$obj->FLUSH($fh)"
223 Flush any buffered write data. May possibly be called on readable
224 handles too. Should return 0 on success, \-1 on error.
225 .ie n .IP $obj\->SETLINEBUF($fh) 4
226 .el .IP \f(CW$obj\fR\->SETLINEBUF($fh) 4
227 .IX Item "$obj->SETLINEBUF($fh)"
228 Optional. No return.
229 .ie n .IP $obj\->CLEARERR($fh) 4
230 .el .IP \f(CW$obj\fR\->CLEARERR($fh) 4
231 .IX Item "$obj->CLEARERR($fh)"
232 Optional. No return.
233 .ie n .IP $obj\->ERROR($fh) 4
234 .el .IP \f(CW$obj\fR\->ERROR($fh) 4
235 .IX Item "$obj->ERROR($fh)"
236 Optional. Returns error state. Default is no error until a mechanism
237 to signal error (die?) is worked out.
238 .ie n .IP $obj\->EOF($fh) 4
239 .el .IP \f(CW$obj\fR\->EOF($fh) 4
240 .IX Item "$obj->EOF($fh)"
241 Optional. Returns end\-of\-file state. Default is a function of the return
242 value of FILL or READ.
243 .SH EXAMPLES
244 .IX Header "EXAMPLES"
245 Check the PerlIO::via:: namespace on CPAN for examples of PerlIO layers
246 implemented in Perl. To give you an idea how simple the implementation of
247 a PerlIO layer can look, a simple example is included here.
248 .SS "Example \- a Hexadecimal Handle"
249 .IX Subsection "Example - a Hexadecimal Handle"
250 Given the following module, PerlIO::via::Hex :
251 .PP
252 .Vb 1
253 \& package PerlIO::via::Hex;
254 \&
255 \& sub PUSHED
256 \& {
257 \& my ($class,$mode,$fh) = @_;
258 \& # When writing we buffer the data
259 \& my $buf = \*(Aq\*(Aq;
260 \& return bless \e$buf,$class;
261 \& }
262 \&
263 \& sub FILL
264 \& {
265 \& my ($obj,$fh) = @_;
266 \& my $line = <$fh>;
267 \& return (defined $line) ? pack("H*", $line) : undef;
268 \& }
269 \&
270 \& sub WRITE
271 \& {
272 \& my ($obj,$buf,$fh) = @_;
273 \& $$obj .= unpack("H*", $buf);
274 \& return length($buf);
275 \& }
276 \&
277 \& sub FLUSH
278 \& {
279 \& my ($obj,$fh) = @_;
280 \& print $fh $$obj or return \-1;
281 \& $$obj = \*(Aq\*(Aq;
282 \& return 0;
283 \& }
284 \&
285 \& 1;
286 .Ve
287 .PP
288 The following code opens up an output handle that will convert any
289 output to a hexadecimal dump of the output bytes: for example "A" will
290 be converted to "41" (on ASCII\-based machines, on EBCDIC platforms
291 the "A" will become "c1")
292 .PP
293 .Vb 2
294 \& use PerlIO::via::Hex;
295 \& open(my $fh, ">:via(Hex)", "foo.hex");
296 .Ve
297 .PP
298 and the following code will read the hexdump in and convert it
299 on the fly back into bytes:
300 .PP
301 .Vb 1
302 \& open(my $fh, "<:via(Hex)", "foo.hex");
303 .Ve