📄 perliol.1
字号:
needed a mechanism to allow data to be translated between perl'sinternal encoding (conceptually at least Unicode as \s-1UTF\-8\s0), and the\&\*(L"native\*(R" format used by the system. This is provided by the\&\*(L":encoding(xxxx)\*(R" layer which typically sits above the buffering layer..IP "\(bu" 4A layer can be added that does \*(L"\en\*(R" to \s-1CRLF\s0 translation. This layercan be used on any platform, not just those that normally do suchthings..Sh "Per-instance flag bits".IX Subsection "Per-instance flag bits"The generic flag bits are a hybrid of \f(CW\*(C`O_XXXXX\*(C'\fR style flags deducedfrom the mode string passed to \f(CW\*(C`PerlIO_open()\*(C'\fR, and state bits fortypical buffer layers..IP "\s-1PERLIO_F_EOF\s0" 4.IX Item "PERLIO_F_EOF"End of file..IP "\s-1PERLIO_F_CANWRITE\s0" 4.IX Item "PERLIO_F_CANWRITE"Writes are permitted, i.e. opened as \*(L"w\*(R" or \*(L"r+\*(R" or \*(L"a\*(R", etc..IP "\s-1PERLIO_F_CANREAD\s0" 4.IX Item "PERLIO_F_CANREAD"Reads are permitted i.e. opened \*(L"r\*(R" or \*(L"w+\*(R" (or even \*(L"a+\*(R" \- ick)..IP "\s-1PERLIO_F_ERROR\s0" 4.IX Item "PERLIO_F_ERROR"An error has occurred (for \f(CW\*(C`PerlIO_error()\*(C'\fR)..IP "\s-1PERLIO_F_TRUNCATE\s0" 4.IX Item "PERLIO_F_TRUNCATE"Truncate file suggested by open mode..IP "\s-1PERLIO_F_APPEND\s0" 4.IX Item "PERLIO_F_APPEND"All writes should be appends..IP "\s-1PERLIO_F_CRLF\s0" 4.IX Item "PERLIO_F_CRLF"Layer is performing Win32\-like \*(L"\en\*(R" mapped to \s-1CR\s0,LF for output and \s-1CR\s0,LFmapped to \*(L"\en\*(R" for input. Normally the provided \*(L"crlf\*(R" layer is the onlylayer that need bother about this. \f(CW\*(C`PerlIO_binmode()\*(C'\fR will mess with thisflag rather than add/remove layers if the \f(CW\*(C`PERLIO_K_CANCRLF\*(C'\fR bit is setfor the layers class..IP "\s-1PERLIO_F_UTF8\s0" 4.IX Item "PERLIO_F_UTF8"Data written to this layer should be \s-1UTF\-8\s0 encoded; data providedby this layer should be considered \s-1UTF\-8\s0 encoded. Can be set on any layerby \*(L":utf8\*(R" dummy layer. Also set on \*(L":encoding\*(R" layer..IP "\s-1PERLIO_F_UNBUF\s0" 4.IX Item "PERLIO_F_UNBUF"Layer is unbuffered \- i.e. write to next layer down should occur foreach write to this layer..IP "\s-1PERLIO_F_WRBUF\s0" 4.IX Item "PERLIO_F_WRBUF"The buffer for this layer currently holds data written to it but not sentto next layer..IP "\s-1PERLIO_F_RDBUF\s0" 4.IX Item "PERLIO_F_RDBUF"The buffer for this layer currently holds unconsumed data read fromlayer below..IP "\s-1PERLIO_F_LINEBUF\s0" 4.IX Item "PERLIO_F_LINEBUF"Layer is line buffered. Write data should be passed to next layer downwhenever a \*(L"\en\*(R" is seen. Any data beyond the \*(L"\en\*(R" should then beprocessed..IP "\s-1PERLIO_F_TEMP\s0" 4.IX Item "PERLIO_F_TEMP"File has been \f(CW\*(C`unlink()\*(C'\fRed, or should be deleted on \f(CW\*(C`close()\*(C'\fR..IP "\s-1PERLIO_F_OPEN\s0" 4.IX Item "PERLIO_F_OPEN"Handle is open..IP "\s-1PERLIO_F_FASTGETS\s0" 4.IX Item "PERLIO_F_FASTGETS"This instance of this layer supports the "fast \f(CW\*(C`gets\*(C'\fR" interface.Normally set based on \f(CW\*(C`PERLIO_K_FASTGETS\*(C'\fR for the class and by theexistence of the function(s) in the table. However a class thatnormally provides that interface may need to avoid it on aparticular instance. The \*(L"pending\*(R" layer needs to do this whenit is pushed above a layer which does not support the interface.(Perl's \f(CW\*(C`sv_gets()\*(C'\fR does not expect the streams fast \f(CW\*(C`gets\*(C'\fR behaviourto change during one \*(L"get\*(R".).Sh "Methods in Detail".IX Subsection "Methods in Detail".IP "fsize" 4.IX Item "fsize".Vb 1\& Size_t fsize;.Ve.SpSize of the function table. This is compared against the value PerlIOcode \*(L"knows\*(R" as a compatibility check. Future versions \fImay\fR be ableto tolerate layers compiled against an old version of the headers..IP "name" 4.IX Item "name".Vb 1\& char * name;.Ve.SpThe name of the layer whose \fIopen()\fR method Perl should invoke on\&\fIopen()\fR. For example if the layer is called \s-1APR\s0, you will call:.Sp.Vb 1\& open $fh, ">:APR", ....Ve.Spand Perl knows that it has to invoke the \fIPerlIOAPR_open()\fR methodimplemented by the \s-1APR\s0 layer..IP "size" 4.IX Item "size".Vb 1\& Size_t size;.Ve.SpThe size of the per-instance data structure, e.g.:.Sp.Vb 1\& sizeof(PerlIOAPR).Ve.SpIf this field is zero then \f(CW\*(C`PerlIO_pushed\*(C'\fR does not malloc anythingand assumes layer's Pushed function will do any required layer stackmanipulation \- used to avoid malloc/free overhead for dummy layers.If the field is non-zero it must be at least the size of \f(CW\*(C`PerlIOl\*(C'\fR,\&\f(CW\*(C`PerlIO_pushed\*(C'\fR will allocate memory for the layer's data structuresand link new layer onto the stream's stack. (If the layer's Pushedmethod returns an error indication the layer is popped again.).IP "kind" 4.IX Item "kind".Vb 1\& IV kind;.Ve.RS 4.IP "\(bu" 4\&\s-1PERLIO_K_BUFFERED\s0.SpThe layer is buffered..IP "\(bu" 4\&\s-1PERLIO_K_RAW\s0.SpThe layer is acceptable to have in a binmode(\s-1FH\s0) stack \- i.e. it does not(or will configure itself not to) transform bytes passing through it..IP "\(bu" 4\&\s-1PERLIO_K_CANCRLF\s0.SpLayer can translate between \*(L"\en\*(R" and \s-1CRLF\s0 line ends..IP "\(bu" 4\&\s-1PERLIO_K_FASTGETS\s0.SpLayer allows buffer snooping..IP "\(bu" 4\&\s-1PERLIO_K_MULTIARG\s0.SpUsed when the layer's \fIopen()\fR accepts more arguments than usual. Theextra arguments should come not before the \f(CW\*(C`MODE\*(C'\fR argument. When thisflag is used it's up to the layer to validate the args..RE.RS 4.RE.IP "Pushed" 4.IX Item "Pushed".Vb 1\& IV (*Pushed)(pTHX_ PerlIO *f,const char *mode, SV *arg);.Ve.SpThe only absolutely mandatory method. Called when the layer is pushedonto the stack. The \f(CW\*(C`mode\*(C'\fR argument may be \s-1NULL\s0 if this occurspost-open. The \f(CW\*(C`arg\*(C'\fR will be non\-\f(CW\*(C`NULL\*(C'\fR if an argument string waspassed. In most cases this should call \f(CW\*(C`PerlIOBase_pushed()\*(C'\fR toconvert \f(CW\*(C`mode\*(C'\fR into the appropriate \f(CW\*(C`PERLIO_F_XXXXX\*(C'\fR flags inaddition to any actions the layer itself takes. If a layer is notexpecting an argument it need neither save the one passed to it, norprovide \f(CW\*(C`Getarg()\*(C'\fR (it could perhaps \f(CW\*(C`Perl_warn\*(C'\fR that the argumentwas un-expected)..SpReturns 0 on success. On failure returns \-1 and should set errno..IP "Popped" 4.IX Item "Popped".Vb 1\& IV (*Popped)(pTHX_ PerlIO *f);.Ve.SpCalled when the layer is popped from the stack. A layer will normallybe popped after \f(CW\*(C`Close()\*(C'\fR is called. But a layer can be poppedwithout being closed if the program is dynamically managing layers onthe stream. In such cases \f(CW\*(C`Popped()\*(C'\fR should free any resources(buffers, translation tables, ...) not held directly in the layer'sstruct. It should also \f(CW\*(C`Unread()\*(C'\fR any unconsumed data that has beenread and buffered from the layer below back to that layer, so that itcan be re-provided to what ever is now above..SpReturns 0 on success and failure. If \f(CW\*(C`Popped()\*(C'\fR returns \fItrue\fR then\&\fIperlio.c\fR assumes that either the layer has popped itself, or thelayer is super special and needs to be retained for other reasons.In most cases it should return \fIfalse\fR..IP "Open" 4.IX Item "Open".Vb 1\& PerlIO * (*Open)(...);.Ve.SpThe \f(CW\*(C`Open()\*(C'\fR method has lots of arguments because it combines thefunctions of perl's \f(CW\*(C`open\*(C'\fR, \f(CW\*(C`PerlIO_open\*(C'\fR, perl's \f(CW\*(C`sysopen\*(C'\fR,\&\f(CW\*(C`PerlIO_fdopen\*(C'\fR and \f(CW\*(C`PerlIO_reopen\*(C'\fR. The full prototype is asfollows:.Sp.Vb 6\& 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);.Ve.SpOpen should (perhaps indirectly) call \f(CW\*(C`PerlIO_allocate()\*(C'\fR to allocatea slot in the table and associate it with the layers information forthe opened file, by calling \f(CW\*(C`PerlIO_push\*(C'\fR. The \fIlayers\fR \s-1AV\s0 is anarray of all the layers destined for the \f(CW\*(C`PerlIO *\*(C'\fR, and anyarguments passed to them, \fIn\fR is the index into that array of thelayer being called. The macro \f(CW\*(C`PerlIOArg\*(C'\fR will return a (possibly\&\f(CW\*(C`NULL\*(C'\fR) \s-1SV\s0 * for the argument passed to the layer..SpThe \fImode\fR string is an "\f(CW\*(C`fopen()\*(C'\fR\-like" string which would matchthe regular expression \f(CW\*(C`/^[I#]?[rwa]\e+?[bt]?$/\*(C'\fR..SpThe \f(CW\*(AqI\*(Aq\fR prefix is used during creation of \f(CW\*(C`stdin\*(C'\fR..\f(CW\*(C`stderr\*(C'\fR viaspecial \f(CW\*(C`PerlIO_fdopen\*(C'\fR calls; the \f(CW\*(Aq#\*(Aq\fR prefix means that this is\&\f(CW\*(C`sysopen\*(C'\fR and that \fIimode\fR and \fIperm\fR should be passed to\&\f(CW\*(C`PerlLIO_open3\*(C'\fR; \f(CW\*(Aqr\*(Aq\fR means \fBr\fRead, \f(CW\*(Aqw\*(Aq\fR means \fBw\fRrite and\&\f(CW\*(Aqa\*(Aq\fR means \fBa\fRppend. The \f(CW\*(Aq+\*(Aq\fR suffix means that both reading andwriting/appending are permitted. The \f(CW\*(Aqb\*(Aq\fR suffix means file shouldbe binary, and \f(CW\*(Aqt\*(Aq\fR means it is text. (Almost all layers should dothe \s-1IO\s0 in binary mode, and ignore the b/t bits. The \f(CW\*(C`:crlf\*(C'\fR layershould be pushed to handle the distinction.).SpIf \fIold\fR is not \f(CW\*(C`NULL\*(C'\fR then this is a \f(CW\*(C`PerlIO_reopen\*(C'\fR. Perl itselfdoes not use this (yet?) and semantics are a little vague..SpIf \fIfd\fR not negative then it is the numeric file descriptor \fIfd\fR,which will be open in a manner compatible with the supplied modestring, the call is thus equivalent to \f(CW\*(C`PerlIO_fdopen\*(C'\fR. In this case\&\fInargs\fR will be zero..SpIf \fInargs\fR is greater than zero then it gives the number of argumentspassed to \f(CW\*(C`open\*(C'\fR, otherwise it will be 1 if for example\&\f(CW\*(C`PerlIO_open\*(C'\fR was called. In simple cases SvPV_nolen(*args) is thepathname to open..SpHaving said all that translation-only layers do not need to provide\&\f(CW\*(C`Open()\*(C'\fR at all, but rather leave the opening to a lower level layerand wait to be \*(L"pushed\*(R". If a layer does provide \f(CW\*(C`Open()\*(C'\fR it shouldnormally call the \f(CW\*(C`Open()\*(C'\fR method of next layer down (if any) andthen push itself on top if that succeeds..SpIf \f(CW\*(C`PerlIO_push\*(C'\fR was performed and open has failed, it must\&\f(CW\*(C`PerlIO_pop\*(C'\fR itself, since if it's not, the layer won't be removedand may cause bad problems..SpReturns \f(CW\*(C`NULL\*(C'\fR on failure..IP "Binmode" 4.IX Item "Binmode".Vb 1\& IV (*Binmode)(pTHX_ PerlIO *f);.Ve.SpOptional. Used when \f(CW\*(C`:raw\*(C'\fR layer is pushed (explicitly or as a resultof binmode(\s-1FH\s0)). If not present layer will be popped. If presentshould configure layer as binary (or pop itself) and return 0.If it returns \-1 for error \f(CW\*(C`binmode\*(C'\fR will fail with layerstill on the stack..IP "Getarg" 4.IX Item "Getarg".Vb 2\& SV * (*Getarg)(pTHX_ PerlIO *f,\& CLONE_PARAMS *param, int flags);.Ve.SpOptional. If present should return an \s-1SV\s0 * representing the stringargument passed to the layer when it waspushed. e.g. \*(L":encoding(ascii)\*(R" would return an SvPV with value\&\*(L"ascii\*(R". (\fIparam\fR and \fIflags\fR arguments can be ignored in mostcases).Sp\&\f(CW\*(C`Dup\*(C'\fR uses \f(CW\*(C`Getarg\*(C'\fR to retrieve the argument originally passed to\&\f(CW\*(C`Pushed\*(C'\fR, so you must implement this function if your layer has anextra argument to \f(CW\*(C`Pushed\*(C'\fR and will ever be \f(CW\*(C`Dup\*(C'\fRed..IP "Fileno" 4.IX Item "Fileno".Vb 1\& IV (*Fileno)(pTHX_ PerlIO *f);.Ve.SpReturns the Unix/Posix numeric file descriptor for the handle. Normally\&\f(CW\*(C`PerlIOBase_fileno()\*(C'\fR (which just asks next layer down) will sufficefor this..SpReturns \-1 on error, which is considered to include the case where thelayer cannot provide such a file descriptor..IP "Dup" 4.IX Item "Dup".Vb 2\& PerlIO * (*Dup)(pTHX_ PerlIO *f, PerlIO *o,\& CLONE_PARAMS *param, int flags);.Ve.Sp\&\s-1XXX:\s0 Needs more docs..SpUsed as part of the \*(L"clone\*(R" process when a thread is spawned (in whichcase param will be non-NULL) and when a stream is being duplicated via\&'&' in the \f(CW\*(C`open\*(C'\fR..SpSimilar to \f(CW\*(C`Open\*(C'\fR, returns PerlIO* on success, \f(CW\*(C`NULL\*(C'\fR on failure..IP "Read" 4.IX Item "Read".Vb 1\& SSize_t (*Read)(pTHX_ PerlIO *f, void *vbuf, Size_t count);.Ve.SpBasic read operation..SpTypically will call \f(CW\*(C`Fill\*(C'\fR and manipulate pointers (possibly via the\&\s-1API\s0). \f(CW\*(C`PerlIOBuf_read()\*(C'\fR may be suitable for derived classes whichprovide \*(L"fast gets\*(R" methods..SpReturns actual bytes read, or \-1 on an error..IP "Unread" 4.IX Item "Unread".Vb 2\& SSize_t (*Unread)(pTHX_ PerlIO *f,\& const void *vbuf, Size_t count);.Ve.SpA superset of stdio's \f(CW\*(C`ungetc()\*(C'\fR. Should arrange for future reads tosee the bytes in \f(CW\*(C`vbuf\*(C'\fR. If there is no obviously better implementationthen \f(CW\*(C`PerlIOBase_unread()\*(C'\fR provides the function by pushing a \*(L"fake\*(R"\&\*(L"pending\*(R" layer above the calling layer..SpReturns the number of unread chars..IP "Write" 4.IX Item "Write".Vb 1\& SSize_t (*Write)(PerlIO *f, const void *vbuf, Size_t count);.Ve.SpBasic write operation..SpReturns bytes written or \-1 on an error..IP "Seek" 4.IX Item "Seek".Vb 1\& IV (*Seek)(pTHX_ PerlIO *f, Off_t offset, int whence);.Ve.SpPosition the file pointer. Should normally call its own \f(CW\*(C`Flush\*(C'\fRmethod and then the \f(CW\*(C`Seek\*(C'\fR method of next layer down..SpReturns 0 on success, \-1 on failure..IP "Tell" 4.IX Item "Tell".Vb 1\& Off_t (*Tell)(pTHX_ PerlIO *f);.Ve.SpReturn the file pointer. May be based on layers cached concept ofposition to avoid overhead..SpReturns \-1 on failure to get the file pointer..IP "Close" 4.IX Item "Close".Vb 1\& IV (*Close)(pTHX_ PerlIO *f);.Ve.SpClose the stream. Should normally call \f(CW\*(C`PerlIOBase_close()\*(C'\fR to flushitself and close layers below, and then deallocate any data structures(buffers, translation tables, ...) not held directly in the datastructure..SpReturns 0 on success, \-1 on failure..IP "Flush" 4.IX Item "Flush".Vb 1\& IV (*Flush)(pTHX_ PerlIO *f);.Ve.SpShould make stream's state consistent with layers below. That is, anybuffered write data should be written, and file position of lower layers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -