📄 perliol.1
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "PERLIOL 1".TH PERLIOL 1 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"perliol \- C API for Perl's implementation of IO in Layers..SH "SYNOPSIS".IX Header "SYNOPSIS".Vb 2\& /* Defining a layer ... */\& #include <perliol.h>.Ve.SH "DESCRIPTION".IX Header "DESCRIPTION"This document describes the behavior and implementation of the PerlIOabstraction described in perlapio when \f(CW\*(C`USE_PERLIO\*(C'\fR is defined (and\&\f(CW\*(C`USE_SFIO\*(C'\fR is not)..Sh "History and Background".IX Subsection "History and Background"The PerlIO abstraction was introduced in perl5.003_02 but languished asjust an abstraction until perl5.7.0. However during that time a numberof perl extensions switched to using it, so the \s-1API\s0 is mostly fixed tomaintain (source) compatibility..PPThe aim of the implementation is to provide the PerlIO \s-1API\s0 in a flexibleand platform neutral manner. It is also a trial of an \*(L"Object OrientedC, with vtables\*(R" approach which may be applied to Perl 6..Sh "Basic Structure".IX Subsection "Basic Structure"PerlIO is a stack of layers..PPThe low levels of the stack work with the low-level operating systemcalls (file descriptors in C) getting bytes in and out, the higherlayers of the stack buffer, filter, and otherwise manipulate the I/O,and return characters (or bytes) to Perl. Terms \fIabove\fR and \fIbelow\fRare used to refer to the relative positioning of the stack layers..PPA layer contains a \*(L"vtable\*(R", the table of I/O operations (at C levela table of function pointers), and status flags. The functions in thevtable implement operations like \*(L"open\*(R", \*(L"read\*(R", and \*(L"write\*(R"..PPWhen I/O, for example \*(L"read\*(R", is requested, the request goes from Perlfirst down the stack using \*(L"read\*(R" functions of each layer, then at thebottom the input is requested from the operating system services, thenthe result is returned up the stack, finally being interpreted as Perldata..PPThe requests do not necessarily go always all the way down to theoperating system: that's where PerlIO buffering comes into play..PPWhen you do an \fIopen()\fR and specify extra PerlIO layers to be deployed,the layers you specify are \*(L"pushed\*(R" on top of the already existingdefault stack. One way to see it is that \*(L"operating system ison the left\*(R" and \*(L"Perl is on the right\*(R"..PPWhat exact layers are in this default stack depends on a lot ofthings: your operating system, Perl version, Perl compile timeconfiguration, and Perl runtime configuration. See PerlIO,\&\*(L"\s-1PERLIO\s0\*(R" in perlrun, and open for more information..PP\&\fIbinmode()\fR operates similarly to \fIopen()\fR: by default the specifiedlayers are pushed on top of the existing stack..PPHowever, note that even as the specified layers are \*(L"pushed on top\*(R"for \fIopen()\fR and \fIbinmode()\fR, this doesn't mean that the effects arelimited to the \*(L"top\*(R": PerlIO layers can be very 'active' and inspectand affect layers also deeper in the stack. As an example thereis a layer called \*(L"raw\*(R" which repeatedly \*(L"pops\*(R" layers untilit reaches the first layer that has declared itself capable ofhandling binary data. The \*(L"pushed\*(R" layers are processed in left-to-rightorder..PP\&\fIsysopen()\fR operates (unsurprisingly) at a lower level in the stack than\&\fIopen()\fR. For example in \s-1UNIX\s0 or UNIX-like systems \fIsysopen()\fR operatesdirectly at the level of file descriptors: in the terms of PerlIOlayers, it uses only the \*(L"unix\*(R" layer, which is a rather thin wrapperon top of the \s-1UNIX\s0 file descriptors..Sh "Layers vs Disciplines".IX Subsection "Layers vs Disciplines"Initial discussion of the ability to modify \s-1IO\s0 streams behaviour usedthe term \*(L"discipline\*(R" for the entities which were added. This came (Ibelieve) from the use of the term in \*(L"sfio\*(R", which in turn borrowed itfrom \*(L"line disciplines\*(R" on Unix terminals. However, this document (andthe C code) uses the term \*(L"layer\*(R"..PPThis is, I hope, a natural term given the implementation, and shouldavoid connotations that are inherent in earlier uses of \*(L"discipline\*(R"for things which are rather different..Sh "Data Structures".IX Subsection "Data Structures"The basic data structure is a PerlIOl:.PP.Vb 3\& typedef struct _PerlIO PerlIOl;\& typedef struct _PerlIO_funcs PerlIO_funcs;\& typedef PerlIOl *PerlIO;\&\& struct _PerlIO\& {\& PerlIOl * next; /* Lower layer */\& PerlIO_funcs * tab; /* Functions for this layer */\& IV flags; /* Various flags for state */\& };.Ve.PPA \f(CW\*(C`PerlIOl *\*(C'\fR is a pointer to the struct, and the \fIapplication\fRlevel \f(CW\*(C`PerlIO *\*(C'\fR is a pointer to a \f(CW\*(C`PerlIOl *\*(C'\fR \- i.e. a pointerto a pointer to the struct. This allows the application level \f(CW\*(C`PerlIO *\*(C'\fRto remain constant while the actual \f(CW\*(C`PerlIOl *\*(C'\fR underneathchanges. (Compare perl's \f(CW\*(C`SV *\*(C'\fR which remains constant while its\&\f(CW\*(C`sv_any\*(C'\fR field changes as the scalar's type changes.) An \s-1IO\s0 stream isthen in general represented as a pointer to this linked-list of\&\*(L"layers\*(R"..PPIt should be noted that because of the double indirection in a \f(CW\*(C`PerlIO *\*(C'\fR,a \f(CW\*(C`&(perlio\->next)\*(C'\fR \*(L"is\*(R" a \f(CW\*(C`PerlIO *\*(C'\fR, and so to some degreeat least one layer can use the \*(L"standard\*(R" \s-1API\s0 on the next layer down..PPA \*(L"layer\*(R" is composed of two parts:.IP "1." 4The functions and attributes of the \*(L"layer class\*(R"..IP "2." 4The per-instance data for a particular handle..Sh "Functions and Attributes".IX Subsection "Functions and Attributes"The functions and attributes are accessed via the \*(L"tab\*(R" (for table)member of \f(CW\*(C`PerlIOl\*(C'\fR. The functions (methods of the layer \*(L"class\*(R") arefixed, and are defined by the \f(CW\*(C`PerlIO_funcs\*(C'\fR type. They are broadly thesame as the public \f(CW\*(C`PerlIO_xxxxx\*(C'\fR functions:.PP.Vb 10\& struct _PerlIO_funcs\& {\& Size_t fsize;\& char * name;\& Size_t size;\& IV kind;\& IV (*Pushed)(pTHX_ PerlIO *f,const char *mode,SV *arg, PerlIO_funcs *tab);\& IV (*Popped)(pTHX_ PerlIO *f);\& PerlIO * (*Open)(pTHX_ PerlIO_funcs *tab,\& AV *layers, IV n,\& const char *mode,\& int fd, int imode, int perm,\& PerlIO *old,\& int narg, SV **args);\& IV (*Binmode)(pTHX_ PerlIO *f);\& SV * (*Getarg)(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)\& IV (*Fileno)(pTHX_ PerlIO *f);\& PerlIO * (*Dup)(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, int flags)\& /* Unix\-like functions \- cf sfio line disciplines */\& SSize_t (*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);\& SSize_t (*Unread)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);\& SSize_t (*Write)(pTHX_ PerlIO *f, const void *vbuf, Size_t count);\& IV (*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);\& Off_t (*Tell)(pTHX_ PerlIO *f);\& IV (*Close)(pTHX_ PerlIO *f);\& /* Stdio\-like buffered IO functions */\& IV (*Flush)(pTHX_ PerlIO *f);\& IV (*Fill)(pTHX_ PerlIO *f);\& IV (*Eof)(pTHX_ PerlIO *f);\& IV (*Error)(pTHX_ PerlIO *f);\& void (*Clearerr)(pTHX_ PerlIO *f);\& void (*Setlinebuf)(pTHX_ PerlIO *f);\& /* Perl\*(Aqs snooping functions */\& STDCHAR * (*Get_base)(pTHX_ PerlIO *f);\& Size_t (*Get_bufsiz)(pTHX_ PerlIO *f);\& STDCHAR * (*Get_ptr)(pTHX_ PerlIO *f);\& SSize_t (*Get_cnt)(pTHX_ PerlIO *f);\& void (*Set_ptrcnt)(pTHX_ PerlIO *f,STDCHAR *ptr,SSize_t cnt);\& };.Ve.PPThe first few members of the struct give a function table size forcompatibility check \*(L"name\*(R" for the layer, the size to \f(CW\*(C`malloc\*(C'\fR for the per-instance data,and some flags which are attributes of the class as whole (such as whether it is a bufferinglayer), then follow the functions which fall into four basic groups:.IP "1." 4Opening and setup functions.IP "2." 4Basic \s-1IO\s0 operations.IP "3." 4Stdio class buffering options..IP "4." 4Functions to support Perl's traditional \*(L"fast\*(R" access to the buffer..PPA layer does not have to implement all the functions, but the wholetable has to be present. Unimplemented slots can be \s-1NULL\s0 (which willresult in an error when called) or can be filled in with stubs to\&\*(L"inherit\*(R" behaviour from a \*(L"base class\*(R". This \*(L"inheritance\*(R" is fixedfor all instances of the layer, but as the layer chooses which stubsto populate the table, limited \*(L"multiple inheritance\*(R" is possible..Sh "Per-instance Data".IX Subsection "Per-instance Data"The per-instance data are held in memory beyond the basic PerlIOlstruct, by making a PerlIOl the first member of the layer's structthus:.PP.Vb 10\& typedef struct\& {\& struct _PerlIO base; /* Base "class" info */\& STDCHAR * buf; /* Start of buffer */\& STDCHAR * end; /* End of valid part of buffer */\& STDCHAR * ptr; /* Current position in buffer */\& Off_t posn; /* Offset of buf into the file */\& Size_t bufsiz; /* Real size of buffer */\& IV oneword; /* Emergency buffer */\& } PerlIOBuf;.Ve.PPIn this way (as for perl's scalars) a pointer to a PerlIOBuf can betreated as a pointer to a PerlIOl..Sh "Layers in action.".IX Subsection "Layers in action.".Vb 8\& table perlio unix\& | |\& +\-\-\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-+\& PerlIO \->| |\-\-\->| next |\-\-\->| NULL |\& +\-\-\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-\-\-+ +\-\-\-\-\-\-\-\-+\& | | | buffer | | fd |\& +\-\-\-\-\-\-\-\-\-\-\-+ | | +\-\-\-\-\-\-\-\-+\& | | +\-\-\-\-\-\-\-\-\-\-+.Ve.PPThe above attempts to show how the layer scheme works in a simple case.The application's \f(CW\*(C`PerlIO *\*(C'\fR points to an entry in the table(s)representing open (allocated) handles. For example the first three slotsin the table correspond to \f(CW\*(C`stdin\*(C'\fR,\f(CW\*(C`stdout\*(C'\fR and \f(CW\*(C`stderr\*(C'\fR. The tablein turn points to the current \*(L"top\*(R" layer for the handle \- in this casean instance of the generic buffering layer \*(L"perlio\*(R". That layer in turnpoints to the next layer down \- in this case the low-level \*(L"unix\*(R" layer..PPThe above is roughly equivalent to a \*(L"stdio\*(R" buffered stream, but withmuch more flexibility:.IP "\(bu" 4If Unix level \f(CW\*(C`read\*(C'\fR/\f(CW\*(C`write\*(C'\fR/\f(CW\*(C`lseek\*(C'\fR is not appropriate for (say)sockets then the \*(L"unix\*(R" layer can be replaced (at open time or evendynamically) with a \*(L"socket\*(R" layer..IP "\(bu" 4Different handles can have different buffering schemes. The \*(L"top\*(R"layer could be the \*(L"mmap\*(R" layer if reading disk files was quickerusing \f(CW\*(C`mmap\*(C'\fR than \f(CW\*(C`read\*(C'\fR. An \*(L"unbuffered\*(R" stream can be implementedsimply by not having a buffer layer..IP "\(bu" 4Extra layers can be inserted to process the data as it flows through.This was the driving need for including the scheme in perl 5.7.0+ \- we
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -