perliol.pod
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· POD 代码 · 共 1,040 行 · 第 1/3 页
POD
1,040 行
=head1 NAMEperliol - C API for Perl's implementation of IO in Layers.=head1 SYNOPSIS /* Defining a layer ... */ #include <perliol.h>=head1 DESCRIPTIONThis document describes the behavior and implementation of the PerlIOabstraction described in L<perlapio> when C<USE_PERLIO> is defined (andC<USE_SFIO> is not).=head2 History and BackgroundThe 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 API is mostly fixed tomaintain (source) compatibility.The aim of the implementation is to provide the PerlIO API in a flexibleand platform neutral manner. It is also a trial of an "Object OrientedC, with vtables" approach which may be applied to Perl 6.=head2 Basic StructurePerlIO is a stack of layers.The 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 I<above> and I<below>are used to refer to the relative positioning of the stack layers.A layer contains a "vtable", the table of I/O operations (at C levela table of function pointers), and status flags. The functions in thevtable implement operations like "open", "read", and "write".When I/O, for example "read", is requested, the request goes from Perlfirst down the stack using "read" 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.The requests do not necessarily go always all the way down to theoperating system: that's where PerlIO buffering comes into play.When you do an open() and specify extra PerlIO layers to be deployed,the layers you specify are "pushed" on top of the already existingdefault stack. One way to see it is that "operating system ison the left" and "Perl is on the right".What 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 L<PerlIO>,L<perlrun/PERLIO>, and L<open> for more information.binmode() operates similarly to open(): by default the specifiedlayers are pushed on top of the existing stack.However, note that even as the specified layers are "pushed on top"for open() and binmode(), this doesn't mean that the effects arelimited to the "top": PerlIO layers can be very 'active' and inspectand affect layers also deeper in the stack. As an example thereis a layer called "raw" which repeatedly "pops" layers untilit reaches the first layer that has declared itself capable ofhandling binary data. The "pushed" layers are processed in left-to-rightorder.sysopen() operates (unsurprisingly) at a lower level in the stack thanopen(). For example in UNIX or UNIX-like systems sysopen() operatesdirectly at the level of file descriptors: in the terms of PerlIOlayers, it uses only the "unix" layer, which is a rather thin wrapperon top of the UNIX file descriptors.=head2 Layers vs DisciplinesInitial discussion of the ability to modify IO streams behaviour usedthe term "discipline" for the entities which were added. This came (Ibelieve) from the use of the term in "sfio", which in turn borrowed itfrom "line disciplines" on Unix terminals. However, this document (andthe C code) uses the term "layer".This is, I hope, a natural term given the implementation, and shouldavoid connotations that are inherent in earlier uses of "discipline"for things which are rather different.=head2 Data StructuresThe basic data structure is a PerlIOl: 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 */ };A C<PerlIOl *> is a pointer to the struct, and the I<application>level C<PerlIO *> is a pointer to a C<PerlIOl *> - i.e. a pointerto a pointer to the struct. This allows the application level C<PerlIO *>to remain constant while the actual C<PerlIOl *> underneathchanges. (Compare perl's C<SV *> which remains constant while itsC<sv_any> field changes as the scalar's type changes.) An IO stream isthen in general represented as a pointer to this linked-list of"layers".It should be noted that because of the double indirection in a C<PerlIO *>,a C<< &(perlio->next) >> "is" a C<PerlIO *>, and so to some degreeat least one layer can use the "standard" API on the next layer down.A "layer" is composed of two parts:=over 4=item 1.The functions and attributes of the "layer class".=item 2.The per-instance data for a particular handle.=back=head2 Functions and AttributesThe functions and attributes are accessed via the "tab" (for table)member of C<PerlIOl>. The functions (methods of the layer "class") arefixed, and are defined by the C<PerlIO_funcs> type. They are broadly thesame as the public C<PerlIO_xxxxx> functions: 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's 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); };The first few members of the struct give a function table size forcompatibility check "name" for the layer, the size to C<malloc> 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:=over 4=item 1.Opening and setup functions=item 2.Basic IO operations=item 3.Stdio class buffering options.=item 4.Functions to support Perl's traditional "fast" access to the buffer.=backA layer does not have to implement all the functions, but the wholetable has to be present. Unimplemented slots can be NULL (which willresult in an error when called) or can be filled in with stubs to"inherit" behaviour from a "base class". This "inheritance" is fixedfor all instances of the layer, but as the layer chooses which stubsto populate the table, limited "multiple inheritance" is possible.=head2 Per-instance DataThe per-instance data are held in memory beyond the basic PerlIOlstruct, by making a PerlIOl the first member of the layer's structthus: 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;In this way (as for perl's scalars) a pointer to a PerlIOBuf can betreated as a pointer to a PerlIOl.=head2 Layers in action. table perlio unix | | +-----------+ +----------+ +--------+ PerlIO ->| |--->| next |--->| NULL | +-----------+ +----------+ +--------+ | | | buffer | | fd | +-----------+ | | +--------+ | | +----------+The above attempts to show how the layer scheme works in a simple case.The application's C<PerlIO *> points to an entry in the table(s)representing open (allocated) handles. For example the first three slotsin the table correspond to C<stdin>,C<stdout> and C<stderr>. The tablein turn points to the current "top" layer for the handle - in this casean instance of the generic buffering layer "perlio". That layer in turnpoints to the next layer down - in this case the low-level "unix" layer.The above is roughly equivalent to a "stdio" buffered stream, but withmuch more flexibility:=over 4=item *If Unix level C<read>/C<write>/C<lseek> is not appropriate for (say)sockets then the "unix" layer can be replaced (at open time or evendynamically) with a "socket" layer.=item *Different handles can have different buffering schemes. The "top"layer could be the "mmap" layer if reading disk files was quickerusing C<mmap> than C<read>. An "unbuffered" stream can be implementedsimply by not having a buffer layer.=item *Extra 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+ - weneeded a mechanism to allow data to be translated between perl'sinternal encoding (conceptually at least Unicode as UTF-8), and the"native" format used by the system. This is provided by the":encoding(xxxx)" layer which typically sits above the buffering layer.=item *A layer can be added that does "\n" to CRLF translation. This layercan be used on any platform, not just those that normally do suchthings.=back=head2 Per-instance flag bitsThe generic flag bits are a hybrid of C<O_XXXXX> style flags deducedfrom the mode string passed to C<PerlIO_open()>, and state bits fortypical buffer layers.=over 4=item PERLIO_F_EOFEnd of file.=item PERLIO_F_CANWRITEWrites are permitted, i.e. opened as "w" or "r+" or "a", etc.=item PERLIO_F_CANREADReads are permitted i.e. opened "r" or "w+" (or even "a+" - ick).=item PERLIO_F_ERRORAn error has occurred (for C<PerlIO_error()>).=item PERLIO_F_TRUNCATETruncate file suggested by open mode.=item PERLIO_F_APPENDAll writes should be appends.=item PERLIO_F_CRLFLayer is performing Win32-like "\n" mapped to CR,LF for output and CR,LFmapped to "\n" for input. Normally the provided "crlf" layer is the onlylayer that need bother about this. C<PerlIO_binmode()> will mess with thisflag rather than add/remove layers if the C<PERLIO_K_CANCRLF> bit is setfor the layers class.=item PERLIO_F_UTF8Data written to this layer should be UTF-8 encoded; data providedby this layer should be considered UTF-8 encoded. Can be set on any layerby ":utf8" dummy layer. Also set on ":encoding" layer.=item PERLIO_F_UNBUFLayer is unbuffered - i.e. write to next layer down should occur foreach write to this layer.=item PERLIO_F_WRBUFThe buffer for this layer currently holds data written to it but not sentto next layer.=item PERLIO_F_RDBUFThe buffer for this layer currently holds unconsumed data read fromlayer below.=item PERLIO_F_LINEBUF
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?