2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 <title>perliol - C API for Perl's implementation of IO in Layers.
</title>
6 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
7 <link rev=
"made" href=
"mailto:" />
10 <body style=
"background-color: white">
11 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
12 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
13 <big><strong><span class=
"block"> perliol - C API for Perl's implementation of IO in Layers.
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#synopsis">SYNOPSIS
</a></li>
24 <li><a href=
"#description">DESCRIPTION
</a></li>
27 <li><a href=
"#history_and_background">History and Background
</a></li>
28 <li><a href=
"#basic_structure">Basic Structure
</a></li>
29 <li><a href=
"#layers_vs_disciplines">Layers vs Disciplines
</a></li>
30 <li><a href=
"#data_structures">Data Structures
</a></li>
31 <li><a href=
"#functions_and_attributes">Functions and Attributes
</a></li>
32 <li><a href=
"#perinstance_data">Per-instance Data
</a></li>
33 <li><a href=
"#layers_in_action_">Layers in action.
</a></li>
34 <li><a href=
"#perinstance_flag_bits">Per-instance flag bits
</a></li>
35 <li><a href=
"#methods_in_detail">Methods in Detail
</a></li>
36 <li><a href=
"#utilities">Utilities
</a></li>
37 <li><a href=
"#implementing_perlio_layers">Implementing PerlIO Layers
</a></li>
38 <li><a href=
"#core_layers">Core Layers
</a></li>
39 <li><a href=
"#extension_layers">Extension Layers
</a></li>
42 <li><a href=
"#todo">TODO
</a></li>
49 <h1><a name=
"name">NAME
</a></h1>
50 <p>perliol - C API for Perl's implementation of IO in Layers.
</p>
54 <h1><a name=
"synopsis">SYNOPSIS
</a></h1>
56 /* Defining a layer ... */
57 #include
<<a href=
"//C|\msysgit\mingw\html/pod/perliol.html">perliol
</a>.h
></pre>
61 <h1><a name=
"description">DESCRIPTION
</a></h1>
62 <p>This document describes the behavior and implementation of the PerlIO
63 abstraction described in
<a href=
"file://C|\msysgit\mingw\html/pod/perlapio.html">the perlapio manpage
</a> when
<code>USE_PERLIO
</code> is defined (and
64 <code>USE_SFIO
</code> is not).
</p>
67 <h2><a name=
"history_and_background">History and Background
</a></h2>
68 <p>The PerlIO abstraction was introduced in perl5.003_02 but languished as
69 just an abstraction until perl5.7
.0. However during that time a number
70 of perl extensions switched to using it, so the API is mostly fixed to
71 maintain (source) compatibility.
</p>
72 <p>The aim of the implementation is to provide the PerlIO API in a flexible
73 and platform neutral manner. It is also a trial of an ``Object Oriented
74 C, with vtables'' approach which may be applied to perl6.
</p>
77 <h2><a name=
"basic_structure">Basic Structure
</a></h2>
78 <p>PerlIO is a stack of layers.
</p>
79 <p>The low levels of the stack work with the low-level operating system
80 calls (file descriptors in C) getting bytes in and out, the higher
81 layers of the stack buffer, filter, and otherwise manipulate the I/O,
82 and return characters (or bytes) to Perl. Terms
<em>above
</em> and
<em>below
</em>
83 are used to refer to the relative positioning of the stack layers.
</p>
84 <p>A layer contains a ``vtable'', the table of I/O operations (at C level
85 a table of function pointers), and status flags. The functions in the
86 vtable implement operations like ``open'', ``read'', and ``write''.
</p>
87 <p>When I/O, for example ``read'', is requested, the request goes from Perl
88 first down the stack using ``read'' functions of each layer, then at the
89 bottom the input is requested from the operating system services, then
90 the result is returned up the stack, finally being interpreted as Perl
92 <p>The requests do not necessarily go always all the way down to the
93 operating system: that's where PerlIO buffering comes into play.
</p>
94 <p>When you do an
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()
</code></a> and specify extra PerlIO layers to be deployed,
95 the layers you specify are ``pushed'' on top of the already existing
96 default stack. One way to see it is that ``operating system is
97 on the left'' and ``Perl is on the right''.
</p>
98 <p>What exact layers are in this default stack depends on a lot of
99 things: your operating system, Perl version, Perl compile time
100 configuration, and Perl runtime configuration. See
<a href=
"file://C|\msysgit\mingw\html/lib/Encode/PerlIO.html">the PerlIO manpage
</a>,
101 <a href=
"file://C|\msysgit\mingw\html/pod/perlrun.html#perlio">PERLIO in the perlrun manpage
</a>, and
<a href=
"file://C|\msysgit\mingw\html/lib/open.html">the open manpage
</a> for more information.
</p>
102 <p><a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_binmode"><code>binmode()
</code></a> operates similarly to open(): by default the specified
103 layers are pushed on top of the existing stack.
</p>
104 <p>However, note that even as the specified layers are ``pushed on top''
105 for
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()
</code></a> and binmode(), this doesn't mean that the effects are
106 limited to the ``top'': PerlIO layers can be very 'active' and inspect
107 and affect layers also deeper in the stack. As an example there
108 is a layer called ``raw'' which repeatedly ``pops'' layers until
109 it reaches the first layer that has declared itself capable of
110 handling binary data. The ``pushed'' layers are processed in left-to-right
112 <p><a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sysopen"><code>sysopen()
</code></a> operates (unsurprisingly) at a lower level in the stack than
113 open(). For example in UNIX or UNIX-like systems
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sysopen"><code>sysopen()
</code></a> operates
114 directly at the level of file descriptors: in the terms of PerlIO
115 layers, it uses only the ``unix'' layer, which is a rather thin wrapper
116 on top of the UNIX file descriptors.
</p>
119 <h2><a name=
"layers_vs_disciplines">Layers vs Disciplines
</a></h2>
120 <p>Initial discussion of the ability to modify IO streams behaviour used
121 the term ``discipline'' for the entities which were added. This came (I
122 believe) from the use of the term in ``sfio'', which in turn borrowed it
123 from ``line disciplines'' on Unix terminals. However, this document (and
124 the C code) uses the term ``layer''.
</p>
125 <p>This is, I hope, a natural term given the implementation, and should
126 avoid connotations that are inherent in earlier uses of ``discipline''
127 for things which are rather different.
</p>
130 <h2><a name=
"data_structures">Data Structures
</a></h2>
131 <p>The basic data structure is a PerlIOl:
</p>
133 typedef struct _PerlIO PerlIOl;
134 typedef struct _PerlIO_funcs PerlIO_funcs;
135 typedef PerlIOl *PerlIO;
</pre>
139 PerlIOl * next; /* Lower layer */
140 PerlIO_funcs * tab; /* Functions for this layer */
141 IV flags; /* Various flags for state */
143 <p>A
<code>PerlIOl *
</code> is a pointer to the struct, and the
<em>application
</em>
144 level
<code>PerlIO *
</code> is a pointer to a
<code>PerlIOl *
</code> - i.e. a pointer
145 to a pointer to the struct. This allows the application level
<code>PerlIO *
</code>
146 to remain constant while the actual
<code>PerlIOl *
</code> underneath
147 changes. (Compare perl's
<code>SV *
</code> which remains constant while its
148 <code>sv_any
</code> field changes as the scalar's type changes.) An IO stream is
149 then in general represented as a pointer to this linked-list of
151 <p>It should be noted that because of the double indirection in a
<code>PerlIO *
</code>,
152 a
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_next"><code>&(perlio-
>next)
</code></a> ``is'' a
<code>PerlIO *
</code>, and so to some degree
153 at least one layer can use the ``standard'' API on the next layer down.
</p>
154 <p>A ``layer'' is composed of two parts:
</p>
157 <p>The functions and attributes of the ``layer class''.
</p>
160 <p>The per-instance data for a particular handle.
</p>
165 <h2><a name=
"functions_and_attributes">Functions and Attributes
</a></h2>
166 <p>The functions and attributes are accessed via the ``tab'' (for table)
167 member of
<code>PerlIOl
</code>. The functions (methods of the layer ``class'') are
168 fixed, and are defined by the
<code>PerlIO_funcs
</code> type. They are broadly the
169 same as the public
<code>PerlIO_xxxxx
</code> functions:
</p>
177 IV (*Pushed)(pTHX_ PerlIO *f,const char *mode,SV *arg, PerlIO_funcs *tab);
178 IV (*Popped)(pTHX_ PerlIO *f);
179 PerlIO * (*Open)(pTHX_ PerlIO_funcs *tab,
182 int fd, int imode, int perm,
184 int narg, SV **args);
185 IV (*Binmode)(pTHX_ PerlIO *f);
186 SV * (*Getarg)(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
187 IV (*Fileno)(pTHX_ PerlIO *f);
188 PerlIO * (*Dup)(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
189 /* Unix-like functions - cf sfio line disciplines */
190 SSize_t (*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);
191 SSize_t (*Unread)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);
192 SSize_t (*Write)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);
193 IV (*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);
194 Off_t (*Tell)(pTHX_ PerlIO *f);
195 IV (*Close)(pTHX_ PerlIO *f);
196 /* Stdio-like buffered IO functions */
197 IV (*Flush)(pTHX_ PerlIO *f);
198 IV (*Fill)(pTHX_ PerlIO *f);
199 IV (*Eof)(pTHX_ PerlIO *f);
200 IV (*Error)(pTHX_ PerlIO *f);
201 void (*Clearerr)(pTHX_ PerlIO *f);
202 void (*Setlinebuf)(pTHX_ PerlIO *f);
203 /* Perl's snooping functions */
204 STDCHAR * (*Get_base)(pTHX_ PerlIO *f);
205 Size_t (*Get_bufsiz)(pTHX_ PerlIO *f);
206 STDCHAR * (*Get_ptr)(pTHX_ PerlIO *f);
207 SSize_t (*Get_cnt)(pTHX_ PerlIO *f);
208 void (*Set_ptrcnt)(pTHX_ PerlIO *f,STDCHAR *ptr,SSize_t cnt);
210 <p>The first few members of the struct give a function table size for
211 compatibility check ``name'' for the layer, the size to
<code>malloc
</code> for the per-instance data,
212 and some flags which are attributes of the class as whole (such as whether it is a buffering
213 layer), then follow the functions which fall into four basic groups:
</p>
216 <p>Opening and setup functions
</p>
219 <p>Basic IO operations
</p>
222 <p>Stdio class buffering options.
</p>
225 <p>Functions to support Perl's traditional ``fast'' access to the buffer.
</p>
228 <p>A layer does not have to implement all the functions, but the whole
229 table has to be present. Unimplemented slots can be NULL (which will
230 result in an error when called) or can be filled in with stubs to
231 ``inherit'' behaviour from a ``base class''. This ``inheritance'' is fixed
232 for all instances of the layer, but as the layer chooses which stubs
233 to populate the table, limited ``multiple inheritance'' is possible.
</p>
236 <h2><a name=
"perinstance_data">Per-instance Data
</a></h2>
237 <p>The per-instance data are held in memory beyond the basic PerlIOl
238 struct, by making a PerlIOl the first member of the layer's struct
243 struct _PerlIO base; /* Base
"class
" info */
244 STDCHAR * buf; /* Start of buffer */
245 STDCHAR * end; /* End of valid part of buffer */
246 STDCHAR * ptr; /* Current position in buffer */
247 Off_t posn; /* Offset of buf into the file */
248 Size_t bufsiz; /* Real size of buffer */
249 IV oneword; /* Emergency buffer */
251 <p>In this way (as for perl's scalars) a pointer to a PerlIOBuf can be
252 treated as a pointer to a PerlIOl.
</p>
255 <h2><a name=
"layers_in_action_">Layers in action.
</a></h2>
259 +-----------+ +----------+ +--------+
260 PerlIO -
>| |---
>| next |---
>| NULL |
261 +-----------+ +----------+ +--------+
262 | | | buffer | | fd |
263 +-----------+ | | +--------+
264 | | +----------+
</pre>
265 <p>The above attempts to show how the layer scheme works in a simple case.
266 The application's
<code>PerlIO *
</code> points to an entry in the
<code>table(s)
</code>
267 representing open (allocated) handles. For example the first three slots
268 in the table correspond to
<code>stdin
</code>,
<code>stdout
</code> and
<code>stderr
</code>. The table
269 in turn points to the current ``top'' layer for the handle - in this case
270 an instance of the generic buffering layer ``perlio''. That layer in turn
271 points to the next layer down - in this case the lowlevel ``unix'' layer.
</p>
272 <p>The above is roughly equivalent to a ``stdio'' buffered stream, but with
273 much more flexibility:
</p>
276 <p>If Unix level
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_read"><code>read
</code></a>/
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_write"><code>write
</code></a>/
<code>lseek
</code> is not appropriate for (say)
277 sockets then the ``unix'' layer can be replaced (at open time or even
278 dynamically) with a ``socket'' layer.
</p>
281 <p>Different handles can have different buffering schemes. The ``top''
282 layer could be the ``mmap'' layer if reading disk files was quicker
283 using
<code>mmap
</code> than
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_read"><code>read
</code></a>. An ``unbuffered'' stream can be implemented
284 simply by not having a buffer layer.
</p>
287 <p>Extra layers can be inserted to process the data as it flows through.
288 This was the driving need for including the scheme in perl
5.7.0+ - we
289 needed a mechanism to allow data to be translated between perl's
290 internal encoding (conceptually at least Unicode as UTF-
8), and the
291 ``native'' format used by the system. This is provided by the
292 ``:encoding(xxxx)'' layer which typically sits above the buffering layer.
</p>
295 <p>A layer can be added that does ``\n'' to CRLF translation. This layer
296 can be used on any platform, not just those that normally do such
302 <h2><a name=
"perinstance_flag_bits">Per-instance flag bits
</a></h2>
303 <p>The generic flag bits are a hybrid of
<code>O_XXXXX
</code> style flags deduced
304 from the mode string passed to
<code>PerlIO_open()
</code>, and state bits for
305 typical buffer layers.
</p>
307 <dt><strong><a name=
"item_perlio_f_eof">PERLIO_F_EOF
</a></strong>
313 <dt><strong><a name=
"item_perlio_f_canwrite">PERLIO_F_CANWRITE
</a></strong>
316 <p>Writes are permitted, i.e. opened as ``w'' or ``r+'' or ``a'', etc.
</p>
319 <dt><strong><a name=
"item_perlio_f_canread">PERLIO_F_CANREAD
</a></strong>
322 <p>Reads are permitted i.e. opened ``r'' or ``w+'' (or even ``a+'' - ick).
</p>
325 <dt><strong><a name=
"item_perlio_f_error">PERLIO_F_ERROR
</a></strong>
328 <p>An error has occurred (for
<code>PerlIO_error()
</code>).
</p>
331 <dt><strong><a name=
"item_perlio_f_truncate">PERLIO_F_TRUNCATE
</a></strong>
334 <p>Truncate file suggested by open mode.
</p>
337 <dt><strong><a name=
"item_perlio_f_append">PERLIO_F_APPEND
</a></strong>
340 <p>All writes should be appends.
</p>
343 <dt><strong><a name=
"item_perlio_f_crlf">PERLIO_F_CRLF
</a></strong>
346 <p>Layer is performing Win32-like ``\n'' mapped to CR,LF for output and CR,LF
347 mapped to ``\n'' for input. Normally the provided ``crlf'' layer is the only
348 layer that need bother about this.
<code>PerlIO_binmode()
</code> will mess with this
349 flag rather than add/remove layers if the
<a href=
"#item_perlio_k_cancrlf"><code>PERLIO_K_CANCRLF
</code></a> bit is set
350 for the layers class.
</p>
353 <dt><strong><a name=
"item_perlio_f_utf8">PERLIO_F_UTF8
</a></strong>
356 <p>Data written to this layer should be UTF-
8 encoded; data provided
357 by this layer should be considered UTF-
8 encoded. Can be set on any layer
358 by ``:utf8'' dummy layer. Also set on ``:encoding'' layer.
</p>
361 <dt><strong><a name=
"item_perlio_f_unbuf">PERLIO_F_UNBUF
</a></strong>
364 <p>Layer is unbuffered - i.e. write to next layer down should occur for
365 each write to this layer.
</p>
368 <dt><strong><a name=
"item_perlio_f_wrbuf">PERLIO_F_WRBUF
</a></strong>
371 <p>The buffer for this layer currently holds data written to it but not sent
375 <dt><strong><a name=
"item_perlio_f_rdbuf">PERLIO_F_RDBUF
</a></strong>
378 <p>The buffer for this layer currently holds unconsumed data read from
382 <dt><strong><a name=
"item_perlio_f_linebuf">PERLIO_F_LINEBUF
</a></strong>
385 <p>Layer is line buffered. Write data should be passed to next layer down
386 whenever a ``\n'' is seen. Any data beyond the ``\n'' should then be
390 <dt><strong><a name=
"item_perlio_f_temp">PERLIO_F_TEMP
</a></strong>
393 <p>File has been
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_unlink"><code>unlink()
</code></a>ed, or should be deleted on
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_close"><code>close()
</code></a>.
</p>
396 <dt><strong><a name=
"item_perlio_f_open">PERLIO_F_OPEN
</a></strong>
399 <p>Handle is open.
</p>
402 <dt><strong><a name=
"item_perlio_f_fastgets">PERLIO_F_FASTGETS
</a></strong>
405 <p>This instance of this layer supports the ``fast
<code>gets
</code>'' interface.
406 Normally set based on
<a href=
"#item_perlio_k_fastgets"><code>PERLIO_K_FASTGETS
</code></a> for the class and by the
407 existence of the
<code>function(s)
</code> in the table. However a class that
408 normally provides that interface may need to avoid it on a
409 particular instance. The ``pending'' layer needs to do this when
410 it is pushed above a layer which does not support the interface.
411 (Perl's
<code>sv_gets()
</code> does not expect the streams fast
<code>gets
</code> behaviour
412 to change during one ``get''.)
</p>
418 <h2><a name=
"methods_in_detail">Methods in Detail
</a></h2>
420 <dt><strong><a name=
"item_fsize">fsize
</a></strong>
427 <p>Size of the function table. This is compared against the value PerlIO
428 code ``knows'' as a compatibility check. Future versions
<em>may
</em> be able
429 to tolerate layers compiled against an old version of the headers.
</p>
431 <dt><strong><a name=
"item_name">name
</a></strong>
438 <p>The name of the layer whose
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()
</code></a> method Perl should invoke on
439 open(). For example if the layer is called APR, you will call:
</p>
443 open $fh,
">:APR
", ...
</pre>
446 <p>and Perl knows that it has to invoke the
<code>PerlIOAPR_open()
</code> method
447 implemented by the APR layer.
</p>
449 <dt><strong><a name=
"item_size">size
</a></strong>
456 <p>The size of the per-instance data structure, e.g.:
</p>
460 sizeof(PerlIOAPR)
</pre>
463 <p>If this field is zero then
<code>PerlIO_pushed
</code> does not malloc anything
464 and assumes layer's Pushed function will do any required layer stack
465 manipulation - used to avoid malloc/free overhead for dummy layers.
466 If the field is non-zero it must be at least the size of
<code>PerlIOl
</code>,
467 <code>PerlIO_pushed
</code> will allocate memory for the layer's data structures
468 and link new layer onto the stream's stack. (If the layer's Pushed
469 method returns an error indication the layer is popped again.)
</p>
471 <dt><strong><a name=
"item_kind">kind
</a></strong>
478 <li><strong><a name=
"item_perlio_k_buffered">PERLIO_K_BUFFERED
</a></strong>
480 <p>The layer is buffered.
</p>
482 <li><strong><a name=
"item_perlio_k_raw">PERLIO_K_RAW
</a></strong>
484 <p>The layer is acceptable to have in a
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_binmode"><code>binmode(FH)
</code></a> stack - i.e. it does not
485 (or will configure itself not to) transform bytes passing through it.
</p>
487 <li><strong><a name=
"item_perlio_k_cancrlf">PERLIO_K_CANCRLF
</a></strong>
489 <p>Layer can translate between ``\n'' and CRLF line ends.
</p>
491 <li><strong><a name=
"item_perlio_k_fastgets">PERLIO_K_FASTGETS
</a></strong>
493 <p>Layer allows buffer snooping.
</p>
495 <li><strong><a name=
"item_perlio_k_multiarg">PERLIO_K_MULTIARG
</a></strong>
497 <p>Used when the layer's
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()
</code></a> accepts more arguments than usual. The
498 extra arguments should come not before the
<code>MODE
</code> argument. When this
499 flag is used it's up to the layer to validate the args.
</p>
502 <dt><strong><a name=
"item_pushed">Pushed
</a></strong>
506 IV (*Pushed)(pTHX_ PerlIO *f,const char *mode, SV *arg);
</pre>
509 <p>The only absolutely mandatory method. Called when the layer is pushed
510 onto the stack. The
<code>mode
</code> argument may be NULL if this occurs
511 post-open. The
<code>arg
</code> will be non-
<code>NULL
</code> if an argument string was
512 passed. In most cases this should call
<code>PerlIOBase_pushed()
</code> to
513 convert
<code>mode
</code> into the appropriate
<code>PERLIO_F_XXXXX
</code> flags in
514 addition to any actions the layer itself takes. If a layer is not
515 expecting an argument it need neither save the one passed to it, nor
516 provide
<a href=
"#item_getarg"><code>Getarg()
</code></a> (it could perhaps
<code>Perl_warn
</code> that the argument
517 was un-expected).
</p>
520 <p>Returns
0 on success. On failure returns -
1 and should set errno.
</p>
522 <dt><strong><a name=
"item_popped">Popped
</a></strong>
526 IV (*Popped)(pTHX_ PerlIO *f);
</pre>
529 <p>Called when the layer is popped from the stack. A layer will normally
530 be popped after
<a href=
"#item_close"><code>Close()
</code></a> is called. But a layer can be popped
531 without being closed if the program is dynamically managing layers on
532 the stream. In such cases
<a href=
"#item_popped"><code>Popped()
</code></a> should free any resources
533 (buffers, translation tables, ...) not held directly in the layer's
534 struct. It should also
<a href=
"#item_unread"><code>Unread()
</code></a> any unconsumed data that has been
535 read and buffered from the layer below back to that layer, so that it
536 can be re-provided to what ever is now above.
</p>
539 <p>Returns
0 on success and failure. If
<a href=
"#item_popped"><code>Popped()
</code></a> returns
<em>true
</em> then
540 <em>perlio.c
</em> assumes that either the layer has popped itself, or the
541 layer is super special and needs to be retained for other reasons.
542 In most cases it should return
<em>false
</em>.
</p>
544 <dt><strong><a name=
"item_open">Open
</a></strong>
548 PerlIO * (*Open)(...);
</pre>
551 <p>The
<a href=
"#item_open"><code>Open()
</code></a> method has lots of arguments because it combines the
552 functions of perl's
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open
</code></a>,
<code>PerlIO_open
</code>, perl's
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sysopen"><code>sysopen
</code></a>,
553 <code>PerlIO_fdopen
</code> and
<code>PerlIO_reopen
</code>. The full prototype is as
558 PerlIO * (*Open)(pTHX_ PerlIO_funcs *tab,
561 int fd, int imode, int perm,
563 int narg, SV **args);
</pre>
566 <p>Open should (perhaps indirectly) call
<code>PerlIO_allocate()
</code> to allocate
567 a slot in the table and associate it with the layers information for
568 the opened file, by calling
<code>PerlIO_push
</code>. The
<em>layers
</em> AV is an
569 array of all the layers destined for the
<code>PerlIO *
</code>, and any
570 arguments passed to them,
<em>n
</em> is the index into that array of the
571 layer being called. The macro
<code>PerlIOArg
</code> will return a (possibly
572 <code>NULL
</code>) SV * for the argument passed to the layer.
</p>
575 <p>The
<em>mode
</em> string is an ``
<code>fopen()
</code>-like'' string which would match
576 the regular expression
<code>/^[I#]?[rwa]\+?[bt]?$/
</code>.
</p>
579 <p>The
<code>'I'
</code> prefix is used during creation of
<code>stdin
</code>..
<code>stderr
</code> via
580 special
<code>PerlIO_fdopen
</code> calls; the
<code>'#'
</code> prefix means that this is
581 <a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sysopen"><code>sysopen
</code></a> and that
<em>imode
</em> and
<em>perm
</em> should be passed to
582 <code>PerlLIO_open3
</code>;
<code>'r'
</code> means
<strong>r
</strong>ead,
<code>'w'
</code> means
<strong>w
</strong>rite and
583 <code>'a'
</code> means
<strong>a
</strong>ppend. The
<code>'+'
</code> suffix means that both reading and
584 writing/appending are permitted. The
<code>'b'
</code> suffix means file should
585 be binary, and
<code>'t'
</code> means it is text. (Almost all layers should do
586 the IO in binary mode, and ignore the b/t bits. The
<code>:crlf
</code> layer
587 should be pushed to handle the distinction.)
</p>
590 <p>If
<em>old
</em> is not
<code>NULL
</code> then this is a
<code>PerlIO_reopen
</code>. Perl itself
591 does not use this (yet?) and semantics are a little vague.
</p>
594 <p>If
<em>fd
</em> not negative then it is the numeric file descriptor
<em>fd
</em>,
595 which will be open in a manner compatible with the supplied mode
596 string, the call is thus equivalent to
<code>PerlIO_fdopen
</code>. In this case
597 <em>nargs
</em> will be zero.
</p>
600 <p>If
<em>nargs
</em> is greater than zero then it gives the number of arguments
601 passed to
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open
</code></a>, otherwise it will be
1 if for example
602 <code>PerlIO_open
</code> was called. In simple cases
<code>SvPV_nolen(*args)
</code> is the
603 pathname to open.
</p>
606 <p>Having said all that translation-only layers do not need to provide
607 <a href=
"#item_open"><code>Open()
</code></a> at all, but rather leave the opening to a lower level layer
608 and wait to be ``pushed''. If a layer does provide
<a href=
"#item_open"><code>Open()
</code></a> it should
609 normally call the
<a href=
"#item_open"><code>Open()
</code></a> method of next layer down (if any) and
610 then push itself on top if that succeeds.
</p>
613 <p>If
<code>PerlIO_push
</code> was performed and open has failed, it must
614 <code>PerlIO_pop
</code> itself, since if it's not, the layer won't be removed
615 and may cause bad problems.
</p>
618 <p>Returns
<code>NULL
</code> on failure.
</p>
620 <dt><strong><a name=
"item_binmode">Binmode
</a></strong>
624 IV (*Binmode)(pTHX_ PerlIO *f);
</pre>
627 <p>Optional. Used when
<code>:raw
</code> layer is pushed (explicitly or as a result
628 of binmode(FH)). If not present layer will be popped. If present
629 should configure layer as binary (or pop itself) and return
0.
630 If it returns -
1 for error
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_binmode"><code>binmode
</code></a> will fail with layer
631 still on the stack.
</p>
633 <dt><strong><a name=
"item_getarg">Getarg
</a></strong>
637 SV * (*Getarg)(pTHX_ PerlIO *f,
638 CLONE_PARAMS *param, int flags);
</pre>
641 <p>Optional. If present should return an SV * representing the string
642 argument passed to the layer when it was
643 pushed. e.g. ``:encoding(ascii)'' would return an SvPV with value
644 ``ascii''. (
<em>param
</em> and
<em>flags
</em> arguments can be ignored in most
648 <p><a href=
"#item_dup"><code>Dup
</code></a> uses
<a href=
"#item_getarg"><code>Getarg
</code></a> to retrieve the argument originally passed to
649 <a href=
"#item_pushed"><code>Pushed
</code></a>, so you must implement this function if your layer has an
650 extra argument to
<a href=
"#item_pushed"><code>Pushed
</code></a> and will ever be
<a href=
"#item_dup"><code>Dup
</code></a>ed.
</p>
652 <dt><strong><a name=
"item_fileno">Fileno
</a></strong>
656 IV (*Fileno)(pTHX_ PerlIO *f);
</pre>
659 <p>Returns the Unix/Posix numeric file descriptor for the handle. Normally
660 <code>PerlIOBase_fileno()
</code> (which just asks next layer down) will suffice
664 <p>Returns -
1 on error, which is considered to include the case where the
665 layer cannot provide such a file descriptor.
</p>
667 <dt><strong><a name=
"item_dup">Dup
</a></strong>
671 PerlIO * (*Dup)(pTHX_ PerlIO *f, PerlIO *o,
672 CLONE_PARAMS *param, int flags);
</pre>
675 <p>XXX: Needs more docs.
</p>
678 <p>Used as part of the ``clone'' process when a thread is spawned (in which
679 case param will be non-NULL) and when a stream is being duplicated via
680 '
&' in the
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open
</code></a>.
</p>
683 <p>Similar to
<a href=
"#item_open"><code>Open
</code></a>, returns PerlIO* on success,
<code>NULL
</code> on failure.
</p>
685 <dt><strong><a name=
"item_read">Read
</a></strong>
689 SSize_t (*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);
</pre>
692 <p>Basic read operation.
</p>
695 <p>Typically will call
<a href=
"#item_fill"><code>Fill
</code></a> and manipulate pointers (possibly via the
696 API).
<code>PerlIOBuf_read()
</code> may be suitable for derived classes which
697 provide ``fast gets'' methods.
</p>
700 <p>Returns actual bytes read, or -
1 on an error.
</p>
702 <dt><strong><a name=
"item_unread">Unread
</a></strong>
706 SSize_t (*Unread)(pTHX_ PerlIO *f,
707 const void *vbuf, Size_t count);
</pre>
710 <p>A superset of stdio's
<code>ungetc()
</code>. Should arrange for future reads to
711 see the bytes in
<code>vbuf
</code>. If there is no obviously better implementation
712 then
<code>PerlIOBase_unread()
</code> provides the function by pushing a ``fake''
713 ``pending'' layer above the calling layer.
</p>
716 <p>Returns the number of unread chars.
</p>
718 <dt><strong><a name=
"item_write">Write
</a></strong>
722 SSize_t (*Write)(PerlIO *f, const void *vbuf, Size_t count);
</pre>
725 <p>Basic write operation.
</p>
728 <p>Returns bytes written or -
1 on an error.
</p>
730 <dt><strong><a name=
"item_seek">Seek
</a></strong>
734 IV (*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);
</pre>
737 <p>Position the file pointer. Should normally call its own
<a href=
"#item_flush"><code>Flush
</code></a>
738 method and then the
<a href=
"#item_seek"><code>Seek
</code></a> method of next layer down.
</p>
741 <p>Returns
0 on success, -
1 on failure.
</p>
743 <dt><strong><a name=
"item_tell">Tell
</a></strong>
747 Off_t (*Tell)(pTHX_ PerlIO *f);
</pre>
750 <p>Return the file pointer. May be based on layers cached concept of
751 position to avoid overhead.
</p>
754 <p>Returns -
1 on failure to get the file pointer.
</p>
756 <dt><strong><a name=
"item_close">Close
</a></strong>
760 IV (*Close)(pTHX_ PerlIO *f);
</pre>
763 <p>Close the stream. Should normally call
<code>PerlIOBase_close()
</code> to flush
764 itself and close layers below, and then deallocate any data structures
765 (buffers, translation tables, ...) not held directly in the data
769 <p>Returns
0 on success, -
1 on failure.
</p>
771 <dt><strong><a name=
"item_flush">Flush
</a></strong>
775 IV (*Flush)(pTHX_ PerlIO *f);
</pre>
778 <p>Should make stream's state consistent with layers below. That is, any
779 buffered write data should be written, and file position of lower layers
780 adjusted for data read from below but not actually consumed.
781 (Should perhaps
<a href=
"#item_unread"><code>Unread()
</code></a> such data to the lower layer.)
</p>
784 <p>Returns
0 on success, -
1 on failure.
</p>
786 <dt><strong><a name=
"item_fill">Fill
</a></strong>
790 IV (*Fill)(pTHX_ PerlIO *f);
</pre>
793 <p>The buffer for this layer should be filled (for read) from layer
794 below. When you ``subclass'' PerlIOBuf layer, you want to use its
795 <em>_read
</em> method and to supply your own fill method, which fills the
796 PerlIOBuf's buffer.
</p>
799 <p>Returns
0 on success, -
1 on failure.
</p>
801 <dt><strong><a name=
"item_eof">Eof
</a></strong>
805 IV (*Eof)(pTHX_ PerlIO *f);
</pre>
808 <p>Return end-of-file indicator.
<code>PerlIOBase_eof()
</code> is normally sufficient.
</p>
811 <p>Returns
0 on end-of-file,
1 if not end-of-file, -
1 on error.
</p>
813 <dt><strong><a name=
"item_error">Error
</a></strong>
817 IV (*Error)(pTHX_ PerlIO *f);
</pre>
820 <p>Return error indicator.
<code>PerlIOBase_error()
</code> is normally sufficient.
</p>
823 <p>Returns
1 if there is an error (usually when
<a href=
"#item_perlio_f_error"><code>PERLIO_F_ERROR
</code></a> is set,
826 <dt><strong><a name=
"item_clearerr">Clearerr
</a></strong>
830 void (*Clearerr)(pTHX_ PerlIO *f);
</pre>
833 <p>Clear end-of-file and error indicators. Should call
<code>PerlIOBase_clearerr()
</code>
834 to set the
<code>PERLIO_F_XXXXX
</code> flags, which may suffice.
</p>
836 <dt><strong><a name=
"item_setlinebuf">Setlinebuf
</a></strong>
840 void (*Setlinebuf)(pTHX_ PerlIO *f);
</pre>
843 <p>Mark the stream as line buffered.
<code>PerlIOBase_setlinebuf()
</code> sets the
844 PERLIO_F_LINEBUF flag and is normally sufficient.
</p>
846 <dt><strong><a name=
"item_get_base">Get_base
</a></strong>
850 STDCHAR * (*Get_base)(pTHX_ PerlIO *f);
</pre>
853 <p>Allocate (if not already done so) the read buffer for this layer and
854 return pointer to it. Return NULL on failure.
</p>
856 <dt><strong><a name=
"item_get_bufsiz">Get_bufsiz
</a></strong>
860 Size_t (*Get_bufsiz)(pTHX_ PerlIO *f);
</pre>
863 <p>Return the number of bytes that last
<a href=
"#item_fill"><code>Fill()
</code></a> put in the buffer.
</p>
865 <dt><strong><a name=
"item_get_ptr">Get_ptr
</a></strong>
869 STDCHAR * (*Get_ptr)(pTHX_ PerlIO *f);
</pre>
872 <p>Return the current read pointer relative to this layer's buffer.
</p>
874 <dt><strong><a name=
"item_get_cnt">Get_cnt
</a></strong>
878 SSize_t (*Get_cnt)(pTHX_ PerlIO *f);
</pre>
881 <p>Return the number of bytes left to be read in the current buffer.
</p>
883 <dt><strong><a name=
"item_set_ptrcnt">Set_ptrcnt
</a></strong>
887 void (*Set_ptrcnt)(pTHX_ PerlIO *f,
888 STDCHAR *ptr, SSize_t cnt);
</pre>
891 <p>Adjust the read pointer and count of bytes to match
<code>ptr
</code> and/or
<code>cnt
</code>.
892 The application (or layer above) must ensure they are consistent.
893 (Checking is allowed by the paranoid.)
</p>
898 <h2><a name=
"utilities">Utilities
</a></h2>
899 <p>To ask for the next layer down use PerlIONext(PerlIO *f).
</p>
900 <p>To check that a PerlIO* is valid use PerlIOValid(PerlIO *f). (All
901 this does is really just to check that the pointer is non-NULL and
902 that the pointer behind that is non-NULL.)
</p>
903 <p>PerlIOBase(PerlIO *f) returns the ``Base'' pointer, or in other words,
904 the
<code>PerlIOl*
</code> pointer.
</p>
905 <p>PerlIOSelf(PerlIO* f, type) return the PerlIOBase cast to a type.
</p>
906 <p>Perl_PerlIO_or_Base(PerlIO* f, callback, base, failure, args) either
907 calls the
<em>callback
</em> from the functions of the layer
<em>f
</em> (just by
908 the name of the IO function, like ``Read'') with the
<em>args
</em>, or if
909 there is no such callback, calls the
<em>base
</em> version of the callback
910 with the same args, or if the f is invalid, set errno to EBADF and
911 return
<em>failure
</em>.
</p>
912 <p>Perl_PerlIO_or_fail(PerlIO* f, callback, failure, args) either calls
913 the
<em>callback
</em> of the functions of the layer
<em>f
</em> with the
<em>args
</em>,
914 or if there is no such callback, set errno to EINVAL. Or if the f is
915 invalid, set errno to EBADF and return
<em>failure
</em>.
</p>
916 <p>Perl_PerlIO_or_Base_void(PerlIO* f, callback, base, args) either calls
917 the
<em>callback
</em> of the functions of the layer
<em>f
</em> with the
<em>args
</em>,
918 or if there is no such callback, calls the
<em>base
</em> version of the
919 callback with the same args, or if the f is invalid, set errno to
921 <p>Perl_PerlIO_or_fail_void(PerlIO* f, callback, args) either calls the
922 <em>callback
</em> of the functions of the layer
<em>f
</em> with the
<em>args
</em>, or if
923 there is no such callback, set errno to EINVAL. Or if the f is
924 invalid, set errno to EBADF.
</p>
927 <h2><a name=
"implementing_perlio_layers">Implementing PerlIO Layers
</a></h2>
928 <p>If you find the implementation document unclear or not sufficient,
929 look at the existing PerlIO layer implementations, which include:
</p>
931 <li><strong><a name=
"item_c_implementations">C implementations
</a></strong>
933 <p>The
<em>perlio.c
</em> and
<em>perliol.h
</em> in the Perl core implement the
934 ``unix'', ``perlio'', ``stdio'', ``crlf'', ``utf8'', ``byte'', ``raw'', ``pending''
935 layers, and also the ``mmap'' and ``win32'' layers if applicable.
936 (The ``win32'' is currently unfinished and unused, to see what is used
937 instead in Win32, see
<a href=
"file://C|\msysgit\mingw\html/lib/PerlIO/querying_the_layers_of_filehandles.html">Querying the layers of filehandles in the PerlIO manpage
</a> .)
</p>
938 <p>PerlIO::encoding, PerlIO::scalar, PerlIO::via in the Perl core.
</p>
939 <p>PerlIO::gzip and APR::PerlIO (mod_perl
2.0) on CPAN.
</p>
941 <li><strong><a name=
"item_perl_implementations">Perl implementations
</a></strong>
943 <p>PerlIO::via::QuotedPrint in the Perl core and PerlIO::via::* on CPAN.
</p>
946 <p>If you are creating a PerlIO layer, you may want to be lazy, in other
947 words, implement only the methods that interest you. The other methods
948 you can either replace with the ``blank'' methods
</p>
951 PerlIOBase_noop_fail
</pre>
952 <p>(which do nothing, and return zero and -
1, respectively) or for
953 certain methods you may assume a default behaviour by using a NULL
954 method. The Open method looks for help in the 'parent' layer.
955 The following table summarizes the behaviour:
</p>
957 method behaviour with NULL
</pre>
959 Clearerr PerlIOBase_clearerr
960 Close PerlIOBase_close
963 Error PerlIOBase_error
964 Fileno PerlIOBase_fileno
979 Setlinebuf PerlIOBase_setlinebuf
981 Unread PerlIOBase_unread
984 FAILURE Set errno (to EINVAL in UNIXish, to LIB$_INVARG in VMS) and
985 return -
1 (for numeric return values) or NULL (for pointers)
986 INHERITED Inherited from the layer below
987 SUCCESS Return
0 (for numeric return values) or a pointer
</pre>
990 <h2><a name=
"core_layers">Core Layers
</a></h2>
991 <p>The file
<code>perlio.c
</code> provides the following layers:
</p>
993 <dt><strong><a name=
"item__22unix_22">``unix''
</a></strong>
996 <p>A basic non-buffered layer which calls Unix/POSIX
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_read"><code>read()
</code></a>,
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_write"><code>write()
</code></a>,
997 <code>lseek()
</code>,
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_close"><code>close()
</code></a>. No buffering. Even on platforms that distinguish
998 between O_TEXT and O_BINARY this layer is always O_BINARY.
</p>
1001 <dt><strong><a name=
"item__22perlio_22">``perlio''
</a></strong>
1004 <p>A very complete generic buffering layer which provides the whole of
1005 PerlIO API. It is also intended to be used as a ``base class'' for other
1006 layers. (For example its
<a href=
"#item_read"><code>Read()
</code></a> method is implemented in terms of
1007 the
<a href=
"#item_get_cnt"><code>Get_cnt()
</code></a>/
<a href=
"#item_get_ptr"><code>Get_ptr()
</code></a>/
<a href=
"#item_set_ptrcnt"><code>Set_ptrcnt()
</code></a> methods).
</p>
1010 <p>``perlio'' over ``unix'' provides a complete replacement for stdio as seen
1011 via PerlIO API. This is the default for USE_PERLIO when system's stdio
1012 does not permit perl's ``fast gets'' access, and which do not
1013 distinguish between
<code>O_TEXT
</code> and
<code>O_BINARY
</code>.
</p>
1016 <dt><strong><a name=
"item__22stdio_22">``stdio''
</a></strong>
1019 <p>A layer which provides the PerlIO API via the layer scheme, but
1020 implements it by calling system's stdio. This is (currently) the default
1021 if system's stdio provides sufficient access to allow perl's ``fast gets''
1022 access and which do not distinguish between
<code>O_TEXT
</code> and
<code>O_BINARY
</code>.
</p>
1025 <dt><strong><a name=
"item__22crlf_22">``crlf''
</a></strong>
1028 <p>A layer derived using ``perlio'' as a base class. It provides Win32-like
1029 ``\n'' to CR,LF translation. Can either be applied above ``perlio'' or serve
1030 as the buffer layer itself. ``crlf'' over ``unix'' is the default if system
1031 distinguishes between
<code>O_TEXT
</code> and
<code>O_BINARY
</code> opens. (At some point
1032 ``unix'' will be replaced by a ``native'' Win32 IO layer on that platform,
1033 as Win32's read/write layer has various drawbacks.) The ``crlf'' layer is
1034 a reasonable model for a layer which transforms data in some way.
</p>
1037 <dt><strong><a name=
"item__22mmap_22">``mmap''
</a></strong>
1040 <p>If Configure detects
<code>mmap()
</code> functions this layer is provided (with
1041 ``perlio'' as a ``base'') which does ``read'' operations by mmap()ing the
1042 file. Performance improvement is marginal on modern systems, so it is
1043 mainly there as a proof of concept. It is likely to be unbundled from
1044 the core at some point. The ``mmap'' layer is a reasonable model for a
1045 minimalist ``derived'' layer.
</p>
1048 <dt><strong><a name=
"item__22pending_22">``pending''
</a></strong>
1051 <p>An ``internal'' derivative of ``perlio'' which can be used to provide
1052 <a href=
"#item_unread"><code>Unread()
</code></a> function for layers which have no buffer or cannot be
1053 bothered. (Basically this layer's
<a href=
"#item_fill"><code>Fill()
</code></a> pops itself off the stack
1054 and so resumes reading from layer below.)
</p>
1057 <dt><strong><a name=
"item__22raw_22">``raw''
</a></strong>
1060 <p>A dummy layer which never exists on the layer stack. Instead when
1061 ``pushed'' it actually pops the stack removing itself, it then calls
1062 Binmode function table entry on all the layers in the stack - normally
1063 this (via PerlIOBase_binmode) removes any layers which do not have
1064 <a href=
"#item_perlio_k_raw"><code>PERLIO_K_RAW
</code></a> bit set. Layers can modify that behaviour by defining
1065 their own Binmode entry.
</p>
1068 <dt><strong><a name=
"item__22utf8_22">``utf8''
</a></strong>
1071 <p>Another dummy layer. When pushed it pops itself and sets the
1072 <a href=
"#item_perlio_f_utf8"><code>PERLIO_F_UTF8
</code></a> flag on the layer which was (and now is once more)
1073 the top of the stack.
</p>
1077 <p>In addition
<em>perlio.c
</em> also provides a number of
<code>PerlIOBase_xxxx()
</code>
1078 functions which are intended to be used in the table slots of classes
1079 which do not need to do anything special for a particular method.
</p>
1082 <h2><a name=
"extension_layers">Extension Layers
</a></h2>
1083 <p>Layers can made available by extension modules. When an unknown layer
1084 is encountered the PerlIO code will perform the equivalent of :
</p>
1086 use PerlIO 'layer';
</pre>
1087 <p>Where
<em>layer
</em> is the unknown layer.
<em>PerlIO.pm
</em> will then attempt to:
</p>
1089 require PerlIO::layer;
</pre>
1090 <p>If after that process the layer is still not defined then the
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open
</code></a>
1092 <p>The following extension layers are bundled with perl:
</p>
1094 <dt><strong><a name=
"item__22_3aencoding_22">``:encoding''
</a></strong>
1101 <p>makes this layer available, although
<em>PerlIO.pm
</em> ``knows'' where to
1102 find it. It is an example of a layer which takes an argument as it is
1107 open( $fh,
"<:encoding(iso-
8859-
7)
", $pathname );
</pre>
1109 <dt><strong><a name=
"item__22_3ascalar_22">``:scalar''
</a></strong>
1112 <p>Provides support for reading data from and writing data to a scalar.
</p>
1116 open( $fh,
"+
<:scalar
", \$scalar );
</pre>
1119 <p>When a handle is so opened, then reads get bytes from the string value
1120 of
<em>$scalar
</em>, and writes change the value. In both cases the position
1121 in
<em>$scalar
</em> starts as zero but can be altered via
<code>seek
</code>, and
1122 determined via
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_tell"><code>tell
</code></a>.
</p>
1125 <p>Please note that this layer is implied when calling
<a href=
"file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()
</code></a> thus:
</p>
1129 open( $fh,
"+
<", \$scalar );
</pre>
1132 <dt><strong><a name=
"item__22_3avia_22">``:via''
</a></strong>
1135 <p>Provided to allow layers to be implemented as Perl code. For instance:
</p>
1139 use PerlIO::via::StripHTML;
1140 open( my $fh,
"<:via(StripHTML)
",
"index.html
" );
</pre>
1143 <p>See
<a href=
"file://C|\msysgit\mingw\html/lib/PerlIO/via.html">the PerlIO::via manpage
</a> for details.
</p>
1150 <h1><a name=
"todo">TODO
</a></h1>
1151 <p>Things that need to be done to improve this document.
</p>
1154 <p>Explain how to make a valid fh without going through open()(i.e. apply
1155 a layer). For example if the file is not opened through perl, but we
1156 want to get back a fh, like it was opened by Perl.
</p>
1157 <p>How PerlIO_apply_layera fits in, where its docs, was it made public?
</p>
1158 <p>Currently the example could be something like this:
</p>
1160 PerlIO *foo_to_PerlIO(pTHX_ char *mode, ...)
1162 char *mode; /*
"w
",
"r
", etc */
1163 const char *layers =
":APR
"; /* the layer name */
1164 PerlIO *f = PerlIO_allocate(aTHX);
1169 PerlIO_apply_layers(aTHX_ f, mode, layers);
</pre>
1172 PerlIOAPR *st = PerlIOSelf(f, PerlIOAPR);
1173 /* fill in the st struct, as in _open() */
1175 PerlIOBase(f)-
>flags |= PERLIO_F_OPEN;
</pre>
1183 <p>fix/add the documentation in places marked as XXX.
</p>
1186 <p>The handling of errors by the layer is not specified. e.g. when $!
1187 should be set explicitly, when the error handling should be just
1188 delegated to the top layer.
</p>
1189 <p>Probably give some hints on using
<code>SETERRNO()
</code> or pointers to where they
1193 <p>I think it would help to give some concrete examples to make it easier
1194 to understand the API. Of course I agree that the API has to be
1195 concise, but since there is no second document that is more of a
1196 guide, I think that it'd make it easier to start with the doc which is
1197 an API, but has examples in it in places where things are unclear, to
1198 a person who is not a PerlIO guru (yet).
</p>
1201 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
1202 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
1203 <big><strong><span class=
"block"> perliol - C API for Perl's implementation of IO in Layers.
</span></strong></big>