4 XSLoader::load 'PerlIO::via';
10 PerlIO::via - Helper class for PerlIO layers implemented in perl
14 use PerlIO::via::Layer;
15 open($fh,"<:via(Layer)",...);
17 use Some::Other::Package;
18 open($fh,">:via(Some::Other::Package)",...);
22 The PerlIO::via module allows you to develop PerlIO layers in Perl, without
23 having to go into the nitty gritty of programming C with XS as the interface
26 One example module, L<PerlIO::via::QuotedPrint>, is included with Perl
27 5.8.0, and more example modules are available from CPAN, such as
28 L<PerlIO::via::StripHTML> and L<PerlIO::via::Base64>. The
29 PerlIO::via::StripHTML module for instance, allows you to say:
31 use PerlIO::via::StripHTML;
32 open( my $fh, "<:via(StripHTML)", "index.html" );
35 to obtain the text of an HTML-file in an array with all the HTML-tags
36 automagically removed.
38 Please note that if the layer is created in the PerlIO::via:: namespace, it
39 does B<not> have to be fully qualified. The PerlIO::via module will prefix
40 the PerlIO::via:: namespace if the specified modulename does not exist as a
41 fully qualified module name.
43 =head1 EXPECTED METHODS
45 To create a Perl module that implements a PerlIO layer in Perl (as opposed to
46 in C using XS as the interface to Perl), you need to supply some of the
47 following subroutines. It is recommended to create these Perl modules in the
48 PerlIO::via:: namespace, so that they can easily be located on CPAN and use
49 the default namespace feature of the PerlIO::via module itself.
51 Please note that this is an area of recent development in Perl and that the
52 interface described here is therefore still subject to change (and hopefully
53 will have better documentation and more examples).
55 In the method descriptions below I<$fh> will be
56 a reference to a glob which can be treated as a perl file handle.
57 It refers to the layer below. I<$fh> is not passed if the layer
58 is at the bottom of the stack, for this reason and to maintain
59 some level of "compatibility" with TIEHANDLE classes it is passed last.
63 =item $class->PUSHED([$mode[,$fh]])
65 Should return an object or the class, or -1 on failure. (Compare
66 TIEHANDLE.) The arguments are an optional mode string ("r", "w",
67 "w+", ...) and a filehandle for the PerlIO layer below. Mandatory.
69 When layer is pushed as part of an C<open> call, C<PUSHED> will be called
70 I<before> the actual open occurs whether than be via C<OPEN>, C<SYSOPEN>,
71 C<FDOPEN> or by letting lower layer do the open.
73 =item $obj->POPPED([$fh])
75 Optional - layer is about to be removed.
77 =item $obj->UTF8($bellowFlag,[$fh])
79 Optional - if present it will be called immediately after PUSHED has
80 returned. It should return true value if the layer expects data to be
81 UTF-8 encoded. If it returns true result is as if caller had done
83 ":via(YourClass):utf8"
85 If not present of it it returns false, then stream is left with
87 The I<$bellowFlag> argument will be true if there is a layer below
88 and that layer was expecting UTF-8.
91 =item $obj->OPEN($path,$mode[,$fh])
93 Optional - if not present lower layer does open.
94 If present called for normal opens after layer is pushed.
95 This function is subject to change as there is no easy way
96 to get lower layer to do open and then regain control.
98 =item $obj->BINMODE([,$fh])
100 Optional - if not available layer is popped on binmode($fh) or when C<:raw>
101 is pushed. If present it should return 0 on success -1 on error and undef
104 =item $obj->FDOPEN($fd[,$fh])
106 Optional - if not present lower layer does open.
107 If present called for opens which pass a numeric file
108 descriptor after layer is pushed.
109 This function is subject to change as there is no easy way
110 to get lower layer to do open and then regain control.
112 =item $obj->SYSOPEN($path,$imode,$perm,[,$fh])
114 Optional - if not present lower layer does open.
115 If present called for sysopen style opens which pass a numeric mode
116 and permissions after layer is pushed.
117 This function is subject to change as there is no easy way
118 to get lower layer to do open and then regain control.
120 =item $obj->FILENO($fh)
122 Returns a numeric value for Unix-like file descriptor. Return -1 if
123 there isn't one. Optional. Default is fileno($fh).
125 =item $obj->READ($buffer,$len,$fh)
127 Returns the number of octets placed in $buffer (must be less than or
128 equal to $len). Optional. Default is to use FILL instead.
130 =item $obj->WRITE($buffer,$fh)
132 Returns the number of octets from buffer that have been successfully written.
134 =item $obj->FILL($fh)
136 Should return a string to be placed in the buffer. Optional. If not
137 provided must provide READ or reject handles open for reading in
140 =item $obj->CLOSE($fh)
142 Should return 0 on success, -1 on error.
145 =item $obj->SEEK($posn,$whence,$fh)
147 Should return 0 on success, -1 on error.
148 Optional. Default is to fail, but that is likely to be changed
151 =item $obj->TELL($fh)
153 Returns file postion.
154 Optional. Default to be determined.
156 =item $obj->UNREAD($buffer,$fh)
158 Returns the number of octets from buffer that have been successfully
159 saved to be returned on future FILL/READ calls. Optional. Default is
160 to push data into a temporary layer above this one.
162 =item $obj->FLUSH($fh)
164 Flush any buffered write data. May possibly be called on readable
165 handles too. Should return 0 on success, -1 on error.
167 =item $obj->SETLINEBUF($fh)
171 =item $obj->CLEARERR($fh)
175 =item $obj->ERROR($fh)
177 Optional. Returns error state. Default is no error until a mechanism
178 to signal error (die?) is worked out.
182 Optional. Returns end-of-file state. Default is function of return
183 value of FILL or READ.
189 Check the PerlIO::via:: namespace on CPAN for examples of PerlIO layers
190 implemented in Perl. To give you an idea how simple the implementation of
191 a PerlIO layer can look, as simple example is included here.
193 =head2 Example - a Hexadecimal Handle
195 Given the following module, PerlIO::via::Hex :
197 package PerlIO::via::Hex;
201 my ($class,$mode,$fh) = @_;
202 # When writing we buffer the data
204 return bless \$buf,$class;
211 return (defined $line) ? pack("H*", $line) : undef;
216 my ($obj,$buf,$fh) = @_;
217 $$obj .= unpack("H*", $buf);
224 print $fh $$obj or return -1;
231 the following code opens up an output handle that will convert any
232 output to hexadecimal dump of the output bytes: for example "A" will
233 be converted to "41" (on ASCII-based machines, on EBCDIC platforms
234 the "A" will become "c1")
236 use PerlIO::via::Hex;
237 open(my $fh, ">:via(Hex)", "foo.hex");
239 and the following code will read the hexdump in and convert it
240 on the fly back into bytes:
242 open(my $fh, "<:via(Hex)", "foo.hex");