4 XSLoader::load 'PerlIO::Via';
10 PerlIO::Via - Helper class for PerlIO layers implemented in perl
16 open($fh,"<:Via(Some::Package)",...);
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.
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
30 use MIME::QuotedPrint;
31 open(my $fh, ">Via(MIME::QuotedPrint)", "qp");
35 =item $class->PUSHED([$mode[,$fh]])
37 Should return an object or the class, or -1 on failure. (Compare
38 TIEHANDLE.) The arguments are an optional mode string ("r", "w",
39 "w+", ...) and a filehandle for the PerlIO layer below. Mandatory.
41 =item $obj->POPPED([$fh])
43 Optional - layer is about to be removed.
45 =item $class->OPEN($path,$mode[,$fh])
49 =item $class->FDOPEN($fd)
53 =item $class->SYSOPEN($path,$imode,$perm,$fh)
57 =item $obj->FILENO($fh)
59 Returns a numeric value for Unix-like file descriptor. Return -1 if
60 there isn't one. Optional. Default is fileno($fh).
62 =item $obj->READ($buffer,$len,$fh)
64 Returns the number of octets placed in $buffer (must be less than or
65 equal to $len). Optional. Default is to use FILL instead.
67 =item $obj->WRITE($buffer,$fh)
69 Returns the number of octets from buffer that have been sucessfully written.
73 Should return a string to be placed in the buffer. Optional. If not
74 provided must provide READ or reject handles open for reading in
77 =item $obj->CLOSE($fh)
79 Should return 0 on success, -1 on error.
82 =item $obj->SEEK($posn,$whence,$fh)
84 Should return 0 on success, -1 on error.
85 Optional. Default is to fail, but that is likely to be changed
91 Optional. Default to be determined.
93 =item $obj->UNREAD($buffer,$fh)
95 Returns the number of octets from buffer that have been sucessfully
96 saved to be returned on future FILL/READ calls. Optional. Default is
97 to push data into a temporary layer above this one.
99 =item $obj->FLUSH($fh)
101 Flush any buffered write data. May possibly be called on readable
102 handles too. Should return 0 on success, -1 on error.
104 =item $obj->SETLINEBUF($fh)
108 =item $obj->CLEARERR($fh)
112 =item $obj->ERROR($fh)
114 Optional. Returns error state. Default is no error until a mechanism
115 to signal error (die?) is worked out.
119 Optional. Returns end-of-file state. Default is function of return
120 value of FILL or READ.
124 =head2 Example - a Hexadecimal Handle
126 Given the following module, Hex.pm:
132 my ($class,$mode,$fh) = @_;
133 # When writing we buffer the data
135 return bless \$buf,$class;
142 return (defined $line) ? pack("H*", $line) : undef;
147 my ($obj,$buf,$fh) = @_;
148 $$obj .= unpack("H*", $buf);
155 print $fh $$obj or return -1;
162 the following code opens up an output handle that will convert any
163 output to hexadecimal dump of the output bytes: for example "A" will
164 be converted to "41" (on ASCII-based machines, on EBCDIC platforms
165 the "A" will become "c1")
168 open(my $fh, ">:Via(Hex)", "foo.hex");
170 and the following code will read the hexdump in and convert it
171 on the fly back into bytes:
173 open(my $fh, "<:Via(Hex)", "foo.hex");