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