📄 perlfunc.1
字号:
.PD 0.IP "abs" 8.IX Item "abs".PDReturns the absolute value of its argument.If \s-1VALUE\s0 is omitted, uses \f(CW$_\fR..IP "accept \s-1NEWSOCKET\s0,GENERICSOCKET" 8.IX Xref "accept".IX Item "accept NEWSOCKET,GENERICSOCKET"Accepts an incoming socket connect, just as the \fIaccept\fR\|(2) system calldoes. Returns the packed address if it succeeded, false otherwise.See the example in \*(L"Sockets: Client/Server Communication\*(R" in perlipc..SpOn systems that support a close-on-exec flag on files, the flag willbe set for the newly opened file descriptor, as determined by thevalue of $^F. See \*(L"$^F\*(R" in perlvar..IP "alarm \s-1SECONDS\s0" 8.IX Xref "alarm SIGALRM timer".IX Item "alarm SECONDS".PD 0.IP "alarm" 8.IX Item "alarm".PDArranges to have a \s-1SIGALRM\s0 delivered to this process after thespecified number of wallclock seconds has elapsed. If \s-1SECONDS\s0 is notspecified, the value stored in \f(CW$_\fR is used. (On some machines,unfortunately, the elapsed time may be up to one second less or morethan you specified because of how seconds are counted, and processscheduling may delay the delivery of the signal even further.).SpOnly one timer may be counting at once. Each call disables theprevious timer, and an argument of \f(CW0\fR may be supplied to cancel theprevious timer without starting a new one. The returned value is theamount of time remaining on the previous timer..SpFor delays of finer granularity than one second, the Time::HiRes module(from \s-1CPAN\s0, and starting from Perl 5.8 part of the standarddistribution) provides \fIualarm()\fR. You may also use Perl's four-argumentversion of \fIselect()\fR leaving the first three arguments undefined, or youmight be able to use the \f(CW\*(C`syscall\*(C'\fR interface to access \fIsetitimer\fR\|(2) ifyour system supports it. See perlfaq8 for details..SpIt is usually a mistake to intermix \f(CW\*(C`alarm\*(C'\fR and \f(CW\*(C`sleep\*(C'\fR calls.(\f(CW\*(C`sleep\*(C'\fR may be internally implemented in your system with \f(CW\*(C`alarm\*(C'\fR).SpIf you want to use \f(CW\*(C`alarm\*(C'\fR to time out a system call you need to use an\&\f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR pair. You can't rely on the alarm causing the system call tofail with \f(CW$!\fR set to \f(CW\*(C`EINTR\*(C'\fR because Perl sets up signal handlers torestart system calls on some systems. Using \f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR always works,modulo the caveats given in \*(L"Signals\*(R" in perlipc..Sp.Vb 10\& eval {\& local $SIG{ALRM} = sub { die "alarm\en" }; # NB: \en required\& alarm $timeout;\& $nread = sysread SOCKET, $buffer, $size;\& alarm 0;\& };\& if ($@) {\& die unless $@ eq "alarm\en"; # propagate unexpected errors\& # timed out\& }\& else {\& # didn\*(Aqt\& }.Ve.SpFor more information see perlipc..IP "atan2 Y,X" 8.IX Xref "atan2 arctangent tan tangent".IX Item "atan2 Y,X"Returns the arctangent of Y/X in the range \-PI to \s-1PI\s0..SpFor the tangent operation, you may use the \f(CW\*(C`Math::Trig::tan\*(C'\fRfunction, or use the familiar relation:.Sp.Vb 1\& sub tan { sin($_[0]) / cos($_[0]) }.Ve.SpNote that atan2(0, 0) is not well-defined..IP "bind \s-1SOCKET\s0,NAME" 8.IX Xref "bind".IX Item "bind SOCKET,NAME"Binds a network address to a socket, just as the bind system calldoes. Returns true if it succeeded, false otherwise. \s-1NAME\s0 should be apacked address of the appropriate type for the socket. See the examples in\&\*(L"Sockets: Client/Server Communication\*(R" in perlipc..IP "binmode \s-1FILEHANDLE\s0, \s-1LAYER\s0" 8.IX Xref "binmode binary text DOS Windows".IX Item "binmode FILEHANDLE, LAYER".PD 0.IP "binmode \s-1FILEHANDLE\s0" 8.IX Item "binmode FILEHANDLE".PDArranges for \s-1FILEHANDLE\s0 to be read or written in \*(L"binary\*(R" or \*(L"text\*(R"mode on systems where the run-time libraries distinguish betweenbinary and text files. If \s-1FILEHANDLE\s0 is an expression, the value istaken as the name of the filehandle. Returns true on success,otherwise it returns \f(CW\*(C`undef\*(C'\fR and sets \f(CW$!\fR (errno)..SpOn some systems (in general, \s-1DOS\s0 and Windows-based systems) \fIbinmode()\fRis necessary when you're not working with a text file. For the sakeof portability it is a good idea to always use it when appropriate,and to never use it when it isn't appropriate. Also, people canset their I/O to be by default \s-1UTF\-8\s0 encoded Unicode, not bytes..SpIn other words: regardless of platform, use \fIbinmode()\fR on binary data,like for example images..SpIf \s-1LAYER\s0 is present it is a single string, but may contain multipledirectives. The directives alter the behaviour of the file handle.When \s-1LAYER\s0 is present using binmode on text file makes sense..SpIf \s-1LAYER\s0 is omitted or specified as \f(CW\*(C`:raw\*(C'\fR the filehandle is madesuitable for passing binary data. This includes turning off possible \s-1CRLF\s0translation and marking it as bytes (as opposed to Unicode characters).Note that, despite what may be implied in \fI\*(L"Programming Perl\*(R"\fR (theCamel) or elsewhere, \f(CW\*(C`:raw\*(C'\fR is \fInot\fR simply the inverse of \f(CW\*(C`:crlf\*(C'\fR\&\*(-- other layers which would affect the binary nature of the stream are\&\fIalso\fR disabled. See PerlIO, perlrun and the discussion about the\&\s-1PERLIO\s0 environment variable..SpThe \f(CW\*(C`:bytes\*(C'\fR, \f(CW\*(C`:crlf\*(C'\fR, and \f(CW\*(C`:utf8\*(C'\fR, and any other directives of theform \f(CW\*(C`:...\*(C'\fR, are called I/O \fIlayers\fR. The \f(CW\*(C`open\*(C'\fR pragma can be used toestablish default I/O layers. See open..Sp\&\fIThe \s-1LAYER\s0 parameter of the \fIbinmode()\fI function is described as \*(L"\s-1DISCIPLINE\s0\*(R"in \*(L"Programming Perl, 3rd Edition\*(R". However, since the publishing of thisbook, by many known as \*(L"Camel \s-1III\s0\*(R", the consensus of the naming of thisfunctionality has moved from \*(L"discipline\*(R" to \*(L"layer\*(R". All documentationof this version of Perl therefore refers to \*(L"layers\*(R" rather than to\&\*(L"disciplines\*(R". Now back to the regularly scheduled documentation...\fR.SpTo mark \s-1FILEHANDLE\s0 as \s-1UTF\-8\s0, use \f(CW\*(C`:utf8\*(C'\fR or \f(CW\*(C`:encoding(utf8)\*(C'\fR.\&\f(CW\*(C`:utf8\*(C'\fR just marks the data as \s-1UTF\-8\s0 without further checking,while \f(CW\*(C`:encoding(utf8)\*(C'\fR checks the data for actually being valid\&\s-1UTF\-8\s0. More details can be found in PerlIO::encoding..SpIn general, \fIbinmode()\fR should be called after \fIopen()\fR but before any I/Ois done on the filehandle. Calling \fIbinmode()\fR will normally flush anypending buffered output data (and perhaps pending input data) on thehandle. An exception to this is the \f(CW\*(C`:encoding\*(C'\fR layer thatchanges the default character encoding of the handle, see open.The \f(CW\*(C`:encoding\*(C'\fR layer sometimes needs to be called inmid-stream, and it doesn't flush the stream. The \f(CW\*(C`:encoding\*(C'\fRalso implicitly pushes on top of itself the \f(CW\*(C`:utf8\*(C'\fR layer becauseinternally Perl will operate on \s-1UTF\-8\s0 encoded Unicode characters..SpThe operating system, device drivers, C libraries, and Perl run-timesystem all work together to let the programmer treat a singlecharacter (\f(CW\*(C`\en\*(C'\fR) as the line terminator, irrespective of the externalrepresentation. On many operating systems, the native text filerepresentation matches the internal representation, but on someplatforms the external representation of \f(CW\*(C`\en\*(C'\fR is made up of more thanone character..SpMac \s-1OS\s0, all variants of Unix, and Stream_LF files on \s-1VMS\s0 use a singlecharacter to end each line in the external representation of text (eventhough that single character is \s-1CARRIAGE\s0 \s-1RETURN\s0 on Mac \s-1OS\s0 and \s-1LINE\s0 \s-1FEED\s0on Unix and most \s-1VMS\s0 files). In other systems like \s-1OS/2\s0, \s-1DOS\s0 and thevarious flavors of MS-Windows your program sees a \f(CW\*(C`\en\*(C'\fR as a simple \f(CW\*(C`\ecJ\*(C'\fR,but what's stored in text files are the two characters \f(CW\*(C`\ecM\ecJ\*(C'\fR. Thatmeans that, if you don't use \fIbinmode()\fR on these systems, \f(CW\*(C`\ecM\ecJ\*(C'\fRsequences on disk will be converted to \f(CW\*(C`\en\*(C'\fR on input, and any \f(CW\*(C`\en\*(C'\fR inyour program will be converted back to \f(CW\*(C`\ecM\ecJ\*(C'\fR on output. This is whatyou want for text files, but it can be disastrous for binary files..SpAnother consequence of using \fIbinmode()\fR (on some systems) is thatspecial end-of-file markers will be seen as part of the data stream.For systems from the Microsoft family this means that if your binarydata contains \f(CW\*(C`\ecZ\*(C'\fR, the I/O subsystem will regard it as the end ofthe file, unless you use \fIbinmode()\fR..Sp\&\fIbinmode()\fR is not only important for \fIreadline()\fR and \fIprint()\fR operations,but also when using \fIread()\fR, \fIseek()\fR, \fIsysread()\fR, \fIsyswrite()\fR and \fItell()\fR(see perlport for more details). See the \f(CW$/\fR and \f(CW\*(C`$\e\*(C'\fR variablesin perlvar for how to manually set your input and outputline-termination sequences..IP "bless \s-1REF\s0,CLASSNAME" 8.IX Xref "bless".IX Item "bless REF,CLASSNAME".PD 0.IP "bless \s-1REF\s0" 8.IX Item "bless REF".PDThis function tells the thingy referenced by \s-1REF\s0 that it is now an objectin the \s-1CLASSNAME\s0 package. If \s-1CLASSNAME\s0 is omitted, the current packageis used. Because a \f(CW\*(C`bless\*(C'\fR is often the last thing in a constructor,it returns the reference for convenience. Always use the two-argumentversion if a derived class might inherit the function doing the blessing.See perltoot and perlobj for more about the blessing (and blessings)of objects..SpConsider always blessing objects in CLASSNAMEs that are mixed case.Namespaces with all lowercase names are considered reserved forPerl pragmata. Builtin types have all uppercase names. To preventconfusion, you may wish to avoid such package names as well. Make surethat \s-1CLASSNAME\s0 is a true value..SpSee \*(L"Perl Modules\*(R" in perlmod..IP "break" 8.IX Item "break"Break out of a \f(CW\*(C`given()\*(C'\fR block..SpThis keyword is enabled by the \*(L"switch\*(R" feature: see featurefor more information..IP "caller \s-1EXPR\s0" 8.IX Xref "caller call stack stack stack trace".IX Item "caller EXPR".PD 0.IP "caller" 8.IX Item "caller".PDReturns the context of the current subroutine call. In scalar context,returns the caller's package name if there is a caller, that is, ifwe're in a subroutine or \f(CW\*(C`eval\*(C'\fR or \f(CW\*(C`require\*(C'\fR, and the undefined valueotherwise. In list context, returns.Sp.Vb 2\& # 0 1 2\& ($package, $filename, $line) = caller;.Ve.SpWith \s-1EXPR\s0, it returns some extra information that the debugger uses toprint a stack trace. The value of \s-1EXPR\s0 indicates how many call framesto go back before the current one..Sp.Vb 2\& # 0 1 2 3 4\& ($package, $filename, $line, $subroutine, $hasargs,\&\& # 5 6 7 8 9 10\& $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)\& = caller($i);.Ve.SpHere \f(CW$subroutine\fR may be \f(CW\*(C`(eval)\*(C'\fR if the frame is not a subroutinecall, but an \f(CW\*(C`eval\*(C'\fR. In such a case additional elements \f(CW$evaltext\fR and\&\f(CW$is_require\fR are set: \f(CW$is_require\fR is true if the frame is created by a\&\f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR statement, \f(CW$evaltext\fR contains the text of the\&\f(CW\*(C`eval EXPR\*(C'\fR statement. In particular, for an \f(CW\*(C`eval BLOCK\*(C'\fR statement,\&\f(CW$subroutine\fR is \f(CW\*(C`(eval)\*(C'\fR, but \f(CW$evaltext\fR is undefined. (Note also thateach \f(CW\*(C`use\*(C'\fR statement creates a \f(CW\*(C`require\*(C'\fR frame inside an \f(CW\*(C`eval EXPR\*(C'\fRframe.) \f(CW$subroutine\fR may also be \f(CW\*(C`(unknown)\*(C'\fR if this particularsubroutine happens to have been deleted from the symbol table.\&\f(CW$hasargs\fR is true if a new instance of \f(CW@_\fR was set up for the frame.\&\f(CW$hints\fR and \f(CW$bitmask\fR contain pragmatic hints that the caller wascompiled with. The \f(CW$hints\fR and \f(CW$bitmask\fR values are subject to changebetween versions of Perl, and are not meant for external use..Sp\&\f(CW$hinthash\fR is a reference to a hash containing the value of \f(CW\*(C`%^H\*(C'\fR when thecaller was compiled, or \f(CW\*(C`undef\*(C'\fR if \f(CW\*(C`%^H\*(C'\fR was empty. Do not modify the valuesof this hash, as they are the actual values stored in the optree..SpFurthermore, when called from within the \s-1DB\s0 package, caller returns moredetailed information: it sets the list variable \f(CW@DB::args\fR to be thearguments with which the subroutine was invoked..SpBe aware that the optimizer might have optimized call frames away before\&\f(CW\*(C`caller\*(C'\fR had a chance to get the information. That means that \f(CWcaller(N)\fRmight not return information about the call frame you expect it do, for\&\f(CW\*(C`N > 1\*(C'\fR. In particular, \f(CW@DB::args\fR might have information from theprevious time \f(CW\*(C`caller\*(C'\fR was called..IP "chdir \s-1EXPR\s0" 8.IX Xref "chdir cd directory, change".IX Item "chdir EXPR"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -