1PerlIO::via(3pm)       Perl Programmers Reference Guide       PerlIO::via(3pm)
2
3
4

NAME

6       PerlIO::via - Helper class for PerlIO layers implemented in perl
7

SYNOPSIS

9          use PerlIO::via::Layer;
10          open($fh,"<:via(Layer)",...);
11
12          use Some::Other::Package;
13          open($fh,">:via(Some::Other::Package)",...);
14

DESCRIPTION

16       The PerlIO::via module allows you to develop PerlIO layers in Perl,
17       without having to go into the nitty gritty of programming C with XS as
18       the interface to Perl.
19
20       One example module, PerlIO::via::QuotedPrint, is included with Perl
21       5.8.0, and more example modules are available from CPAN, such as Per‐
22       lIO::via::StripHTML and PerlIO::via::Base64.  The Per‐
23       lIO::via::StripHTML module for instance, allows you to say:
24
25               use PerlIO::via::StripHTML;
26               open( my $fh, "<:via(StripHTML)", "index.html" );
27               my @line = <$fh>;
28
29       to obtain the text of an HTML-file in an array with all the HTML-tags
30       automagically removed.
31
32       Please note that if the layer is created in the PerlIO::via:: names‐
33       pace, it does not have to be fully qualified.  The PerlIO::via module
34       will prefix the PerlIO::via:: namespace if the specified modulename
35       does not exist as a fully qualified module name.
36

EXPECTED METHODS

38       To create a Perl module that implements a PerlIO layer in Perl (as
39       opposed to in C using XS as the interface to Perl), you need to supply
40       some of the following subroutines.  It is recommended to create these
41       Perl modules in the PerlIO::via:: namespace, so that they can easily be
42       located on CPAN and use the default namespace feature of the Per‐
43       lIO::via module itself.
44
45       Please note that this is an area of recent development in Perl and that
46       the interface described here is therefore still subject to change (and
47       hopefully will have better documentation and more examples).
48
49       In the method descriptions below $fh will be a reference to a glob
50       which can be treated as a perl file handle.  It refers to the layer
51       below. $fh is not passed if the layer is at the bottom of the stack,
52       for this reason and to maintain some level of "compatibility" with
53       TIEHANDLE classes it is passed last.
54
55       $class->PUSHED([$mode[,$fh]])
56           Should return an object or the class, or -1 on failure.  (Compare
57           TIEHANDLE.)  The arguments are an optional mode string ("r", "w",
58           "w+", ...) and a filehandle for the PerlIO layer below.  Mandatory.
59
60           When layer is pushed as part of an "open" call, "PUSHED" will be
61           called before the actual open occurs whether than be via "OPEN",
62           "SYSOPEN", "FDOPEN" or by letting lower layer do the open.
63
64       $obj->POPPED([$fh])
65           Optional - layer is about to be removed.
66
67       $obj->UTF8($bellowFlag,[$fh])
68           Optional - if present it will be called immediately after PUSHED
69           has returned. It should return true value if the layer expects data
70           to be UTF-8 encoded. If it returns true result is as if caller had
71           done
72
73              ":via(YourClass):utf8"
74
75           If not present of it it returns false, then stream is left with
76           flag clear.  The $bellowFlag argument will be true if there is a
77           layer below and that layer was expecting UTF-8.
78
79       $obj->OPEN($path,$mode[,$fh])
80           Optional - if not present lower layer does open.  If present called
81           for normal opens after layer is pushed.  This function is subject
82           to change as there is no easy way to get lower layer to do open and
83           then regain control.
84
85       $obj->BINMODE([,$fh])
86           Optional - if not available layer is popped on binmode($fh) or when
87           ":raw" is pushed. If present it should return 0 on success -1 on
88           error and undef to pop the layer.
89
90       $obj->FDOPEN($fd[,$fh])
91           Optional - if not present lower layer does open.  If present called
92           for opens which pass a numeric file descriptor after layer is
93           pushed.  This function is subject to change as there is no easy way
94           to get lower layer to do open and then regain control.
95
96       $obj->SYSOPEN($path,$imode,$perm,[,$fh])
97           Optional - if not present lower layer does open.  If present called
98           for sysopen style opens which pass a numeric mode and permissions
99           after layer is pushed.  This function is subject to change as there
100           is no easy way to get lower layer to do open and then regain con‐
101           trol.
102
103       $obj->FILENO($fh)
104           Returns a numeric value for Unix-like file descriptor. Return -1 if
105           there isn't one.  Optional.  Default is fileno($fh).
106
107       $obj->READ($buffer,$len,$fh)
108           Returns the number of octets placed in $buffer (must be less than
109           or equal to $len).  Optional.  Default is to use FILL instead.
110
111       $obj->WRITE($buffer,$fh)
112           Returns the number of octets from buffer that have been success‐
113           fully written.
114
115       $obj->FILL($fh)
116           Should return a string to be placed in the buffer.  Optional. If
117           not provided must provide READ or reject handles open for reading
118           in PUSHED.
119
120       $obj->CLOSE($fh)
121           Should return 0 on success, -1 on error.  Optional.
122
123       $obj->SEEK($posn,$whence,$fh)
124           Should return 0 on success, -1 on error.  Optional.  Default is to
125           fail, but that is likely to be changed in future.
126
127       $obj->TELL($fh)
128           Returns file postion.  Optional.  Default to be determined.
129
130       $obj->UNREAD($buffer,$fh)
131           Returns the number of octets from buffer that have been success‐
132           fully saved to be returned on future FILL/READ calls.  Optional.
133           Default is to push data into a temporary layer above this one.
134
135       $obj->FLUSH($fh)
136           Flush any buffered write data.  May possibly be called on readable
137           handles too.  Should return 0 on success, -1 on error.
138
139       $obj->SETLINEBUF($fh)
140           Optional. No return.
141
142       $obj->CLEARERR($fh)
143           Optional. No return.
144
145       $obj->ERROR($fh)
146           Optional. Returns error state. Default is no error until a mecha‐
147           nism to signal error (die?) is worked out.
148
149       $obj->EOF($fh)
150           Optional. Returns end-of-file state. Default is function of return
151           value of FILL or READ.
152

EXAMPLES

154       Check the PerlIO::via:: namespace on CPAN for examples of PerlIO layers
155       implemented in Perl.  To give you an idea how simple the implementation
156       of a PerlIO layer can look, as simple example is included here.
157
158       Example - a Hexadecimal Handle
159
160       Given the following module, PerlIO::via::Hex :
161
162           package PerlIO::via::Hex;
163
164           sub PUSHED
165           {
166            my ($class,$mode,$fh) = @_;
167            # When writing we buffer the data
168            my $buf = '';
169            return bless \$buf,$class;
170           }
171
172           sub FILL
173           {
174            my ($obj,$fh) = @_;
175            my $line = <$fh>;
176            return (defined $line) ? pack("H*", $line) : undef;
177           }
178
179           sub WRITE
180           {
181            my ($obj,$buf,$fh) = @_;
182            $$obj .= unpack("H*", $buf);
183            return length($buf);
184           }
185
186           sub FLUSH
187           {
188            my ($obj,$fh) = @_;
189            print $fh $$obj or return -1;
190            $$obj = '';
191            return 0;
192           }
193
194           1;
195
196       the following code opens up an output handle that will convert any out‐
197       put to hexadecimal dump of the output bytes: for example "A" will be
198       converted to "41" (on ASCII-based machines, on EBCDIC platforms the "A"
199       will become "c1")
200
201           use PerlIO::via::Hex;
202           open(my $fh, ">:via(Hex)", "foo.hex");
203
204       and the following code will read the hexdump in and convert it on the
205       fly back into bytes:
206
207           open(my $fh, "<:via(Hex)", "foo.hex");
208
209
210
211perl v5.8.8                       2001-09-21                  PerlIO::via(3pm)
Impressum