Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perliol.html
blob0f05710df6fd6da31d5d52b3915c70dfef6c0d79
1 <?xml version="1.0" ?>
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">
4 <head>
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:" />
8 </head>
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">&nbsp;perliol - C API for Perl's implementation of IO in Layers.</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <ul>
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>
40 </ul>
42 <li><a href="#todo">TODO</a></li>
43 </ul>
44 <!-- INDEX END -->
46 <hr />
47 <p>
48 </p>
49 <h1><a name="name">NAME</a></h1>
50 <p>perliol - C API for Perl's implementation of IO in Layers.</p>
51 <p>
52 </p>
53 <hr />
54 <h1><a name="synopsis">SYNOPSIS</a></h1>
55 <pre>
56 /* Defining a layer ... */
57 #include &lt;<a href="//C|\msysgit\mingw\html/pod/perliol.html">perliol</a>.h&gt;</pre>
58 <p>
59 </p>
60 <hr />
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>
65 <p>
66 </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>
75 <p>
76 </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
91 data.</p>
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
111 order.</p>
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>
118 </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>
129 </p>
130 <h2><a name="data_structures">Data Structures</a></h2>
131 <p>The basic data structure is a PerlIOl:</p>
132 <pre>
133 typedef struct _PerlIO PerlIOl;
134 typedef struct _PerlIO_funcs PerlIO_funcs;
135 typedef PerlIOl *PerlIO;</pre>
136 <pre>
137 struct _PerlIO
139 PerlIOl * next; /* Lower layer */
140 PerlIO_funcs * tab; /* Functions for this layer */
141 IV flags; /* Various flags for state */
142 };</pre>
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
150 ``layers''.</p>
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>&amp;(perlio-&gt;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>
155 <ol>
156 <li>
157 <p>The functions and attributes of the ``layer class''.</p>
158 </li>
159 <li>
160 <p>The per-instance data for a particular handle.</p>
161 </li>
162 </ol>
164 </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>
170 <pre>
171 struct _PerlIO_funcs
173 Size_t fsize;
174 char * name;
175 Size_t size;
176 IV kind;
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,
180 AV *layers, IV n,
181 const char *mode,
182 int fd, int imode, int perm,
183 PerlIO *old,
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);
209 };</pre>
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>
214 <ol>
215 <li>
216 <p>Opening and setup functions</p>
217 </li>
218 <li>
219 <p>Basic IO operations</p>
220 </li>
221 <li>
222 <p>Stdio class buffering options.</p>
223 </li>
224 <li>
225 <p>Functions to support Perl's traditional ``fast'' access to the buffer.</p>
226 </li>
227 </ol>
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>
235 </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
239 thus:</p>
240 <pre>
241 typedef struct
243 struct _PerlIO base; /* Base &quot;class&quot; 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 */
250 } PerlIOBuf;</pre>
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>
254 </p>
255 <h2><a name="layers_in_action_">Layers in action.</a></h2>
256 <pre>
257 table perlio unix
259 +-----------+ +----------+ +--------+
260 PerlIO -&gt;| |---&gt;| next |---&gt;| 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>
274 <ul>
275 <li>
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>
279 </li>
280 <li>
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>
285 </li>
286 <li>
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>
293 </li>
294 <li>
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
297 things.</p>
298 </li>
299 </ul>
301 </p>
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>
306 <dl>
307 <dt><strong><a name="item_perlio_f_eof">PERLIO_F_EOF</a></strong>
309 <dd>
310 <p>End of file.</p>
311 </dd>
312 </li>
313 <dt><strong><a name="item_perlio_f_canwrite">PERLIO_F_CANWRITE</a></strong>
315 <dd>
316 <p>Writes are permitted, i.e. opened as ``w'' or ``r+'' or ``a'', etc.</p>
317 </dd>
318 </li>
319 <dt><strong><a name="item_perlio_f_canread">PERLIO_F_CANREAD</a></strong>
321 <dd>
322 <p>Reads are permitted i.e. opened ``r'' or ``w+'' (or even ``a+'' - ick).</p>
323 </dd>
324 </li>
325 <dt><strong><a name="item_perlio_f_error">PERLIO_F_ERROR</a></strong>
327 <dd>
328 <p>An error has occurred (for <code>PerlIO_error()</code>).</p>
329 </dd>
330 </li>
331 <dt><strong><a name="item_perlio_f_truncate">PERLIO_F_TRUNCATE</a></strong>
333 <dd>
334 <p>Truncate file suggested by open mode.</p>
335 </dd>
336 </li>
337 <dt><strong><a name="item_perlio_f_append">PERLIO_F_APPEND</a></strong>
339 <dd>
340 <p>All writes should be appends.</p>
341 </dd>
342 </li>
343 <dt><strong><a name="item_perlio_f_crlf">PERLIO_F_CRLF</a></strong>
345 <dd>
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>
351 </dd>
352 </li>
353 <dt><strong><a name="item_perlio_f_utf8">PERLIO_F_UTF8</a></strong>
355 <dd>
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>
359 </dd>
360 </li>
361 <dt><strong><a name="item_perlio_f_unbuf">PERLIO_F_UNBUF</a></strong>
363 <dd>
364 <p>Layer is unbuffered - i.e. write to next layer down should occur for
365 each write to this layer.</p>
366 </dd>
367 </li>
368 <dt><strong><a name="item_perlio_f_wrbuf">PERLIO_F_WRBUF</a></strong>
370 <dd>
371 <p>The buffer for this layer currently holds data written to it but not sent
372 to next layer.</p>
373 </dd>
374 </li>
375 <dt><strong><a name="item_perlio_f_rdbuf">PERLIO_F_RDBUF</a></strong>
377 <dd>
378 <p>The buffer for this layer currently holds unconsumed data read from
379 layer below.</p>
380 </dd>
381 </li>
382 <dt><strong><a name="item_perlio_f_linebuf">PERLIO_F_LINEBUF</a></strong>
384 <dd>
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
387 processed.</p>
388 </dd>
389 </li>
390 <dt><strong><a name="item_perlio_f_temp">PERLIO_F_TEMP</a></strong>
392 <dd>
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>
394 </dd>
395 </li>
396 <dt><strong><a name="item_perlio_f_open">PERLIO_F_OPEN</a></strong>
398 <dd>
399 <p>Handle is open.</p>
400 </dd>
401 </li>
402 <dt><strong><a name="item_perlio_f_fastgets">PERLIO_F_FASTGETS</a></strong>
404 <dd>
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>
413 </dd>
414 </li>
415 </dl>
417 </p>
418 <h2><a name="methods_in_detail">Methods in Detail</a></h2>
419 <dl>
420 <dt><strong><a name="item_fsize">fsize</a></strong>
422 <dd>
423 <pre>
424 Size_t fsize;</pre>
425 </dd>
426 <dd>
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>
430 </dd>
431 <dt><strong><a name="item_name">name</a></strong>
433 <dd>
434 <pre>
435 char * name;</pre>
436 </dd>
437 <dd>
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>
440 </dd>
441 <dd>
442 <pre>
443 open $fh, &quot;&gt;:APR&quot;, ...</pre>
444 </dd>
445 <dd>
446 <p>and Perl knows that it has to invoke the <code>PerlIOAPR_open()</code> method
447 implemented by the APR layer.</p>
448 </dd>
449 <dt><strong><a name="item_size">size</a></strong>
451 <dd>
452 <pre>
453 Size_t size;</pre>
454 </dd>
455 <dd>
456 <p>The size of the per-instance data structure, e.g.:</p>
457 </dd>
458 <dd>
459 <pre>
460 sizeof(PerlIOAPR)</pre>
461 </dd>
462 <dd>
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>
470 </dd>
471 <dt><strong><a name="item_kind">kind</a></strong>
473 <dd>
474 <pre>
475 IV kind;</pre>
476 </dd>
477 <ul>
478 <li><strong><a name="item_perlio_k_buffered">PERLIO_K_BUFFERED</a></strong>
480 <p>The layer is buffered.</p>
481 </li>
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>
486 </li>
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>
490 </li>
491 <li><strong><a name="item_perlio_k_fastgets">PERLIO_K_FASTGETS</a></strong>
493 <p>Layer allows buffer snooping.</p>
494 </li>
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>
500 </li>
501 </ul>
502 <dt><strong><a name="item_pushed">Pushed</a></strong>
504 <dd>
505 <pre>
506 IV (*Pushed)(pTHX_ PerlIO *f,const char *mode, SV *arg);</pre>
507 </dd>
508 <dd>
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>
518 </dd>
519 <dd>
520 <p>Returns 0 on success. On failure returns -1 and should set errno.</p>
521 </dd>
522 <dt><strong><a name="item_popped">Popped</a></strong>
524 <dd>
525 <pre>
526 IV (*Popped)(pTHX_ PerlIO *f);</pre>
527 </dd>
528 <dd>
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>
537 </dd>
538 <dd>
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>
543 </dd>
544 <dt><strong><a name="item_open">Open</a></strong>
546 <dd>
547 <pre>
548 PerlIO * (*Open)(...);</pre>
549 </dd>
550 <dd>
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
554 follows:</p>
555 </dd>
556 <dd>
557 <pre>
558 PerlIO * (*Open)(pTHX_ PerlIO_funcs *tab,
559 AV *layers, IV n,
560 const char *mode,
561 int fd, int imode, int perm,
562 PerlIO *old,
563 int narg, SV **args);</pre>
564 </dd>
565 <dd>
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>
573 </dd>
574 <dd>
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>
577 </dd>
578 <dd>
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>
588 </dd>
589 <dd>
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>
592 </dd>
593 <dd>
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>
598 </dd>
599 <dd>
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>
604 </dd>
605 <dd>
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>
611 </dd>
612 <dd>
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>
616 </dd>
617 <dd>
618 <p>Returns <code>NULL</code> on failure.</p>
619 </dd>
620 <dt><strong><a name="item_binmode">Binmode</a></strong>
622 <dd>
623 <pre>
624 IV (*Binmode)(pTHX_ PerlIO *f);</pre>
625 </dd>
626 <dd>
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>
632 </dd>
633 <dt><strong><a name="item_getarg">Getarg</a></strong>
635 <dd>
636 <pre>
637 SV * (*Getarg)(pTHX_ PerlIO *f,
638 CLONE_PARAMS *param, int flags);</pre>
639 </dd>
640 <dd>
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
645 cases)</p>
646 </dd>
647 <dd>
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>
651 </dd>
652 <dt><strong><a name="item_fileno">Fileno</a></strong>
654 <dd>
655 <pre>
656 IV (*Fileno)(pTHX_ PerlIO *f);</pre>
657 </dd>
658 <dd>
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
661 for this.</p>
662 </dd>
663 <dd>
664 <p>Returns -1 on error, which is considered to include the case where the
665 layer cannot provide such a file descriptor.</p>
666 </dd>
667 <dt><strong><a name="item_dup">Dup</a></strong>
669 <dd>
670 <pre>
671 PerlIO * (*Dup)(pTHX_ PerlIO *f, PerlIO *o,
672 CLONE_PARAMS *param, int flags);</pre>
673 </dd>
674 <dd>
675 <p>XXX: Needs more docs.</p>
676 </dd>
677 <dd>
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 '&amp;' in the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a>.</p>
681 </dd>
682 <dd>
683 <p>Similar to <a href="#item_open"><code>Open</code></a>, returns PerlIO* on success, <code>NULL</code> on failure.</p>
684 </dd>
685 <dt><strong><a name="item_read">Read</a></strong>
687 <dd>
688 <pre>
689 SSize_t (*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);</pre>
690 </dd>
691 <dd>
692 <p>Basic read operation.</p>
693 </dd>
694 <dd>
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>
698 </dd>
699 <dd>
700 <p>Returns actual bytes read, or -1 on an error.</p>
701 </dd>
702 <dt><strong><a name="item_unread">Unread</a></strong>
704 <dd>
705 <pre>
706 SSize_t (*Unread)(pTHX_ PerlIO *f,
707 const void *vbuf, Size_t count);</pre>
708 </dd>
709 <dd>
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>
714 </dd>
715 <dd>
716 <p>Returns the number of unread chars.</p>
717 </dd>
718 <dt><strong><a name="item_write">Write</a></strong>
720 <dd>
721 <pre>
722 SSize_t (*Write)(PerlIO *f, const void *vbuf, Size_t count);</pre>
723 </dd>
724 <dd>
725 <p>Basic write operation.</p>
726 </dd>
727 <dd>
728 <p>Returns bytes written or -1 on an error.</p>
729 </dd>
730 <dt><strong><a name="item_seek">Seek</a></strong>
732 <dd>
733 <pre>
734 IV (*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);</pre>
735 </dd>
736 <dd>
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>
739 </dd>
740 <dd>
741 <p>Returns 0 on success, -1 on failure.</p>
742 </dd>
743 <dt><strong><a name="item_tell">Tell</a></strong>
745 <dd>
746 <pre>
747 Off_t (*Tell)(pTHX_ PerlIO *f);</pre>
748 </dd>
749 <dd>
750 <p>Return the file pointer. May be based on layers cached concept of
751 position to avoid overhead.</p>
752 </dd>
753 <dd>
754 <p>Returns -1 on failure to get the file pointer.</p>
755 </dd>
756 <dt><strong><a name="item_close">Close</a></strong>
758 <dd>
759 <pre>
760 IV (*Close)(pTHX_ PerlIO *f);</pre>
761 </dd>
762 <dd>
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
766 structure.</p>
767 </dd>
768 <dd>
769 <p>Returns 0 on success, -1 on failure.</p>
770 </dd>
771 <dt><strong><a name="item_flush">Flush</a></strong>
773 <dd>
774 <pre>
775 IV (*Flush)(pTHX_ PerlIO *f);</pre>
776 </dd>
777 <dd>
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>
782 </dd>
783 <dd>
784 <p>Returns 0 on success, -1 on failure.</p>
785 </dd>
786 <dt><strong><a name="item_fill">Fill</a></strong>
788 <dd>
789 <pre>
790 IV (*Fill)(pTHX_ PerlIO *f);</pre>
791 </dd>
792 <dd>
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>
797 </dd>
798 <dd>
799 <p>Returns 0 on success, -1 on failure.</p>
800 </dd>
801 <dt><strong><a name="item_eof">Eof</a></strong>
803 <dd>
804 <pre>
805 IV (*Eof)(pTHX_ PerlIO *f);</pre>
806 </dd>
807 <dd>
808 <p>Return end-of-file indicator. <code>PerlIOBase_eof()</code> is normally sufficient.</p>
809 </dd>
810 <dd>
811 <p>Returns 0 on end-of-file, 1 if not end-of-file, -1 on error.</p>
812 </dd>
813 <dt><strong><a name="item_error">Error</a></strong>
815 <dd>
816 <pre>
817 IV (*Error)(pTHX_ PerlIO *f);</pre>
818 </dd>
819 <dd>
820 <p>Return error indicator. <code>PerlIOBase_error()</code> is normally sufficient.</p>
821 </dd>
822 <dd>
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,
824 0 otherwise.</p>
825 </dd>
826 <dt><strong><a name="item_clearerr">Clearerr</a></strong>
828 <dd>
829 <pre>
830 void (*Clearerr)(pTHX_ PerlIO *f);</pre>
831 </dd>
832 <dd>
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>
835 </dd>
836 <dt><strong><a name="item_setlinebuf">Setlinebuf</a></strong>
838 <dd>
839 <pre>
840 void (*Setlinebuf)(pTHX_ PerlIO *f);</pre>
841 </dd>
842 <dd>
843 <p>Mark the stream as line buffered. <code>PerlIOBase_setlinebuf()</code> sets the
844 PERLIO_F_LINEBUF flag and is normally sufficient.</p>
845 </dd>
846 <dt><strong><a name="item_get_base">Get_base</a></strong>
848 <dd>
849 <pre>
850 STDCHAR * (*Get_base)(pTHX_ PerlIO *f);</pre>
851 </dd>
852 <dd>
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>
855 </dd>
856 <dt><strong><a name="item_get_bufsiz">Get_bufsiz</a></strong>
858 <dd>
859 <pre>
860 Size_t (*Get_bufsiz)(pTHX_ PerlIO *f);</pre>
861 </dd>
862 <dd>
863 <p>Return the number of bytes that last <a href="#item_fill"><code>Fill()</code></a> put in the buffer.</p>
864 </dd>
865 <dt><strong><a name="item_get_ptr">Get_ptr</a></strong>
867 <dd>
868 <pre>
869 STDCHAR * (*Get_ptr)(pTHX_ PerlIO *f);</pre>
870 </dd>
871 <dd>
872 <p>Return the current read pointer relative to this layer's buffer.</p>
873 </dd>
874 <dt><strong><a name="item_get_cnt">Get_cnt</a></strong>
876 <dd>
877 <pre>
878 SSize_t (*Get_cnt)(pTHX_ PerlIO *f);</pre>
879 </dd>
880 <dd>
881 <p>Return the number of bytes left to be read in the current buffer.</p>
882 </dd>
883 <dt><strong><a name="item_set_ptrcnt">Set_ptrcnt</a></strong>
885 <dd>
886 <pre>
887 void (*Set_ptrcnt)(pTHX_ PerlIO *f,
888 STDCHAR *ptr, SSize_t cnt);</pre>
889 </dd>
890 <dd>
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>
894 </dd>
895 </dl>
897 </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
920 EBADF.</p>
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>
926 </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>
930 <ul>
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>
940 </li>
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>
944 </li>
945 </ul>
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>
949 <pre>
950 PerlIOBase_noop_ok
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>
956 <pre>
957 method behaviour with NULL</pre>
958 <pre>
959 Clearerr PerlIOBase_clearerr
960 Close PerlIOBase_close
961 Dup PerlIOBase_dup
962 Eof PerlIOBase_eof
963 Error PerlIOBase_error
964 Fileno PerlIOBase_fileno
965 Fill FAILURE
966 Flush SUCCESS
967 Getarg SUCCESS
968 Get_base FAILURE
969 Get_bufsiz FAILURE
970 Get_cnt FAILURE
971 Get_ptr FAILURE
972 Open INHERITED
973 Popped SUCCESS
974 Pushed SUCCESS
975 Read PerlIOBase_read
976 Seek FAILURE
977 Set_cnt FAILURE
978 Set_ptrcnt FAILURE
979 Setlinebuf PerlIOBase_setlinebuf
980 Tell FAILURE
981 Unread PerlIOBase_unread
982 Write FAILURE</pre>
983 <pre>
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>
989 </p>
990 <h2><a name="core_layers">Core Layers</a></h2>
991 <p>The file <code>perlio.c</code> provides the following layers:</p>
992 <dl>
993 <dt><strong><a name="item__22unix_22">``unix''</a></strong>
995 <dd>
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>
999 </dd>
1000 </li>
1001 <dt><strong><a name="item__22perlio_22">``perlio''</a></strong>
1003 <dd>
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>
1008 </dd>
1009 <dd>
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>
1014 </dd>
1015 </li>
1016 <dt><strong><a name="item__22stdio_22">``stdio''</a></strong>
1018 <dd>
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>
1023 </dd>
1024 </li>
1025 <dt><strong><a name="item__22crlf_22">``crlf''</a></strong>
1027 <dd>
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>
1035 </dd>
1036 </li>
1037 <dt><strong><a name="item__22mmap_22">``mmap''</a></strong>
1039 <dd>
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>
1046 </dd>
1047 </li>
1048 <dt><strong><a name="item__22pending_22">``pending''</a></strong>
1050 <dd>
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>
1055 </dd>
1056 </li>
1057 <dt><strong><a name="item__22raw_22">``raw''</a></strong>
1059 <dd>
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>
1066 </dd>
1067 </li>
1068 <dt><strong><a name="item__22utf8_22">``utf8''</a></strong>
1070 <dd>
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>
1074 </dd>
1075 </li>
1076 </dl>
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>
1081 </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>
1085 <pre>
1086 use PerlIO 'layer';</pre>
1087 <p>Where <em>layer</em> is the unknown layer. <em>PerlIO.pm</em> will then attempt to:</p>
1088 <pre>
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>
1091 will fail.</p>
1092 <p>The following extension layers are bundled with perl:</p>
1093 <dl>
1094 <dt><strong><a name="item__22_3aencoding_22">``:encoding''</a></strong>
1096 <dd>
1097 <pre>
1098 use Encoding;</pre>
1099 </dd>
1100 <dd>
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
1103 called thus:</p>
1104 </dd>
1105 <dd>
1106 <pre>
1107 open( $fh, &quot;&lt;:encoding(iso-8859-7)&quot;, $pathname );</pre>
1108 </dd>
1109 <dt><strong><a name="item__22_3ascalar_22">``:scalar''</a></strong>
1111 <dd>
1112 <p>Provides support for reading data from and writing data to a scalar.</p>
1113 </dd>
1114 <dd>
1115 <pre>
1116 open( $fh, &quot;+&lt;:scalar&quot;, \$scalar );</pre>
1117 </dd>
1118 <dd>
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>
1123 </dd>
1124 <dd>
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>
1126 </dd>
1127 <dd>
1128 <pre>
1129 open( $fh, &quot;+&lt;&quot;, \$scalar );</pre>
1130 </dd>
1131 </li>
1132 <dt><strong><a name="item__22_3avia_22">``:via''</a></strong>
1134 <dd>
1135 <p>Provided to allow layers to be implemented as Perl code. For instance:</p>
1136 </dd>
1137 <dd>
1138 <pre>
1139 use PerlIO::via::StripHTML;
1140 open( my $fh, &quot;&lt;:via(StripHTML)&quot;, &quot;index.html&quot; );</pre>
1141 </dd>
1142 <dd>
1143 <p>See <a href="file://C|\msysgit\mingw\html/lib/PerlIO/via.html">the PerlIO::via manpage</a> for details.</p>
1144 </dd>
1145 </li>
1146 </dl>
1148 </p>
1149 <hr />
1150 <h1><a name="todo">TODO</a></h1>
1151 <p>Things that need to be done to improve this document.</p>
1152 <ul>
1153 <li>
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>
1159 <pre>
1160 PerlIO *foo_to_PerlIO(pTHX_ char *mode, ...)
1162 char *mode; /* &quot;w&quot;, &quot;r&quot;, etc */
1163 const char *layers = &quot;:APR&quot;; /* the layer name */
1164 PerlIO *f = PerlIO_allocate(aTHX);
1165 if (!f) {
1166 return NULL;
1167 }</pre>
1168 <pre>
1169 PerlIO_apply_layers(aTHX_ f, mode, layers);</pre>
1170 <pre>
1171 if (f) {
1172 PerlIOAPR *st = PerlIOSelf(f, PerlIOAPR);
1173 /* fill in the st struct, as in _open() */
1174 st-&gt;file = file;
1175 PerlIOBase(f)-&gt;flags |= PERLIO_F_OPEN;</pre>
1176 <pre>
1177 return f;
1179 return NULL;
1180 }</pre>
1181 </li>
1182 <li>
1183 <p>fix/add the documentation in places marked as XXX.</p>
1184 </li>
1185 <li>
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
1190 can be found.</p>
1191 </li>
1192 <li>
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>
1199 </li>
1200 </ul>
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">&nbsp;perliol - C API for Perl's implementation of IO in Layers.</span></strong></big>
1204 </td></tr>
1205 </table>
1207 </body>
1209 </html>