PerlIO::via(3) Perl Programmers Reference Guide PerlIO::via(3) NAME PerlIO::via - Helper class for PerlIO layers implemented in(1,8) perl SYNOPSIS use PerlIO::via::Layer; open(2,3,n)($fh,"<:via(Layer)",...); use Some::Other::Package; open(2,3,n)($fh,">:via(Some::Other::Package)",...); DESCRIPTION The PerlIO::via module allows you to develop PerlIO layers in(1,8) Perl, without having to go into the nitty gritty of programming C with XS as the interface to Perl. One example module, PerlIO::via::QuotedPrint, is included with Perl 5.8.0, and more example modules are available from CPAN, such as Per- lIO::via::StripHTML and PerlIO::via::Base64. The Per- lIO::via::StripHTML module for instance, allows you to say: use PerlIO::via::StripHTML; open(2,3,n)( my $fh, "<:via(StripHTML)", "index.html" ); my @line = <$fh>; to obtain the text of an HTML-file in(1,8) an array with all the HTML-tags automagically removed. Please note that if(3,n) the layer is created in(1,8) the PerlIO::via:: names- pace, it does not have to be fully qualified. The PerlIO::via module will prefix the PerlIO::via:: namespace if(3,n) the specified modulename does not exist as a fully qualified module name. EXPECTED METHODS To create a Perl module that implements a PerlIO layer in(1,8) Perl (as opposed to in(1,8) C using XS as the interface to Perl), you need to supply some of the following subroutines. It is recommended to create these Perl modules in(1,8) the PerlIO::via:: namespace, so that they can easily be located on CPAN and use the default namespace feature of the Per- lIO::via module itself. Please note that this is an area of recent development in(1,8) Perl and that the interface described here is therefore still subject to change (and hopefully will have better documentation and more examples). In the method descriptions below $fh will be a reference to a glob(1,3,7,n) which can be treated as a perl file(1,n) handle. It refers to the layer below. $fh is not passed if(3,n) the layer is at the bottom of the stack, for this reason and to maintain some level of "compatibility" with TIEHANDLE classes it is passed last. $class->PUSHED([$mode[,$fh]]) Should return an object or the class, or -1 on failure. (Compare TIEHANDLE.) The arguments are an optional mode string(3,n) ("r", "w", "w+", ...) and a filehandle for the PerlIO layer below. Mandatory. When layer is pushed as part of an "open(2,3,n)" call, "PUSHED" will be called before the actual open(2,3,n) occurs whether than be via "OPEN", "SYSOPEN", "FDOPEN" or by letting lower layer do the open. $obj->POPPED([$fh]) Optional - layer is about to be removed. $obj->UTF8($bellowFlag,[$fh]) Optional - if(3,n) present it will be called immediately after PUSHED has returned. It should return true value if(3,n) the layer expects data to be UTF-8 encoded. If it returns true result is as if(3,n) caller had done ":via(YourClass):utf8" If not present of it it returns false, then stream is left with flag clear. The $bellowFlag argument will be true if(3,n) there is a layer below and that layer was expecting UTF-8. $obj->OPEN($path,$mode[,$fh]) Optional - if(3,n) not present lower layer does open. If present called for normal opens after layer is pushed. This function is subject to change as there is no easy way to get lower layer to do open(2,3,n) and then regain control. $obj->BINMODE([,$fh]) Optional - if(3,n) not available layer is popped on binmode($fh) or when ":raw" is pushed. If present it should return 0 on success -1 on error(8,n) and undef to pop the layer. $obj->FDOPEN($fd[,$fh]) Optional - if(3,n) not present lower layer does open. If present called for opens which pass a numeric file(1,n) descriptor after layer is pushed. This function is subject to change as there is no easy way to get lower layer to do open(2,3,n) and then regain control. $obj->SYSOPEN($path,$imode,$perm,[,$fh]) Optional - if(3,n) not present lower layer does open. If present called for sysopen style opens which pass a numeric mode and permissions after layer is pushed. This function is subject to change as there is no easy way to get lower layer to do open(2,3,n) and then regain con- trol. $obj->FILENO($fh) Returns a numeric value for Unix-like file(1,n) descriptor. Return -1 if(3,n) there isn't one. Optional. Default is fileno($fh). $obj->READ($buffer,$len,$fh) Returns the number of octets placed in(1,8) $buffer (must be less(1,3) than or equal to $len). Optional. Default is to use FILL instead. $obj->WRITE($buffer,$fh) Returns the number of octets from buffer that have been success- fully written. $obj->FILL($fh) Should return a string(3,n) to be placed in(1,8) the buffer. Optional. If not provided must provide READ or reject handles open(2,3,n) for reading in(1,8) PUSHED. $obj->CLOSE($fh) Should return 0 on success, -1 on error. Optional. $obj->SEEK($posn,$whence,$fh) Should return 0 on success, -1 on error. Optional. Default is to fail, but that is likely to be changed in(1,8) future. $obj->TELL($fh) Returns file(1,n) postion. Optional. Default to be determined. $obj->UNREAD($buffer,$fh) Returns the number of octets from buffer that have been success- fully saved to be returned on future FILL/READ calls. Optional. Default is to push data into a temporary layer above this one. $obj->FLUSH($fh) Flush any buffered write(1,2) data. May possibly be called on readable handles too. Should return 0 on success, -1 on error. $obj->SETLINEBUF($fh) Optional. No return. $obj->CLEARERR($fh) Optional. No return. $obj->ERROR($fh) Optional. Returns error(8,n) state. Default is no error(8,n) until a mecha- nism to signal(2,7) error(8,n) (die?) is worked out. $obj->EOF($fh) Optional. Returns end-of-file state. Default is function of return value of FILL or READ. EXAMPLES Check the PerlIO::via:: namespace on CPAN for examples of PerlIO layers implemented in(1,8) Perl. To give you an idea how simple the implementation of a PerlIO layer can look(1,8,3 Search::Dict), as simple example is included here. Example - a Hexadecimal Handle Given the following module, PerlIO::via::Hex : package PerlIO::via::Hex; sub PUSHED { my ($class,$mode,$fh) = @_; # When writing we buffer the data my $buf = ''; return bless \$buf,$class; } sub FILL { my ($obj,$fh) = @_; my $line = <$fh>; return (defined $line) ? pack(3,n,n pack-old)("H*", $line) : undef; } sub WRITE { my ($obj,$buf,$fh) = @_; $$obj .= unpack("H*", $buf); return length($buf); } sub FLUSH { my ($obj,$fh) = @_; print $fh $$obj or return -1; $$obj = ''; return 0; } 1; the following code opens up an output handle that will convert any out- put to hexadecimal dump of the output bytes: for example "A" will be converted to "41" (on ASCII-based machines, on EBCDIC platforms the "A" will become "c1") use PerlIO::via::Hex; open(2,3,n)(my $fh, ">:via(Hex)", "foo.hex"); and the following code will read(2,n,1 builtins) the hexdump in(1,8) and convert it on the fly back into bytes: open(2,3,n)(my $fh, "<:via(Hex)", "foo.hex"); perl v5.8.5 2001-09-21 PerlIO::via(3)