1PERLIOL(1)             Perl Programmers Reference Guide             PERLIOL(1)
2
3
4

NAME

6       perliol - C API for Perl's implementation of IO in Layers.
7

SYNOPSIS

9           /* Defining a layer ... */
10           #include <perliol.h>
11

DESCRIPTION

13       This document describes the behavior and implementation of the PerlIO
14       abstraction described in perlapio when "USE_PERLIO" is defined (and
15       "USE_SFIO" is not).
16
17   History and Background
18       The PerlIO abstraction was introduced in perl5.003_02 but languished as
19       just an abstraction until perl5.7.0. However during that time a number
20       of perl extensions switched to using it, so the API is mostly fixed to
21       maintain (source) compatibility.
22
23       The aim of the implementation is to provide the PerlIO API in a
24       flexible and platform neutral manner. It is also a trial of an "Object
25       Oriented C, with vtables" approach which may be applied to Perl 6.
26
27   Basic Structure
28       PerlIO is a stack of layers.
29
30       The low levels of the stack work with the low-level operating system
31       calls (file descriptors in C) getting bytes in and out, the higher
32       layers of the stack buffer, filter, and otherwise manipulate the I/O,
33       and return characters (or bytes) to Perl.  Terms above and below are
34       used to refer to the relative positioning of the stack layers.
35
36       A layer contains a "vtable", the table of I/O operations (at C level a
37       table of function pointers), and status flags.  The functions in the
38       vtable implement operations like "open", "read", and "write".
39
40       When I/O, for example "read", is requested, the request goes from Perl
41       first down the stack using "read" functions of each layer, then at the
42       bottom the input is requested from the operating system services, then
43       the result is returned up the stack, finally being interpreted as Perl
44       data.
45
46       The requests do not necessarily go always all the way down to the
47       operating system: that's where PerlIO buffering comes into play.
48
49       When you do an open() and specify extra PerlIO layers to be deployed,
50       the layers you specify are "pushed" on top of the already existing
51       default stack.  One way to see it is that "operating system is on the
52       left" and "Perl is on the right".
53
54       What exact layers are in this default stack depends on a lot of things:
55       your operating system, Perl version, Perl compile time configuration,
56       and Perl runtime configuration.  See PerlIO, "PERLIO" in perlrun, and
57       open for more information.
58
59       binmode() operates similarly to open(): by default the specified layers
60       are pushed on top of the existing stack.
61
62       However, note that even as the specified layers are "pushed on top" for
63       open() and binmode(), this doesn't mean that the effects are limited to
64       the "top": PerlIO layers can be very 'active' and inspect and affect
65       layers also deeper in the stack.  As an example there is a layer called
66       "raw" which repeatedly "pops" layers until it reaches the first layer
67       that has declared itself capable of handling binary data.  The "pushed"
68       layers are processed in left-to-right order.
69
70       sysopen() operates (unsurprisingly) at a lower level in the stack than
71       open().  For example in UNIX or UNIX-like systems sysopen() operates
72       directly at the level of file descriptors: in the terms of PerlIO
73       layers, it uses only the "unix" layer, which is a rather thin wrapper
74       on top of the UNIX file descriptors.
75
76   Layers vs Disciplines
77       Initial discussion of the ability to modify IO streams behaviour used
78       the term "discipline" for the entities which were added. This came (I
79       believe) from the use of the term in "sfio", which in turn borrowed it
80       from "line disciplines" on Unix terminals. However, this document (and
81       the C code) uses the term "layer".
82
83       This is, I hope, a natural term given the implementation, and should
84       avoid connotations that are inherent in earlier uses of "discipline"
85       for things which are rather different.
86
87   Data Structures
88       The basic data structure is a PerlIOl:
89
90               typedef struct _PerlIO PerlIOl;
91               typedef struct _PerlIO_funcs PerlIO_funcs;
92               typedef PerlIOl *PerlIO;
93
94               struct _PerlIO
95               {
96                PerlIOl *      next;       /* Lower layer */
97                PerlIO_funcs * tab;        /* Functions for this layer */
98                IV             flags;      /* Various flags for state */
99               };
100
101       A "PerlIOl *" is a pointer to the struct, and the application level
102       "PerlIO *" is a pointer to a "PerlIOl *" - i.e. a pointer to a pointer
103       to the struct. This allows the application level "PerlIO *" to remain
104       constant while the actual "PerlIOl *" underneath changes. (Compare
105       perl's "SV *" which remains constant while its "sv_any" field changes
106       as the scalar's type changes.) An IO stream is then in general
107       represented as a pointer to this linked-list of "layers".
108
109       It should be noted that because of the double indirection in a "PerlIO
110       *", a "&(perlio->next)" "is" a "PerlIO *", and so to some degree at
111       least one layer can use the "standard" API on the next layer down.
112
113       A "layer" is composed of two parts:
114
115       1.  The functions and attributes of the "layer class".
116
117       2.  The per-instance data for a particular handle.
118
119   Functions and Attributes
120       The functions and attributes are accessed via the "tab" (for table)
121       member of "PerlIOl". The functions (methods of the layer "class") are
122       fixed, and are defined by the "PerlIO_funcs" type. They are broadly the
123       same as the public "PerlIO_xxxxx" functions:
124
125         struct _PerlIO_funcs
126         {
127          Size_t               fsize;
128          char *               name;
129          Size_t               size;
130          IV           kind;
131          IV           (*Pushed)(pTHX_ PerlIO *f,const char *mode,SV *arg, PerlIO_funcs *tab);
132          IV           (*Popped)(pTHX_ PerlIO *f);
133          PerlIO *     (*Open)(pTHX_ PerlIO_funcs *tab,
134                               PerlIO_list_t *layers, IV n,
135                               const char *mode,
136                               int fd, int imode, int perm,
137                               PerlIO *old,
138                               int narg, SV **args);
139          IV           (*Binmode)(pTHX_ PerlIO *f);
140          SV *         (*Getarg)(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
141          IV           (*Fileno)(pTHX_ PerlIO *f);
142          PerlIO *     (*Dup)(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)
143          /* Unix-like functions - cf sfio line disciplines */
144          SSize_t      (*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);
145          SSize_t      (*Unread)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);
146          SSize_t      (*Write)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);
147          IV           (*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);
148          Off_t        (*Tell)(pTHX_ PerlIO *f);
149          IV           (*Close)(pTHX_ PerlIO *f);
150          /* Stdio-like buffered IO functions */
151          IV           (*Flush)(pTHX_ PerlIO *f);
152          IV           (*Fill)(pTHX_ PerlIO *f);
153          IV           (*Eof)(pTHX_ PerlIO *f);
154          IV           (*Error)(pTHX_ PerlIO *f);
155          void         (*Clearerr)(pTHX_ PerlIO *f);
156          void         (*Setlinebuf)(pTHX_ PerlIO *f);
157          /* Perl's snooping functions */
158          STDCHAR *    (*Get_base)(pTHX_ PerlIO *f);
159          Size_t       (*Get_bufsiz)(pTHX_ PerlIO *f);
160          STDCHAR *    (*Get_ptr)(pTHX_ PerlIO *f);
161          SSize_t      (*Get_cnt)(pTHX_ PerlIO *f);
162          void         (*Set_ptrcnt)(pTHX_ PerlIO *f,STDCHAR *ptr,SSize_t cnt);
163         };
164
165       The first few members of the struct give a function table size for
166       compatibility check "name" for the layer, the  size to "malloc" for the
167       per-instance data, and some flags which are attributes of the class as
168       whole (such as whether it is a buffering layer), then follow the
169       functions which fall into four basic groups:
170
171       1.  Opening and setup functions
172
173       2.  Basic IO operations
174
175       3.  Stdio class buffering options.
176
177       4.  Functions to support Perl's traditional "fast" access to the
178           buffer.
179
180       A layer does not have to implement all the functions, but the whole
181       table has to be present. Unimplemented slots can be NULL (which will
182       result in an error when called) or can be filled in with stubs to
183       "inherit" behaviour from a "base class". This "inheritance" is fixed
184       for all instances of the layer, but as the layer chooses which stubs to
185       populate the table, limited "multiple inheritance" is possible.
186
187   Per-instance Data
188       The per-instance data are held in memory beyond the basic PerlIOl
189       struct, by making a PerlIOl the first member of the layer's struct
190       thus:
191
192               typedef struct
193               {
194                struct _PerlIO base;       /* Base "class" info */
195                STDCHAR *      buf;        /* Start of buffer */
196                STDCHAR *      end;        /* End of valid part of buffer */
197                STDCHAR *      ptr;        /* Current position in buffer */
198                Off_t          posn;       /* Offset of buf into the file */
199                Size_t         bufsiz;     /* Real size of buffer */
200                IV             oneword;    /* Emergency buffer */
201               } PerlIOBuf;
202
203       In this way (as for perl's scalars) a pointer to a PerlIOBuf can be
204       treated as a pointer to a PerlIOl.
205
206   Layers in action.
207                       table           perlio          unix
208                   |           |
209                   +-----------+    +----------+    +--------+
210          PerlIO ->|           |--->|  next    |--->|  NULL  |
211                   +-----------+    +----------+    +--------+
212                   |           |    |  buffer  |    |   fd   |
213                   +-----------+    |          |    +--------+
214                   |           |    +----------+
215
216       The above attempts to show how the layer scheme works in a simple case.
217       The application's "PerlIO *" points to an entry in the table(s)
218       representing open (allocated) handles. For example the first three
219       slots in the table correspond to "stdin","stdout" and "stderr". The
220       table in turn points to the current "top" layer for the handle - in
221       this case an instance of the generic buffering layer "perlio". That
222       layer in turn points to the next layer down - in this case the low-
223       level "unix" layer.
224
225       The above is roughly equivalent to a "stdio" buffered stream, but with
226       much more flexibility:
227
228       ·   If Unix level "read"/"write"/"lseek" is not appropriate for (say)
229           sockets then the "unix" layer can be replaced (at open time or even
230           dynamically) with a "socket" layer.
231
232       ·   Different handles can have different buffering schemes. The "top"
233           layer could be the "mmap" layer if reading disk files was quicker
234           using "mmap" than "read". An "unbuffered" stream can be implemented
235           simply by not having a buffer layer.
236
237       ·   Extra layers can be inserted to process the data as it flows
238           through.  This was the driving need for including the scheme in
239           perl 5.7.0+ - we needed a mechanism to allow data to be translated
240           between perl's internal encoding (conceptually at least Unicode as
241           UTF-8), and the "native" format used by the system. This is
242           provided by the ":encoding(xxxx)" layer which typically sits above
243           the buffering layer.
244
245       ·   A layer can be added that does "\n" to CRLF translation. This layer
246           can be used on any platform, not just those that normally do such
247           things.
248
249   Per-instance flag bits
250       The generic flag bits are a hybrid of "O_XXXXX" style flags deduced
251       from the mode string passed to "PerlIO_open()", and state bits for
252       typical buffer layers.
253
254       PERLIO_F_EOF
255           End of file.
256
257       PERLIO_F_CANWRITE
258           Writes are permitted, i.e. opened as "w" or "r+" or "a", etc.
259
260       PERLIO_F_CANREAD
261           Reads are permitted i.e. opened "r" or "w+" (or even "a+" - ick).
262
263       PERLIO_F_ERROR
264           An error has occurred (for "PerlIO_error()").
265
266       PERLIO_F_TRUNCATE
267           Truncate file suggested by open mode.
268
269       PERLIO_F_APPEND
270           All writes should be appends.
271
272       PERLIO_F_CRLF
273           Layer is performing Win32-like "\n" mapped to CR,LF for output and
274           CR,LF mapped to "\n" for input. Normally the provided "crlf" layer
275           is the only layer that need bother about this. "PerlIO_binmode()"
276           will mess with this flag rather than add/remove layers if the
277           "PERLIO_K_CANCRLF" bit is set for the layers class.
278
279       PERLIO_F_UTF8
280           Data written to this layer should be UTF-8 encoded; data provided
281           by this layer should be considered UTF-8 encoded. Can be set on any
282           layer by ":utf8" dummy layer. Also set on ":encoding" layer.
283
284       PERLIO_F_UNBUF
285           Layer is unbuffered - i.e. write to next layer down should occur
286           for each write to this layer.
287
288       PERLIO_F_WRBUF
289           The buffer for this layer currently holds data written to it but
290           not sent to next layer.
291
292       PERLIO_F_RDBUF
293           The buffer for this layer currently holds unconsumed data read from
294           layer below.
295
296       PERLIO_F_LINEBUF
297           Layer is line buffered. Write data should be passed to next layer
298           down whenever a "\n" is seen. Any data beyond the "\n" should then
299           be processed.
300
301       PERLIO_F_TEMP
302           File has been "unlink()"ed, or should be deleted on "close()".
303
304       PERLIO_F_OPEN
305           Handle is open.
306
307       PERLIO_F_FASTGETS
308           This instance of this layer supports the "fast "gets"" interface.
309           Normally set based on "PERLIO_K_FASTGETS" for the class and by the
310           existence of the function(s) in the table. However a class that
311           normally provides that interface may need to avoid it on a
312           particular instance. The "pending" layer needs to do this when it
313           is pushed above a layer which does not support the interface.
314           (Perl's "sv_gets()" does not expect the streams fast "gets"
315           behaviour to change during one "get".)
316
317   Methods in Detail
318       fsize
319                   Size_t fsize;
320
321           Size of the function table. This is compared against the value
322           PerlIO code "knows" as a compatibility check. Future versions may
323           be able to tolerate layers compiled against an old version of the
324           headers.
325
326       name
327                   char * name;
328
329           The name of the layer whose open() method Perl should invoke on
330           open().  For example if the layer is called APR, you will call:
331
332             open $fh, ">:APR", ...
333
334           and Perl knows that it has to invoke the PerlIOAPR_open() method
335           implemented by the APR layer.
336
337       size
338                   Size_t size;
339
340           The size of the per-instance data structure, e.g.:
341
342             sizeof(PerlIOAPR)
343
344           If this field is zero then "PerlIO_pushed" does not malloc anything
345           and assumes layer's Pushed function will do any required layer
346           stack manipulation - used to avoid malloc/free overhead for dummy
347           layers.  If the field is non-zero it must be at least the size of
348           "PerlIOl", "PerlIO_pushed" will allocate memory for the layer's
349           data structures and link new layer onto the stream's stack. (If the
350           layer's Pushed method returns an error indication the layer is
351           popped again.)
352
353       kind
354                   IV kind;
355
356           ·   PERLIO_K_BUFFERED
357
358               The layer is buffered.
359
360           ·   PERLIO_K_RAW
361
362               The layer is acceptable to have in a binmode(FH) stack - i.e.
363               it does not (or will configure itself not to) transform bytes
364               passing through it.
365
366           ·   PERLIO_K_CANCRLF
367
368               Layer can translate between "\n" and CRLF line ends.
369
370           ·   PERLIO_K_FASTGETS
371
372               Layer allows buffer snooping.
373
374           ·   PERLIO_K_MULTIARG
375
376               Used when the layer's open() accepts more arguments than usual.
377               The extra arguments should come not before the "MODE" argument.
378               When this flag is used it's up to the layer to validate the
379               args.
380
381       Pushed
382                   IV      (*Pushed)(pTHX_ PerlIO *f,const char *mode, SV *arg);
383
384           The only absolutely mandatory method. Called when the layer is
385           pushed onto the stack.  The "mode" argument may be NULL if this
386           occurs post-open. The "arg" will be non-"NULL" if an argument
387           string was passed. In most cases this should call
388           "PerlIOBase_pushed()" to convert "mode" into the appropriate
389           "PERLIO_F_XXXXX" flags in addition to any actions the layer itself
390           takes.  If a layer is not expecting an argument it need neither
391           save the one passed to it, nor provide "Getarg()" (it could perhaps
392           "Perl_warn" that the argument was un-expected).
393
394           Returns 0 on success. On failure returns -1 and should set errno.
395
396       Popped
397                   IV      (*Popped)(pTHX_ PerlIO *f);
398
399           Called when the layer is popped from the stack. A layer will
400           normally be popped after "Close()" is called. But a layer can be
401           popped without being closed if the program is dynamically managing
402           layers on the stream. In such cases "Popped()" should free any
403           resources (buffers, translation tables, ...) not held directly in
404           the layer's struct.  It should also "Unread()" any unconsumed data
405           that has been read and buffered from the layer below back to that
406           layer, so that it can be re-provided to what ever is now above.
407
408           Returns 0 on success and failure.  If "Popped()" returns true then
409           perlio.c assumes that either the layer has popped itself, or the
410           layer is super special and needs to be retained for other reasons.
411           In most cases it should return false.
412
413       Open
414                   PerlIO *        (*Open)(...);
415
416           The "Open()" method has lots of arguments because it combines the
417           functions of perl's "open", "PerlIO_open", perl's "sysopen",
418           "PerlIO_fdopen" and "PerlIO_reopen".  The full prototype is as
419           follows:
420
421            PerlIO *       (*Open)(pTHX_ PerlIO_funcs *tab,
422                                   PerlIO_list_t *layers, IV n,
423                                   const char *mode,
424                                   int fd, int imode, int perm,
425                                   PerlIO *old,
426                                   int narg, SV **args);
427
428           Open should (perhaps indirectly) call "PerlIO_allocate()" to
429           allocate a slot in the table and associate it with the layers
430           information for the opened file, by calling "PerlIO_push".  The
431           layers is an array of all the layers destined for the "PerlIO *",
432           and any arguments passed to them, n is the index into that array of
433           the layer being called. The macro "PerlIOArg" will return a
434           (possibly "NULL") SV * for the argument passed to the layer.
435
436           The mode string is an ""fopen()"-like" string which would match the
437           regular expression "/^[I#]?[rwa]\+?[bt]?$/".
438
439           The 'I' prefix is used during creation of "stdin".."stderr" via
440           special "PerlIO_fdopen" calls; the '#' prefix means that this is
441           "sysopen" and that imode and perm should be passed to
442           "PerlLIO_open3"; 'r' means read, 'w' means write and 'a' means
443           append. The '+' suffix means that both reading and
444           writing/appending are permitted.  The 'b' suffix means file should
445           be binary, and 't' means it is text. (Almost all layers should do
446           the IO in binary mode, and ignore the b/t bits. The ":crlf" layer
447           should be pushed to handle the distinction.)
448
449           If old is not "NULL" then this is a "PerlIO_reopen". Perl itself
450           does not use this (yet?) and semantics are a little vague.
451
452           If fd not negative then it is the numeric file descriptor fd, which
453           will be open in a manner compatible with the supplied mode string,
454           the call is thus equivalent to "PerlIO_fdopen". In this case nargs
455           will be zero.
456
457           If nargs is greater than zero then it gives the number of arguments
458           passed to "open", otherwise it will be 1 if for example
459           "PerlIO_open" was called.  In simple cases SvPV_nolen(*args) is the
460           pathname to open.
461
462           Having said all that translation-only layers do not need to provide
463           "Open()" at all, but rather leave the opening to a lower level
464           layer and wait to be "pushed".  If a layer does provide "Open()" it
465           should normally call the "Open()" method of next layer down (if
466           any) and then push itself on top if that succeeds.
467
468           If "PerlIO_push" was performed and open has failed, it must
469           "PerlIO_pop" itself, since if it's not, the layer won't be removed
470           and may cause bad problems.
471
472           Returns "NULL" on failure.
473
474       Binmode
475                   IV        (*Binmode)(pTHX_ PerlIO *f);
476
477           Optional. Used when ":raw" layer is pushed (explicitly or as a
478           result of binmode(FH)). If not present layer will be popped. If
479           present should configure layer as binary (or pop itself) and return
480           0.  If it returns -1 for error "binmode" will fail with layer still
481           on the stack.
482
483       Getarg
484                   SV *      (*Getarg)(pTHX_ PerlIO *f,
485                                       CLONE_PARAMS *param, int flags);
486
487           Optional. If present should return an SV * representing the string
488           argument passed to the layer when it was pushed. e.g.
489           ":encoding(ascii)" would return an SvPV with value "ascii". (param
490           and flags arguments can be ignored in most cases)
491
492           "Dup" uses "Getarg" to retrieve the argument originally passed to
493           "Pushed", so you must implement this function if your layer has an
494           extra argument to "Pushed" and will ever be "Dup"ed.
495
496       Fileno
497                   IV        (*Fileno)(pTHX_ PerlIO *f);
498
499           Returns the Unix/Posix numeric file descriptor for the handle.
500           Normally "PerlIOBase_fileno()" (which just asks next layer down)
501           will suffice for this.
502
503           Returns -1 on error, which is considered to include the case where
504           the layer cannot provide such a file descriptor.
505
506       Dup
507                   PerlIO * (*Dup)(pTHX_ PerlIO *f, PerlIO *o,
508                                   CLONE_PARAMS *param, int flags);
509
510           XXX: Needs more docs.
511
512           Used as part of the "clone" process when a thread is spawned (in
513           which case param will be non-NULL) and when a stream is being
514           duplicated via '&' in the "open".
515
516           Similar to "Open", returns PerlIO* on success, "NULL" on failure.
517
518       Read
519                   SSize_t (*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);
520
521           Basic read operation.
522
523           Typically will call "Fill" and manipulate pointers (possibly via
524           the API).  "PerlIOBuf_read()" may be suitable for derived classes
525           which provide "fast gets" methods.
526
527           Returns actual bytes read, or -1 on an error.
528
529       Unread
530                   SSize_t (*Unread)(pTHX_ PerlIO *f,
531                                     const void *vbuf, Size_t count);
532
533           A superset of stdio's "ungetc()". Should arrange for future reads
534           to see the bytes in "vbuf". If there is no obviously better
535           implementation then "PerlIOBase_unread()" provides the function by
536           pushing a "fake" "pending" layer above the calling layer.
537
538           Returns the number of unread chars.
539
540       Write
541                   SSize_t (*Write)(PerlIO *f, const void *vbuf, Size_t count);
542
543           Basic write operation.
544
545           Returns bytes written or -1 on an error.
546
547       Seek
548                   IV      (*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);
549
550           Position the file pointer. Should normally call its own "Flush"
551           method and then the "Seek" method of next layer down.
552
553           Returns 0 on success, -1 on failure.
554
555       Tell
556                   Off_t   (*Tell)(pTHX_ PerlIO *f);
557
558           Return the file pointer. May be based on layers cached concept of
559           position to avoid overhead.
560
561           Returns -1 on failure to get the file pointer.
562
563       Close
564                   IV      (*Close)(pTHX_ PerlIO *f);
565
566           Close the stream. Should normally call "PerlIOBase_close()" to
567           flush itself and close layers below, and then deallocate any data
568           structures (buffers, translation tables, ...) not  held directly in
569           the data structure.
570
571           Returns 0 on success, -1 on failure.
572
573       Flush
574                   IV      (*Flush)(pTHX_ PerlIO *f);
575
576           Should make stream's state consistent with layers below. That is,
577           any buffered write data should be written, and file position of
578           lower layers adjusted for data read from below but not actually
579           consumed.  (Should perhaps "Unread()" such data to the lower
580           layer.)
581
582           Returns 0 on success, -1 on failure.
583
584       Fill
585                   IV      (*Fill)(pTHX_ PerlIO *f);
586
587           The buffer for this layer should be filled (for read) from layer
588           below.  When you "subclass" PerlIOBuf layer, you want to use its
589           _read method and to supply your own fill method, which fills the
590           PerlIOBuf's buffer.
591
592           Returns 0 on success, -1 on failure.
593
594       Eof
595                   IV      (*Eof)(pTHX_ PerlIO *f);
596
597           Return end-of-file indicator. "PerlIOBase_eof()" is normally
598           sufficient.
599
600           Returns 0 on end-of-file, 1 if not end-of-file, -1 on error.
601
602       Error
603                   IV      (*Error)(pTHX_ PerlIO *f);
604
605           Return error indicator. "PerlIOBase_error()" is normally
606           sufficient.
607
608           Returns 1 if there is an error (usually when "PERLIO_F_ERROR" is
609           set, 0 otherwise.
610
611       Clearerr
612                   void    (*Clearerr)(pTHX_ PerlIO *f);
613
614           Clear end-of-file and error indicators. Should call
615           "PerlIOBase_clearerr()" to set the "PERLIO_F_XXXXX" flags, which
616           may suffice.
617
618       Setlinebuf
619                   void    (*Setlinebuf)(pTHX_ PerlIO *f);
620
621           Mark the stream as line buffered. "PerlIOBase_setlinebuf()" sets
622           the PERLIO_F_LINEBUF flag and is normally sufficient.
623
624       Get_base
625                   STDCHAR *       (*Get_base)(pTHX_ PerlIO *f);
626
627           Allocate (if not already done so) the read buffer for this layer
628           and return pointer to it. Return NULL on failure.
629
630       Get_bufsiz
631                   Size_t  (*Get_bufsiz)(pTHX_ PerlIO *f);
632
633           Return the number of bytes that last "Fill()" put in the buffer.
634
635       Get_ptr
636                   STDCHAR *       (*Get_ptr)(pTHX_ PerlIO *f);
637
638           Return the current read pointer relative to this layer's buffer.
639
640       Get_cnt
641                   SSize_t (*Get_cnt)(pTHX_ PerlIO *f);
642
643           Return the number of bytes left to be read in the current buffer.
644
645       Set_ptrcnt
646                   void    (*Set_ptrcnt)(pTHX_ PerlIO *f,
647                                         STDCHAR *ptr, SSize_t cnt);
648
649           Adjust the read pointer and count of bytes to match "ptr" and/or
650           "cnt".  The application (or layer above) must ensure they are
651           consistent.  (Checking is allowed by the paranoid.)
652
653   Utilities
654       To ask for the next layer down use PerlIONext(PerlIO *f).
655
656       To check that a PerlIO* is valid use PerlIOValid(PerlIO *f).  (All this
657       does is really just to check that the pointer is non-NULL and that the
658       pointer behind that is non-NULL.)
659
660       PerlIOBase(PerlIO *f) returns the "Base" pointer, or in other words,
661       the "PerlIOl*" pointer.
662
663       PerlIOSelf(PerlIO* f, type) return the PerlIOBase cast to a type.
664
665       Perl_PerlIO_or_Base(PerlIO* f, callback, base, failure, args) either
666       calls the callback from the functions of the layer f (just by the name
667       of the IO function, like "Read") with the args, or if there is no such
668       callback, calls the base version of the callback with the same args, or
669       if the f is invalid, set errno to EBADF and return failure.
670
671       Perl_PerlIO_or_fail(PerlIO* f, callback, failure, args) either calls
672       the callback of the functions of the layer f with the args, or if there
673       is no such callback, set errno to EINVAL.  Or if the f is invalid, set
674       errno to EBADF and return failure.
675
676       Perl_PerlIO_or_Base_void(PerlIO* f, callback, base, args) either calls
677       the callback of the functions of the layer f with the args, or if there
678       is no such callback, calls the base version of the callback with the
679       same args, or if the f is invalid, set errno to EBADF.
680
681       Perl_PerlIO_or_fail_void(PerlIO* f, callback, args) either calls the
682       callback of the functions of the layer f with the args, or if there is
683       no such callback, set errno to EINVAL.  Or if the f is invalid, set
684       errno to EBADF.
685
686   Implementing PerlIO Layers
687       If you find the implementation document unclear or not sufficient, look
688       at the existing PerlIO layer implementations, which include:
689
690       ·   C implementations
691
692           The perlio.c and perliol.h in the Perl core implement the "unix",
693           "perlio", "stdio", "crlf", "utf8", "byte", "raw", "pending" layers,
694           and also the "mmap" and "win32" layers if applicable.  (The "win32"
695           is currently unfinished and unused, to see what is used instead in
696           Win32, see "Querying the layers of filehandles" in PerlIO .)
697
698           PerlIO::encoding, PerlIO::scalar, PerlIO::via in the Perl core.
699
700           PerlIO::gzip and APR::PerlIO (mod_perl 2.0) on CPAN.
701
702       ·   Perl implementations
703
704           PerlIO::via::QuotedPrint in the Perl core and PerlIO::via::* on
705           CPAN.
706
707       If you are creating a PerlIO layer, you may want to be lazy, in other
708       words, implement only the methods that interest you.  The other methods
709       you can either replace with the "blank" methods
710
711           PerlIOBase_noop_ok
712           PerlIOBase_noop_fail
713
714       (which do nothing, and return zero and -1, respectively) or for certain
715       methods you may assume a default behaviour by using a NULL method.  The
716       Open method looks for help in the 'parent' layer.  The following table
717       summarizes the behaviour:
718
719           method      behaviour with NULL
720
721           Clearerr    PerlIOBase_clearerr
722           Close       PerlIOBase_close
723           Dup         PerlIOBase_dup
724           Eof         PerlIOBase_eof
725           Error       PerlIOBase_error
726           Fileno      PerlIOBase_fileno
727           Fill        FAILURE
728           Flush       SUCCESS
729           Getarg      SUCCESS
730           Get_base    FAILURE
731           Get_bufsiz  FAILURE
732           Get_cnt     FAILURE
733           Get_ptr     FAILURE
734           Open        INHERITED
735           Popped      SUCCESS
736           Pushed      SUCCESS
737           Read        PerlIOBase_read
738           Seek        FAILURE
739           Set_cnt     FAILURE
740           Set_ptrcnt  FAILURE
741           Setlinebuf  PerlIOBase_setlinebuf
742           Tell        FAILURE
743           Unread      PerlIOBase_unread
744           Write       FAILURE
745
746        FAILURE        Set errno (to EINVAL in UNIXish, to LIB$_INVARG in VMS) and
747                       return -1 (for numeric return values) or NULL (for pointers)
748        INHERITED      Inherited from the layer below
749        SUCCESS        Return 0 (for numeric return values) or a pointer
750
751   Core Layers
752       The file "perlio.c" provides the following layers:
753
754       "unix"
755           A basic non-buffered layer which calls Unix/POSIX "read()",
756           "write()", "lseek()", "close()". No buffering. Even on platforms
757           that distinguish between O_TEXT and O_BINARY this layer is always
758           O_BINARY.
759
760       "perlio"
761           A very complete generic buffering layer which provides the whole of
762           PerlIO API. It is also intended to be used as a "base class" for
763           other layers. (For example its "Read()" method is implemented in
764           terms of the "Get_cnt()"/"Get_ptr()"/"Set_ptrcnt()" methods).
765
766           "perlio" over "unix" provides a complete replacement for stdio as
767           seen via PerlIO API. This is the default for USE_PERLIO when
768           system's stdio does not permit perl's "fast gets" access, and which
769           do not distinguish between "O_TEXT" and "O_BINARY".
770
771       "stdio"
772           A layer which provides the PerlIO API via the layer scheme, but
773           implements it by calling system's stdio. This is (currently) the
774           default if system's stdio provides sufficient access to allow
775           perl's "fast gets" access and which do not distinguish between
776           "O_TEXT" and "O_BINARY".
777
778       "crlf"
779           A layer derived using "perlio" as a base class. It provides
780           Win32-like "\n" to CR,LF translation. Can either be applied above
781           "perlio" or serve as the buffer layer itself. "crlf" over "unix" is
782           the default if system distinguishes between "O_TEXT" and "O_BINARY"
783           opens. (At some point "unix" will be replaced by a "native" Win32
784           IO layer on that platform, as Win32's read/write layer has various
785           drawbacks.) The "crlf" layer is a reasonable model for a layer
786           which transforms data in some way.
787
788       "mmap"
789           If Configure detects "mmap()" functions this layer is provided
790           (with "perlio" as a "base") which does "read" operations by
791           mmap()ing the file. Performance improvement is marginal on modern
792           systems, so it is mainly there as a proof of concept. It is likely
793           to be unbundled from the core at some point. The "mmap" layer is a
794           reasonable model for a minimalist "derived" layer.
795
796       "pending"
797           An "internal" derivative of "perlio" which can be used to provide
798           Unread() function for layers which have no buffer or cannot be
799           bothered.  (Basically this layer's "Fill()" pops itself off the
800           stack and so resumes reading from layer below.)
801
802       "raw"
803           A dummy layer which never exists on the layer stack. Instead when
804           "pushed" it actually pops the stack removing itself, it then calls
805           Binmode function table entry on all the layers in the stack -
806           normally this (via PerlIOBase_binmode) removes any layers which do
807           not have "PERLIO_K_RAW" bit set. Layers can modify that behaviour
808           by defining their own Binmode entry.
809
810       "utf8"
811           Another dummy layer. When pushed it pops itself and sets the
812           "PERLIO_F_UTF8" flag on the layer which was (and now is once more)
813           the top of the stack.
814
815       In addition perlio.c also provides a number of "PerlIOBase_xxxx()"
816       functions which are intended to be used in the table slots of classes
817       which do not need to do anything special for a particular method.
818
819   Extension Layers
820       Layers can made available by extension modules. When an unknown layer
821       is encountered the PerlIO code will perform the equivalent of :
822
823          use PerlIO 'layer';
824
825       Where layer is the unknown layer. PerlIO.pm will then attempt to:
826
827          require PerlIO::layer;
828
829       If after that process the layer is still not defined then the "open"
830       will fail.
831
832       The following extension layers are bundled with perl:
833
834       ":encoding"
835              use Encoding;
836
837           makes this layer available, although PerlIO.pm "knows" where to
838           find it.  It is an example of a layer which takes an argument as it
839           is called thus:
840
841              open( $fh, "<:encoding(iso-8859-7)", $pathname );
842
843       ":scalar"
844           Provides support for reading data from and writing data to a
845           scalar.
846
847              open( $fh, "+<:scalar", \$scalar );
848
849           When a handle is so opened, then reads get bytes from the string
850           value of $scalar, and writes change the value. In both cases the
851           position in $scalar starts as zero but can be altered via "seek",
852           and determined via "tell".
853
854           Please note that this layer is implied when calling open() thus:
855
856              open( $fh, "+<", \$scalar );
857
858       ":via"
859           Provided to allow layers to be implemented as Perl code.  For
860           instance:
861
862              use PerlIO::via::StripHTML;
863              open( my $fh, "<:via(StripHTML)", "index.html" );
864
865           See PerlIO::via for details.
866

TODO

868       Things that need to be done to improve this document.
869
870       ·   Explain how to make a valid fh without going through open()(i.e.
871           apply a layer). For example if the file is not opened through perl,
872           but we want to get back a fh, like it was opened by Perl.
873
874           How PerlIO_apply_layera fits in, where its docs, was it made
875           public?
876
877           Currently the example could be something like this:
878
879             PerlIO *foo_to_PerlIO(pTHX_ char *mode, ...)
880             {
881                 char *mode; /* "w", "r", etc */
882                 const char *layers = ":APR"; /* the layer name */
883                 PerlIO *f = PerlIO_allocate(aTHX);
884                 if (!f) {
885                     return NULL;
886                 }
887
888                 PerlIO_apply_layers(aTHX_ f, mode, layers);
889
890                 if (f) {
891                     PerlIOAPR *st = PerlIOSelf(f, PerlIOAPR);
892                     /* fill in the st struct, as in _open() */
893                     st->file = file;
894                     PerlIOBase(f)->flags |= PERLIO_F_OPEN;
895
896                     return f;
897                 }
898                 return NULL;
899             }
900
901       ·   fix/add the documentation in places marked as XXX.
902
903       ·   The handling of errors by the layer is not specified. e.g. when $!
904           should be set explicitly, when the error handling should be just
905           delegated to the top layer.
906
907           Probably give some hints on using SETERRNO() or pointers to where
908           they can be found.
909
910       ·   I think it would help to give some concrete examples to make it
911           easier to understand the API. Of course I agree that the API has to
912           be concise, but since there is no second document that is more of a
913           guide, I think that it'd make it easier to start with the doc which
914           is an API, but has examples in it in places where things are
915           unclear, to a person who is not a PerlIO guru (yet).
916
917
918
919perl v5.10.1                      2009-02-12                        PERLIOL(1)
Impressum