perlfunc.pod
来自「ARM上的如果你对底层感兴趣」· POD 代码 · 共 1,705 行 · 第 1/5 页
POD
1,705 行
open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
or die "Can't open mailbox: $!";
lock();
print MBOX $msg,"\n\n";
unlock();
See also L<DB_File> for other flock() examples.
=item fork
Does a fork(2) system call. Returns the child pid to the parent process,
C<0> to the child process, or C<undef> if the fork is unsuccessful.
Note: unflushed buffers remain unflushed in both processes, which means
you may need to set C<$|> ($AUTOFLUSH in English) or call the C<autoflush()>
method of C<IO::Handle> to avoid duplicate output.
If you C<fork()> without ever waiting on your children, you will accumulate
zombies:
$SIG{CHLD} = sub { wait };
There's also the double-fork trick (error checking on
C<fork()> returns omitted);
unless ($pid = fork) {
unless (fork) {
exec "what you really wanna do";
die "no exec";
# ... or ...
## (some_perl_code_here)
exit 0;
}
exit 0;
}
waitpid($pid,0);
See also L<perlipc> for more examples of forking and reaping
moribund children.
Note that if your forked child inherits system file descriptors like
STDIN and STDOUT that are actually connected by a pipe or socket, even
if you exit, then the remote server (such as, say, httpd or rsh) won't think
you're done. You should reopen those to F</dev/null> if it's any issue.
=item format
Declare a picture format for use by the C<write()> function. For
example:
format Something =
Test: @<<<<<<<< @||||| @>>>>>
$str, $%, '$' . int($num)
.
$str = "widget";
$num = $cost/$quantity;
$~ = 'Something';
write;
See L<perlform> for many details and examples.
=item formline PICTURE,LIST
This is an internal function used by C<format>s, though you may call it,
too. It formats (see L<perlform>) a list of values according to the
contents of PICTURE, placing the output into the format output
accumulator, C<$^A> (or C<$ACCUMULATOR> in English).
Eventually, when a C<write()> is done, the contents of
C<$^A> are written to some filehandle, but you could also read C<$^A>
yourself and then set C<$^A> back to C<"">. Note that a format typically
does one C<formline()> per line of form, but the C<formline()> function itself
doesn't care how many newlines are embedded in the PICTURE. This means
that the C<~> and C<~~> tokens will treat the entire PICTURE as a single line.
You may therefore need to use multiple formlines to implement a single
record format, just like the format compiler.
Be careful if you put double quotes around the picture, because an "C<@>"
character may be taken to mean the beginning of an array name.
C<formline()> always returns TRUE. See L<perlform> for other examples.
=item getc FILEHANDLE
=item getc
Returns the next character from the input file attached to FILEHANDLE,
or the undefined value at end of file, or if there was an error. If
FILEHANDLE is omitted, reads from STDIN. This is not particularly
efficient. It cannot be used to get unbuffered single-characters,
however. For that, try something more like:
if ($BSD_STYLE) {
system "stty cbreak </dev/tty >/dev/tty 2>&1";
}
else {
system "stty", '-icanon', 'eol', "\001";
}
$key = getc(STDIN);
if ($BSD_STYLE) {
system "stty -cbreak </dev/tty >/dev/tty 2>&1";
}
else {
system "stty", 'icanon', 'eol', '^@'; # ASCII null
}
print "\n";
Determination of whether $BSD_STYLE should be set
is left as an exercise to the reader.
The C<POSIX::getattr()> function can do this more portably on systems
purporting POSIX compliance.
See also the C<Term::ReadKey> module from your nearest CPAN site;
details on CPAN can be found on L<perlmod/CPAN>.
=item getlogin
Implements the C library function of the same name, which on most
systems returns the current login from F</etc/utmp>, if any. If null,
use C<getpwuid()>.
$login = getlogin || getpwuid($<) || "Kilroy";
Do not consider C<getlogin()> for authentication: it is not as
secure as C<getpwuid()>.
=item getpeername SOCKET
Returns the packed sockaddr address of other end of the SOCKET connection.
use Socket;
$hersockaddr = getpeername(SOCK);
($port, $iaddr) = unpack_sockaddr_in($hersockaddr);
$herhostname = gethostbyaddr($iaddr, AF_INET);
$herstraddr = inet_ntoa($iaddr);
=item getpgrp PID
Returns the current process group for the specified PID. Use
a PID of C<0> to get the current process group for the
current process. Will raise an exception if used on a machine that
doesn't implement getpgrp(2). If PID is omitted, returns process
group of current process. Note that the POSIX version of C<getpgrp()>
does not accept a PID argument, so only C<PID==0> is truly portable.
=item getppid
Returns the process id of the parent process.
=item getpriority WHICH,WHO
Returns the current priority for a process, a process group, or a user.
(See L<getpriority(2)>.) Will raise a fatal exception if used on a
machine that doesn't implement getpriority(2).
=item getpwnam NAME
=item getgrnam NAME
=item gethostbyname NAME
=item getnetbyname NAME
=item getprotobyname NAME
=item getpwuid UID
=item getgrgid GID
=item getservbyname NAME,PROTO
=item gethostbyaddr ADDR,ADDRTYPE
=item getnetbyaddr ADDR,ADDRTYPE
=item getprotobynumber NUMBER
=item getservbyport PORT,PROTO
=item getpwent
=item getgrent
=item gethostent
=item getnetent
=item getprotoent
=item getservent
=item setpwent
=item setgrent
=item sethostent STAYOPEN
=item setnetent STAYOPEN
=item setprotoent STAYOPEN
=item setservent STAYOPEN
=item endpwent
=item endgrent
=item endhostent
=item endnetent
=item endprotoent
=item endservent
These routines perform the same functions as their counterparts in the
system library. In list context, the return values from the
various get routines are as follows:
($name,$passwd,$uid,$gid,
$quota,$comment,$gcos,$dir,$shell,$expire) = getpw*
($name,$passwd,$gid,$members) = getgr*
($name,$aliases,$addrtype,$length,@addrs) = gethost*
($name,$aliases,$addrtype,$net) = getnet*
($name,$aliases,$proto) = getproto*
($name,$aliases,$port,$proto) = getserv*
(If the entry doesn't exist you get a null list.)
In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn't exist you get the undefined value.) For example:
$uid = getpwnam($name);
$name = getpwuid($num);
$name = getpwent();
$gid = getgrnam($name);
$name = getgrgid($num;
$name = getgrent();
#etc.
In I<getpw*()> the fields C<$quota>, C<$comment>, and C<$expire> are special
cases in the sense that in many systems they are unsupported. If the
C<$quota> is unsupported, it is an empty scalar. If it is supported, it
usually encodes the disk quota. If the C<$comment> field is unsupported,
it is an empty scalar. If it is supported it usually encodes some
administrative comment about the user. In some systems the $quota
field may be C<$change> or C<$age>, fields that have to do with password
aging. In some systems the C<$comment> field may be C<$class>. The C<$expire>
field, if present, encodes the expiration period of the account or the
password. For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
F<pwd.h> file. You can also find out from within Perl which meaning
your C<$quota> and C<$comment> fields have and whether you have the C<$expire>
field by using the C<Config> module and the values C<d_pwquota>, C<d_pwage>,
C<d_pwchange>, C<d_pwcomment>, and C<d_pwexpire>.
The C<$members> value returned by I<getgr*()> is a space separated list of
the login names of the members of the group.
For the I<gethost*()> functions, if the C<h_errno> variable is supported in
C, it will be returned to you via C<$?> if the function call fails. The
C<@addrs> value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call. In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:
($a,$b,$c,$d) = unpack('C4',$addr[0]);
If you get tired of remembering which element of the return list contains
which return value, by-name interfaces are also provided in modules:
C<File::stat>, C<Net::hostent>, C<Net::netent>, C<Net::protoent>, C<Net::servent>,
C<Time::gmtime>, C<Time::localtime>, and C<User::grent>. These override the
normal built-in, replacing them with versions that return objects with
the appropriate names for each field. For example:
use File::stat;
use User::pwent;
$is_his = (stat($filename)->uid == pwent($whoever)->uid);
Even though it looks like they're the same method calls (uid),
they aren't, because a C<File::stat> object is different from a C<User::pwent> object.
=item getsockname SOCKET
Returns the packed sockaddr address of this end of the SOCKET connection.
use Socket;
$mysockaddr = getsockname(SOCK);
($port, $myaddr) = unpack_sockaddr_in($mysockaddr);
=item getsockopt SOCKET,LEVEL,OPTNAME
Returns the socket option requested, or undef if there is an error.
=item glob EXPR
=item glob
Returns the value of EXPR with filename expansions such as the standard Unix shell F</bin/sh> would
do. This is the internal function implementing the C<E<lt>*.cE<gt>>
operator, but you can use it directly. If EXPR is omitted, C<$_> is used.
The C<E<lt>*.cE<gt>> operator is discussed in more detail in
L<perlop/"I/O Operators">.
=item gmtime EXPR
Converts a time as returned by the time function to a 9-element array
with the time localized for the standard Greenwich time zone.
Typically used as follows:
# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
gmtime(time);
All array elements are numeric, and come straight out of a struct tm.
In particular this means that C<$mon> has the range C<0..11> and C<$wday> has
the range C<0..6> with sunday as day C<0>. Also, C<$year> is the number of
years since 1900, that is, C<$year> is C<123> in year 2023, I<not> simply the last two digits of the year.
If EXPR is omitted, does C<gmtime(time())>.
In scalar context, returns the ctime(3) value:
$now_string = gmtime; # e.g., "Thu Oct 13 04:54:34 1994"
Also see the C<timegm()> function provided by the C<Time::Local> module,
and the strftime(3) function available via the POSIX module.
This scalar value is B<not> locale dependent, see L<perllocale>, but
instead a Perl builtin. Also see the C<Time::Local> module, and the
strftime(3) and mktime(3) function available via the POSIX module. To
get somewhat similar but locale dependent date strings, set up your
locale environment variables appropriately (please see L<perllocale>)
and try for example:
use POSIX qw(strftime);
$now_
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?