📄 perlguts.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 "PERLGUTS 1".TH PERLGUTS 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"perlguts \- Introduction to the Perl API.SH "DESCRIPTION".IX Header "DESCRIPTION"This document attempts to describe how to use the Perl \s-1API\s0, as well asto provide some info on the basic workings of the Perl core. It is farfrom complete and probably contains many errors. Please refer anyquestions or comments to the author below..SH "Variables".IX Header "Variables".Sh "Datatypes".IX Subsection "Datatypes"Perl has three typedefs that handle Perl's three main data types:.PP.Vb 3\& SV Scalar Value\& AV Array Value\& HV Hash Value.Ve.PPEach typedef has specific routines that manipulate the various data types..ie n .Sh "What is an ""\s-1IV\s0""?".el .Sh "What is an ``\s-1IV\s0''?".IX Subsection "What is an IV?"Perl uses a special typedef \s-1IV\s0 which is a simple signed integer type that isguaranteed to be large enough to hold a pointer (as well as an integer).Additionally, there is the \s-1UV\s0, which is simply an unsigned \s-1IV\s0..PPPerl also uses two special typedefs, I32 and I16, which will always be atleast 32\-bits and 16\-bits long, respectively. (Again, there are U32 and U16,as well.) They will usually be exactly 32 and 16 bits long, but on Craysthey will both be 64 bits..Sh "Working with SVs".IX Subsection "Working with SVs"An \s-1SV\s0 can be created and loaded with one command. There are five types ofvalues that can be loaded: an integer value (\s-1IV\s0), an unsigned integervalue (\s-1UV\s0), a double (\s-1NV\s0), a string (\s-1PV\s0), and another scalar (\s-1SV\s0)..PPThe seven routines are:.PP.Vb 7\& SV* newSViv(IV);\& SV* newSVuv(UV);\& SV* newSVnv(double);\& SV* newSVpv(const char*, STRLEN);\& SV* newSVpvn(const char*, STRLEN);\& SV* newSVpvf(const char*, ...);\& SV* newSVsv(SV*);.Ve.PP\&\f(CW\*(C`STRLEN\*(C'\fR is an integer type (Size_t, usually defined as size_t in\&\fIconfig.h\fR) guaranteed to be large enough to represent the size ofany string that perl can handle..PPIn the unlikely case of a \s-1SV\s0 requiring more complex initialisation, youcan create an empty \s-1SV\s0 with newSV(len). If \f(CW\*(C`len\*(C'\fR is 0 an empty \s-1SV\s0 oftype \s-1NULL\s0 is returned, else an \s-1SV\s0 of type \s-1PV\s0 is returned with len + 1 (forthe \s-1NUL\s0) bytes of storage allocated, accessible via SvPVX. In both casesthe \s-1SV\s0 has value undef..PP.Vb 2\& SV *sv = newSV(0); /* no storage allocated */\& SV *sv = newSV(10); /* 10 (+1) bytes of uninitialised storage allocated */.Ve.PPTo change the value of an \fIalready-existing\fR \s-1SV\s0, there are eight routines:.PP.Vb 8\& void sv_setiv(SV*, IV);\& void sv_setuv(SV*, UV);\& void sv_setnv(SV*, double);\& void sv_setpv(SV*, const char*);\& void sv_setpvn(SV*, const char*, STRLEN)\& void sv_setpvf(SV*, const char*, ...);\& void sv_vsetpvfn(SV*, const char*, STRLEN, va_list *, SV **, I32, bool *);\& void sv_setsv(SV*, SV*);.Ve.PPNotice that you can choose to specify the length of the string to beassigned by using \f(CW\*(C`sv_setpvn\*(C'\fR, \f(CW\*(C`newSVpvn\*(C'\fR, or \f(CW\*(C`newSVpv\*(C'\fR, or you mayallow Perl to calculate the length by using \f(CW\*(C`sv_setpv\*(C'\fR or by specifying0 as the second argument to \f(CW\*(C`newSVpv\*(C'\fR. Be warned, though, that Perl willdetermine the string's length by using \f(CW\*(C`strlen\*(C'\fR, which depends on thestring terminating with a \s-1NUL\s0 character..PPThe arguments of \f(CW\*(C`sv_setpvf\*(C'\fR are processed like \f(CW\*(C`sprintf\*(C'\fR, and theformatted output becomes the value..PP\&\f(CW\*(C`sv_vsetpvfn\*(C'\fR is an analogue of \f(CW\*(C`vsprintf\*(C'\fR, but it allows you to specifyeither a pointer to a variable argument list or the address and length ofan array of SVs. The last argument points to a boolean; on return, if thatboolean is true, then locale-specific information has been used to formatthe string, and the string's contents are therefore untrustworthy (seeperlsec). This pointer may be \s-1NULL\s0 if that information is notimportant. Note that this function requires you to specify the length ofthe format..PPThe \f(CW\*(C`sv_set*()\*(C'\fR functions are not generic enough to operate on valuesthat have \*(L"magic\*(R". See \*(L"Magic Virtual Tables\*(R" later in this document..PPAll SVs that contain strings should be terminated with a \s-1NUL\s0 character.If it is not NUL-terminated there is a risk ofcore dumps and corruptions from code which passes the string to Cfunctions or system calls which expect a NUL-terminated string.Perl's own functions typically add a trailing \s-1NUL\s0 for this reason.Nevertheless, you should be very careful when you pass a string storedin an \s-1SV\s0 to a C function or system call..PPTo access the actual value that an \s-1SV\s0 points to, you can use the macros:.PP.Vb 5\& SvIV(SV*)\& SvUV(SV*)\& SvNV(SV*)\& SvPV(SV*, STRLEN len)\& SvPV_nolen(SV*).Ve.PPwhich will automatically coerce the actual scalar type into an \s-1IV\s0, \s-1UV\s0, double,or string..PPIn the \f(CW\*(C`SvPV\*(C'\fR macro, the length of the string returned is placed into thevariable \f(CW\*(C`len\*(C'\fR (this is a macro, so you do \fInot\fR use \f(CW&len\fR). If you donot care what the length of the data is, use the \f(CW\*(C`SvPV_nolen\*(C'\fR macro.Historically the \f(CW\*(C`SvPV\*(C'\fR macro with the global variable \f(CW\*(C`PL_na\*(C'\fR has beenused in this case. But that can be quite inefficient because \f(CW\*(C`PL_na\*(C'\fR mustbe accessed in thread-local storage in threaded Perl. In any case, rememberthat Perl allows arbitrary strings of data that may both contain NULs andmight not be terminated by a \s-1NUL\s0..PPAlso remember that C doesn't allow you to safely say \f(CW\*(C`foo(SvPV(s, len),len);\*(C'\fR. It might work with your compiler, but it won't work for everyone.Break this sort of statement up into separate assignments:.PP.Vb 5\& SV *s;\& STRLEN len;\& char * ptr;\& ptr = SvPV(s, len);\& foo(ptr, len);.Ve.PPIf you want to know if the scalar value is \s-1TRUE\s0, you can use:.PP.Vb 1\& SvTRUE(SV*).Ve.PPAlthough Perl will automatically grow strings for you, if you need to forcePerl to allocate more memory for your \s-1SV\s0, you can use the macro.PP.Vb 1\& SvGROW(SV*, STRLEN newlen).Ve.PPwhich will determine if more memory needs to be allocated. If so, it willcall the function \f(CW\*(C`sv_grow\*(C'\fR. Note that \f(CW\*(C`SvGROW\*(C'\fR can only increase, notdecrease, the allocated memory of an \s-1SV\s0 and that it does not automaticallyadd a byte for the a trailing \s-1NUL\s0 (perl's own string functions typically do\&\f(CW\*(C`SvGROW(sv, len + 1)\*(C'\fR)..PPIf you have an \s-1SV\s0 and want to know what kind of data Perl thinks is storedin it, you can use the following macros to check the type of \s-1SV\s0 you have..PP.Vb 3\& SvIOK(SV*)\& SvNOK(SV*)\& SvPOK(SV*).Ve.PPYou can get and set the current length of the string stored in an \s-1SV\s0 with
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -