⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch25.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:

print("getnetbyname() Reads /etc/networks [$name]\n");

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getnetent">

getnetent ( )</A></H3>

<P>

<B>Category:</B> Network<BR>

<B>Return Value in Scalar Context:</B> The next network name in

<TT>/etc/networks</TT> or undefined.

<BR>

<B>Return Value in Array Context:</B> (<TT>$name</TT>,

<TT>$aliases</TT>, <TT>$addrtype</TT>,

<TT>$net</TT>) or an empty list.<BR>

<B>Definition:</B> When called repeatedly, it iterates over the

information in the <TT>/etc/networks</TT>

system file.

<BLOCKQUOTE>

<PRE>

($name, $alias, $addrtype, $net) = getnetent();

print(&quot;getnetent() Reads /etc/networks [$name, $addrtype]\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getpeernameSOCKET">

getpeername (SOCKET)</A></H3>

<P>

<B>Category:</B> Sockets<BR>

<B>Return Value:</B> SCALAR, the address of the remote side of

a socket connection represented by <TT>SOCKET</TT>.

<BR>

<B>Definition:</B> Gets the packed address of the remote side

of a socket. The address can then be used with the <TT>unpack()</TT>

fuNCtion to retrieve the protocol family, port and ip address

values.

<BLOCKQUOTE>

<PRE>

$sockaddr = 'S n a4 x8';

$packedRemoteAddr = getpeername(S);

($family, $port, $remoteAddr) = unpack($sockaddr,$packedRemoteAddr);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getpgrpPID">

getpgrp (PID)</A></H3>

<P>

<B>Category:</B> Groups, Process, UNIX<BR>

<B>Return Value:</B> SCALAR, the current process group for <TT>PID</TT>.

If <TT>PID</TT> is not specified or

0 is used, the current group of the current process is returned.

<BR>

<B>Definition:</B> Finds the current process group for a given

<TT>pid</TT>.

<BLOCKQUOTE>

<PRE>

print(&quot;getpgrp() &quot;, getpgrp(0), &quot;\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getppid">

getppid ( )</A></H3>

<P>

<B>Category:</B> Process, UNIX<BR>

<B>Return Value:</B> SCALAR, the pid of the parent process.<BR>

<B>Definition:</B> Finds the pid of the parent process.

<BLOCKQUOTE>

<PRE>

print(&quot;getppid() &quot;, getppid(), &quot;\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getpriorityWHICHWHO">

getpriority (WHICH, WHO)</A></H3>

<P>

<B>Category:</B> Process, UNIX<BR>

<B>Return Value:</B> SCALAR, the current priority associated with

the parameters.<BR>

<B>Definition:</B> Returns the current priority of <TT>WHO</TT>

(the pid, group pid, uid, or 0 for the current process). The <TT>WHICH</TT>

parameter can one of <TT>PRIO_PROCESS</TT>

(0), <TT>PRIO_PGGRP</TT> (1), <TT>PRIO_USER</TT>

(2).

<BLOCKQUOTE>

<PRE>

print(&quot;getpriority() &quot;, getpriority(0, 0), &quot;\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getprotobynameNAME">

getprotobyname (NAME)</A></H3>

<P>

<B>Category:</B> Protocols, UNIX<BR>

<B>Return Value: </B>in Scalar Context: The protocol number assigned

to <TT>NAME</TT>.<BR>

<B>Return Value in Array Context:</B> (<TT>$name</TT>,

<TT>$aliases</TT>, <TT>$proto</TT>)

or an empty list. <TT>$proto</TT>

is the protocol number.<BR>

<B>Definition:</B> Looks in the <TT>/etc/protocols</TT>

system file for the protocol called <TT>NAME</TT>.

<BLOCKQUOTE>

<PRE>

($name, $alias, $proto) = getprotobyname(&quot;IP&quot;);

print(&quot;getprotobyname() /etc/proto [$name, $alias, $proto].\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getprotobynumberNUMBER">

getprotobynumber (NUMBER)</A></H3>

<P>

<B>Category:</B> Protocols, UNIX<BR>

<B>Return Value: </B>in Scalar Context: The protocol name associated

with <TT>NUMBER</TT>.<BR>

<B>Return Value in Array Context:</B> (<TT>$name</TT>,

<TT>$aliases</TT>, <TT>$proto</TT>)

or an empty list.<BR>

<B>Definition:</B> Looks in the <TT>/etc/protocols</TT>

system file for <TT>NUMBER</TT>.

<BLOCKQUOTE>

<PRE>

($name, $alias, $proto) = getprotobynumber(0);

print(&quot;getprotobynumber() /etc/protocols [$name, $alias, $proto].\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getprotoent">

getprotoent ( )</A></H3>

<P>

<B>Category:</B> Protocols, UNIX<BR>

<B>Return Value:</B> ARRAY. (<TT>$name</TT>,

<TT>$aliases</TT>, <TT>$proto</TT>)

or an empty list.<BR>

<B>Definition:</B> When called repeatedly, <TT>getprotoent()</TT>

iterates over the <TT>/etc/protocols</TT>

system file.

<BLOCKQUOTE>

<PRE>

($name, $alias, $proto) = getprotoent();

print(&quot;getprotoent() Closes /etc/protocols [$name, $alias, $proto].\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getpwent">

getpwent ( )</A></H3>

<P>

<B>Category:</B> Password, UNIX<BR>

<B>Return Value: </B>in Scalar Context: The username.<BR>

<B>Return Value in Array Context:</B> ARRAY. (<TT>$name</TT>,

<TT>$passwd</TT>, <TT>$uid</TT>,

<TT>$gid</TT>, <TT>$quota</TT>,

<TT>$comment</TT>, <TT>$gcos</TT>,

<TT>$dir</TT>, <TT>$shell</TT>)

or an empty list.<BR>

<B>Definition:</B> When called repeatedly, <TT>getpwent()</TT>

iterates over the <TT>/etc/passwd</TT>

system file.

<BLOCKQUOTE>

<PRE>

($name, $pass, $uid, $gid, $quota, $name, $gcos, $dir, $shell) = getpwent();

print(&quot;getpwent() /etc/passwd [$dir, $shell].\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getpwnamNAME">

getpwnam (NAME)</A></H3>

<P>

<B>Category:</B> Password, UNIX<BR>

<B>Return Value: </B>in Scalar Context: The userid of <TT>NAME</TT>.

<BR>

<B>Return Value in Array Context:</B> (<TT>$name,

$passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell</TT>)

or an empty list.<BR>

<B>Definition:</B> Looks in the <TT>/etc/passwd</TT>

system file for <TT>NAME</TT>.

<BLOCKQUOTE>

<PRE>

($name, $pass, $uid, $gid, $quota, $name, 

    $gcos, $dir, $shell) = getpwnam(&quot;root&quot;);

print(&quot;getpwnam() /etc/passwd [$dir, $shell].\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getpwuidUID">

getpwuid (UID)</A></H3>

<P>

<B>Category:</B> Password, UNIX<BR>

<B>Return Value: </B>in Scalar Context: The username of <TT>UID</TT>.

<BR>

<B>Return Value in Array Context:</B> (<TT>$name</TT>,

<TT>$passwd</TT>, <TT>$uid</TT>,

<TT>$gid</TT>, <TT>$quota</TT>,

<TT>$comment</TT>, <TT>$gcos</TT>,

<TT>$dir</TT>, <TT>$shell</TT>)

or an empty list.<BR>

<B>Definition:</B> Looks in the <TT>/etc/passwd</TT>

system file for <TT>UID</TT>.

<BLOCKQUOTE>

<PRE>

($name, $pass, $uid, $gid, $quota, $name, 

    $gcos, $dir, $shell) = getpwuid(0);

print(&quot;getpwuid() /etc/passwd [$dir, $shell].\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getservbynameNAMEPROTOCOL">

getservbyname (NAME, PROTOCOL)</A></H3>

<P>

<B>Category:</B> Protocol, Service, Socket, UNIX<BR>

<B>Return Value: </B>in Scalar Context: The port number.<BR>

<B>Return Value in Array Context:</B> (<TT>$name</TT>,

<TT>$aliases</TT>, <TT>$port</TT>,

<TT>$proto</TT>) or an empty list.

<BR>

<B>Definition:</B> Gets services by name. Looks in the <TT>/etc/services</TT>

system file.

<BLOCKQUOTE>

<PRE>

($name, $aliases, $port, $protol) = getservbyname(&quot;tcpmux&quot;, &quot;tcp&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getservbyportPORTNUMBERPROTOCOL">

getservbyport (PORT_NUMBER, PROTOCOL)</A></H3>

<P>

<B>Category:</B> Protocol, Service, Socket, UNIX<BR>

<B>Return Value: </B>in Scalar Context: The service name.<BR>

<B>Return Value in Array Context:</B> (<TT>$name</TT>,

<TT>$aliases</TT>, <TT>$port</TT>,

<TT>$proto</TT>) or an empty list.

<BR>

<B>Definition:</B> Gets services by port. Looks in the <TT>/etc/services</TT>

system file.

<BLOCKQUOTE>

<PRE>

($name, $aliases, $port, $protol) = getservbyport(512, &quot;tcp&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getservent">

getservent ( )</A></H3>

<P>

<B>Category:</B> Protocol, Service, Socket, UNIX<BR>

<B>Return Value: </B>in Scalar Context: The next service name.

<BR>

<B>Return Value in Array Context:</B> (<TT>$name</TT>,

<TT>$aliases</TT>, <TT>$port</TT>,

<TT>$proto</TT>) or an empty list.

<BR>

<B>Definition:</B> When called repeatedly, iterates over the <TT>/etc/services</TT>

system file.

<BLOCKQUOTE>

<PRE>

($name, $aliases, $port, $protol) = getservent();

print(&quot;getservent() /etc/servers [$name].\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getsocknameSOCKET">

getsockname (SOCKET)</A></H3>

<P>

<B>Category:</B> Sockets<BR>

<B>Return Value:</B> SCALAR, the packed address of the local end

of the socket.<BR>

<B>Definition:</B> Finds out the address of your script's socket.

<BLOCKQUOTE>

<PRE>

$packedAddr = getsockname(S);

($family, $port, $localAddr) = unpack('S n a4 x8', $packedAddr);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="getsockoptSOCKETLEVELOPTNAME">

getsockopt (SOCKET, LEVEL, OPTNAME)</A></H3>

<P>

<B>Category:</B> Sockets<BR>

<B>Return Value:</B> SCALAR, the socket option requested or the

undefined value.<BR>

<B>Definition:</B> Gets the value of a specified socket option.

<H3><A NAME="globEXPR">

glob (EXPR)</A></H3>

<P>

<B>Category:</B> File<BR>

<B>Return Value:</B> ARRAY, the list of files represented by <TT>EXPR</TT>.

<BR>

<B>Definition:</B> Looks for file name that match <TT>EXPR</TT>.

You can use wildcards in <TT>EXPR</TT>.

<BLOCKQUOTE>

<PRE>

@files = glob(&quot;*.txt&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="gmtimeEXPR">

gmtime ([EXPR])</A></H3>

<P>

<B>Category:</B> Time<BR>

<B>Return Value: </B>in Scalar Context: A string like 'Sat Jul

13 07:34:46 1986' describing <TT>EXPR</TT>.

<BR>

<B>Return Value in Array Context:</B> (<TT>$sec</TT>,

<TT>$min</TT>, <TT>$hour</TT>,

<TT>$mday</TT>, <TT>$mon</TT>,

<TT>$year</TT>, <TT>$wday</TT>,

<TT>$ydat</TT>, <TT>$isdst</TT>).

<BR>

<B>Definition:</B> Breaks <TT>EXPR</TT>

(a number of seconds siNCe 1st Jan 1970) into a 9-element list.

If no argument is used the current time is used. If your system

supports POSIX time zones, the time returned is localized for

the Greenwich Mean Time time zone. Note that <TT>$mon</TT>

ranges from 0..11, <TT>$wday</TT>

ranges from 0..6, and <TT>$year</TT>

does not handle centuries.

<BLOCKQUOTE>

<PRE>

($sec, $min, $hour, $mday, $mon, $year, $wday, $ydat, $isdst) = gmtime();

print &quot;gmtime() 19$year-$mon-$mday\n&quot;;

</PRE>

</BLOCKQUOTE>

<H3><A NAME="grepBLOCKEXPRLIST">

grep (BLOCK | EXPR, LIST)</A></H3>

<P>

<B>Category:</B> Regular Expressions<BR>

<B>Return Value: </B>in Scalar Context: The number of times that

<TT>BLOCK</TT> or <TT>EXPR</TT>

evaluated to true.<BR>

<B>Return Value in Array Context:</B> A list of the elements of

<TT>LIST</TT> that causes <TT>BLOCK</TT>

or <TT>EXPR</TT> to evaluate as true.

<BR>

<B>Definition:</B> Evaluates the expression or block for each

of the elements in <TT>LIST</TT>.

Think of this fuNCtion as having an internal <TT>foreach</TT>

loop. Each element in <TT>LIST</TT>

is assigned to <TT>$_</TT> and then

the block or expression is evaluated. The most common use for

this is with a pattern match operation as the expression, and

a list of strings to be processed. You may be tempted to use <TT>grep()</TT>

as an easy way to interate over an array as shown in the second

example below, <I>don't do this</I>. Use the <TT>map()</TT>

fuNCtion instead.

<BLOCKQUOTE>

<PRE>

# Look for all elements that begin with the letter T.

@a = ('One', 'Two', 'Three', 'Four', 'Five');

print(&quot;grep(), &quot;, grep(/^T/, @a), &quot;\n&quot;);



# Print all elements in a list.

@a = ('One', 'Two', 'Three', 'Four', 'Five');

grep( print(&quot;$_\n&quot;), @a);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="hexEXPR">

hex (EXPR)</A></H3>

<P>

<B>Category:</B> Math, String<BR>

<B>Return Value:</B> SCALAR, the decimal value of <TT>EXPR</TT>.

<BR>

<B>Definition:</B> Converts <TT>EXPR</TT>

from hexadecimal to decimal. For example, <TT>hex('FF0')</TT>

will return <TT>'4080'</TT>. You can

use the string returned as a number because Perl will automatically

convert strings to numbers in numeric contexts.

<BLOCKQUOTE>

<PRE>

print(&quot;hex() &quot;, hex(&quot;ff&quot;), &quot;\n&quot;);

</PRE>

</BLOCKQUOTE>

<H3><A NAME="import">

import ( )</A></H3>

<P>

<B>Category:</B> Miscellaneous<BR>

<B>Return Value:</B> None. <BR>

<B>Definition:</B> This is the only user-defined fuNCtion in this

list. If a module has an <TT>import()</TT>

fuNCtion then the <TT>use()</TT> fuNCtion

will call it as the module is being loaded. You can use the <TT>import()</TT>

fuNCtion to initialize variables, open files, or do any other

setup work.

<H3><A NAME="indexSTRINGSUBSTRINGPOSITION">

index (STRING, SUBSTRING, [POSITION])</A></H3>

<P>

<B>Category:</B> String<BR>

<B>Return Value:</B> SCALAR, the position of the first occurreNCe

of <TT>SUBSTRING</TT> in <TT>STRING</TT>

at or after <TT>POSITION</TT> or -1

if not found.<BR>

<B>Definition:</B> When called repeatedly, you can iterate over

all the occurreNCes of <TT>SUBSTRING</TT>

in <TT>STRING</TT>. The returned value

is an offset from <TT>$[<

⌨️ 快捷键说明

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