perlfunc.pod

来自「MSYS在windows下模拟了一个类unix的终端」· POD 代码 · 共 1,621 行 · 第 1/5 页

POD
1,621
字号
access() family of system calls.  Also note that the C<-x> and C<-X> mayunder this pragma return true even if there are no execute permissionbits set (nor any extra execute permission ACLs).  This strangeness isdue to the underlying system calls' definitions.  Read thedocumentation for the C<filetest> pragma for more information.Note that C<-s/a/b/> does not do a negated substitution.  SayingC<-exp($foo)> still works as expected, however--only single lettersfollowing a minus are interpreted as file tests.The C<-T> and C<-B> switches work as follows.  The first block or so of thefile is examined for odd characters such as strange control codes orcharacters with the high bit set.  If too many strange characters (>30%)are found, it's a C<-B> file, otherwise it's a C<-T> file.  Also, any filecontaining null in the first block is considered a binary file.  If C<-T>or C<-B> is used on a filehandle, the current stdio buffer is examinedrather than the first block.  Both C<-T> and C<-B> return true on a nullfile, or a file at EOF when testing a filehandle.  Because you have toread a file to do the C<-T> test, on most occasions you want to use a C<-f>against the file first, as in C<next unless -f $file && -T $file>.If any of the file tests (or either the C<stat> or C<lstat> operators) are giventhe special filehandle consisting of a solitary underline, then the statstructure of the previous file test (or stat operator) is used, savinga system call.  (This doesn't work with C<-t>, and you need to rememberthat lstat() and C<-l> will leave values in the stat structure for thesymbolic link, not the real file.)  Example:    print "Can do.\n" if -r $a || -w _ || -x _;    stat($filename);    print "Readable\n" if -r _;    print "Writable\n" if -w _;    print "Executable\n" if -x _;    print "Setuid\n" if -u _;    print "Setgid\n" if -g _;    print "Sticky\n" if -k _;    print "Text\n" if -T _;    print "Binary\n" if -B _;=item abs VALUE=item absReturns the absolute value of its argument.If VALUE is omitted, uses C<$_>.=item accept NEWSOCKET,GENERICSOCKETAccepts an incoming socket connect, just as the accept(2) system calldoes.  Returns the packed address if it succeeded, false otherwise.See the example in L<perlipc/"Sockets: Client/Server Communication">.On 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<perlvar/$^F>.=item alarm SECONDS=item alarmArranges to have a SIGALRM delivered to this process after thespecified number of seconds have elapsed.  If SECONDS is not specified,the value stored in C<$_> is used. (On some machines,unfortunately, the elapsed time may be up to one second less than youspecified because of how seconds are counted.)  Only one timer may becounting at once.  Each call disables the previous timer, and anargument of C<0> may be supplied to cancel the previous timer withoutstarting a new one.  The returned value is the amount of time remainingon the previous timer.For delays of finer granularity than one second, you may use Perl'sfour-argument version of select() leaving the first three argumentsundefined, or you might be able to use the C<syscall> interface toaccess setitimer(2) if your system supports it.  The Time::HiRes modulefrom CPAN may also prove useful.It is usually a mistake to intermix C<alarm> and C<sleep> calls.(C<sleep> may be internally implemented in your system with C<alarm>)If you want to use C<alarm> to time out a system call you need to use anC<eval>/C<die> pair.  You can't rely on the alarm causing the system call tofail with C<$!> set to C<EINTR> because Perl sets up signal handlers torestart system calls on some systems.  Using C<eval>/C<die> always works,modulo the caveats given in L<perlipc/"Signals">.    eval {	local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required	alarm $timeout;	$nread = sysread SOCKET, $buffer, $size;	alarm 0;    };    if ($@) {	die unless $@ eq "alarm\n";   # propagate unexpected errors    	# timed out    }    else {    	# didn't    }=item atan2 Y,XReturns the arctangent of Y/X in the range -PI to PI.For the tangent operation, you may use the C<Math::Trig::tan>function, or use the familiar relation:    sub tan { sin($_[0]) / cos($_[0])  }=item bind SOCKET,NAMEBinds a network address to a socket, just as the bind system calldoes.  Returns true if it succeeded, false otherwise.  NAME should be apacked address of the appropriate type for the socket.  See the examples inL<perlipc/"Sockets: Client/Server Communication">.=item binmode FILEHANDLE, DISCIPLINE=item binmode FILEHANDLEArranges for FILEHANDLE to be read or written in "binary" or "text" modeon systems where the run-time libraries distinguish between binary andtext files.  If FILEHANDLE is an expression, the value is taken as thename of the filehandle.  DISCIPLINE can be either of C<":raw"> forbinary mode or C<":crlf"> for "text" mode.  If the DISCIPLINE isomitted, it defaults to C<":raw">.binmode() should be called after open() but before any I/O is done onthe filehandle.On many systems binmode() currently has no effect, but in future, itwill be extended to support user-defined input and output disciplines.On some systems binmode() is necessary when you're not working with atext file.  For the sake of portability it is a good idea to always useit when appropriate, and to never use it when it isn't appropriate.In other words:  Regardless of platform, use binmode() on binaryfiles, and do not use binmode() on text files.The C<open> pragma can be used to establish default disciplines.See L<open>.The operating system, device drivers, C libraries, and Perl run-timesystem all work together to let the programmer treat a singlecharacter (C<\n>) 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 C<\n> is made up of more thanone character.Mac OS and all variants of Unix use a single character to end each linein the external representation of text (even though that singlecharacter is not necessarily the same across these platforms).Consequently binmode() has no effect on these operating systems.  Inother systems like VMS, MS-DOS and the various flavors of MS-Windowsyour program sees a C<\n> as a simple C<\cJ>, but what's stored in textfiles are the two characters C<\cM\cJ>.  That means that, if you don'tuse binmode() on these systems, C<\cM\cJ> sequences on disk will beconverted to C<\n> on input, and any C<\n> in your program will beconverted back to C<\cM\cJ> on output.  This is what you want for textfiles, but it can be disastrous for binary files.Another consequence of using binmode() (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 C<\cZ>, the I/O subsystem will regard it as the end ofthe file, unless you use binmode().binmode() is not only important for readline() and print() operations,but also when using read(), seek(), sysread(), syswrite() and tell()(see L<perlport> for more details).  See the C<$/> and C<$\> variablesin L<perlvar> for how to manually set your input and outputline-termination sequences.=item bless REF,CLASSNAME=item bless REFThis function tells the thingy referenced by REF that it is now an objectin the CLASSNAME package.  If CLASSNAME is omitted, the current packageis used.  Because a C<bless> is often the last thing in a constructor,it returns the reference for convenience.  Always use the two-argumentversion if the function doing the blessing might be inherited by aderived class.  See L<perltoot> and L<perlobj> for more about the blessing(and blessings) of objects.Consider 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, so to preventconfusion, you may wish to avoid such package names as well.  Make surethat CLASSNAME is a true value.See L<perlmod/"Perl Modules">.=item caller EXPR=item callerReturns 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 C<eval> or C<require>, and the undefined valueotherwise.  In list context, returns    ($package, $filename, $line) = caller;With EXPR, it returns some extra information that the debugger uses toprint a stack trace.  The value of EXPR indicates how many call framesto go back before the current one.    ($package, $filename, $line, $subroutine, $hasargs,    $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller($i);Here $subroutine may be C<(eval)> if the frame is not a subroutinecall, but an C<eval>.  In such a case additional elements $evaltext andC<$is_require> are set: C<$is_require> is true if the frame is created by aC<require> or C<use> statement, $evaltext contains the text of theC<eval EXPR> statement.  In particular, for an C<eval BLOCK> statement,$filename is C<(eval)>, but $evaltext is undefined.  (Note also thateach C<use> statement creates a C<require> frame inside an C<eval EXPR>)frame.  C<$hasargs> is true if a new instance of C<@_> was set up for theframe.  C<$hints> and C<$bitmask> contain pragmatic hints that the callerwas compiled with.  The C<$hints> and C<$bitmask> values are subject tochange between versions of Perl, and are not meant for external use.Furthermore, when called from within the DB package, caller returns moredetailed information: it sets the list variable C<@DB::args> to be thearguments with which the subroutine was invoked.Be aware that the optimizer might have optimized call frames away beforeC<caller> had a chance to get the information.  That means that C<caller(N)>might not return information about the call frame you expect it do, forC<< N > 1 >>.  In particular, C<@DB::args> might have information from the previous time C<caller> was called.=item chdir EXPRChanges the working directory to EXPR, if possible.  If EXPR is omitted,changes to the directory specified by C<$ENV{HOME}>, if set; if not,changes to the directory specified by C<$ENV{LOGDIR}>.  If neither isset, C<chdir> does nothing.  It returns true upon success, falseotherwise.  See the example under C<die>.=item chmod LISTChanges the permissions of a list of files.  The first element of thelist must be the numerical mode, which should probably be an octalnumber, and which definitely should I<not> a string of octal digits:C<0644> is okay, C<'0644'> is not.  Returns the number of filessuccessfully changed.  See also L</oct>, if all you have is a string.    $cnt = chmod 0755, 'foo', 'bar';    chmod 0755, @executables;    $mode = '0644'; chmod $mode, 'foo';      # !!! sets mode to                                             # --w----r-T    $mode = '0644'; chmod oct($mode), 'foo'; # this is better    $mode = 0644;   chmod $mode, 'foo';      # this is bestYou can also import the symbolic C<S_I*> constants from the Fcntlmodule:    use Fcntl ':mode';    chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables;    # This is identical to the chmod 0755 of the above example.=item chomp VARIABLE=item chomp LIST=item chompThis safer version of L</chop> removes any trailing stringthat corresponds to the current value of C<$/> (also known as$INPUT_RECORD_SEPARATOR in the C<English> module).  It returns the totalnumber of characters removed from all its arguments.  It's often used toremove the newline from the end of an input record when you're worriedthat the final record may be missing its newline.  When in paragraphmode (C<$/ = "">), it removes all trailing newlines from the string.When in slurp mode (C<$/ = undef>) or fixed-length record mode (C<$/> isa reference to an integer or the like, see L<perlvar>) chomp() won'tremove anything.  If VARIABLE is omitted, it chomps C<$_>.  Example:    while (<>) {	chomp;	# avoid \n on last field	@array = split(/:/);	# ...    }If VARIABLE is a hash, it chomps the hash's values, but not its keys.You can actually chomp anything that's an lvalue, including an assignment:    chomp($cwd = `pwd`);    chomp($answer = <STDIN>);If you chomp a list, each element is chomped, and the total number ofcharacters removed is returned.=item chop VARIABLE=item chop LIST=item chopChops off the last character of a string and returns the characterchopped.  It is much more efficient than C<s/.$//s> because it neitherscans nor copies the string.  If VARIABLE is omitted, chops C<$_>.If VARIABLE is a hash, it chops the hash's values, but not its keys.You can actually chop anything that's an lvalue, including an assignment.If you chop a list, each element is chopped.  Only the value of thelast C<chop> is returned.Note that C<chop> returns the last character.  To return all but the lastcharacter, use C<substr($string, 0, -1)>.=item chown LISTChanges the owner (and group) of a list of files.  The first twoelements of the list must be the I<numeric> uid and gid, in thatorder.  A value of -1 in either position is interpreted by mostsystems to leave that value unchanged.  Returns the number of filessuccessfully changed.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?