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
22       PerlIO::via::StripHTML and PerlIO::via::Base64.  The
23       PerlIO::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::
33       namespace, it does not have to be fully qualified.  The PerlIO::via
34       module will prefix the PerlIO::via:: namespace if the specified
35       modulename 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
43       PerlIO::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 the layer is pushed as part of an "open" call, "PUSHED" will
61           be called before the actual open occurs, whether that be via
62           "OPEN", "SYSOPEN", "FDOPEN" or by letting a lower layer do the
63           open.
64
65       $obj->POPPED([$fh])
66           Optional - called when the layer is about to be removed.
67
68       $obj->UTF8($belowFlag,[$fh])
69           Optional - if present it will be called immediately after PUSHED
70           has returned. It should return a true value if the layer expects
71           data to be UTF-8 encoded. If it returns true, the result is as if
72           the caller had done
73
74              ":via(YourClass):utf8"
75
76           If not present or if it returns false, then the stream is left with
77           the UTF-8 flag clear.  The $belowFlag argument will be true if
78           there is a layer below and that layer was expecting UTF-8.
79
80       $obj->OPEN($path,$mode,[$fh])
81           Optional - if not present a lower layer does the open.  If present,
82           called for normal opens after the layer is pushed.  This function
83           is subject to change as there is no easy way to get a lower layer
84           to do the open and then regain control.
85
86       $obj->BINMODE([$fh])
87           Optional - if not present the layer is popped on binmode($fh) or
88           when ":raw" is pushed. If present it should return 0 on success, -1
89           on error, or undef to pop the layer.
90
91       $obj->FDOPEN($fd,[$fh])
92           Optional - if not present a lower layer does the open.  If present,
93           called after the layer is pushed for opens which pass a numeric
94           file descriptor.  This function is subject to change as there is no
95           easy way to get a lower layer to do the open and then regain
96           control.
97
98       $obj->SYSOPEN($path,$imode,$perm,[$fh])
99           Optional - if not present a lower layer does the open.  If present,
100           called after the layer is pushed for sysopen style opens which pass
101           a numeric mode and permissions.  This function is subject to change
102           as there is no easy way to get a lower layer to do the open and
103           then regain control.
104
105       $obj->FILENO($fh)
106           Returns a numeric value for a Unix-like file descriptor. Returns -1
107           if there isn't one.  Optional.  Default is fileno($fh).
108
109       $obj->READ($buffer,$len,$fh)
110           Returns the number of octets placed in $buffer (must be less than
111           or equal to $len).  Optional.  Default is to use FILL instead.
112
113       $obj->WRITE($buffer,$fh)
114           Returns the number of octets from $buffer that have been
115           successfully written.
116
117       $obj->FILL($fh)
118           Should return a string to be placed in the buffer.  Optional. If
119           not provided, must provide READ or reject handles open for reading
120           in PUSHED.
121
122       $obj->CLOSE($fh)
123           Should return 0 on success, -1 on error.  Optional.
124
125       $obj->SEEK($posn,$whence,$fh)
126           Should return 0 on success, -1 on error.  Optional.  Default is to
127           fail, but that is likely to be changed in future.
128
129       $obj->TELL($fh)
130           Returns file position.  Optional.  Default to be determined.
131
132       $obj->UNREAD($buffer,$fh)
133           Returns the number of octets from $buffer that have been
134           successfully saved to be returned on future FILL/READ calls.
135           Optional.  Default is to push data into a temporary layer above
136           this one.
137
138       $obj->FLUSH($fh)
139           Flush any buffered write data.  May possibly be called on readable
140           handles too.  Should return 0 on success, -1 on error.
141
142       $obj->SETLINEBUF($fh)
143           Optional. No return.
144
145       $obj->CLEARERR($fh)
146           Optional. No return.
147
148       $obj->ERROR($fh)
149           Optional. Returns error state. Default is no error until a
150           mechanism to signal error (die?) is worked out.
151
152       $obj->EOF($fh)
153           Optional. Returns end-of-file state. Default is a function of the
154           return value of FILL or READ.
155

EXAMPLES

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