Imported upstream version 1.5
[manpages-zh.git] / src / man1 / perlfunc.1
blobd919b7854928bcaa9f13abec4f016584c0f5d342
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.13
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
131 .IX Title "PERLFUNC 1"
132 .TH PERLFUNC 1 "2003-09-02" "perl v5.8.1" "Perl Programmers Reference Guide"
133 .SH NAME
134 perlfunc \- Perl 内部函数
135 .SH "描述 DESCRIPTION"
136 .IX Header "DESCRIPTION"
137 这一章里的函数可以作为表达式来使用。
138 Perl 中的函数主要分为两类:数组运算符和命名的一元运算符。
139 不同之处在于他们的优先级关系。(参阅 perlop 中的优先级表 ) 
140 数组运算符需要一个以上的参数,而一元运算符不能超过一个参数。
141 因此,一个逗号将结束一个一元运算符,  但对于数组运算符,只是起到分隔的作用。
142 一元运算符一般只提供一个标量作为参数,而数组运算符可能会提供标量或者数组作为参数。
143 如果二者都有,标量参数一般在前面,数组参数跟在后面。 (注意,可以只有一个数组变量) 
144 例如, splice() 有三个标量变量,后面加上一个数组, 相反 gethostbyname() 有四个标量变量。
146 在语法描述中,数组运算符需要一个用LIST标识的数组作为参数。
147 这些 数组可能由标量参数和数组值混合组成;
148 数组值将包含在数组中,每个元素被插入数组中,
149 形成一个更长一维的数组值。 数组的元素应该用逗号分开。
151 下面列出的任何函数可以在参数两边有括号,也可以没有。(语法描述中省略括号)
152 如果你使用括号,一个简单的规则是 (偶尔会令人吃惊): 
153 如果是函数,没有优先级的问题;如果它是一个数组运算符或者一元运算符
154 那么就要考虑优先级。并且,函数两边的空白和 "(" 是不计算的--因此,
155 有时候需要小心行事。看看下面的几个例子:
157 .Vb 5
158 \&    print 1+2+4;        # Prints 7.
159 \&    print(1+2) + 4;     # Prints 3.
160 \&    print (1+2)+4;      # Also prints 3!
161 \&    print +(1+2)+4;     # Prints 7.
162 \&    print ((1+2)+4);    # Prints 7.
165 前面说得似乎有点抽象,那么你在运行PERL时带上-w开关你将得到一些
166 警告信息,您可以根据这些信息再体会一下。例如,上面的例子会产生如下信息:
168 .Vb 2
169 \&    print (...) interpreted as function at - line 1.
170 \&    Useless use of integer addition in void context at - line 1.
173 有些函数根本不需要参数,因此工作方式和一元运算符和数组运算符都不同, 
174 \f(CW\*(C`time\*(C'\fR 和 \f(CW\*(C`endpwent\*(C'\fR 算是两个典型吧.  
175 如, \f(CW\*(C`time+86_400\*(C'\fR 实际上是 \&\f(CW\*(C`time() + 86_400\*(C'\fR.
177 对于可以用在标量或者数组的上下文中的函数,非失败性的错误在标量环境下
178 通常暗示返回了未定义的值或在数组环境下返回一个空的数组。
180 记住下面的重要原则:
181 没有规则和数组环境下的表达式的行为和他的标量环境的行为有关系,反之亦然。 这可能产生两种完全不同的情况。在标量环境下,每个运算符和函数决定怎样以最恰当的次序返回值。 有些运算符在数组环境下返回数组的长度.,有些运算符返回的一个元素,有些返回数组中的最后一个元素,有些返回成功执行的操作的语句数。通常,他们返回一些你需要的值,除非你需要连续性。
183 在标量环境下的命名数组在第一眼看上去时和在标量环境下的列表有很大的不同。 在标量环境下,你不能得到一个像 \f(CW\*(C`(1,2,3)\*(C'\fR 的列表,因为在编译时,编译器是知道当前环境的,它将在那里产生标量的逗号运算符, 而不是用于分隔数组元素的逗号. 也就是说,它永远不会以一个数组开始。
185 一般说来, PERL中的函数对应相应的系统调用 (如\fIchown\fR\|(2), \fIfork\fR\|(2), \fIclosedir\fR\|(2), 等等.) 成功调用后返回真值,否则返回 \f(CW\*(C`undef\*(C'\fR , 下面将会提到。这一点和C的接口不一样,C中出错时将返回\f(CW\*(C`\-1\*(C'\fR .但是也有几个例外,他们是  \f(CW\*(C`wait\*(C'\fR,
186 \&\f(CW\*(C`waitpid\*(C'\fR, 和 \f(CW\*(C`syscall\*(C'\fR 。
187 系统调用出错时出错信息将通过特殊变量$!返回。其他的函数则不会,除非发生意外。
188 .Sh "函数分类 Perl Functions by Category"
189 .IX Subsection "Perl Functions by Category"
190 下面是Perl中的函数(包括看起来像函数的,如某些关键词,命名运算符)的分类. 有些函数在多处出现了。
191 .IP "标量和字符串函数 Functions for SCALARs or strings" 4
192 .IX Item "Functions for SCALARs or strings"
193 \&\f(CW\*(C`chomp\*(C'\fR, \f(CW\*(C`chop\*(C'\fR, \f(CW\*(C`chr\*(C'\fR, \f(CW\*(C`crypt\*(C'\fR, \f(CW\*(C`hex\*(C'\fR, \f(CW\*(C`index\*(C'\fR, \f(CW\*(C`lc\*(C'\fR, \f(CW\*(C`lcfirst\*(C'\fR,
194 \&\f(CW\*(C`length\*(C'\fR, \f(CW\*(C`oct\*(C'\fR, \f(CW\*(C`ord\*(C'\fR, \f(CW\*(C`pack\*(C'\fR, \f(CW\*(C`q/STRING/\*(C'\fR, \f(CW\*(C`qq/STRING/\*(C'\fR, \f(CW\*(C`reverse\*(C'\fR,
195 \&\f(CW\*(C`rindex\*(C'\fR, \f(CW\*(C`sprintf\*(C'\fR, \f(CW\*(C`substr\*(C'\fR, \f(CW\*(C`tr///\*(C'\fR, \f(CW\*(C`uc\*(C'\fR, \f(CW\*(C`ucfirst\*(C'\fR, \f(CW\*(C`y///\*(C'\fR
196 .IP "正则表达式和模式匹配 Regular expressions and pattern matching" 4
197 .IX Item "Regular expressions and pattern matching"
198 \&\f(CW\*(C`m//\*(C'\fR, \f(CW\*(C`pos\*(C'\fR, \f(CW\*(C`quotemeta\*(C'\fR, \f(CW\*(C`s///\*(C'\fR, \f(CW\*(C`split\*(C'\fR, \f(CW\*(C`study\*(C'\fR, \f(CW\*(C`qr//\*(C'\fR
199 .IP "数字运算 Numeric functions" 4
200 .IX Item "Numeric functions"
201 \&\f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`atan2\*(C'\fR, \f(CW\*(C`cos\*(C'\fR, \f(CW\*(C`exp\*(C'\fR, \f(CW\*(C`hex\*(C'\fR, \f(CW\*(C`int\*(C'\fR, \f(CW\*(C`log\*(C'\fR, \f(CW\*(C`oct\*(C'\fR, \f(CW\*(C`rand\*(C'\fR,
202 \&\f(CW\*(C`sin\*(C'\fR, \f(CW\*(C`sqrt\*(C'\fR, \f(CW\*(C`srand\*(C'\fR
203 .ie n .IP "真实数组函数 Functions for real @ARRAYs" 4
204 .el .IP "Functions for real \f(CW@ARRAYs\fR" 4
205 .IX Item "Functions for real @ARRAYs"
206 \&\f(CW\*(C`pop\*(C'\fR, \f(CW\*(C`push\*(C'\fR, \f(CW\*(C`shift\*(C'\fR, \f(CW\*(C`splice\*(C'\fR, \f(CW\*(C`unshift\*(C'\fR
207 .IP "列表数据函数 Functions for list data" 4
208 .IX Item "Functions for list data"
209 \&\f(CW\*(C`grep\*(C'\fR, \f(CW\*(C`join\*(C'\fR, \f(CW\*(C`map\*(C'\fR, \f(CW\*(C`qw/STRING/\*(C'\fR, \f(CW\*(C`reverse\*(C'\fR, \f(CW\*(C`sort\*(C'\fR, \f(CW\*(C`unpack\*(C'\fR
210 .ie n .IP "真实哈希函数 Functions for real %HASHes" 4
211 .el .IP "Functions for real \f(CW%HASHes\fR" 4
212 .IX Item "Functions for real %HASHes"
213 \&\f(CW\*(C`delete\*(C'\fR, \f(CW\*(C`each\*(C'\fR, \f(CW\*(C`exists\*(C'\fR, \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR
214 .IP "输入输出 Input and output functions" 4
215 .IX Item "Input and output functions"
216 \&\f(CW\*(C`binmode\*(C'\fR, \f(CW\*(C`close\*(C'\fR, \f(CW\*(C`closedir\*(C'\fR, \f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR, \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`eof\*(C'\fR,
217 \&\f(CW\*(C`fileno\*(C'\fR, \f(CW\*(C`flock\*(C'\fR, \f(CW\*(C`format\*(C'\fR, \f(CW\*(C`getc\*(C'\fR, \f(CW\*(C`print\*(C'\fR, \f(CW\*(C`printf\*(C'\fR, \f(CW\*(C`read\*(C'\fR,
218 \&\f(CW\*(C`readdir\*(C'\fR, \f(CW\*(C`rewinddir\*(C'\fR, \f(CW\*(C`seek\*(C'\fR, \f(CW\*(C`seekdir\*(C'\fR, \f(CW\*(C`select\*(C'\fR, \f(CW\*(C`syscall\*(C'\fR,
219 \&\f(CW\*(C`sysread\*(C'\fR, \f(CW\*(C`sysseek\*(C'\fR, \f(CW\*(C`syswrite\*(C'\fR, \f(CW\*(C`tell\*(C'\fR, \f(CW\*(C`telldir\*(C'\fR, \f(CW\*(C`truncate\*(C'\fR,
220 \&\f(CW\*(C`warn\*(C'\fR, \f(CW\*(C`write\*(C'\fR
221 .IP "定长的数据或记录 Functions for fixed length data or records" 4
222 .IX Item "Functions for fixed length data or records"
223 \&\f(CW\*(C`pack\*(C'\fR, \f(CW\*(C`read\*(C'\fR, \f(CW\*(C`syscall\*(C'\fR, \f(CW\*(C`sysread\*(C'\fR, \f(CW\*(C`syswrite\*(C'\fR, \f(CW\*(C`unpack\*(C'\fR, \f(CW\*(C`vec\*(C'\fR
224 .IP "文件目录控制 Functions for filehandles, files, or directories" 4
225 .IX Item "Functions for filehandles, files, or directories"
226 \&\f(CW\*(C`\-\f(CIX\f(CW\*(C'\fR, \f(CW\*(C`chdir\*(C'\fR, \f(CW\*(C`chmod\*(C'\fR, \f(CW\*(C`chown\*(C'\fR, \f(CW\*(C`chroot\*(C'\fR, \f(CW\*(C`fcntl\*(C'\fR, \f(CW\*(C`glob\*(C'\fR,
227 \&\f(CW\*(C`ioctl\*(C'\fR, \f(CW\*(C`link\*(C'\fR, \f(CW\*(C`lstat\*(C'\fR, \f(CW\*(C`mkdir\*(C'\fR, \f(CW\*(C`open\*(C'\fR, \f(CW\*(C`opendir\*(C'\fR,
228 \&\f(CW\*(C`readlink\*(C'\fR, \f(CW\*(C`rename\*(C'\fR, \f(CW\*(C`rmdir\*(C'\fR, \f(CW\*(C`stat\*(C'\fR, \f(CW\*(C`symlink\*(C'\fR, \f(CW\*(C`sysopen\*(C'\fR,
229 \&\f(CW\*(C`umask\*(C'\fR, \f(CW\*(C`unlink\*(C'\fR, \f(CW\*(C`utime\*(C'\fR
230 .IP "流控制关键词 Keywords related to the control flow of your perl program" 4
231 .IX Item "Keywords related to the control flow of your perl program"
232 \&\f(CW\*(C`caller\*(C'\fR, \f(CW\*(C`continue\*(C'\fR, \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`do\*(C'\fR, \f(CW\*(C`dump\*(C'\fR, \f(CW\*(C`eval\*(C'\fR, \f(CW\*(C`exit\*(C'\fR,
233 \&\f(CW\*(C`goto\*(C'\fR, \f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, \f(CW\*(C`redo\*(C'\fR, \f(CW\*(C`return\*(C'\fR, \f(CW\*(C`sub\*(C'\fR, \f(CW\*(C`wantarray\*(C'\fR
234 .IP "作用域关键词 Keywords related to scoping" 4
235 .IX Item "Keywords related to scoping"
236 \&\f(CW\*(C`caller\*(C'\fR, \f(CW\*(C`import\*(C'\fR, \f(CW\*(C`local\*(C'\fR, \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`our\*(C'\fR, \f(CW\*(C`package\*(C'\fR, \f(CW\*(C`use\*(C'\fR
237 .IP "杂项 Miscellaneous functions" 4
238 .IX Item "Miscellaneous functions"
239 \&\f(CW\*(C`defined\*(C'\fR, \f(CW\*(C`dump\*(C'\fR, \f(CW\*(C`eval\*(C'\fR, \f(CW\*(C`formline\*(C'\fR, \f(CW\*(C`local\*(C'\fR, \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`our\*(C'\fR, \f(CW\*(C`reset\*(C'\fR,
240 \&\f(CW\*(C`scalar\*(C'\fR, \f(CW\*(C`undef\*(C'\fR, \f(CW\*(C`wantarray\*(C'\fR
241 .IP "进程和进程组 Functions for processes and process groups" 4
242 .IX Item "Functions for processes and process groups"
243 \&\f(CW\*(C`alarm\*(C'\fR, \f(CW\*(C`exec\*(C'\fR, \f(CW\*(C`fork\*(C'\fR, \f(CW\*(C`getpgrp\*(C'\fR, \f(CW\*(C`getppid\*(C'\fR, \f(CW\*(C`getpriority\*(C'\fR, \f(CW\*(C`kill\*(C'\fR,
244 \&\f(CW\*(C`pipe\*(C'\fR, \f(CW\*(C`qx/STRING/\*(C'\fR, \f(CW\*(C`setpgrp\*(C'\fR, \f(CW\*(C`setpriority\*(C'\fR, \f(CW\*(C`sleep\*(C'\fR, \f(CW\*(C`system\*(C'\fR,
245 \&\f(CW\*(C`times\*(C'\fR, \f(CW\*(C`wait\*(C'\fR, \f(CW\*(C`waitpid\*(C'\fR
246 .IP "模块关键词 Keywords related to perl modules" 4
247 .IX Item "Keywords related to perl modules"
248 \&\f(CW\*(C`do\*(C'\fR, \f(CW\*(C`import\*(C'\fR, \f(CW\*(C`no\*(C'\fR, \f(CW\*(C`package\*(C'\fR, \f(CW\*(C`require\*(C'\fR, \f(CW\*(C`use\*(C'\fR
249 .IP "类和面向对象关键词 Keywords related to classes and object-orientedness" 4
250 .IX Item "Keywords related to classes and object-orientedness"
251 \&\f(CW\*(C`bless\*(C'\fR, \f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR, \f(CW\*(C`package\*(C'\fR, \f(CW\*(C`ref\*(C'\fR, \f(CW\*(C`tie\*(C'\fR, \f(CW\*(C`tied\*(C'\fR,
252 \&\f(CW\*(C`untie\*(C'\fR, \f(CW\*(C`use\*(C'\fR
253 .IP "底层 socket 函数 Low-level socket functions" 4
254 .IX Item "Low-level socket functions"
255 \&\f(CW\*(C`accept\*(C'\fR, \f(CW\*(C`bind\*(C'\fR, \f(CW\*(C`connect\*(C'\fR, \f(CW\*(C`getpeername\*(C'\fR, \f(CW\*(C`getsockname\*(C'\fR,
256 \&\f(CW\*(C`getsockopt\*(C'\fR, \f(CW\*(C`listen\*(C'\fR, \f(CW\*(C`recv\*(C'\fR, \f(CW\*(C`send\*(C'\fR, \f(CW\*(C`setsockopt\*(C'\fR, \f(CW\*(C`shutdown\*(C'\fR,
257 \&\f(CW\*(C`socket\*(C'\fR, \f(CW\*(C`socketpair\*(C'\fR
258 .IP "SysV 进程间通讯 System V interprocess communication functions" 4
259 .IX Item "System V interprocess communication functions"
260 \&\f(CW\*(C`msgctl\*(C'\fR, \f(CW\*(C`msgget\*(C'\fR, \f(CW\*(C`msgrcv\*(C'\fR, \f(CW\*(C`msgsnd\*(C'\fR, \f(CW\*(C`semctl\*(C'\fR, \f(CW\*(C`semget\*(C'\fR, \f(CW\*(C`semop\*(C'\fR,
261 \&\f(CW\*(C`shmctl\*(C'\fR, \f(CW\*(C`shmget\*(C'\fR, \f(CW\*(C`shmread\*(C'\fR, \f(CW\*(C`shmwrite\*(C'\fR
262 .IP "获取用户信息 Fetching user and group info" 4
263 .IX Item "Fetching user and group info"
264 \&\f(CW\*(C`endgrent\*(C'\fR, \f(CW\*(C`endhostent\*(C'\fR, \f(CW\*(C`endnetent\*(C'\fR, \f(CW\*(C`endpwent\*(C'\fR, \f(CW\*(C`getgrent\*(C'\fR,
265 \&\f(CW\*(C`getgrgid\*(C'\fR, \f(CW\*(C`getgrnam\*(C'\fR, \f(CW\*(C`getlogin\*(C'\fR, \f(CW\*(C`getpwent\*(C'\fR, \f(CW\*(C`getpwnam\*(C'\fR,
266 \&\f(CW\*(C`getpwuid\*(C'\fR, \f(CW\*(C`setgrent\*(C'\fR, \f(CW\*(C`setpwent\*(C'\fR
267 .IP "获取网络信息 Fetching network info" 4
268 .IX Item "Fetching network info"
269 \&\f(CW\*(C`endprotoent\*(C'\fR, \f(CW\*(C`endservent\*(C'\fR, \f(CW\*(C`gethostbyaddr\*(C'\fR, \f(CW\*(C`gethostbyname\*(C'\fR,
270 \&\f(CW\*(C`gethostent\*(C'\fR, \f(CW\*(C`getnetbyaddr\*(C'\fR, \f(CW\*(C`getnetbyname\*(C'\fR, \f(CW\*(C`getnetent\*(C'\fR,
271 \&\f(CW\*(C`getprotobyname\*(C'\fR, \f(CW\*(C`getprotobynumber\*(C'\fR, \f(CW\*(C`getprotoent\*(C'\fR,
272 \&\f(CW\*(C`getservbyname\*(C'\fR, \f(CW\*(C`getservbyport\*(C'\fR, \f(CW\*(C`getservent\*(C'\fR, \f(CW\*(C`sethostent\*(C'\fR,
273 \&\f(CW\*(C`setnetent\*(C'\fR, \f(CW\*(C`setprotoent\*(C'\fR, \f(CW\*(C`setservent\*(C'\fR
274 .IP "时间函数 Time-related functions" 4
275 .IX Item "Time-related functions"
276 \&\f(CW\*(C`gmtime\*(C'\fR, \f(CW\*(C`localtime\*(C'\fR, \f(CW\*(C`time\*(C'\fR, \f(CW\*(C`times\*(C'\fR
277 .IP "PERL5中的新函数 Functions new in perl5" 4
278 .IX Item "Functions new in perl5"
279 \&\f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`bless\*(C'\fR, \f(CW\*(C`chomp\*(C'\fR, \f(CW\*(C`chr\*(C'\fR, \f(CW\*(C`exists\*(C'\fR, \f(CW\*(C`formline\*(C'\fR, \f(CW\*(C`glob\*(C'\fR,
280 \&\f(CW\*(C`import\*(C'\fR, \f(CW\*(C`lc\*(C'\fR, \f(CW\*(C`lcfirst\*(C'\fR, \f(CW\*(C`map\*(C'\fR, \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`no\*(C'\fR, \f(CW\*(C`our\*(C'\fR, \f(CW\*(C`prototype\*(C'\fR,
281 \&\f(CW\*(C`qx\*(C'\fR, \f(CW\*(C`qw\*(C'\fR, \f(CW\*(C`readline\*(C'\fR, \f(CW\*(C`readpipe\*(C'\fR, \f(CW\*(C`ref\*(C'\fR, \f(CW\*(C`sub*\*(C'\fR, \f(CW\*(C`sysopen\*(C'\fR, \f(CW\*(C`tie\*(C'\fR,
282 \&\f(CW\*(C`tied\*(C'\fR, \f(CW\*(C`uc\*(C'\fR, \f(CW\*(C`ucfirst\*(C'\fR, \f(CW\*(C`untie\*(C'\fR, \f(CW\*(C`use\*(C'\fR
284 * \- \f(CW\*(C`sub\*(C'\fR was a keyword in perl4, but in perl5 it is an
285 operator, which can be used in expressions.
286 .IP "过时的函数 Functions obsoleted in perl5" 4
287 .IX Item "Functions obsoleted in perl5"
288 \&\f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR
289 .Sh "可移植性 Portability"
290 .IX Subsection "Portability"
291 Perl 诞生于UNIX,因此可以访问所有的一般系统调用。
292 在非UNIX环境中,某些UNIX下有的调用是没有实现的,或者有轻微的区别。受到影响的有:
294 \&\f(CW\*(C`\-X\*(C'\fR, \f(CW\*(C`binmode\*(C'\fR, \f(CW\*(C`chmod\*(C'\fR, \f(CW\*(C`chown\*(C'\fR, \f(CW\*(C`chroot\*(C'\fR, \f(CW\*(C`crypt\*(C'\fR,
295 \&\f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR, \f(CW\*(C`dump\*(C'\fR, \f(CW\*(C`endgrent\*(C'\fR, \f(CW\*(C`endhostent\*(C'\fR,
296 \&\f(CW\*(C`endnetent\*(C'\fR, \f(CW\*(C`endprotoent\*(C'\fR, \f(CW\*(C`endpwent\*(C'\fR, \f(CW\*(C`endservent\*(C'\fR, \f(CW\*(C`exec\*(C'\fR,
297 \&\f(CW\*(C`fcntl\*(C'\fR, \f(CW\*(C`flock\*(C'\fR, \f(CW\*(C`fork\*(C'\fR, \f(CW\*(C`getgrent\*(C'\fR, \f(CW\*(C`getgrgid\*(C'\fR, \f(CW\*(C`gethostbyname\*(C'\fR,
298 \&\f(CW\*(C`gethostent\*(C'\fR, \f(CW\*(C`getlogin\*(C'\fR, \f(CW\*(C`getnetbyaddr\*(C'\fR, \f(CW\*(C`getnetbyname\*(C'\fR, \f(CW\*(C`getnetent\*(C'\fR,
299 \&\f(CW\*(C`getppid\*(C'\fR, \f(CW\*(C`getprgp\*(C'\fR, \f(CW\*(C`getpriority\*(C'\fR, \f(CW\*(C`getprotobynumber\*(C'\fR,
300 \&\f(CW\*(C`getprotoent\*(C'\fR, \f(CW\*(C`getpwent\*(C'\fR, \f(CW\*(C`getpwnam\*(C'\fR, \f(CW\*(C`getpwuid\*(C'\fR,
301 \&\f(CW\*(C`getservbyport\*(C'\fR, \f(CW\*(C`getservent\*(C'\fR, \f(CW\*(C`getsockopt\*(C'\fR, \f(CW\*(C`glob\*(C'\fR, \f(CW\*(C`ioctl\*(C'\fR,
302 \&\f(CW\*(C`kill\*(C'\fR, \f(CW\*(C`link\*(C'\fR, \f(CW\*(C`lstat\*(C'\fR, \f(CW\*(C`msgctl\*(C'\fR, \f(CW\*(C`msgget\*(C'\fR, \f(CW\*(C`msgrcv\*(C'\fR,
303 \&\f(CW\*(C`msgsnd\*(C'\fR, \f(CW\*(C`open\*(C'\fR, \f(CW\*(C`pipe\*(C'\fR, \f(CW\*(C`readlink\*(C'\fR, \f(CW\*(C`rename\*(C'\fR, \f(CW\*(C`select\*(C'\fR, \f(CW\*(C`semctl\*(C'\fR,
304 \&\f(CW\*(C`semget\*(C'\fR, \f(CW\*(C`semop\*(C'\fR, \f(CW\*(C`setgrent\*(C'\fR, \f(CW\*(C`sethostent\*(C'\fR, \f(CW\*(C`setnetent\*(C'\fR,
305 \&\f(CW\*(C`setpgrp\*(C'\fR, \f(CW\*(C`setpriority\*(C'\fR, \f(CW\*(C`setprotoent\*(C'\fR, \f(CW\*(C`setpwent\*(C'\fR,
306 \&\f(CW\*(C`setservent\*(C'\fR, \f(CW\*(C`setsockopt\*(C'\fR, \f(CW\*(C`shmctl\*(C'\fR, \f(CW\*(C`shmget\*(C'\fR, \f(CW\*(C`shmread\*(C'\fR,
307 \&\f(CW\*(C`shmwrite\*(C'\fR, \f(CW\*(C`socket\*(C'\fR, \f(CW\*(C`socketpair\*(C'\fR,
308 \&\f(CW\*(C`stat\*(C'\fR, \f(CW\*(C`symlink\*(C'\fR, \f(CW\*(C`syscall\*(C'\fR, \f(CW\*(C`sysopen\*(C'\fR, \f(CW\*(C`system\*(C'\fR,
309 \&\f(CW\*(C`times\*(C'\fR, \f(CW\*(C`truncate\*(C'\fR, \f(CW\*(C`umask\*(C'\fR, \f(CW\*(C`unlink\*(C'\fR,
310 \&\f(CW\*(C`utime\*(C'\fR, \f(CW\*(C`wait\*(C'\fR, \f(CW\*(C`waitpid\*(C'\fR
312 参见 perlport 和其他平台的说明文档以获得更多关于移植性的资料
313 .Sh "按字母顺序排列的PERL函数 Alphabetical Listing of Perl Functions"
314 .IX Subsection "Alphabetical Listing of Perl Functions"
315 .IP "\-X \s-1FILEHANDLE\s0" 8
316 .IX Item "-X FILEHANDLE"
317 .PD 0
318 .IP "\-X \s-1EXPR\s0" 8
319 .IX Item "-X EXPR"
320 .IP "\-X" 8
321 .IX Item "-X"
323 A file test, where X is one of the letters listed below.  This unary
324 operator takes one argument, either a filename or a filehandle, and
325 tests the associated file to see if something is true about it.  If the
326 argument is omitted, tests \f(CW$_\fR, except for \f(CW\*(C`\-t\*(C'\fR, which tests \s-1STDIN\s0.
327 Unless otherwise documented, it returns \f(CW1\fR for true and \f(CW''\fR for false, or
328 the undefined value if the file doesn't exist.  Despite the funny
329 names, precedence is the same as any other named unary operator, and
330 the argument may be parenthesized like any other unary operator.  The
331 operator may be any of:
332 .IX Xref "-r -w -x -o -R -W -X -O -e -z -s -f -d -l -p -S -b -c -t -u -g -k -T -B -M -A -C"
334 .Vb 4
335 \&    -r  File is readable by effective uid/gid.
336 \&    -w  File is writable by effective uid/gid.
337 \&    -x  File is executable by effective uid/gid.
338 \&    -o  File is owned by effective uid.
341 .Vb 4
342 \&    -R  File is readable by real uid/gid.
343 \&    -W  File is writable by real uid/gid.
344 \&    -X  File is executable by real uid/gid.
345 \&    -O  File is owned by real uid.
348 .Vb 3
349 \&    -e  File exists.
350 \&    -z  File has zero size (is empty).
351 \&    -s  File has nonzero size (returns size in bytes).
354 .Vb 8
355 \&    -f  File is a plain file.
356 \&    -d  File is a directory.
357 \&    -l  File is a symbolic link.
358 \&    -p  File is a named pipe (FIFO), or Filehandle is a pipe.
359 \&    -S  File is a socket.
360 \&    -b  File is a block special file.
361 \&    -c  File is a character special file.
362 \&    -t  Filehandle is opened to a tty.
365 .Vb 3
366 \&    -u  File has setuid bit set.
367 \&    -g  File has setgid bit set.
368 \&    -k  File has sticky bit set.
371 .Vb 2
372 \&    -T  File is an ASCII text file (heuristic guess).
373 \&    -B  File is a "binary" file (opposite of -T).
376 .Vb 3
377 \&    -M  Script start time minus file modification time, in days.
378 \&    -A  Same for access time.
379 \&    -C  Same for inode change time (Unix, may differ for other platforms)
382 Example:
384 .Vb 5
385 \&    while (<>) {
386 \&        chomp;
387 \&        next unless -f $_;      # ignore specials
388 \&        #...
389 \&    }
392 The interpretation of the file permission operators \f(CW\*(C`\-r\*(C'\fR, \f(CW\*(C`\-R\*(C'\fR,
393 \&\f(CW\*(C`\-w\*(C'\fR, \f(CW\*(C`\-W\*(C'\fR, \f(CW\*(C`\-x\*(C'\fR, and \f(CW\*(C`\-X\*(C'\fR is by default based solely on the mode
394 of the file and the uids and gids of the user.  There may be other
395 reasons you can't actually read, write, or execute the file.  Such
396 reasons may be for example network filesystem access controls, ACLs
397 (access control lists), read-only filesystems, and unrecognized
398 executable formats.
400 Also note that, for the superuser on the local filesystems, the \f(CW\*(C`\-r\*(C'\fR,
401 \&\f(CW\*(C`\-R\*(C'\fR, \f(CW\*(C`\-w\*(C'\fR, and \f(CW\*(C`\-W\*(C'\fR tests always return 1, and \f(CW\*(C`\-x\*(C'\fR and \f(CW\*(C`\-X\*(C'\fR return 1
402 if any execute bit is set in the mode.  Scripts run by the superuser
403 may thus need to do a \fIstat()\fR to determine the actual mode of the file,
404 or temporarily set their effective uid to something else.
406 If you are using ACLs, there is a pragma called \f(CW\*(C`filetest\*(C'\fR that may
407 produce more accurate results than the bare \fIstat()\fR mode bits.
408 When under the \f(CW\*(C`use filetest 'access'\*(C'\fR the above-mentioned filetests
409 will test whether the permission can (not) be granted using the
410 \&\fIaccess()\fR family of system calls.  Also note that the \f(CW\*(C`\-x\*(C'\fR and \f(CW\*(C`\-X\*(C'\fR may
411 under this pragma return true even if there are no execute permission
412 bits set (nor any extra execute permission ACLs).  This strangeness is
413 due to the underlying system calls' definitions.  Read the
414 documentation for the \f(CW\*(C`filetest\*(C'\fR pragma for more information.
416 Note that \f(CW\*(C`\-s/a/b/\*(C'\fR does not do a negated substitution.  Saying
417 \&\f(CW\*(C`\-exp($foo)\*(C'\fR still works as expected, however\*(--only single letters
418 following a minus are interpreted as file tests.
420 The \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR switches work as follows.  The first block or so of the
421 file is examined for odd characters such as strange control codes or
422 characters with the high bit set.  If too many strange characters (>30%)
423 are found, it's a \f(CW\*(C`\-B\*(C'\fR file, otherwise it's a \f(CW\*(C`\-T\*(C'\fR file.  Also, any file
424 containing null in the first block is considered a binary file.  If \f(CW\*(C`\-T\*(C'\fR
425 or \f(CW\*(C`\-B\*(C'\fR is used on a filehandle, the current \s-1IO\s0 buffer is examined
426 rather than the first block.  Both \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR return true on a null
427 file, or a file at \s-1EOF\s0 when testing a filehandle.  Because you have to
428 read a file to do the \f(CW\*(C`\-T\*(C'\fR test, on most occasions you want to use a \f(CW\*(C`\-f\*(C'\fR
429 against the file first, as in \f(CW\*(C`next unless \-f $file && \-T $file\*(C'\fR.
431 If any of the file tests (or either the \f(CW\*(C`stat\*(C'\fR or \f(CW\*(C`lstat\*(C'\fR operators) are given
432 the special filehandle consisting of a solitary underline, then the stat
433 structure of the previous file test (or stat operator) is used, saving
434 a system call.  (This doesn't work with \f(CW\*(C`\-t\*(C'\fR, and you need to remember
435 that \fIlstat()\fR and \f(CW\*(C`\-l\*(C'\fR will leave values in the stat structure for the
436 symbolic link, not the real file.)  (Also, if the stat buffer was filled by
437 a \f(CW\*(C`lstat\*(C'\fR call, \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR will reset it with the results of \f(CW\*(C`stat _\*(C'\fR).
438 Example:
440 .Vb 1
441 \&    print "Can do.\en" if -r $a || -w _ || -x _;
444 .Vb 9
445 \&    stat($filename);
446 \&    print "Readable\en" if -r _;
447 \&    print "Writable\en" if -w _;
448 \&    print "Executable\en" if -x _;
449 \&    print "Setuid\en" if -u _;
450 \&    print "Setgid\en" if -g _;
451 \&    print "Sticky\en" if -k _;
452 \&    print "Text\en" if -T _;
453 \&    print "Binary\en" if -B _;
455 .IP "abs \s-1VALUE\s0" 8
456 .IX Item "abs VALUE"
457 .PD 0
458 .IP "abs" 8
459 .IX Item "abs"
461 Returns the absolute value of its argument.
462 If \s-1VALUE\s0 is omitted, uses \f(CW$_\fR.
463 .IP "accept \s-1NEWSOCKET\s0,GENERICSOCKET" 8
464 .IX Item "accept NEWSOCKET,GENERICSOCKET"
465 Accepts an incoming socket connect, just as the \fIaccept\fR\|(2) system call
466 does.  Returns the packed address if it succeeded, false otherwise.
467 See the example in \*(L"Sockets: Client/Server Communication\*(R" in perlipc.
469 On systems that support a close-on-exec flag on files, the flag will
470 be set for the newly opened file descriptor, as determined by the
471 value of $^F.  See \*(L"$^F\*(R" in perlvar.
472 .IP "alarm \s-1SECONDS\s0" 8
473 .IX Item "alarm SECONDS"
474 .PD 0
475 .IP "alarm" 8
476 .IX Item "alarm"
478 Arranges to have a \s-1SIGALRM\s0 delivered to this process after the
479 specified number of wallclock seconds have elapsed.  If \s-1SECONDS\s0 is not
480 specified, the value stored in \f(CW$_\fR is used. (On some machines,
481 unfortunately, the elapsed time may be up to one second less or more
482 than you specified because of how seconds are counted, and process
483 scheduling may delay the delivery of the signal even further.)
485 Only one timer may be counting at once.  Each call disables the
486 previous timer, and an argument of \f(CW0\fR may be supplied to cancel the
487 previous timer without starting a new one.  The returned value is the
488 amount of time remaining on the previous timer.
490 For delays of finer granularity than one second, you may use Perl's
491 four-argument version of \fIselect()\fR leaving the first three arguments
492 undefined, or you might be able to use the \f(CW\*(C`syscall\*(C'\fR interface to
493 access \fIsetitimer\fR\|(2) if your system supports it.  The Time::HiRes
494 module (from \s-1CPAN\s0, and starting from Perl 5.8 part of the standard
495 distribution) may also prove useful.
497 It is usually a mistake to intermix \f(CW\*(C`alarm\*(C'\fR and \f(CW\*(C`sleep\*(C'\fR calls.
498 (\f(CW\*(C`sleep\*(C'\fR may be internally implemented in your system with \f(CW\*(C`alarm\*(C'\fR)
500 If you want to use \f(CW\*(C`alarm\*(C'\fR to time out a system call you need to use an
501 \&\f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR pair.  You can't rely on the alarm causing the system call to
502 fail with \f(CW$!\fR set to \f(CW\*(C`EINTR\*(C'\fR because Perl sets up signal handlers to
503 restart system calls on some systems.  Using \f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR always works,
504 modulo the caveats given in \*(L"Signals\*(R" in perlipc.
506 .Vb 13
507 \&    eval {
508 \&        local $SIG{ALRM} = sub { die "alarm\en" }; # NB: \en required
509 \&        alarm $timeout;
510 \&        $nread = sysread SOCKET, $buffer, $size;
511 \&        alarm 0;
512 \&    };
513 \&    if ($@) {
514 \&        die unless $@ eq "alarm\en";   # propagate unexpected errors
515 \&        # timed out
516 \&    }
517 \&    else {
518 \&        # didn't
519 \&    }
522 For more information see perlipc.
523 .IP "atan2 Y,X" 8
524 .IX Item "atan2 Y,X"
525 Returns the arctangent of Y/X in the range \-PI to \s-1PI\s0.
527 For the tangent operation, you may use the \f(CW\*(C`Math::Trig::tan\*(C'\fR
528 function, or use the familiar relation:
530 .Vb 1
531 \&    sub tan { sin($_[0]) / cos($_[0])  }
533 .IP "bind \s-1SOCKET\s0,NAME" 8
534 .IX Item "bind SOCKET,NAME"
535 Binds a network address to a socket, just as the bind system call
536 does.  Returns true if it succeeded, false otherwise.  \s-1NAME\s0 should be a
537 packed address of the appropriate type for the socket.  See the examples in
538 \&\*(L"Sockets: Client/Server Communication\*(R" in perlipc.
539 .IP "binmode \s-1FILEHANDLE\s0, \s-1LAYER\s0" 8
540 .IX Item "binmode FILEHANDLE, LAYER"
541 .PD 0
542 .IP "binmode \s-1FILEHANDLE\s0" 8
543 .IX Item "binmode FILEHANDLE"
545 Arranges for \s-1FILEHANDLE\s0 to be read or written in \*(L"binary\*(R" or \*(L"text\*(R"
546 mode on systems where the run-time libraries distinguish between
547 binary and text files.  If \s-1FILEHANDLE\s0 is an expression, the value is
548 taken as the name of the filehandle.  Returns true on success,
549 otherwise it returns \f(CW\*(C`undef\*(C'\fR and sets \f(CW$!\fR (errno).
551 On some systems (in general, \s-1DOS\s0 and Windows-based systems) \fIbinmode()\fR
552 is necessary when you're not working with a text file.  For the sake
553 of portability it is a good idea to always use it when appropriate,
554 and to never use it when it isn't appropriate.  Also, people can
555 set their I/O to be by default \s-1UTF\-8\s0 encoded Unicode, not bytes.
557 In other words: regardless of platform, use \fIbinmode()\fR on binary data,
558 like for example images.
560 If \s-1LAYER\s0 is present it is a single string, but may contain multiple
561 directives. The directives alter the behaviour of the file handle.
562 When \s-1LAYER\s0 is present using binmode on text file makes sense.
564 If \s-1LAYER\s0 is omitted or specified as \f(CW\*(C`:raw\*(C'\fR the filehandle is made
565 suitable for passing binary data. This includes turning off possible \s-1CRLF\s0
566 translation and marking it as bytes (as opposed to Unicode characters).
567 Note that as despite what may be implied in \fI\*(L"Programming Perl\*(R"\fR
568 (the Camel) or elsewhere \f(CW\*(C`:raw\*(C'\fR is \fInot\fR the simply inverse of \f(CW\*(C`:crlf\*(C'\fR
569 \&\*(-- other layers which would affect binary nature of the stream are
570 \&\fIalso\fR disabled. See PerlIO, perlrun and the discussion about the
571 \&\s-1PERLIO\s0 environment variable.
573 The \f(CW\*(C`:bytes\*(C'\fR, \f(CW\*(C`:crlf\*(C'\fR, and \f(CW\*(C`:utf8\*(C'\fR, and any other directives of the
574 form \f(CW\*(C`:...\*(C'\fR, are called I/O \fIlayers\fR.  The \f(CW\*(C`open\*(C'\fR pragma can be used to
575 establish default I/O layers.  See open.
577 \&\fIThe \s-1LAYER\s0 parameter of the \fIbinmode()\fI function is described as \*(L"\s-1DISCIPLINE\s0\*(R"
578 in \*(L"Programming Perl, 3rd Edition\*(R".  However, since the publishing of this
579 book, by many known as \*(L"Camel \s-1III\s0\*(R", the consensus of the naming of this
580 functionality has moved from \*(L"discipline\*(R" to \*(L"layer\*(R".  All documentation
581 of this version of Perl therefore refers to \*(L"layers\*(R" rather than to
582 \&\*(L"disciplines\*(R".  Now back to the regularly scheduled documentation...\fR
584 To mark \s-1FILEHANDLE\s0 as \s-1UTF\-8\s0, use \f(CW\*(C`:utf8\*(C'\fR.
586 In general, \fIbinmode()\fR should be called after \fIopen()\fR but before any I/O
587 is done on the filehandle.  Calling \fIbinmode()\fR will normally flush any
588 pending buffered output data (and perhaps pending input data) on the
589 handle.  An exception to this is the \f(CW\*(C`:encoding\*(C'\fR layer that
590 changes the default character encoding of the handle, see open.
591 The \f(CW\*(C`:encoding\*(C'\fR layer sometimes needs to be called in
592 mid\-stream, and it doesn't flush the stream.  The \f(CW\*(C`:encoding\*(C'\fR
593 also implicitly pushes on top of itself the \f(CW\*(C`:utf8\*(C'\fR layer because
594 internally Perl will operate on \s-1UTF\-8\s0 encoded Unicode characters.
596 The operating system, device drivers, C libraries, and Perl run-time
597 system all work together to let the programmer treat a single
598 character (\f(CW\*(C`\en\*(C'\fR) as the line terminator, irrespective of the external
599 representation.  On many operating systems, the native text file
600 representation matches the internal representation, but on some
601 platforms the external representation of \f(CW\*(C`\en\*(C'\fR is made up of more than
602 one character.
604 Mac \s-1OS\s0, all variants of Unix, and Stream_LF files on \s-1VMS\s0 use a single
605 character to end each line in the external representation of text (even
606 though that single character is \s-1CARRIAGE\s0 \s-1RETURN\s0 on Mac \s-1OS\s0 and \s-1LINE\s0 \s-1FEED\s0
607 on Unix and most \s-1VMS\s0 files). In other systems like \s-1OS/2\s0, \s-1DOS\s0 and the
608 various flavors of MS-Windows your program sees a \f(CW\*(C`\en\*(C'\fR as a simple \f(CW\*(C`\ecJ\*(C'\fR,
609 but what's stored in text files are the two characters \f(CW\*(C`\ecM\ecJ\*(C'\fR.  That
610 means that, if you don't use \fIbinmode()\fR on these systems, \f(CW\*(C`\ecM\ecJ\*(C'\fR
611 sequences on disk will be converted to \f(CW\*(C`\en\*(C'\fR on input, and any \f(CW\*(C`\en\*(C'\fR in
612 your program will be converted back to \f(CW\*(C`\ecM\ecJ\*(C'\fR on output.  This is what
613 you want for text files, but it can be disastrous for binary files.
615 Another consequence of using \fIbinmode()\fR (on some systems) is that
616 special end-of-file markers will be seen as part of the data stream.
617 For systems from the Microsoft family this means that if your binary
618 data contains \f(CW\*(C`\ecZ\*(C'\fR, the I/O subsystem will regard it as the end of
619 the file, unless you use \fIbinmode()\fR.
621 \&\fIbinmode()\fR is not only important for \fIreadline()\fR and \fIprint()\fR operations,
622 but also when using \fIread()\fR, \fIseek()\fR, \fIsysread()\fR, \fIsyswrite()\fR and \fItell()\fR
623 (see perlport for more details).  See the \f(CW$/\fR and \f(CW\*(C`$\e\*(C'\fR variables
624 in perlvar for how to manually set your input and output
625 line-termination sequences.
626 .IP "bless \s-1REF\s0,CLASSNAME" 8
627 .IX Item "bless REF,CLASSNAME"
628 .PD 0
629 .IP "bless \s-1REF\s0" 8
630 .IX Item "bless REF"
632 This function tells the thingy referenced by \s-1REF\s0 that it is now an object
633 in the \s-1CLASSNAME\s0 package.  If \s-1CLASSNAME\s0 is omitted, the current package
634 is used.  Because a \f(CW\*(C`bless\*(C'\fR is often the last thing in a constructor,
635 it returns the reference for convenience.  Always use the two-argument
636 version if the function doing the blessing might be inherited by a
637 derived class.  See perltoot and perlobj for more about the blessing
638 (and blessings) of objects.
640 Consider always blessing objects in CLASSNAMEs that are mixed case.
641 Namespaces with all lowercase names are considered reserved for
642 Perl pragmata.  Builtin types have all uppercase names, so to prevent
643 confusion, you may wish to avoid such package names as well.  Make sure
644 that \s-1CLASSNAME\s0 is a true value.
646 See \*(L"Perl Modules\*(R" in perlmod.
647 .IP "caller \s-1EXPR\s0" 8
648 .IX Item "caller EXPR"
649 .PD 0
650 .IP "caller" 8
651 .IX Item "caller"
653 Returns the context of the current subroutine call.  In scalar context,
654 returns the caller's package name if there is a caller, that is, if
655 we're in a subroutine or \f(CW\*(C`eval\*(C'\fR or \f(CW\*(C`require\*(C'\fR, and the undefined value
656 otherwise.  In list context, returns
658 .Vb 1
659 \&    ($package, $filename, $line) = caller;
662 With \s-1EXPR\s0, it returns some extra information that the debugger uses to
663 print a stack trace.  The value of \s-1EXPR\s0 indicates how many call frames
664 to go back before the current one.
666 .Vb 2
667 \&    ($package, $filename, $line, $subroutine, $hasargs,
668 \&    $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller($i);
671 Here \f(CW$subroutine\fR may be \f(CW\*(C`(eval)\*(C'\fR if the frame is not a subroutine
672 call, but an \f(CW\*(C`eval\*(C'\fR.  In such a case additional elements \f(CW$evaltext\fR and
673 \&\f(CW$is_require\fR are set: \f(CW$is_require\fR is true if the frame is created by a
674 \&\f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR statement, \f(CW$evaltext\fR contains the text of the
675 \&\f(CW\*(C`eval EXPR\*(C'\fR statement.  In particular, for an \f(CW\*(C`eval BLOCK\*(C'\fR statement,
676 \&\f(CW$filename\fR is \f(CW\*(C`(eval)\*(C'\fR, but \f(CW$evaltext\fR is undefined.  (Note also that
677 each \f(CW\*(C`use\*(C'\fR statement creates a \f(CW\*(C`require\*(C'\fR frame inside an \f(CW\*(C`eval EXPR\*(C'\fR
678 frame.)  \f(CW$subroutine\fR may also be \f(CW\*(C`(unknown)\*(C'\fR if this particular
679 subroutine happens to have been deleted from the symbol table.
680 \&\f(CW$hasargs\fR is true if a new instance of \f(CW@_\fR was set up for the frame.
681 \&\f(CW$hints\fR and \f(CW$bitmask\fR contain pragmatic hints that the caller was
682 compiled with.  The \f(CW$hints\fR and \f(CW$bitmask\fR values are subject to change
683 between versions of Perl, and are not meant for external use.
685 Furthermore, when called from within the \s-1DB\s0 package, caller returns more
686 detailed information: it sets the list variable \f(CW@DB::args\fR to be the
687 arguments with which the subroutine was invoked.
689 Be aware that the optimizer might have optimized call frames away before
690 \&\f(CW\*(C`caller\*(C'\fR had a chance to get the information.  That means that \f(CWcaller(N)\fR
691 might not return information about the call frame you expect it do, for
692 \&\f(CW\*(C`N > 1\*(C'\fR.  In particular, \f(CW@DB::args\fR might have information from the
693 previous time \f(CW\*(C`caller\*(C'\fR was called.
694 .IP "chdir \s-1EXPR\s0" 8
695 .IX Item "chdir EXPR"
696 Changes the working directory to \s-1EXPR\s0, if possible. If \s-1EXPR\s0 is omitted,
697 changes to the directory specified by \f(CW$ENV{HOME}\fR, if set; if not,
698 changes to the directory specified by \f(CW$ENV{LOGDIR}\fR. (Under \s-1VMS\s0, the
699 variable \f(CW$ENV{SYS$LOGIN}\fR is also checked, and used if it is set.) If
700 neither is set, \f(CW\*(C`chdir\*(C'\fR does nothing. It returns true upon success,
701 false otherwise. See the example under \f(CW\*(C`die\*(C'\fR.
702 .IP "chmod \s-1LIST\s0" 8
703 .IX Item "chmod LIST"
704 Changes the permissions of a list of files.  The first element of the
705 list must be the numerical mode, which should probably be an octal
706 number, and which definitely should \fInot\fR a string of octal digits:
707 \&\f(CW0644\fR is okay, \f(CW'0644'\fR is not.  Returns the number of files
708 successfully changed.  See also \*(L"oct\*(R", if all you have is a string.
710 .Vb 6
711 \&    $cnt = chmod 0755, 'foo', 'bar';
712 \&    chmod 0755, @executables;
713 \&    $mode = '0644'; chmod $mode, 'foo';      # !!! sets mode to
714 \&                                             # --w----r-T
715 \&    $mode = '0644'; chmod oct($mode), 'foo'; # this is better
716 \&    $mode = 0644;   chmod $mode, 'foo';      # this is best
719 You can also import the symbolic \f(CW\*(C`S_I*\*(C'\fR constants from the Fcntl
720 module:
722 .Vb 1
723 \&    use Fcntl ':mode';
726 .Vb 2
727 \&    chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables;
728 \&    # This is identical to the chmod 0755 of the above example.
730 .IP "chomp \s-1VARIABLE\s0" 8
731 .IX Item "chomp VARIABLE"
732 .PD 0
733 .IP "chomp( \s-1LIST\s0 )" 8
734 .IX Item "chomp( LIST )"
735 .IP "chomp" 8
736 .IX Item "chomp"
738 This safer version of \*(L"chop\*(R" removes any trailing string
739 that corresponds to the current value of \f(CW$/\fR (also known as
740 \&\f(CW$INPUT_RECORD_SEPARATOR\fR in the \f(CW\*(C`English\*(C'\fR module).  It returns the total
741 number of characters removed from all its arguments.  It's often used to
742 remove the newline from the end of an input record when you're worried
743 that the final record may be missing its newline.  When in paragraph
744 mode (\f(CW\*(C`$/ = ""\*(C'\fR), it removes all trailing newlines from the string.
745 When in slurp mode (\f(CW\*(C`$/ = undef\*(C'\fR) or fixed-length record mode (\f(CW$/\fR is
746 a reference to an integer or the like, see perlvar) \fIchomp()\fR won't
747 remove anything.
748 If \s-1VARIABLE\s0 is omitted, it chomps \f(CW$_\fR.  Example:
750 .Vb 5
751 \&    while (<>) {
752 \&        chomp;  # avoid \en on last field
753 \&        @array = split(/:/);
754 \&        # ...
755 \&    }
758 If \s-1VARIABLE\s0 is a hash, it chomps the hash's values, but not its keys.
760 You can actually chomp anything that's an lvalue, including an assignment:
762 .Vb 2
763 \&    chomp($cwd = `pwd`);
764 \&    chomp($answer = <STDIN>);
767 If you chomp a list, each element is chomped, and the total number of
768 characters removed is returned.
770 Note that parentheses are necessary when you're chomping anything
771 that is not a simple variable.  This is because \f(CW\*(C`chomp $cwd = `pwd`;\*(C'\fR
772 is interpreted as \f(CW\*(C`(chomp $cwd) = `pwd`;\*(C'\fR, rather than as
773 \&\f(CW\*(C`chomp( $cwd = `pwd` )\*(C'\fR which you might expect.  Similarly,
774 \&\f(CW\*(C`chomp $a, $b\*(C'\fR is interpreted as \f(CW\*(C`chomp($a), $b\*(C'\fR rather than
775 as \f(CW\*(C`chomp($a, $b)\*(C'\fR.
776 .IP "chop \s-1VARIABLE\s0" 8
777 .IX Item "chop VARIABLE"
778 .PD 0
779 .IP "chop( \s-1LIST\s0 )" 8
780 .IX Item "chop( LIST )"
781 .IP "chop" 8
782 .IX Item "chop"
784 Chops off the last character of a string and returns the character
785 chopped.  It is much more efficient than \f(CW\*(C`s/.$//s\*(C'\fR because it neither
786 scans nor copies the string.  If \s-1VARIABLE\s0 is omitted, chops \f(CW$_\fR.
787 If \s-1VARIABLE\s0 is a hash, it chops the hash's values, but not its keys.
789 You can actually chop anything that's an lvalue, including an assignment.
791 If you chop a list, each element is chopped.  Only the value of the
792 last \f(CW\*(C`chop\*(C'\fR is returned.
794 Note that \f(CW\*(C`chop\*(C'\fR returns the last character.  To return all but the last
795 character, use \f(CW\*(C`substr($string, 0, \-1)\*(C'\fR.
797 See also \*(L"chomp\*(R".
798 .IP "chown \s-1LIST\s0" 8
799 .IX Item "chown LIST"
800 Changes the owner (and group) of a list of files.  The first two
801 elements of the list must be the \fInumeric\fR uid and gid, in that
802 order.  A value of \-1 in either position is interpreted by most
803 systems to leave that value unchanged.  Returns the number of files
804 successfully changed.
806 .Vb 2
807 \&    $cnt = chown $uid, $gid, 'foo', 'bar';
808 \&    chown $uid, $gid, @filenames;
811 Here's an example that looks up nonnumeric uids in the passwd file:
813 .Vb 4
814 \&    print "User: ";
815 \&    chomp($user = <STDIN>);
816 \&    print "Files: ";
817 \&    chomp($pattern = <STDIN>);
820 .Vb 2
821 \&    ($login,$pass,$uid,$gid) = getpwnam($user)
822 \&        or die "$user not in passwd file";
825 .Vb 2
826 \&    @ary = glob($pattern);      # expand filenames
827 \&    chown $uid, $gid, @ary;
830 On most systems, you are not allowed to change the ownership of the
831 file unless you're the superuser, although you should be able to change
832 the group to any of your secondary groups.  On insecure systems, these
833 restrictions may be relaxed, but this is not a portable assumption.
834 On \s-1POSIX\s0 systems, you can detect this condition this way:
836 .Vb 2
837 \&    use POSIX qw(sysconf _PC_CHOWN_RESTRICTED);
838 \&    $can_chown_giveaway = not sysconf(_PC_CHOWN_RESTRICTED);
840 .IP "chr \s-1NUMBER\s0" 8
841 .IX Item "chr NUMBER"
842 .PD 0
843 .IP "chr" 8
844 .IX Item "chr"
846 Returns the character represented by that \s-1NUMBER\s0 in the character set.
847 For example, \f(CW\*(C`chr(65)\*(C'\fR is \f(CW"A"\fR in either \s-1ASCII\s0 or Unicode, and
848 chr(0x263a) is a Unicode smiley face.  Note that characters from 128
849 to 255 (inclusive) are by default not encoded in \s-1UTF\-8\s0 Unicode for
850 backward compatibility reasons (but see encoding).
852 If \s-1NUMBER\s0 is omitted, uses \f(CW$_\fR.
854 For the reverse, use \*(L"ord\*(R".
856 Note that under the \f(CW\*(C`bytes\*(C'\fR pragma the \s-1NUMBER\s0 is masked to
857 the low eight bits.
859 See perlunicode and encoding for more about Unicode.
860 .IP "chroot \s-1FILENAME\s0" 8
861 .IX Item "chroot FILENAME"
862 .PD 0
863 .IP "chroot" 8
864 .IX Item "chroot"
866 This function works like the system call by the same name: it makes the
867 named directory the new root directory for all further pathnames that
868 begin with a \f(CW\*(C`/\*(C'\fR by your process and all its children.  (It doesn't
869 change your current working directory, which is unaffected.)  For security
870 reasons, this call is restricted to the superuser.  If \s-1FILENAME\s0 is
871 omitted, does a \f(CW\*(C`chroot\*(C'\fR to \f(CW$_\fR.
872 .IP "close \s-1FILEHANDLE\s0" 8
873 .IX Item "close FILEHANDLE"
874 .PD 0
875 .IP "close" 8
876 .IX Item "close"
878 Closes the file or pipe associated with the file handle, returning
879 true only if \s-1IO\s0 buffers are successfully flushed and closes the system
880 file descriptor.  Closes the currently selected filehandle if the
881 argument is omitted.
883 You don't have to close \s-1FILEHANDLE\s0 if you are immediately going to do
884 another \f(CW\*(C`open\*(C'\fR on it, because \f(CW\*(C`open\*(C'\fR will close it for you.  (See
885 \&\f(CW\*(C`open\*(C'\fR.)  However, an explicit \f(CW\*(C`close\*(C'\fR on an input file resets the line
886 counter (\f(CW$.\fR), while the implicit close done by \f(CW\*(C`open\*(C'\fR does not.
888 If the file handle came from a piped open \f(CW\*(C`close\*(C'\fR will additionally
889 return false if one of the other system calls involved fails or if the
890 program exits with non-zero status.  (If the only problem was that the
891 program exited non-zero \f(CW$!\fR will be set to \f(CW0\fR.)  Closing a pipe
892 also waits for the process executing on the pipe to complete, in case you
893 want to look at the output of the pipe afterwards, and
894 implicitly puts the exit status value of that command into \f(CW$?\fR.
896 Prematurely closing the read end of a pipe (i.e. before the process
897 writing to it at the other end has closed it) will result in a
898 \&\s-1SIGPIPE\s0 being delivered to the writer.  If the other end can't
899 handle that, be sure to read all the data before closing the pipe.
901 Example:
903 .Vb 8
904 \&    open(OUTPUT, '|sort >foo')  # pipe to sort
905 \&        or die "Can't start sort: $!";
906 \&    #...                        # print stuff to output
907 \&    close OUTPUT                # wait for sort to finish
908 \&        or warn $! ? "Error closing sort pipe: $!"
909 \&                   : "Exit status $? from sort";
910 \&    open(INPUT, 'foo')          # get sort's results
911 \&        or die "Can't open 'foo' for input: $!";
914 \&\s-1FILEHANDLE\s0 may be an expression whose value can be used as an indirect
915 filehandle, usually the real filehandle name.
916 .IP "closedir \s-1DIRHANDLE\s0" 8
917 .IX Item "closedir DIRHANDLE"
918 Closes a directory opened by \f(CW\*(C`opendir\*(C'\fR and returns the success of that
919 system call.
920 .IP "connect \s-1SOCKET\s0,NAME" 8
921 .IX Item "connect SOCKET,NAME"
922 Attempts to connect to a remote socket, just as the connect system call
923 does.  Returns true if it succeeded, false otherwise.  \s-1NAME\s0 should be a
924 packed address of the appropriate type for the socket.  See the examples in
925 \&\*(L"Sockets: Client/Server Communication\*(R" in perlipc.
926 .IP "continue \s-1BLOCK\s0" 8
927 .IX Item "continue BLOCK"
928 Actually a flow control statement rather than a function.  If there is a
929 \&\f(CW\*(C`continue\*(C'\fR \s-1BLOCK\s0 attached to a \s-1BLOCK\s0 (typically in a \f(CW\*(C`while\*(C'\fR or
930 \&\f(CW\*(C`foreach\*(C'\fR), it is always executed just before the conditional is about to
931 be evaluated again, just like the third part of a \f(CW\*(C`for\*(C'\fR loop in C.  Thus
932 it can be used to increment a loop variable, even when the loop has been
933 continued via the \f(CW\*(C`next\*(C'\fR statement (which is similar to the C \f(CW\*(C`continue\*(C'\fR
934 statement).
936 \&\f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, or \f(CW\*(C`redo\*(C'\fR may appear within a \f(CW\*(C`continue\*(C'\fR
937 block.  \f(CW\*(C`last\*(C'\fR and \f(CW\*(C`redo\*(C'\fR will behave as if they had been executed within
938 the main block.  So will \f(CW\*(C`next\*(C'\fR, but since it will execute a \f(CW\*(C`continue\*(C'\fR
939 block, it may be more entertaining.
941 .Vb 9
942 \&    while (EXPR) {
943 \&        ### redo always comes here
944 \&        do_something;
945 \&    } continue {
946 \&        ### next always comes here
947 \&        do_something_else;
948 \&        # then back the top to re-check EXPR
949 \&    }
950 \&    ### last always comes here
953 Omitting the \f(CW\*(C`continue\*(C'\fR section is semantically equivalent to using an
954 empty one, logically enough.  In that case, \f(CW\*(C`next\*(C'\fR goes directly back
955 to check the condition at the top of the loop.
956 .IP "cos \s-1EXPR\s0" 8
957 .IX Item "cos EXPR"
958 .PD 0
959 .IP "cos" 8
960 .IX Item "cos"
962 Returns the cosine of \s-1EXPR\s0 (expressed in radians).  If \s-1EXPR\s0 is omitted,
963 takes cosine of \f(CW$_\fR.
965 For the inverse cosine operation, you may use the \f(CW\*(C`Math::Trig::acos()\*(C'\fR
966 function, or use this relation:
968 .Vb 1
969 \&    sub acos { atan2( sqrt(1 - $_[0] * $_[0]), $_[0] ) }
971 .IP "crypt \s-1PLAINTEXT\s0,SALT" 8
972 .IX Item "crypt PLAINTEXT,SALT"
973 Encrypts a string exactly like the \fIcrypt\fR\|(3) function in the C library
974 (assuming that you actually have a version there that has not been
975 extirpated as a potential munition).  This can prove useful for checking
976 the password file for lousy passwords, amongst other things.  Only the
977 guys wearing white hats should do this.
979 Note that crypt is intended to be a one-way function, much like
980 breaking eggs to make an omelette.  There is no (known) corresponding
981 decrypt function (in other words, the \fIcrypt()\fR is a one-way hash
982 function).  As a result, this function isn't all that useful for
983 cryptography.  (For that, see your nearby \s-1CPAN\s0 mirror.)
985 When verifying an existing encrypted string you should use the
986 encrypted text as the salt (like \f(CW\*(C`crypt($plain, $crypted) eq
987 $crypted\*(C'\fR).  This allows your code to work with the standard crypt
988 and with more exotic implementations.  In other words, do not assume
989 anything about the returned string itself, or how many bytes in
990 the encrypted string matter.
992 Traditionally the result is a string of 13 bytes: two first bytes of
993 the salt, followed by 11 bytes from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR, and only
994 the first eight bytes of the encrypted string mattered, but
995 alternative hashing schemes (like \s-1MD5\s0), higher level security schemes
996 (like C2), and implementations on non-UNIX platforms may produce
997 different strings.
999 When choosing a new salt create a random two character string whose
1000 characters come from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR (like \f(CW\*(C`join '', ('.',
1001 \&'/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]\*(C'\fR).  This set of
1002 characters is just a recommendation; the characters allowed in
1003 the salt depend solely on your system's crypt library, and Perl can't
1004 restrict what salts \f(CW\*(C`crypt()\*(C'\fR accepts.
1006 Here's an example that makes sure that whoever runs this program knows
1007 their own password:
1009 .Vb 1
1010 \&    $pwd = (getpwuid($<))[1];
1013 .Vb 5
1014 \&    system "stty -echo";
1015 \&    print "Password: ";
1016 \&    chomp($word = <STDIN>);
1017 \&    print "\en";
1018 \&    system "stty echo";
1021 .Vb 5
1022 \&    if (crypt($word, $pwd) ne $pwd) {
1023 \&        die "Sorry...\en";
1024 \&    } else {
1025 \&        print "ok\en";
1026 \&    }
1029 Of course, typing in your own password to whoever asks you
1030 for it is unwise.
1032 The crypt function is unsuitable for encrypting large quantities
1033 of data, not least of all because you can't get the information
1034 back.  Look at the \fIby\-module/Crypt\fR and \fIby\-module/PGP\fR directories
1035 on your favorite \s-1CPAN\s0 mirror for a slew of potentially useful
1036 modules.
1038 If using \fIcrypt()\fR on a Unicode string (which \fIpotentially\fR has
1039 characters with codepoints above 255), Perl tries to make sense
1040 of the situation by trying to downgrade (a copy of the string)
1041 the string back to an eight-bit byte string before calling \fIcrypt()\fR
1042 (on that copy).  If that works, good.  If not, \fIcrypt()\fR dies with
1043 \&\f(CW\*(C`Wide character in crypt\*(C'\fR.
1044 .IP "dbmclose \s-1HASH\s0" 8
1045 .IX Item "dbmclose HASH"
1046 [This function has been largely superseded by the \f(CW\*(C`untie\*(C'\fR function.]
1048 Breaks the binding between a \s-1DBM\s0 file and a hash.
1049 .IP "dbmopen \s-1HASH\s0,DBNAME,MASK" 8
1050 .IX Item "dbmopen HASH,DBNAME,MASK"
1051 [This function has been largely superseded by the \f(CW\*(C`tie\*(C'\fR function.]
1053 This binds a \fIdbm\fR\|(3), \fIndbm\fR\|(3), \fIsdbm\fR\|(3), \fIgdbm\fR\|(3), or Berkeley \s-1DB\s0 file to a
1054 hash.  \s-1HASH\s0 is the name of the hash.  (Unlike normal \f(CW\*(C`open\*(C'\fR, the first
1055 argument is \fInot\fR a filehandle, even though it looks like one).  \s-1DBNAME\s0
1056 is the name of the database (without the \fI.dir\fR or \fI.pag\fR extension if
1057 any).  If the database does not exist, it is created with protection
1058 specified by \s-1MASK\s0 (as modified by the \f(CW\*(C`umask\*(C'\fR).  If your system supports
1059 only the older \s-1DBM\s0 functions, you may perform only one \f(CW\*(C`dbmopen\*(C'\fR in your
1060 program.  In older versions of Perl, if your system had neither \s-1DBM\s0 nor
1061 ndbm, calling \f(CW\*(C`dbmopen\*(C'\fR produced a fatal error; it now falls back to
1062 \&\fIsdbm\fR\|(3).
1064 If you don't have write access to the \s-1DBM\s0 file, you can only read hash
1065 variables, not set them.  If you want to test whether you can write,
1066 either use file tests or try setting a dummy hash entry inside an \f(CW\*(C`eval\*(C'\fR,
1067 which will trap the error.
1069 Note that functions such as \f(CW\*(C`keys\*(C'\fR and \f(CW\*(C`values\*(C'\fR may return huge lists
1070 when used on large \s-1DBM\s0 files.  You may prefer to use the \f(CW\*(C`each\*(C'\fR
1071 function to iterate over large \s-1DBM\s0 files.  Example:
1073 .Vb 6
1074 \&    # print out history file offsets
1075 \&    dbmopen(%HIST,'/usr/lib/news/history',0666);
1076 \&    while (($key,$val) = each %HIST) {
1077 \&        print $key, ' = ', unpack('L',$val), "\en";
1078 \&    }
1079 \&    dbmclose(%HIST);
1082 See also AnyDBM_File for a more general description of the pros and
1083 cons of the various dbm approaches, as well as DB_File for a particularly
1084 rich implementation.
1086 You can control which \s-1DBM\s0 library you use by loading that library
1087 before you call \fIdbmopen()\fR:
1089 .Vb 3
1090 \&    use DB_File;
1091 \&    dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")
1092 \&        or die "Can't open netscape history file: $!";
1094 .IP "defined \s-1EXPR\s0" 8
1095 .IX Item "defined EXPR"
1096 .PD 0
1097 .IP "defined" 8
1098 .IX Item "defined"
1100 Returns a Boolean value telling whether \s-1EXPR\s0 has a value other than
1101 the undefined value \f(CW\*(C`undef\*(C'\fR.  If \s-1EXPR\s0 is not present, \f(CW$_\fR will be
1102 checked.
1104 Many operations return \f(CW\*(C`undef\*(C'\fR to indicate failure, end of file,
1105 system error, uninitialized variable, and other exceptional
1106 conditions.  This function allows you to distinguish \f(CW\*(C`undef\*(C'\fR from
1107 other values.  (A simple Boolean test will not distinguish among
1108 \&\f(CW\*(C`undef\*(C'\fR, zero, the empty string, and \f(CW"0"\fR, which are all equally
1109 false.)  Note that since \f(CW\*(C`undef\*(C'\fR is a valid scalar, its presence
1110 doesn't \fInecessarily\fR indicate an exceptional condition: \f(CW\*(C`pop\*(C'\fR
1111 returns \f(CW\*(C`undef\*(C'\fR when its argument is an empty array, \fIor\fR when the
1112 element to return happens to be \f(CW\*(C`undef\*(C'\fR.
1114 You may also use \f(CW\*(C`defined(&func)\*(C'\fR to check whether subroutine \f(CW&func\fR
1115 has ever been defined.  The return value is unaffected by any forward
1116 declarations of \f(CW&func\fR.  Note that a subroutine which is not defined
1117 may still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR method that
1118 makes it spring into existence the first time that it is called \*(-- see
1119 perlsub.
1121 Use of \f(CW\*(C`defined\*(C'\fR on aggregates (hashes and arrays) is deprecated.  It
1122 used to report whether memory for that aggregate has ever been
1123 allocated.  This behavior may disappear in future versions of Perl.
1124 You should instead use a simple test for size:
1126 .Vb 2
1127 \&    if (@an_array) { print "has array elements\en" }
1128 \&    if (%a_hash)   { print "has hash members\en"   }
1131 When used on a hash element, it tells you whether the value is defined,
1132 not whether the key exists in the hash.  Use \*(L"exists\*(R" for the latter
1133 purpose.
1135 Examples:
1137 .Vb 6
1138 \&    print if defined $switch{'D'};
1139 \&    print "$val\en" while defined($val = pop(@ary));
1140 \&    die "Can't readlink $sym: $!"
1141 \&        unless defined($value = readlink $sym);
1142 \&    sub foo { defined &$bar ? &$bar(@_) : die "No bar"; }
1143 \&    $debugging = 0 unless defined $debugging;
1146 Note:  Many folks tend to overuse \f(CW\*(C`defined\*(C'\fR, and then are surprised to
1147 discover that the number \f(CW0\fR and \f(CW""\fR (the zero-length string) are, in fact,
1148 defined values.  For example, if you say
1150 .Vb 1
1151 \&    "ab" =~ /a(.*)b/;
1154 The pattern match succeeds, and \f(CW$1\fR is defined, despite the fact that it
1155 matched \*(L"nothing\*(R".  But it didn't really match nothing\*(--rather, it
1156 matched something that happened to be zero characters long.  This is all
1157 very above-board and honest.  When a function returns an undefined value,
1158 it's an admission that it couldn't give you an honest answer.  So you
1159 should use \f(CW\*(C`defined\*(C'\fR only when you're questioning the integrity of what
1160 you're trying to do.  At other times, a simple comparison to \f(CW0\fR or \f(CW""\fR is
1161 what you want.
1163 See also \*(L"undef\*(R", \*(L"exists\*(R", \*(L"ref\*(R".
1164 .IP "delete \s-1EXPR\s0" 8
1165 .IX Item "delete EXPR"
1166 Given an expression that specifies a hash element, array element, hash slice,
1167 or array slice, deletes the specified element(s) from the hash or array.
1168 In the case of an array, if the array elements happen to be at the end,
1169 the size of the array will shrink to the highest element that tests
1170 true for \fIexists()\fR (or 0 if no such element exists).
1172 Returns each element so deleted or the undefined value if there was no such
1173 element.  Deleting from \f(CW$ENV{}\fR modifies the environment.  Deleting from
1174 a hash tied to a \s-1DBM\s0 file deletes the entry from the \s-1DBM\s0 file.  Deleting
1175 from a \f(CW\*(C`tie\*(C'\fRd hash or array may not necessarily return anything.
1177 Deleting an array element effectively returns that position of the array
1178 to its initial, uninitialized state.  Subsequently testing for the same
1179 element with \fIexists()\fR will return false.  Note that deleting array
1180 elements in the middle of an array will not shift the index of the ones
1181 after them down\*(--use \fIsplice()\fR for that.  See \*(L"exists\*(R".
1183 The following (inefficiently) deletes all the values of \f(CW%HASH\fR and \f(CW@ARRAY:\fR
1185 .Vb 3
1186 \&    foreach $key (keys %HASH) {
1187 \&        delete $HASH{$key};
1188 \&    }
1191 .Vb 3
1192 \&    foreach $index (0 .. $#ARRAY) {
1193 \&        delete $ARRAY[$index];
1194 \&    }
1197 And so do these:
1199 .Vb 1
1200 \&    delete @HASH{keys %HASH};
1203 .Vb 1
1204 \&    delete @ARRAY[0 .. $#ARRAY];
1207 But both of these are slower than just assigning the empty list
1208 or undefining \f(CW%HASH\fR or \f(CW@ARRAY:\fR
1210 .Vb 2
1211 \&    %HASH = ();         # completely empty %HASH
1212 \&    undef %HASH;        # forget %HASH ever existed
1215 .Vb 2
1216 \&    @ARRAY = ();        # completely empty @ARRAY
1217 \&    undef @ARRAY;       # forget @ARRAY ever existed
1220 Note that the \s-1EXPR\s0 can be arbitrarily complicated as long as the final
1221 operation is a hash element, array element,  hash slice, or array slice
1222 lookup:
1224 .Vb 2
1225 \&    delete $ref->[$x][$y]{$key};
1226 \&    delete @{$ref->[$x][$y]}{$key1, $key2, @morekeys};
1229 .Vb 2
1230 \&    delete $ref->[$x][$y][$index];
1231 \&    delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices];
1233 .IP "die \s-1LIST\s0" 8
1234 .IX Item "die LIST"
1235 Outside an \f(CW\*(C`eval\*(C'\fR, prints the value of \s-1LIST\s0 to \f(CW\*(C`STDERR\*(C'\fR and
1236 exits with the current value of \f(CW$!\fR (errno).  If \f(CW$!\fR is \f(CW0\fR,
1237 exits with the value of \f(CW\*(C`($? >> 8)\*(C'\fR (backtick `command`
1238 status).  If \f(CW\*(C`($? >> 8)\*(C'\fR is \f(CW0\fR, exits with \f(CW255\fR.  Inside
1239 an \f(CW\*(C`eval(),\*(C'\fR the error message is stuffed into \f(CW$@\fR and the
1240 \&\f(CW\*(C`eval\*(C'\fR is terminated with the undefined value.  This makes
1241 \&\f(CW\*(C`die\*(C'\fR the way to raise an exception.
1243 Equivalent examples:
1245 .Vb 2
1246 \&    die "Can't cd to spool: $!\en" unless chdir '/usr/spool/news';
1247 \&    chdir '/usr/spool/news' or die "Can't cd to spool: $!\en"
1250 If the last element of \s-1LIST\s0 does not end in a newline, the current
1251 script line number and input line number (if any) are also printed,
1252 and a newline is supplied.  Note that the \*(L"input line number\*(R" (also
1253 known as \*(L"chunk\*(R") is subject to whatever notion of \*(L"line\*(R" happens to
1254 be currently in effect, and is also available as the special variable
1255 \&\f(CW$.\fR.  See \*(L"$/\*(R" in perlvar and \*(L"$.\*(R" in perlvar.
1257 Hint: sometimes appending \f(CW", stopped"\fR to your message will cause it
1258 to make better sense when the string \f(CW"at foo line 123"\fR is appended.
1259 Suppose you are running script \*(L"canasta\*(R".
1261 .Vb 2
1262 \&    die "/etc/games is no good";
1263 \&    die "/etc/games is no good, stopped";
1266 produce, respectively
1268 .Vb 2
1269 \&    /etc/games is no good at canasta line 123.
1270 \&    /etc/games is no good, stopped at canasta line 123.
1273 See also \fIexit()\fR, \fIwarn()\fR, and the Carp module.
1275 If \s-1LIST\s0 is empty and \f(CW$@\fR already contains a value (typically from a
1276 previous eval) that value is reused after appending \f(CW"\et...propagated"\fR.
1277 This is useful for propagating exceptions:
1279 .Vb 2
1280 \&    eval { ... };
1281 \&    die unless $@ =~ /Expected exception/;
1284 If \s-1LIST\s0 is empty and \f(CW$@\fR contains an object reference that has a
1285 \&\f(CW\*(C`PROPAGATE\*(C'\fR method, that method will be called with additional file
1286 and line number parameters.  The return value replaces the value in
1287 \&\f(CW$@\fR.  ie. as if \f(CW\*(C`$@ = eval { $@\->PROPAGATE(_\|_FILE_\|_, _\|_LINE_\|_) };\*(C'\fR
1288 were called.
1290 If \f(CW$@\fR is empty then the string \f(CW"Died"\fR is used.
1292 \&\fIdie()\fR can also be called with a reference argument.  If this happens to be
1293 trapped within an \fIeval()\fR, $@ contains the reference.  This behavior permits
1294 a more elaborate exception handling implementation using objects that
1295 maintain arbitrary state about the nature of the exception.  Such a scheme
1296 is sometimes preferable to matching particular string values of $@ using
1297 regular expressions.  Here's an example:
1299 .Vb 9
1300 \&    eval { ... ; die Some::Module::Exception->new( FOO => "bar" ) };
1301 \&    if ($@) {
1302 \&        if (ref($@) && UNIVERSAL::isa($@,"Some::Module::Exception")) {
1303 \&            # handle Some::Module::Exception
1304 \&        }
1305 \&        else {
1306 \&            # handle all other possible exceptions
1307 \&        }
1308 \&    }
1311 Because perl will stringify uncaught exception messages before displaying
1312 them, you may want to overload stringification operations on such custom
1313 exception objects.  See overload for details about that.
1315 You can arrange for a callback to be run just before the \f(CW\*(C`die\*(C'\fR
1316 does its deed, by setting the \f(CW$SIG{_\|_DIE_\|_}\fR hook.  The associated
1317 handler will be called with the error text and can change the error
1318 message, if it sees fit, by calling \f(CW\*(C`die\*(C'\fR again.  See
1319 \&\*(L"$SIG{expr}\*(R" in perlvar for details on setting \f(CW%SIG\fR entries, and
1320 \&\*(L"eval \s-1BLOCK\s0\*(R" for some examples.  Although this feature was meant
1321 to be run only right before your program was to exit, this is not
1322 currently the case\*(--the \f(CW$SIG{_\|_DIE_\|_}\fR hook is currently called
1323 even inside \fIeval()\fRed blocks/strings!  If one wants the hook to do
1324 nothing in such situations, put
1326 .Vb 1
1327 \&        die @_ if $^S;
1330 as the first line of the handler (see \*(L"$^S\*(R" in perlvar).  Because
1331 this promotes strange action at a distance, this counterintuitive
1332 behavior may be fixed in a future release.
1333 .IP "do \s-1BLOCK\s0" 8
1334 .IX Item "do BLOCK"
1335 Not really a function.  Returns the value of the last command in the
1336 sequence of commands indicated by \s-1BLOCK\s0.  When modified by a loop
1337 modifier, executes the \s-1BLOCK\s0 once before testing the loop condition.
1338 (On other statements the loop modifiers test the conditional first.)
1340 \&\f(CW\*(C`do BLOCK\*(C'\fR does \fInot\fR count as a loop, so the loop control statements
1341 \&\f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, or \f(CW\*(C`redo\*(C'\fR cannot be used to leave or restart the block.
1342 See perlsyn for alternative strategies.
1343 .IP "do \s-1SUBROUTINE\s0(\s-1LIST\s0)" 8
1344 .IX Item "do SUBROUTINE(LIST)"
1345 A deprecated form of subroutine call.  See perlsub.
1346 .IP "do \s-1EXPR\s0" 8
1347 .IX Item "do EXPR"
1348 Uses the value of \s-1EXPR\s0 as a filename and executes the contents of the
1349 file as a Perl script.  Its primary use is to include subroutines
1350 from a Perl subroutine library.
1352 .Vb 1
1353 \&    do 'stat.pl';
1356 is just like
1358 .Vb 1
1359 \&    eval `cat stat.pl`;
1362 except that it's more efficient and concise, keeps track of the current
1363 filename for error messages, searches the \f(CW@INC\fR libraries, and updates
1364 \&\f(CW%INC\fR if the file is found.  See \*(L"Predefined Names\*(R" in perlvar for these
1365 variables.  It also differs in that code evaluated with \f(CW\*(C`do FILENAME\*(C'\fR
1366 cannot see lexicals in the enclosing scope; \f(CW\*(C`eval STRING\*(C'\fR does.  It's the
1367 same, however, in that it does reparse the file every time you call it,
1368 so you probably don't want to do this inside a loop.
1370 If \f(CW\*(C`do\*(C'\fR cannot read the file, it returns undef and sets \f(CW$!\fR to the
1371 error.  If \f(CW\*(C`do\*(C'\fR can read the file but cannot compile it, it
1372 returns undef and sets an error message in \f(CW$@\fR.   If the file is
1373 successfully compiled, \f(CW\*(C`do\*(C'\fR returns the value of the last expression
1374 evaluated.
1376 Note that inclusion of library modules is better done with the
1377 \&\f(CW\*(C`use\*(C'\fR and \f(CW\*(C`require\*(C'\fR operators, which also do automatic error checking
1378 and raise an exception if there's a problem.
1380 You might like to use \f(CW\*(C`do\*(C'\fR to read in a program configuration
1381 file.  Manual error checking can be done this way:
1383 .Vb 10
1384 \&    # read in config files: system first, then user
1385 \&    for $file ("/share/prog/defaults.rc",
1386 \&               "$ENV{HOME}/.someprogrc")
1387 \&   {
1388 \&        unless ($return = do $file) {
1389 \&            warn "couldn't parse $file: $@" if $@;
1390 \&            warn "couldn't do $file: $!"    unless defined $return;
1391 \&            warn "couldn't run $file"       unless $return;
1392 \&        }
1393 \&    }
1395 .IP "dump \s-1LABEL\s0" 8
1396 .IX Item "dump LABEL"
1397 .PD 0
1398 .IP "dump" 8
1399 .IX Item "dump"
1401 This function causes an immediate core dump.  See also the \fB\-u\fR
1402 command-line switch in perlrun, which does the same thing.
1403 Primarily this is so that you can use the \fBundump\fR program (not
1404 supplied) to turn your core dump into an executable binary after
1405 having initialized all your variables at the beginning of the
1406 program.  When the new binary is executed it will begin by executing
1407 a \f(CW\*(C`goto LABEL\*(C'\fR (with all the restrictions that \f(CW\*(C`goto\*(C'\fR suffers).
1408 Think of it as a goto with an intervening core dump and reincarnation.
1409 If \f(CW\*(C`LABEL\*(C'\fR is omitted, restarts the program from the top.
1411 \&\fB\s-1WARNING\s0\fR: Any files opened at the time of the dump will \fInot\fR
1412 be open any more when the program is reincarnated, with possible
1413 resulting confusion on the part of Perl.
1415 This function is now largely obsolete, partly because it's very
1416 hard to convert a core file into an executable, and because the
1417 real compiler backends for generating portable bytecode and compilable
1418 C code have superseded it.  That's why you should now invoke it as
1419 \&\f(CW\*(C`CORE::dump()\*(C'\fR, if you don't want to be warned against a possible
1420 typo.
1422 If you're looking to use dump to speed up your program, consider
1423 generating bytecode or native C code as described in perlcc.  If
1424 you're just trying to accelerate a \s-1CGI\s0 script, consider using the
1425 \&\f(CW\*(C`mod_perl\*(C'\fR extension to \fBApache\fR, or the \s-1CPAN\s0 module, CGI::Fast.
1426 You might also consider autoloading or selfloading, which at least
1427 make your program \fIappear\fR to run faster.
1428 .IP "each \s-1HASH\s0" 8
1429 .IX Item "each HASH"
1430 When called in list context, returns a 2\-element list consisting of the
1431 key and value for the next element of a hash, so that you can iterate over
1432 it.  When called in scalar context, returns only the key for the next
1433 element in the hash.
1435 Entries are returned in an apparently random order.  The actual random
1436 order is subject to change in future versions of perl, but it is
1437 guaranteed to be in the same order as either the \f(CW\*(C`keys\*(C'\fR or \f(CW\*(C`values\*(C'\fR
1438 function would produce on the same (unmodified) hash.  Since Perl
1439 5.8.1 the ordering is different even between different runs of Perl
1440 for security reasons (see \*(L"Algorithmic Complexity Attacks\*(R" in perlsec).
1442 When the hash is entirely read, a null array is returned in list context
1443 (which when assigned produces a false (\f(CW0\fR) value), and \f(CW\*(C`undef\*(C'\fR in
1444 scalar context.  The next call to \f(CW\*(C`each\*(C'\fR after that will start iterating
1445 again.  There is a single iterator for each hash, shared by all \f(CW\*(C`each\*(C'\fR,
1446 \&\f(CW\*(C`keys\*(C'\fR, and \f(CW\*(C`values\*(C'\fR function calls in the program; it can be reset by
1447 reading all the elements from the hash, or by evaluating \f(CW\*(C`keys HASH\*(C'\fR or
1448 \&\f(CW\*(C`values HASH\*(C'\fR.  If you add or delete elements of a hash while you're
1449 iterating over it, you may get entries skipped or duplicated, so
1450 don't.  Exception: It is always safe to delete the item most recently
1451 returned by \f(CW\*(C`each()\*(C'\fR, which means that the following code will work:
1453 .Vb 4
1454 \&        while (($key, $value) = each %hash) {
1455 \&          print $key, "\en";
1456 \&          delete $hash{$key};   # This is safe
1457 \&        }
1460 The following prints out your environment like the \fIprintenv\fR\|(1) program,
1461 only in a different order:
1463 .Vb 3
1464 \&    while (($key,$value) = each %ENV) {
1465 \&        print "$key=$value\en";
1466 \&    }
1469 See also \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR and \f(CW\*(C`sort\*(C'\fR.
1470 .IP "eof \s-1FILEHANDLE\s0" 8
1471 .IX Item "eof FILEHANDLE"
1472 .PD 0
1473 .IP "eof ()" 8
1474 .IX Item "eof ()"
1475 .IP "eof" 8
1476 .IX Item "eof"
1478 Returns 1 if the next read on \s-1FILEHANDLE\s0 will return end of file, or if
1479 \&\s-1FILEHANDLE\s0 is not open.  \s-1FILEHANDLE\s0 may be an expression whose value
1480 gives the real filehandle.  (Note that this function actually
1481 reads a character and then \f(CW\*(C`ungetc\*(C'\fRs it, so isn't very useful in an
1482 interactive context.)  Do not read from a terminal file (or call
1483 \&\f(CW\*(C`eof(FILEHANDLE)\*(C'\fR on it) after end-of-file is reached.  File types such
1484 as terminals may lose the end-of-file condition if you do.
1486 An \f(CW\*(C`eof\*(C'\fR without an argument uses the last file read.  Using \f(CW\*(C`eof()\*(C'\fR
1487 with empty parentheses is very different.  It refers to the pseudo file
1488 formed from the files listed on the command line and accessed via the
1489 \&\f(CW\*(C`<>\*(C'\fR operator.  Since \f(CW\*(C`<>\*(C'\fR isn't explicitly opened,
1490 as a normal filehandle is, an \f(CW\*(C`eof()\*(C'\fR before \f(CW\*(C`<>\*(C'\fR has been
1491 used will cause \f(CW@ARGV\fR to be examined to determine if input is
1492 available.   Similarly, an \f(CW\*(C`eof()\*(C'\fR after \f(CW\*(C`<>\*(C'\fR has returned
1493 end-of-file will assume you are processing another \f(CW@ARGV\fR list,
1494 and if you haven't set \f(CW@ARGV\fR, will read input from \f(CW\*(C`STDIN\*(C'\fR;
1495 see \*(L"I/O Operators\*(R" in perlop.
1497 In a \f(CW\*(C`while (<>)\*(C'\fR loop, \f(CW\*(C`eof\*(C'\fR or \f(CW\*(C`eof(ARGV)\*(C'\fR can be used to
1498 detect the end of each file, \f(CW\*(C`eof()\*(C'\fR will only detect the end of the
1499 last file.  Examples:
1501 .Vb 7
1502 \&    # reset line numbering on each input file
1503 \&    while (<>) {
1504 \&        next if /^\es*#/;        # skip comments
1505 \&        print "$.\et$_";
1506 \&    } continue {
1507 \&        close ARGV  if eof;     # Not eof()!
1508 \&    }
1511 .Vb 8
1512 \&    # insert dashes just before last line of last file
1513 \&    while (<>) {
1514 \&        if (eof()) {            # check for end of last file
1515 \&            print "--------------\en";
1516 \&        }
1517 \&        print;
1518 \&        last if eof();          # needed if we're reading from a terminal
1519 \&    }
1522 Practical hint: you almost never need to use \f(CW\*(C`eof\*(C'\fR in Perl, because the
1523 input operators typically return \f(CW\*(C`undef\*(C'\fR when they run out of data, or if
1524 there was an error.
1525 .IP "eval \s-1EXPR\s0" 8
1526 .IX Item "eval EXPR"
1527 .PD 0
1528 .IP "eval \s-1BLOCK\s0" 8
1529 .IX Item "eval BLOCK"
1531 In the first form, the return value of \s-1EXPR\s0 is parsed and executed as if it
1532 were a little Perl program.  The value of the expression (which is itself
1533 determined within scalar context) is first parsed, and if there weren't any
1534 errors, executed in the lexical context of the current Perl program, so
1535 that any variable settings or subroutine and format definitions remain
1536 afterwards.  Note that the value is parsed every time the eval executes.
1537 If \s-1EXPR\s0 is omitted, evaluates \f(CW$_\fR.  This form is typically used to
1538 delay parsing and subsequent execution of the text of \s-1EXPR\s0 until run time.
1540 In the second form, the code within the \s-1BLOCK\s0 is parsed only once\*(--at the
1541 same time the code surrounding the eval itself was parsed\*(--and executed
1542 within the context of the current Perl program.  This form is typically
1543 used to trap exceptions more efficiently than the first (see below), while
1544 also providing the benefit of checking the code within \s-1BLOCK\s0 at compile
1545 time.
1547 The final semicolon, if any, may be omitted from the value of \s-1EXPR\s0 or within
1548 the \s-1BLOCK\s0.
1550 In both forms, the value returned is the value of the last expression
1551 evaluated inside the mini\-program; a return statement may be also used, just
1552 as with subroutines.  The expression providing the return value is evaluated
1553 in void, scalar, or list context, depending on the context of the eval itself.
1554 See \*(L"wantarray\*(R" for more on how the evaluation context can be determined.
1556 If there is a syntax error or runtime error, or a \f(CW\*(C`die\*(C'\fR statement is
1557 executed, an undefined value is returned by \f(CW\*(C`eval\*(C'\fR, and \f(CW$@\fR is set to the
1558 error message.  If there was no error, \f(CW$@\fR is guaranteed to be a null
1559 string.  Beware that using \f(CW\*(C`eval\*(C'\fR neither silences perl from printing
1560 warnings to \s-1STDERR\s0, nor does it stuff the text of warning messages into \f(CW$@\fR.
1561 To do either of those, you have to use the \f(CW$SIG{_\|_WARN_\|_}\fR facility, or
1562 turn off warnings inside the \s-1BLOCK\s0 or \s-1EXPR\s0 using \f(CW\*(C`no\ warnings\ 'all'\*(C'\fR.
1563 See \*(L"warn\*(R", perlvar, warnings and perllexwarn.
1565 Note that, because \f(CW\*(C`eval\*(C'\fR traps otherwise-fatal errors, it is useful for
1566 determining whether a particular feature (such as \f(CW\*(C`socket\*(C'\fR or \f(CW\*(C`symlink\*(C'\fR)
1567 is implemented.  It is also Perl's exception trapping mechanism, where
1568 the die operator is used to raise exceptions.
1570 If the code to be executed doesn't vary, you may use the eval-BLOCK
1571 form to trap run-time errors without incurring the penalty of
1572 recompiling each time.  The error, if any, is still returned in \f(CW$@\fR.
1573 Examples:
1575 .Vb 2
1576 \&    # make divide-by-zero nonfatal
1577 \&    eval { $answer = $a / $b; }; warn $@ if $@;
1580 .Vb 2
1581 \&    # same thing, but less efficient
1582 \&    eval '$answer = $a / $b'; warn $@ if $@;
1585 .Vb 2
1586 \&    # a compile-time error
1587 \&    eval { $answer = };                 # WRONG
1590 .Vb 2
1591 \&    # a run-time error
1592 \&    eval '$answer =';   # sets $@
1595 Due to the current arguably broken state of \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks, when using
1596 the \f(CW\*(C`eval{}\*(C'\fR form as an exception trap in libraries, you may wish not
1597 to trigger any \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks that user code may have installed.
1598 You can use the \f(CW\*(C`local $SIG{_\|_DIE_\|_}\*(C'\fR construct for this purpose,
1599 as shown in this example:
1601 .Vb 3
1602 \&    # a very private exception trap for divide-by-zero
1603 \&    eval { local $SIG{'__DIE__'}; $answer = $a / $b; };
1604 \&    warn $@ if $@;
1607 This is especially significant, given that \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks can call
1608 \&\f(CW\*(C`die\*(C'\fR again, which has the effect of changing their error messages:
1610 .Vb 7
1611 \&    # __DIE__ hooks may modify error messages
1612 \&    {
1613 \&       local $SIG{'__DIE__'} =
1614 \&              sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x };
1615 \&       eval { die "foo lives here" };
1616 \&       print $@ if $@;                # prints "bar lives here"
1617 \&    }
1620 Because this promotes action at a distance, this counterintuitive behavior
1621 may be fixed in a future release.
1623 With an \f(CW\*(C`eval\*(C'\fR, you should be especially careful to remember what's
1624 being looked at when:
1626 .Vb 2
1627 \&    eval $x;            # CASE 1
1628 \&    eval "$x";          # CASE 2
1631 .Vb 2
1632 \&    eval '$x';          # CASE 3
1633 \&    eval { $x };        # CASE 4
1636 .Vb 2
1637 \&    eval "\e$$x++";      # CASE 5
1638 \&    $$x++;              # CASE 6
1641 Cases 1 and 2 above behave identically: they run the code contained in
1642 the variable \f(CW$x\fR.  (Although case 2 has misleading double quotes making
1643 the reader wonder what else might be happening (nothing is).)  Cases 3
1644 and 4 likewise behave in the same way: they run the code \f(CW'$x'\fR, which
1645 does nothing but return the value of \f(CW$x\fR.  (Case 4 is preferred for
1646 purely visual reasons, but it also has the advantage of compiling at
1647 compile-time instead of at run\-time.)  Case 5 is a place where
1648 normally you \fIwould\fR like to use double quotes, except that in this
1649 particular situation, you can just use symbolic references instead, as
1650 in case 6.
1652 \&\f(CW\*(C`eval BLOCK\*(C'\fR does \fInot\fR count as a loop, so the loop control statements
1653 \&\f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, or \f(CW\*(C`redo\*(C'\fR cannot be used to leave or restart the block.
1655 Note that as a very special case, an \f(CW\*(C`eval ''\*(C'\fR executed within the \f(CW\*(C`DB\*(C'\fR
1656 package doesn't see the usual surrounding lexical scope, but rather the
1657 scope of the first non-DB piece of code that called it. You don't normally
1658 need to worry about this unless you are writing a Perl debugger.
1659 .IP "exec \s-1LIST\s0" 8
1660 .IX Item "exec LIST"
1661 .PD 0
1662 .IP "exec \s-1PROGRAM\s0 \s-1LIST\s0" 8
1663 .IX Item "exec PROGRAM LIST"
1665 The \f(CW\*(C`exec\*(C'\fR function executes a system command \fIand never returns\fR\-\-
1666 use \f(CW\*(C`system\*(C'\fR instead of \f(CW\*(C`exec\*(C'\fR if you want it to return.  It fails and
1667 returns false only if the command does not exist \fIand\fR it is executed
1668 directly instead of via your system's command shell (see below).
1670 Since it's a common mistake to use \f(CW\*(C`exec\*(C'\fR instead of \f(CW\*(C`system\*(C'\fR, Perl
1671 warns you if there is a following statement which isn't \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`warn\*(C'\fR,
1672 or \f(CW\*(C`exit\*(C'\fR (if \f(CW\*(C`\-w\*(C'\fR is set  \-  but you always do that).   If you
1673 \&\fIreally\fR want to follow an \f(CW\*(C`exec\*(C'\fR with some other statement, you
1674 can use one of these styles to avoid the warning:
1676 .Vb 2
1677 \&    exec ('foo')   or print STDERR "couldn't exec foo: $!";
1678 \&    { exec ('foo') }; print STDERR "couldn't exec foo: $!";
1681 If there is more than one argument in \s-1LIST\s0, or if \s-1LIST\s0 is an array
1682 with more than one value, calls \fIexecvp\fR\|(3) with the arguments in \s-1LIST\s0.
1683 If there is only one scalar argument or an array with one element in it,
1684 the argument is checked for shell metacharacters, and if there are any,
1685 the entire argument is passed to the system's command shell for parsing
1686 (this is \f(CW\*(C`/bin/sh \-c\*(C'\fR on Unix platforms, but varies on other platforms).
1687 If there are no shell metacharacters in the argument, it is split into
1688 words and passed directly to \f(CW\*(C`execvp\*(C'\fR, which is more efficient.
1689 Examples:
1691 .Vb 2
1692 \&    exec '/bin/echo', 'Your arguments are: ', @ARGV;
1693 \&    exec "sort $outfile | uniq";
1696 If you don't really want to execute the first argument, but want to lie
1697 to the program you are executing about its own name, you can specify
1698 the program you actually want to run as an \*(L"indirect object\*(R" (without a
1699 comma) in front of the \s-1LIST\s0.  (This always forces interpretation of the
1700 \&\s-1LIST\s0 as a multivalued list, even if there is only a single scalar in
1701 the list.)  Example:
1703 .Vb 2
1704 \&    $shell = '/bin/csh';
1705 \&    exec $shell '-sh';          # pretend it's a login shell
1708 or, more directly,
1710 .Vb 1
1711 \&    exec {'/bin/csh'} '-sh';    # pretend it's a login shell
1714 When the arguments get executed via the system shell, results will
1715 be subject to its quirks and capabilities.  See \*(L"`STRING`\*(R" in perlop
1716 for details.
1718 Using an indirect object with \f(CW\*(C`exec\*(C'\fR or \f(CW\*(C`system\*(C'\fR is also more
1719 secure.  This usage (which also works fine with \fIsystem()\fR) forces
1720 interpretation of the arguments as a multivalued list, even if the
1721 list had just one argument.  That way you're safe from the shell
1722 expanding wildcards or splitting up words with whitespace in them.
1724 .Vb 1
1725 \&    @args = ( "echo surprise" );
1728 .Vb 3
1729 \&    exec @args;               # subject to shell escapes
1730 \&                                # if @args == 1
1731 \&    exec { $args[0] } @args;  # safe even with one-arg list
1734 The first version, the one without the indirect object, ran the \fIecho\fR
1735 program, passing it \f(CW"surprise"\fR an argument.  The second version
1736 didn't\*(--it tried to run a program literally called \fI\*(L"echo surprise\*(R"\fR,
1737 didn't find it, and set \f(CW$?\fR to a non-zero value indicating failure.
1739 Beginning with v5.6.0, Perl will attempt to flush all files opened for
1740 output before the exec, but this may not be supported on some platforms
1741 (see perlport).  To be safe, you may need to set \f(CW$|\fR ($AUTOFLUSH
1742 in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method of \f(CW\*(C`IO::Handle\*(C'\fR on any
1743 open handles in order to avoid lost output.
1745 Note that \f(CW\*(C`exec\*(C'\fR will not call your \f(CW\*(C`END\*(C'\fR blocks, nor will it call
1746 any \f(CW\*(C`DESTROY\*(C'\fR methods in your objects.
1747 .IP "exists \s-1EXPR\s0" 8
1748 .IX Item "exists EXPR"
1749 Given an expression that specifies a hash element or array element,
1750 returns true if the specified element in the hash or array has ever
1751 been initialized, even if the corresponding value is undefined.  The
1752 element is not autovivified if it doesn't exist.
1754 .Vb 3
1755 \&    print "Exists\en"    if exists $hash{$key};
1756 \&    print "Defined\en"   if defined $hash{$key};
1757 \&    print "True\en"      if $hash{$key};
1760 .Vb 3
1761 \&    print "Exists\en"    if exists $array[$index];
1762 \&    print "Defined\en"   if defined $array[$index];
1763 \&    print "True\en"      if $array[$index];
1766 A hash or array element can be true only if it's defined, and defined if
1767 it exists, but the reverse doesn't necessarily hold true.
1769 Given an expression that specifies the name of a subroutine,
1770 returns true if the specified subroutine has ever been declared, even
1771 if it is undefined.  Mentioning a subroutine name for exists or defined
1772 does not count as declaring it.  Note that a subroutine which does not
1773 exist may still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR
1774 method that makes it spring into existence the first time that it is
1775 called \*(-- see perlsub.
1777 .Vb 2
1778 \&    print "Exists\en"    if exists &subroutine;
1779 \&    print "Defined\en"   if defined &subroutine;
1782 Note that the \s-1EXPR\s0 can be arbitrarily complicated as long as the final
1783 operation is a hash or array key lookup or subroutine name:
1785 .Vb 2
1786 \&    if (exists $ref->{A}->{B}->{$key})  { }
1787 \&    if (exists $hash{A}{B}{$key})       { }
1790 .Vb 2
1791 \&    if (exists $ref->{A}->{B}->[$ix])   { }
1792 \&    if (exists $hash{A}{B}[$ix])        { }
1795 .Vb 1
1796 \&    if (exists &{$ref->{A}{B}{$key}})   { }
1799 Although the deepest nested array or hash will not spring into existence
1800 just because its existence was tested, any intervening ones will.
1801 Thus \f(CW\*(C`$ref\->{"A"}\*(C'\fR and \f(CW\*(C`$ref\->{"A"}\->{"B"}\*(C'\fR will spring
1802 into existence due to the existence test for the \f(CW$key\fR element above.
1803 This happens anywhere the arrow operator is used, including even:
1805 .Vb 3
1806 \&    undef $ref;
1807 \&    if (exists $ref->{"Some key"})      { }
1808 \&    print $ref;             # prints HASH(0x80d3d5c)
1811 This surprising autovivification in what does not at first\*(--or even
1812 second\*(--glance appear to be an lvalue context may be fixed in a future
1813 release.
1815 See \*(L"Pseudo\-hashes: Using an array as a hash\*(R" in perlref for specifics
1816 on how \fIexists()\fR acts when used on a pseudo\-hash.
1818 Use of a subroutine call, rather than a subroutine name, as an argument
1819 to \fIexists()\fR is an error.
1821 .Vb 2
1822 \&    exists &sub;        # OK
1823 \&    exists &sub();      # Error
1825 .IP "exit \s-1EXPR\s0" 8
1826 .IX Item "exit EXPR"
1827 Evaluates \s-1EXPR\s0 and exits immediately with that value.    Example:
1829 .Vb 2
1830 \&    $ans = <STDIN>;
1831 \&    exit 0 if $ans =~ /^[Xx]/;
1834 See also \f(CW\*(C`die\*(C'\fR.  If \s-1EXPR\s0 is omitted, exits with \f(CW0\fR status.  The only
1835 universally recognized values for \s-1EXPR\s0 are \f(CW0\fR for success and \f(CW1\fR
1836 for error; other values are subject to interpretation depending on the
1837 environment in which the Perl program is running.  For example, exiting
1838 69 (\s-1EX_UNAVAILABLE\s0) from a \fIsendmail\fR incoming-mail filter will cause
1839 the mailer to return the item undelivered, but that's not true everywhere.
1841 Don't use \f(CW\*(C`exit\*(C'\fR to abort a subroutine if there's any chance that
1842 someone might want to trap whatever error happened.  Use \f(CW\*(C`die\*(C'\fR instead,
1843 which can be trapped by an \f(CW\*(C`eval\*(C'\fR.
1845 The \fIexit()\fR function does not always exit immediately.  It calls any
1846 defined \f(CW\*(C`END\*(C'\fR routines first, but these \f(CW\*(C`END\*(C'\fR routines may not
1847 themselves abort the exit.  Likewise any object destructors that need to
1848 be called are called before the real exit.  If this is a problem, you
1849 can call \f(CW\*(C`POSIX:_exit($status)\*(C'\fR to avoid \s-1END\s0 and destructor processing.
1850 See perlmod for details.
1851 .IP "exp \s-1EXPR\s0" 8
1852 .IX Item "exp EXPR"
1853 .PD 0
1854 .IP "exp" 8
1855 .IX Item "exp"
1857 Returns \fIe\fR (the natural logarithm base) to the power of \s-1EXPR\s0.
1858 If \s-1EXPR\s0 is omitted, gives \f(CW\*(C`exp($_)\*(C'\fR.
1859 .IP "fcntl \s-1FILEHANDLE\s0,FUNCTION,SCALAR" 8
1860 .IX Item "fcntl FILEHANDLE,FUNCTION,SCALAR"
1861 Implements the \fIfcntl\fR\|(2) function.  You'll probably have to say
1863 .Vb 1
1864 \&    use Fcntl;
1867 first to get the correct constant definitions.  Argument processing and
1868 value return works just like \f(CW\*(C`ioctl\*(C'\fR below.
1869 For example:
1871 .Vb 3
1872 \&    use Fcntl;
1873 \&    fcntl($filehandle, F_GETFL, $packed_return_buffer)
1874 \&        or die "can't fcntl F_GETFL: $!";
1877 You don't have to check for \f(CW\*(C`defined\*(C'\fR on the return from \f(CW\*(C`fcntl\*(C'\fR.
1878 Like \f(CW\*(C`ioctl\*(C'\fR, it maps a \f(CW0\fR return from the system call into
1879 \&\f(CW"0 but true"\fR in Perl.  This string is true in boolean context and \f(CW0\fR
1880 in numeric context.  It is also exempt from the normal \fB\-w\fR warnings
1881 on improper numeric conversions.
1883 Note that \f(CW\*(C`fcntl\*(C'\fR will produce a fatal error if used on a machine that
1884 doesn't implement \fIfcntl\fR\|(2).  See the Fcntl module or your \fIfcntl\fR\|(2)
1885 manpage to learn what functions are available on your system.
1886 .IP "fileno \s-1FILEHANDLE\s0" 8
1887 .IX Item "fileno FILEHANDLE"
1888 Returns the file descriptor for a filehandle, or undefined if the
1889 filehandle is not open.  This is mainly useful for constructing
1890 bitmaps for \f(CW\*(C`select\*(C'\fR and low-level \s-1POSIX\s0 tty-handling operations.
1891 If \s-1FILEHANDLE\s0 is an expression, the value is taken as an indirect
1892 filehandle, generally its name.
1894 You can use this to find out whether two handles refer to the
1895 same underlying descriptor:
1897 .Vb 3
1898 \&    if (fileno(THIS) == fileno(THAT)) {
1899 \&        print "THIS and THAT are dups\en";
1900 \&    }
1903 (Filehandles connected to memory objects via new features of \f(CW\*(C`open\*(C'\fR may
1904 return undefined even though they are open.)
1905 .IP "flock \s-1FILEHANDLE\s0,OPERATION" 8
1906 .IX Item "flock FILEHANDLE,OPERATION"
1907 Calls \fIflock\fR\|(2), or an emulation of it, on \s-1FILEHANDLE\s0.  Returns true
1908 for success, false on failure.  Produces a fatal error if used on a
1909 machine that doesn't implement \fIflock\fR\|(2), \fIfcntl\fR\|(2) locking, or \fIlockf\fR\|(3).
1910 \&\f(CW\*(C`flock\*(C'\fR is Perl's portable file locking interface, although it locks
1911 only entire files, not records.
1913 Two potentially non-obvious but traditional \f(CW\*(C`flock\*(C'\fR semantics are
1914 that it waits indefinitely until the lock is granted, and that its locks
1915 \&\fBmerely advisory\fR.  Such discretionary locks are more flexible, but offer
1916 fewer guarantees.  This means that files locked with \f(CW\*(C`flock\*(C'\fR may be
1917 modified by programs that do not also use \f(CW\*(C`flock\*(C'\fR.  See perlport,
1918 your port's specific documentation, or your system-specific local manpages
1919 for details.  It's best to assume traditional behavior if you're writing
1920 portable programs.  (But if you're not, you should as always feel perfectly
1921 free to write for your own system's idiosyncrasies (sometimes called
1922 \&\*(L"features\*(R").  Slavish adherence to portability concerns shouldn't get
1923 in the way of your getting your job done.)
1925 \&\s-1OPERATION\s0 is one of \s-1LOCK_SH\s0, \s-1LOCK_EX\s0, or \s-1LOCK_UN\s0, possibly combined with
1926 \&\s-1LOCK_NB\s0.  These constants are traditionally valued 1, 2, 8 and 4, but
1927 you can use the symbolic names if you import them from the Fcntl module,
1928 either individually, or as a group using the ':flock' tag.  \s-1LOCK_SH\s0
1929 requests a shared lock, \s-1LOCK_EX\s0 requests an exclusive lock, and \s-1LOCK_UN\s0
1930 releases a previously requested lock.  If \s-1LOCK_NB\s0 is bitwise\-or'ed with
1931 \&\s-1LOCK_SH\s0 or \s-1LOCK_EX\s0 then \f(CW\*(C`flock\*(C'\fR will return immediately rather than blocking
1932 waiting for the lock (check the return status to see if you got it).
1934 To avoid the possibility of miscoordination, Perl now flushes \s-1FILEHANDLE\s0
1935 before locking or unlocking it.
1937 Note that the emulation built with \fIlockf\fR\|(3) doesn't provide shared
1938 locks, and it requires that \s-1FILEHANDLE\s0 be open with write intent.  These
1939 are the semantics that \fIlockf\fR\|(3) implements.  Most if not all systems
1940 implement \fIlockf\fR\|(3) in terms of \fIfcntl\fR\|(2) locking, though, so the
1941 differing semantics shouldn't bite too many people.
1943 Note that the \fIfcntl\fR\|(2) emulation of \fIflock\fR\|(3) requires that \s-1FILEHANDLE\s0
1944 be open with read intent to use \s-1LOCK_SH\s0 and requires that it be open
1945 with write intent to use \s-1LOCK_EX\s0.
1947 Note also that some versions of \f(CW\*(C`flock\*(C'\fR cannot lock things over the
1948 network; you would need to use the more system-specific \f(CW\*(C`fcntl\*(C'\fR for
1949 that.  If you like you can force Perl to ignore your system's \fIflock\fR\|(2)
1950 function, and so provide its own \fIfcntl\fR\|(2)\-based emulation, by passing
1951 the switch \f(CW\*(C`\-Ud_flock\*(C'\fR to the \fIConfigure\fR program when you configure
1952 perl.
1954 Here's a mailbox appender for \s-1BSD\s0 systems.
1956 .Vb 1
1957 \&    use Fcntl ':flock'; # import LOCK_* constants
1960 .Vb 6
1961 \&    sub lock {
1962 \&        flock(MBOX,LOCK_EX);
1963 \&        # and, in case someone appended
1964 \&        # while we were waiting...
1965 \&        seek(MBOX, 0, 2);
1966 \&    }
1969 .Vb 3
1970 \&    sub unlock {
1971 \&        flock(MBOX,LOCK_UN);
1972 \&    }
1975 .Vb 2
1976 \&    open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
1977 \&            or die "Can't open mailbox: $!";
1980 .Vb 3
1981 \&    lock();
1982 \&    print MBOX $msg,"\en\en";
1983 \&    unlock();
1986 On systems that support a real \fIflock()\fR, locks are inherited across \fIfork()\fR
1987 calls, whereas those that must resort to the more capricious \fIfcntl()\fR
1988 function lose the locks, making it harder to write servers.
1990 See also DB_File for other \fIflock()\fR examples.
1991 .IP "fork" 8
1992 .IX Item "fork"
1993 Does a \fIfork\fR\|(2) system call to create a new process running the
1994 same program at the same point.  It returns the child pid to the
1995 parent process, \f(CW0\fR to the child process, or \f(CW\*(C`undef\*(C'\fR if the fork is
1996 unsuccessful.  File descriptors (and sometimes locks on those descriptors)
1997 are shared, while everything else is copied.  On most systems supporting
1998 \&\fIfork()\fR, great care has gone into making it extremely efficient (for
1999 example, using copy-on-write technology on data pages), making it the
2000 dominant paradigm for multitasking over the last few decades.
2002 Beginning with v5.6.0, Perl will attempt to flush all files opened for
2003 output before forking the child process, but this may not be supported
2004 on some platforms (see perlport).  To be safe, you may need to set
2005 \&\f(CW$|\fR ($AUTOFLUSH in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method of
2006 \&\f(CW\*(C`IO::Handle\*(C'\fR on any open handles in order to avoid duplicate output.
2008 If you \f(CW\*(C`fork\*(C'\fR without ever waiting on your children, you will
2009 accumulate zombies.  On some systems, you can avoid this by setting
2010 \&\f(CW$SIG{CHLD}\fR to \f(CW"IGNORE"\fR.  See also perlipc for more examples of
2011 forking and reaping moribund children.
2013 Note that if your forked child inherits system file descriptors like
2014 \&\s-1STDIN\s0 and \s-1STDOUT\s0 that are actually connected by a pipe or socket, even
2015 if you exit, then the remote server (such as, say, a \s-1CGI\s0 script or a
2016 backgrounded job launched from a remote shell) won't think you're done.
2017 You should reopen those to \fI/dev/null\fR if it's any issue.
2018 .IP "format" 8
2019 .IX Item "format"
2020 Declare a picture format for use by the \f(CW\*(C`write\*(C'\fR function.  For
2021 example:
2023 .Vb 4
2024 \&    format Something =
2025 \&        Test: @<<<<<<<< @||||| @>>>>>
2026 \&              $str,     $%,    '$' . int($num)
2027 \&    .
2030 .Vb 4
2031 \&    $str = "widget";
2032 \&    $num = $cost/$quantity;
2033 \&    $~ = 'Something';
2034 \&    write;
2037 See perlform for many details and examples.
2038 .IP "formline \s-1PICTURE\s0,LIST" 8
2039 .IX Item "formline PICTURE,LIST"
2040 This is an internal function used by \f(CW\*(C`format\*(C'\fRs, though you may call it,
2041 too.  It formats (see perlform) a list of values according to the
2042 contents of \s-1PICTURE\s0, placing the output into the format output
2043 accumulator, \f(CW$^A\fR (or \f(CW$ACCUMULATOR\fR in English).
2044 Eventually, when a \f(CW\*(C`write\*(C'\fR is done, the contents of
2045 \&\f(CW$^A\fR are written to some filehandle, but you could also read \f(CW$^A\fR
2046 yourself and then set \f(CW$^A\fR back to \f(CW""\fR.  Note that a format typically
2047 does one \f(CW\*(C`formline\*(C'\fR per line of form, but the \f(CW\*(C`formline\*(C'\fR function itself
2048 doesn't care how many newlines are embedded in the \s-1PICTURE\s0.  This means
2049 that the \f(CW\*(C`~\*(C'\fR and \f(CW\*(C`~~\*(C'\fR tokens will treat the entire \s-1PICTURE\s0 as a single line.
2050 You may therefore need to use multiple formlines to implement a single
2051 record format, just like the format compiler.
2053 Be careful if you put double quotes around the picture, because an \f(CW\*(C`@\*(C'\fR
2054 character may be taken to mean the beginning of an array name.
2055 \&\f(CW\*(C`formline\*(C'\fR always returns true.  See perlform for other examples.
2056 .IP "getc \s-1FILEHANDLE\s0" 8
2057 .IX Item "getc FILEHANDLE"
2058 .PD 0
2059 .IP "getc" 8
2060 .IX Item "getc"
2062 Returns the next character from the input file attached to \s-1FILEHANDLE\s0,
2063 or the undefined value at end of file, or if there was an error (in
2064 the latter case \f(CW$!\fR is set).  If \s-1FILEHANDLE\s0 is omitted, reads from
2065 \&\s-1STDIN\s0.  This is not particularly efficient.  However, it cannot be
2066 used by itself to fetch single characters without waiting for the user
2067 to hit enter.  For that, try something more like:
2069 .Vb 6
2070 \&    if ($BSD_STYLE) {
2071 \&        system "stty cbreak </dev/tty >/dev/tty 2>&1";
2072 \&    }
2073 \&    else {
2074 \&        system "stty", '-icanon', 'eol', "\e001";
2075 \&    }
2078 .Vb 1
2079 \&    $key = getc(STDIN);
2082 .Vb 7
2083 \&    if ($BSD_STYLE) {
2084 \&        system "stty -cbreak </dev/tty >/dev/tty 2>&1";
2085 \&    }
2086 \&    else {
2087 \&        system "stty", 'icanon', 'eol', '^@'; # ASCII null
2088 \&    }
2089 \&    print "\en";
2092 Determination of whether \f(CW$BSD_STYLE\fR should be set
2093 is left as an exercise to the reader.
2095 The \f(CW\*(C`POSIX::getattr\*(C'\fR function can do this more portably on
2096 systems purporting \s-1POSIX\s0 compliance.  See also the \f(CW\*(C`Term::ReadKey\*(C'\fR
2097 module from your nearest \s-1CPAN\s0 site; details on \s-1CPAN\s0 can be found on
2098 \&\*(L"\s-1CPAN\s0\*(R" in perlmodlib.
2099 .IP "getlogin" 8
2100 .IX Item "getlogin"
2101 Implements the C library function of the same name, which on most
2102 systems returns the current login from \fI/etc/utmp\fR, if any.  If null,
2103 use \f(CW\*(C`getpwuid\*(C'\fR.
2105 .Vb 1
2106 \&    $login = getlogin || getpwuid($<) || "Kilroy";
2109 Do not consider \f(CW\*(C`getlogin\*(C'\fR for authentication: it is not as
2110 secure as \f(CW\*(C`getpwuid\*(C'\fR.
2111 .IP "getpeername \s-1SOCKET\s0" 8
2112 .IX Item "getpeername SOCKET"
2113 Returns the packed sockaddr address of other end of the \s-1SOCKET\s0 connection.
2115 .Vb 5
2116 \&    use Socket;
2117 \&    $hersockaddr    = getpeername(SOCK);
2118 \&    ($port, $iaddr) = sockaddr_in($hersockaddr);
2119 \&    $herhostname    = gethostbyaddr($iaddr, AF_INET);
2120 \&    $herstraddr     = inet_ntoa($iaddr);
2122 .IP "getpgrp \s-1PID\s0" 8
2123 .IX Item "getpgrp PID"
2124 Returns the current process group for the specified \s-1PID\s0.  Use
2125 a \s-1PID\s0 of \f(CW0\fR to get the current process group for the
2126 current process.  Will raise an exception if used on a machine that
2127 doesn't implement \fIgetpgrp\fR\|(2).  If \s-1PID\s0 is omitted, returns process
2128 group of current process.  Note that the \s-1POSIX\s0 version of \f(CW\*(C`getpgrp\*(C'\fR
2129 does not accept a \s-1PID\s0 argument, so only \f(CW\*(C`PID==0\*(C'\fR is truly portable.
2130 .IP "getppid" 8
2131 .IX Item "getppid"
2132 Returns the process id of the parent process.
2134 Note for Linux users: on Linux, the C functions \f(CW\*(C`getpid()\*(C'\fR and
2135 \&\f(CW\*(C`getppid()\*(C'\fR return different values from different threads. In order to
2136 be portable, this behavior is not reflected by the perl-level function
2137 \&\f(CW\*(C`getppid()\*(C'\fR, that returns a consistent value across threads. If you want
2138 to call the underlying \f(CW\*(C`getppid()\*(C'\fR, you may use the \s-1CPAN\s0 module
2139 \&\f(CW\*(C`Linux::Pid\*(C'\fR.
2140 .IP "getpriority \s-1WHICH\s0,WHO" 8
2141 .IX Item "getpriority WHICH,WHO"
2142 Returns the current priority for a process, a process group, or a user.
2143 (See \fIgetpriority\fR\|(2).)  Will raise a fatal exception if used on a
2144 machine that doesn't implement \fIgetpriority\fR\|(2).
2145 .IP "getpwnam \s-1NAME\s0" 8
2146 .IX Item "getpwnam NAME"
2147 .PD 0
2148 .IP "getgrnam \s-1NAME\s0" 8
2149 .IX Item "getgrnam NAME"
2150 .IP "gethostbyname \s-1NAME\s0" 8
2151 .IX Item "gethostbyname NAME"
2152 .IP "getnetbyname \s-1NAME\s0" 8
2153 .IX Item "getnetbyname NAME"
2154 .IP "getprotobyname \s-1NAME\s0" 8
2155 .IX Item "getprotobyname NAME"
2156 .IP "getpwuid \s-1UID\s0" 8
2157 .IX Item "getpwuid UID"
2158 .IP "getgrgid \s-1GID\s0" 8
2159 .IX Item "getgrgid GID"
2160 .IP "getservbyname \s-1NAME\s0,PROTO" 8
2161 .IX Item "getservbyname NAME,PROTO"
2162 .IP "gethostbyaddr \s-1ADDR\s0,ADDRTYPE" 8
2163 .IX Item "gethostbyaddr ADDR,ADDRTYPE"
2164 .IP "getnetbyaddr \s-1ADDR\s0,ADDRTYPE" 8
2165 .IX Item "getnetbyaddr ADDR,ADDRTYPE"
2166 .IP "getprotobynumber \s-1NUMBER\s0" 8
2167 .IX Item "getprotobynumber NUMBER"
2168 .IP "getservbyport \s-1PORT\s0,PROTO" 8
2169 .IX Item "getservbyport PORT,PROTO"
2170 .IP "getpwent" 8
2171 .IX Item "getpwent"
2172 .IP "getgrent" 8
2173 .IX Item "getgrent"
2174 .IP "gethostent" 8
2175 .IX Item "gethostent"
2176 .IP "getnetent" 8
2177 .IX Item "getnetent"
2178 .IP "getprotoent" 8
2179 .IX Item "getprotoent"
2180 .IP "getservent" 8
2181 .IX Item "getservent"
2182 .IP "setpwent" 8
2183 .IX Item "setpwent"
2184 .IP "setgrent" 8
2185 .IX Item "setgrent"
2186 .IP "sethostent \s-1STAYOPEN\s0" 8
2187 .IX Item "sethostent STAYOPEN"
2188 .IP "setnetent \s-1STAYOPEN\s0" 8
2189 .IX Item "setnetent STAYOPEN"
2190 .IP "setprotoent \s-1STAYOPEN\s0" 8
2191 .IX Item "setprotoent STAYOPEN"
2192 .IP "setservent \s-1STAYOPEN\s0" 8
2193 .IX Item "setservent STAYOPEN"
2194 .IP "endpwent" 8
2195 .IX Item "endpwent"
2196 .IP "endgrent" 8
2197 .IX Item "endgrent"
2198 .IP "endhostent" 8
2199 .IX Item "endhostent"
2200 .IP "endnetent" 8
2201 .IX Item "endnetent"
2202 .IP "endprotoent" 8
2203 .IX Item "endprotoent"
2204 .IP "endservent" 8
2205 .IX Item "endservent"
2207 These routines perform the same functions as their counterparts in the
2208 system library.  In list context, the return values from the
2209 various get routines are as follows:
2211 .Vb 7
2212 \&    ($name,$passwd,$uid,$gid,
2213 \&       $quota,$comment,$gcos,$dir,$shell,$expire) = getpw*
2214 \&    ($name,$passwd,$gid,$members) = getgr*
2215 \&    ($name,$aliases,$addrtype,$length,@addrs) = gethost*
2216 \&    ($name,$aliases,$addrtype,$net) = getnet*
2217 \&    ($name,$aliases,$proto) = getproto*
2218 \&    ($name,$aliases,$port,$proto) = getserv*
2221 (If the entry doesn't exist you get a null list.)
2223 The exact meaning of the \f(CW$gcos\fR field varies but it usually contains
2224 the real name of the user (as opposed to the login name) and other
2225 information pertaining to the user.  Beware, however, that in many
2226 system users are able to change this information and therefore it
2227 cannot be trusted and therefore the \f(CW$gcos\fR is tainted (see
2228 perlsec).  The \f(CW$passwd\fR and \f(CW$shell\fR, user's encrypted password and
2229 login shell, are also tainted, because of the same reason.
2231 In scalar context, you get the name, unless the function was a
2232 lookup by name, in which case you get the other thing, whatever it is.
2233 (If the entry doesn't exist you get the undefined value.)  For example:
2235 .Vb 7
2236 \&    $uid   = getpwnam($name);
2237 \&    $name  = getpwuid($num);
2238 \&    $name  = getpwent();
2239 \&    $gid   = getgrnam($name);
2240 \&    $name  = getgrgid($num);
2241 \&    $name  = getgrent();
2242 \&    #etc.
2245 In \fIgetpw*()\fR the fields \f(CW$quota\fR, \f(CW$comment\fR, and \f(CW$expire\fR are special
2246 cases in the sense that in many systems they are unsupported.  If the
2247 \&\f(CW$quota\fR is unsupported, it is an empty scalar.  If it is supported, it
2248 usually encodes the disk quota.  If the \f(CW$comment\fR field is unsupported,
2249 it is an empty scalar.  If it is supported it usually encodes some
2250 administrative comment about the user.  In some systems the \f(CW$quota\fR
2251 field may be \f(CW$change\fR or \f(CW$age\fR, fields that have to do with password
2252 aging.  In some systems the \f(CW$comment\fR field may be \f(CW$class\fR.  The \f(CW$expire\fR
2253 field, if present, encodes the expiration period of the account or the
2254 password.  For the availability and the exact meaning of these fields
2255 in your system, please consult your \fIgetpwnam\fR\|(3) documentation and your
2256 \&\fIpwd.h\fR file.  You can also find out from within Perl what your
2257 \&\f(CW$quota\fR and \f(CW$comment\fR fields mean and whether you have the \f(CW$expire\fR field
2258 by using the \f(CW\*(C`Config\*(C'\fR module and the values \f(CW\*(C`d_pwquota\*(C'\fR, \f(CW\*(C`d_pwage\*(C'\fR,
2259 \&\f(CW\*(C`d_pwchange\*(C'\fR, \f(CW\*(C`d_pwcomment\*(C'\fR, and \f(CW\*(C`d_pwexpire\*(C'\fR.  Shadow password
2260 files are only supported if your vendor has implemented them in the
2261 intuitive fashion that calling the regular C library routines gets the
2262 shadow versions if you're running under privilege or if there exists
2263 the \fIshadow\fR\|(3) functions as found in System V ( this includes Solaris
2264 and Linux.)  Those systems which implement a proprietary shadow password
2265 facility are unlikely to be supported.
2267 The \f(CW$members\fR value returned by \fIgetgr*()\fR is a space separated list of
2268 the login names of the members of the group.
2270 For the \fIgethost*()\fR functions, if the \f(CW\*(C`h_errno\*(C'\fR variable is supported in
2271 C, it will be returned to you via \f(CW$?\fR if the function call fails.  The
2272 \&\f(CW@addrs\fR value returned by a successful call is a list of the raw
2273 addresses returned by the corresponding system library call.  In the
2274 Internet domain, each address is four bytes long and you can unpack it
2275 by saying something like:
2277 .Vb 1
2278 \&    ($a,$b,$c,$d) = unpack('C4',$addr[0]);
2281 The Socket library makes this slightly easier:
2283 .Vb 3
2284 \&    use Socket;
2285 \&    $iaddr = inet_aton("127.1"); # or whatever address
2286 \&    $name  = gethostbyaddr($iaddr, AF_INET);
2289 .Vb 2
2290 \&    # or going the other way
2291 \&    $straddr = inet_ntoa($iaddr);
2294 If you get tired of remembering which element of the return list
2295 contains which return value, by-name interfaces are provided
2296 in standard modules: \f(CW\*(C`File::stat\*(C'\fR, \f(CW\*(C`Net::hostent\*(C'\fR, \f(CW\*(C`Net::netent\*(C'\fR,
2297 \&\f(CW\*(C`Net::protoent\*(C'\fR, \f(CW\*(C`Net::servent\*(C'\fR, \f(CW\*(C`Time::gmtime\*(C'\fR, \f(CW\*(C`Time::localtime\*(C'\fR,
2298 and \f(CW\*(C`User::grent\*(C'\fR.  These override the normal built\-ins, supplying
2299 versions that return objects with the appropriate names
2300 for each field.  For example:
2302 .Vb 3
2303 \&   use File::stat;
2304 \&   use User::pwent;
2305 \&   $is_his = (stat($filename)->uid == pwent($whoever)->uid);
2308 Even though it looks like they're the same method calls (uid),
2309 they aren't, because a \f(CW\*(C`File::stat\*(C'\fR object is different from
2310 a \f(CW\*(C`User::pwent\*(C'\fR object.
2311 .IP "getsockname \s-1SOCKET\s0" 8
2312 .IX Item "getsockname SOCKET"
2313 Returns the packed sockaddr address of this end of the \s-1SOCKET\s0 connection,
2314 in case you don't know the address because you have several different
2315 IPs that the connection might have come in on.
2317 .Vb 6
2318 \&    use Socket;
2319 \&    $mysockaddr = getsockname(SOCK);
2320 \&    ($port, $myaddr) = sockaddr_in($mysockaddr);
2321 \&    printf "Connect to %s [%s]\en",
2322 \&       scalar gethostbyaddr($myaddr, AF_INET),
2323 \&       inet_ntoa($myaddr);
2325 .IP "getsockopt \s-1SOCKET\s0,LEVEL,OPTNAME" 8
2326 .IX Item "getsockopt SOCKET,LEVEL,OPTNAME"
2327 Returns the socket option requested, or undef if there is an error.
2328 .IP "glob \s-1EXPR\s0" 8
2329 .IX Item "glob EXPR"
2330 .PD 0
2331 .IP "glob" 8
2332 .IX Item "glob"
2334 In list context, returns a (possibly empty) list of filename expansions on
2335 the value of \s-1EXPR\s0 such as the standard Unix shell \fI/bin/csh\fR would do. In
2336 scalar context, glob iterates through such filename expansions, returning
2337 undef when the list is exhausted. This is the internal function
2338 implementing the \f(CW\*(C`<*.c>\*(C'\fR operator, but you can use it directly. If
2339 \&\s-1EXPR\s0 is omitted, \f(CW$_\fR is used.  The \f(CW\*(C`<*.c>\*(C'\fR operator is discussed in
2340 more detail in \*(L"I/O Operators\*(R" in perlop.
2342 Beginning with v5.6.0, this operator is implemented using the standard
2343 \&\f(CW\*(C`File::Glob\*(C'\fR extension.  See File::Glob for details.
2344 .IP "gmtime \s-1EXPR\s0" 8
2345 .IX Item "gmtime EXPR"
2346 Converts a time as returned by the time function to an 8\-element list
2347 with the time localized for the standard Greenwich time zone.
2348 Typically used as follows:
2350 .Vb 3
2351 \&    #  0    1    2     3     4    5     6     7
2352 \&    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =
2353 \&                                            gmtime(time);
2356 All list elements are numeric, and come straight out of the C `struct
2357 tm'.  \f(CW$sec\fR, \f(CW$min\fR, and \f(CW$hour\fR are the seconds, minutes, and hours of the
2358 specified time.  \f(CW$mday\fR is the day of the month, and \f(CW$mon\fR is the month
2359 itself, in the range \f(CW0..11\fR with 0 indicating January and 11
2360 indicating December.  \f(CW$year\fR is the number of years since 1900.  That
2361 is, \f(CW$year\fR is \f(CW123\fR in year 2023.  \f(CW$wday\fR is the day of the week, with
2362 0 indicating Sunday and 3 indicating Wednesday.  \f(CW$yday\fR is the day of
2363 the year, in the range \f(CW0..364\fR (or \f(CW0..365\fR in leap years.)
2365 Note that the \f(CW$year\fR element is \fInot\fR simply the last two digits of
2366 the year.  If you assume it is, then you create non\-Y2K\-compliant
2367 programs\*(--and you wouldn't want to do that, would you?
2369 The proper way to get a complete 4\-digit year is simply:
2371 .Vb 1
2372 \&        $year += 1900;
2375 And to get the last two digits of the year (e.g., '01' in 2001) do:
2377 .Vb 1
2378 \&        $year = sprintf("%02d", $year % 100);
2381 If \s-1EXPR\s0 is omitted, \f(CW\*(C`gmtime()\*(C'\fR uses the current time (\f(CW\*(C`gmtime(time)\*(C'\fR).
2383 In scalar context, \f(CW\*(C`gmtime()\*(C'\fR returns the \fIctime\fR\|(3) value:
2385 .Vb 1
2386 \&    $now_string = gmtime;  # e.g., "Thu Oct 13 04:54:34 1994"
2389 Also see the \f(CW\*(C`timegm\*(C'\fR function provided by the \f(CW\*(C`Time::Local\*(C'\fR module,
2390 and the \fIstrftime\fR\|(3) function available via the \s-1POSIX\s0 module.
2392 This scalar value is \fBnot\fR locale dependent (see perllocale), but
2393 is instead a Perl builtin.  Also see the \f(CW\*(C`Time::Local\*(C'\fR module, and the
2394 \&\fIstrftime\fR\|(3) and \fImktime\fR\|(3) functions available via the \s-1POSIX\s0 module.  To
2395 get somewhat similar but locale dependent date strings, set up your
2396 locale environment variables appropriately (please see perllocale)
2397 and try for example:
2399 .Vb 2
2400 \&    use POSIX qw(strftime);
2401 \&    $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;
2404 Note that the \f(CW%a\fR and \f(CW%b\fR escapes, which represent the short forms
2405 of the day of the week and the month of the year, may not necessarily
2406 be three characters wide in all locales.
2407 .IP "goto \s-1LABEL\s0" 8
2408 .IX Item "goto LABEL"
2409 .PD 0
2410 .IP "goto \s-1EXPR\s0" 8
2411 .IX Item "goto EXPR"
2412 .IP "goto &NAME" 8
2413 .IX Item "goto &NAME"
2415 The \f(CW\*(C`goto\-LABEL\*(C'\fR form finds the statement labeled with \s-1LABEL\s0 and resumes
2416 execution there.  It may not be used to go into any construct that
2417 requires initialization, such as a subroutine or a \f(CW\*(C`foreach\*(C'\fR loop.  It
2418 also can't be used to go into a construct that is optimized away,
2419 or to get out of a block or subroutine given to \f(CW\*(C`sort\*(C'\fR.
2420 It can be used to go almost anywhere else within the dynamic scope,
2421 including out of subroutines, but it's usually better to use some other
2422 construct such as \f(CW\*(C`last\*(C'\fR or \f(CW\*(C`die\*(C'\fR.  The author of Perl has never felt the
2423 need to use this form of \f(CW\*(C`goto\*(C'\fR (in Perl, that is\*(--C is another matter).
2424 (The difference being that C does not offer named loops combined with
2425 loop control.  Perl does, and this replaces most structured uses of \f(CW\*(C`goto\*(C'\fR
2426 in other languages.)
2428 The \f(CW\*(C`goto\-EXPR\*(C'\fR form expects a label name, whose scope will be resolved
2429 dynamically.  This allows for computed \f(CW\*(C`goto\*(C'\fRs per \s-1FORTRAN\s0, but isn't
2430 necessarily recommended if you're optimizing for maintainability:
2432 .Vb 1
2433 \&    goto ("FOO", "BAR", "GLARCH")[$i];
2436 The \f(CW\*(C`goto\-&NAME\*(C'\fR form is quite different from the other forms of
2437 \&\f(CW\*(C`goto\*(C'\fR.  In fact, it isn't a goto in the normal sense at all, and
2438 doesn't have the stigma associated with other gotos.  Instead, it
2439 exits the current subroutine (losing any changes set by \fIlocal()\fR) and
2440 immediately calls in its place the named subroutine using the current
2441 value of \f(CW@_\fR.  This is used by \f(CW\*(C`AUTOLOAD\*(C'\fR subroutines that wish to
2442 load another subroutine and then pretend that the other subroutine had
2443 been called in the first place (except that any modifications to \f(CW@_\fR
2444 in the current subroutine are propagated to the other subroutine.)
2445 After the \f(CW\*(C`goto\*(C'\fR, not even \f(CW\*(C`caller\*(C'\fR will be able to tell that this
2446 routine was called first.
2448 \&\s-1NAME\s0 needn't be the name of a subroutine; it can be a scalar variable
2449 containing a code reference, or a block which evaluates to a code
2450 reference.
2451 .IP "grep \s-1BLOCK\s0 \s-1LIST\s0" 8
2452 .IX Item "grep BLOCK LIST"
2453 .PD 0
2454 .IP "grep \s-1EXPR\s0,LIST" 8
2455 .IX Item "grep EXPR,LIST"
2457 This is similar in spirit to, but not the same as, \fIgrep\fR\|(1) and its
2458 relatives.  In particular, it is not limited to using regular expressions.
2460 Evaluates the \s-1BLOCK\s0 or \s-1EXPR\s0 for each element of \s-1LIST\s0 (locally setting
2461 \&\f(CW$_\fR to each element) and returns the list value consisting of those
2462 elements for which the expression evaluated to true.  In scalar
2463 context, returns the number of times the expression was true.
2465 .Vb 1
2466 \&    @foo = grep(!/^#/, @bar);    # weed out comments
2469 or equivalently,
2471 .Vb 1
2472 \&    @foo = grep {!/^#/} @bar;    # weed out comments
2475 Note that \f(CW$_\fR is an alias to the list value, so it can be used to
2476 modify the elements of the \s-1LIST\s0.  While this is useful and supported,
2477 it can cause bizarre results if the elements of \s-1LIST\s0 are not variables.
2478 Similarly, grep returns aliases into the original list, much as a for
2479 loop's index variable aliases the list elements.  That is, modifying an
2480 element of a list returned by grep (for example, in a \f(CW\*(C`foreach\*(C'\fR, \f(CW\*(C`map\*(C'\fR
2481 or another \f(CW\*(C`grep\*(C'\fR) actually modifies the element in the original list.
2482 This is usually something to be avoided when writing clear code.
2484 See also \*(L"map\*(R" for a list composed of the results of the \s-1BLOCK\s0 or \s-1EXPR\s0.
2485 .IP "hex \s-1EXPR\s0" 8
2486 .IX Item "hex EXPR"
2487 .PD 0
2488 .IP "hex" 8
2489 .IX Item "hex"
2491 Interprets \s-1EXPR\s0 as a hex string and returns the corresponding value.
2492 (To convert strings that might start with either 0, 0x, or 0b, see
2493 \&\*(L"oct\*(R".)  If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
2495 .Vb 2
2496 \&    print hex '0xAf'; # prints '175'
2497 \&    print hex 'aF';   # same
2500 Hex strings may only represent integers.  Strings that would cause
2501 integer overflow trigger a warning.  Leading whitespace is not stripped,
2502 unlike \fIoct()\fR.
2503 .IP "import" 8
2504 .IX Item "import"
2505 There is no builtin \f(CW\*(C`import\*(C'\fR function.  It is just an ordinary
2506 method (subroutine) defined (or inherited) by modules that wish to export
2507 names to another module.  The \f(CW\*(C`use\*(C'\fR function calls the \f(CW\*(C`import\*(C'\fR method
2508 for the package used.  See also \*(L"use\*(R", perlmod, and Exporter.
2509 .IP "index \s-1STR\s0,SUBSTR,POSITION" 8
2510 .IX Item "index STR,SUBSTR,POSITION"
2511 .PD 0
2512 .IP "index \s-1STR\s0,SUBSTR" 8
2513 .IX Item "index STR,SUBSTR"
2515 The index function searches for one string within another, but without
2516 the wildcard-like behavior of a full regular-expression pattern match.
2517 It returns the position of the first occurrence of \s-1SUBSTR\s0 in \s-1STR\s0 at
2518 or after \s-1POSITION\s0.  If \s-1POSITION\s0 is omitted, starts searching from the
2519 beginning of the string.  The return value is based at \f(CW0\fR (or whatever
2520 you've set the \f(CW$[\fR variable to\*(--but don't do that).  If the substring
2521 is not found, returns one less than the base, ordinarily \f(CW\*(C`\-1\*(C'\fR.
2522 .IP "int \s-1EXPR\s0" 8
2523 .IX Item "int EXPR"
2524 .PD 0
2525 .IP "int" 8
2526 .IX Item "int"
2528 Returns the integer portion of \s-1EXPR\s0.  If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
2529 You should not use this function for rounding: one because it truncates
2530 towards \f(CW0\fR, and two because machine representations of floating point
2531 numbers can sometimes produce counterintuitive results.  For example,
2532 \&\f(CW\*(C`int(\-6.725/0.025)\*(C'\fR produces \-268 rather than the correct \-269; that's
2533 because it's really more like \-268.99999999999994315658 instead.  Usually,
2534 the \f(CW\*(C`sprintf\*(C'\fR, \f(CW\*(C`printf\*(C'\fR, or the \f(CW\*(C`POSIX::floor\*(C'\fR and \f(CW\*(C`POSIX::ceil\*(C'\fR
2535 functions will serve you better than will \fIint()\fR.
2536 .IP "ioctl \s-1FILEHANDLE\s0,FUNCTION,SCALAR" 8
2537 .IX Item "ioctl FILEHANDLE,FUNCTION,SCALAR"
2538 Implements the \fIioctl\fR\|(2) function.  You'll probably first have to say
2540 .Vb 1
2541 \&    require "ioctl.ph"; # probably in /usr/local/lib/perl/ioctl.ph
2544 to get the correct function definitions.  If \fIioctl.ph\fR doesn't
2545 exist or doesn't have the correct definitions you'll have to roll your
2546 own, based on your C header files such as \fI<sys/ioctl.h>\fR.
2547 (There is a Perl script called \fBh2ph\fR that comes with the Perl kit that
2548 may help you in this, but it's nontrivial.)  \s-1SCALAR\s0 will be read and/or
2549 written depending on the FUNCTION\*(--a pointer to the string value of \s-1SCALAR\s0
2550 will be passed as the third argument of the actual \f(CW\*(C`ioctl\*(C'\fR call.  (If \s-1SCALAR\s0
2551 has no string value but does have a numeric value, that value will be
2552 passed rather than a pointer to the string value.  To guarantee this to be
2553 true, add a \f(CW0\fR to the scalar before using it.)  The \f(CW\*(C`pack\*(C'\fR and \f(CW\*(C`unpack\*(C'\fR
2554 functions may be needed to manipulate the values of structures used by
2555 \&\f(CW\*(C`ioctl\*(C'\fR.
2557 The return value of \f(CW\*(C`ioctl\*(C'\fR (and \f(CW\*(C`fcntl\*(C'\fR) is as follows:
2559 .Vb 4
2560 \&        if OS returns:          then Perl returns:
2561 \&            -1                    undefined value
2562 \&             0                  string "0 but true"
2563 \&        anything else               that number
2566 Thus Perl returns true on success and false on failure, yet you can
2567 still easily determine the actual value returned by the operating
2568 system:
2570 .Vb 2
2571 \&    $retval = ioctl(...) || -1;
2572 \&    printf "System returned %d\en", $retval;
2575 The special string "\f(CW0\fR but true" is exempt from \fB\-w\fR complaints
2576 about improper numeric conversions.
2578 Here's an example of setting a filehandle named \f(CW\*(C`REMOTE\*(C'\fR to be
2579 non-blocking at the system level.  You'll have to negotiate \f(CW$|\fR
2580 on your own, though.
2582 .Vb 1
2583 \&    use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
2586 .Vb 2
2587 \&    $flags = fcntl(REMOTE, F_GETFL, 0)
2588 \&                or die "Can't get flags for the socket: $!\en";
2591 .Vb 2
2592 \&    $flags = fcntl(REMOTE, F_SETFL, $flags | O_NONBLOCK)
2593 \&                or die "Can't set flags for the socket: $!\en";
2595 .IP "join \s-1EXPR\s0,LIST" 8
2596 .IX Item "join EXPR,LIST"
2597 Joins the separate strings of \s-1LIST\s0 into a single string with fields
2598 separated by the value of \s-1EXPR\s0, and returns that new string.  Example:
2600 .Vb 1
2601 \&    $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
2604 Beware that unlike \f(CW\*(C`split\*(C'\fR, \f(CW\*(C`join\*(C'\fR doesn't take a pattern as its
2605 first argument.  Compare \*(L"split\*(R".
2606 .IP "keys \s-1HASH\s0" 8
2607 .IX Item "keys HASH"
2608 Returns a list consisting of all the keys of the named hash.
2609 (In scalar context, returns the number of keys.)
2611 The keys are returned in an apparently random order.  The actual
2612 random order is subject to change in future versions of perl, but it
2613 is guaranteed to be the same order as either the \f(CW\*(C`values\*(C'\fR or \f(CW\*(C`each\*(C'\fR
2614 function produces (given that the hash has not been modified).  Since
2615 Perl 5.8.1 the ordering is different even between different runs of
2616 Perl for security reasons (see \*(L"Algorithmic Complexity Attacks\*(R" in perlsec).
2618 As a side effect, calling \fIkeys()\fR resets the \s-1HASH\s0's internal iterator,
2619 see \*(L"each\*(R".
2621 Here is yet another way to print your environment:
2623 .Vb 5
2624 \&    @keys = keys %ENV;
2625 \&    @values = values %ENV;
2626 \&    while (@keys) {
2627 \&        print pop(@keys), '=', pop(@values), "\en";
2628 \&    }
2631 or how about sorted by key:
2633 .Vb 3
2634 \&    foreach $key (sort(keys %ENV)) {
2635 \&        print $key, '=', $ENV{$key}, "\en";
2636 \&    }
2639 The returned values are copies of the original keys in the hash, so
2640 modifying them will not affect the original hash.  Compare \*(L"values\*(R".
2642 To sort a hash by value, you'll need to use a \f(CW\*(C`sort\*(C'\fR function.
2643 Here's a descending numeric sort of a hash by its values:
2645 .Vb 3
2646 \&    foreach $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
2647 \&        printf "%4d %s\en", $hash{$key}, $key;
2648 \&    }
2651 As an lvalue \f(CW\*(C`keys\*(C'\fR allows you to increase the number of hash buckets
2652 allocated for the given hash.  This can gain you a measure of efficiency if
2653 you know the hash is going to get big.  (This is similar to pre-extending
2654 an array by assigning a larger number to $#array.)  If you say
2656 .Vb 1
2657 \&    keys %hash = 200;
2660 then \f(CW%hash\fR will have at least 200 buckets allocated for it\-\-256 of them,
2661 in fact, since it rounds up to the next power of two.  These
2662 buckets will be retained even if you do \f(CW\*(C`%hash = ()\*(C'\fR, use \f(CW\*(C`undef
2663 %hash\*(C'\fR if you want to free the storage while \f(CW%hash\fR is still in scope.
2664 You can't shrink the number of buckets allocated for the hash using
2665 \&\f(CW\*(C`keys\*(C'\fR in this way (but you needn't worry about doing this by accident,
2666 as trying has no effect).
2668 See also \f(CW\*(C`each\*(C'\fR, \f(CW\*(C`values\*(C'\fR and \f(CW\*(C`sort\*(C'\fR.
2669 .IP "kill \s-1SIGNAL\s0, \s-1LIST\s0" 8
2670 .IX Item "kill SIGNAL, LIST"
2671 Sends a signal to a list of processes.  Returns the number of
2672 processes successfully signaled (which is not necessarily the
2673 same as the number actually killed).
2675 .Vb 2
2676 \&    $cnt = kill 1, $child1, $child2;
2677 \&    kill 9, @goners;
2680 If \s-1SIGNAL\s0 is zero, no signal is sent to the process.  This is a
2681 useful way to check that a child process is alive and hasn't changed
2682 its \s-1UID\s0.  See perlport for notes on the portability of this
2683 construct.
2685 Unlike in the shell, if \s-1SIGNAL\s0 is negative, it kills
2686 process groups instead of processes.  (On System V, a negative \fI\s-1PROCESS\s0\fR
2687 number will also kill process groups, but that's not portable.)  That
2688 means you usually want to use positive not negative signals.  You may also
2689 use a signal name in quotes.
2691 See \*(L"Signals\*(R" in perlipc for more details.
2692 .IP "last \s-1LABEL\s0" 8
2693 .IX Item "last LABEL"
2694 .PD 0
2695 .IP "last" 8
2696 .IX Item "last"
2698 The \f(CW\*(C`last\*(C'\fR command is like the \f(CW\*(C`break\*(C'\fR statement in C (as used in
2699 loops); it immediately exits the loop in question.  If the \s-1LABEL\s0 is
2700 omitted, the command refers to the innermost enclosing loop.  The
2701 \&\f(CW\*(C`continue\*(C'\fR block, if any, is not executed:
2703 .Vb 4
2704 \&    LINE: while (<STDIN>) {
2705 \&        last LINE if /^$/;      # exit when done with header
2706 \&        #...
2707 \&    }
2710 \&\f(CW\*(C`last\*(C'\fR cannot be used to exit a block which returns a value such as
2711 \&\f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR or \f(CW\*(C`do {}\*(C'\fR, and should not be used to exit
2712 a \fIgrep()\fR or \fImap()\fR operation.
2714 Note that a block by itself is semantically identical to a loop
2715 that executes once.  Thus \f(CW\*(C`last\*(C'\fR can be used to effect an early
2716 exit out of such a block.
2718 See also \*(L"continue\*(R" for an illustration of how \f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
2719 \&\f(CW\*(C`redo\*(C'\fR work.
2720 .IP "lc \s-1EXPR\s0" 8
2721 .IX Item "lc EXPR"
2722 .PD 0
2723 .IP "lc" 8
2724 .IX Item "lc"
2726 Returns a lowercased version of \s-1EXPR\s0.  This is the internal function
2727 implementing the \f(CW\*(C`\eL\*(C'\fR escape in double-quoted strings.  Respects
2728 current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use locale\*(C'\fR in force.  See perllocale
2729 and perlunicode for more details about locale and Unicode support.
2731 If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
2732 .IP "lcfirst \s-1EXPR\s0" 8
2733 .IX Item "lcfirst EXPR"
2734 .PD 0
2735 .IP "lcfirst" 8
2736 .IX Item "lcfirst"
2738 Returns the value of \s-1EXPR\s0 with the first character lowercased.  This
2739 is the internal function implementing the \f(CW\*(C`\el\*(C'\fR escape in
2740 double-quoted strings.  Respects current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use
2741 locale\*(C'\fR in force.  See perllocale and perlunicode for more
2742 details about locale and Unicode support.
2744 If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
2745 .IP "length \s-1EXPR\s0" 8
2746 .IX Item "length EXPR"
2747 .PD 0
2748 .IP "length" 8
2749 .IX Item "length"
2751 Returns the length in \fIcharacters\fR of the value of \s-1EXPR\s0.  If \s-1EXPR\s0 is
2752 omitted, returns length of \f(CW$_\fR.  Note that this cannot be used on
2753 an entire array or hash to find out how many elements these have.
2754 For that, use \f(CW\*(C`scalar @array\*(C'\fR and \f(CW\*(C`scalar keys %hash\*(C'\fR respectively.
2756 Note the \fIcharacters\fR: if the \s-1EXPR\s0 is in Unicode, you will get the
2757 number of characters, not the number of bytes.  To get the length
2758 in bytes, use \f(CW\*(C`do { use bytes; length(EXPR) }\*(C'\fR, see bytes.
2759 .IP "link \s-1OLDFILE\s0,NEWFILE" 8
2760 .IX Item "link OLDFILE,NEWFILE"
2761 Creates a new filename linked to the old filename.  Returns true for
2762 success, false otherwise.
2763 .IP "listen \s-1SOCKET\s0,QUEUESIZE" 8
2764 .IX Item "listen SOCKET,QUEUESIZE"
2765 Does the same thing that the listen system call does.  Returns true if
2766 it succeeded, false otherwise.  See the example in
2767 \&\*(L"Sockets: Client/Server Communication\*(R" in perlipc.
2768 .IP "local \s-1EXPR\s0" 8
2769 .IX Item "local EXPR"
2770 You really probably want to be using \f(CW\*(C`my\*(C'\fR instead, because \f(CW\*(C`local\*(C'\fR isn't
2771 what most people think of as \*(L"local\*(R".  See
2772 \&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub for details.
2774 A local modifies the listed variables to be local to the enclosing
2775 block, file, or eval.  If more than one value is listed, the list must
2776 be placed in parentheses.  See \*(L"Temporary Values via \fIlocal()\fR\*(R" in perlsub
2777 for details, including issues with tied arrays and hashes.
2778 .IP "localtime \s-1EXPR\s0" 8
2779 .IX Item "localtime EXPR"
2780 Converts a time as returned by the time function to a 9\-element list
2781 with the time analyzed for the local time zone.  Typically used as
2782 follows:
2784 .Vb 3
2785 \&    #  0    1    2     3     4    5     6     7     8
2786 \&    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
2787 \&                                                localtime(time);
2790 All list elements are numeric, and come straight out of the C `struct
2791 tm'.  \f(CW$sec\fR, \f(CW$min\fR, and \f(CW$hour\fR are the seconds, minutes, and hours of the
2792 specified time.  \f(CW$mday\fR is the day of the month, and \f(CW$mon\fR is the month
2793 itself, in the range \f(CW0..11\fR with 0 indicating January and 11
2794 indicating December.  \f(CW$year\fR is the number of years since 1900.  That
2795 is, \f(CW$year\fR is \f(CW123\fR in year 2023.  \f(CW$wday\fR is the day of the week, with
2796 0 indicating Sunday and 3 indicating Wednesday.  \f(CW$yday\fR is the day of
2797 the year, in the range \f(CW0..364\fR (or \f(CW0..365\fR in leap years.)  \f(CW$isdst\fR
2798 is true if the specified time occurs during daylight savings time,
2799 false otherwise.
2801 Note that the \f(CW$year\fR element is \fInot\fR simply the last two digits of
2802 the year.  If you assume it is, then you create non\-Y2K\-compliant
2803 programs\*(--and you wouldn't want to do that, would you?
2805 The proper way to get a complete 4\-digit year is simply:
2807 .Vb 1
2808 \&        $year += 1900;
2811 And to get the last two digits of the year (e.g., '01' in 2001) do:
2813 .Vb 1
2814 \&        $year = sprintf("%02d", $year % 100);
2817 If \s-1EXPR\s0 is omitted, \f(CW\*(C`localtime()\*(C'\fR uses the current time (\f(CW\*(C`localtime(time)\*(C'\fR).
2819 In scalar context, \f(CW\*(C`localtime()\*(C'\fR returns the \fIctime\fR\|(3) value:
2821 .Vb 1
2822 \&    $now_string = localtime;  # e.g., "Thu Oct 13 04:54:34 1994"
2825 This scalar value is \fBnot\fR locale dependent, see perllocale, but
2826 instead a Perl builtin.  Also see the \f(CW\*(C`Time::Local\*(C'\fR module
2827 (to convert the second, minutes, hours, ... back to seconds since the
2828 stroke of midnight the 1st of January 1970, the value returned by
2829 \&\fItime()\fR), and the \fIstrftime\fR\|(3) and \fImktime\fR\|(3) functions available via the
2830 \&\s-1POSIX\s0 module.  To get somewhat similar but locale dependent date
2831 strings, set up your locale environment variables appropriately
2832 (please see perllocale) and try for example:
2834 .Vb 2
2835 \&    use POSIX qw(strftime);
2836 \&    $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
2839 Note that the \f(CW%a\fR and \f(CW%b\fR, the short forms of the day of the week
2840 and the month of the year, may not necessarily be three characters wide.
2841 .IP "lock \s-1THING\s0" 8
2842 .IX Item "lock THING"
2843 This function places an advisory lock on a shared variable, or referenced
2844 object contained in \fI\s-1THING\s0\fR until the lock goes out of scope.
2846 \&\fIlock()\fR is a \*(L"weak keyword\*(R" : this means that if you've defined a function
2847 by this name (before any calls to it), that function will be called
2848 instead. (However, if you've said \f(CW\*(C`use threads\*(C'\fR, \fIlock()\fR is always a
2849 keyword.) See threads.
2850 .IP "log \s-1EXPR\s0" 8
2851 .IX Item "log EXPR"
2852 .PD 0
2853 .IP "log" 8
2854 .IX Item "log"
2856 Returns the natural logarithm (base \fIe\fR) of \s-1EXPR\s0.  If \s-1EXPR\s0 is omitted,
2857 returns log of \f(CW$_\fR.  To get the log of another base, use basic algebra:
2858 The base-N log of a number is equal to the natural log of that number
2859 divided by the natural log of N.  For example:
2861 .Vb 4
2862 \&    sub log10 {
2863 \&        my $n = shift;
2864 \&        return log($n)/log(10);
2865 \&    }
2868 See also \*(L"exp\*(R" for the inverse operation.
2869 .IP "lstat \s-1EXPR\s0" 8
2870 .IX Item "lstat EXPR"
2871 .PD 0
2872 .IP "lstat" 8
2873 .IX Item "lstat"
2875 Does the same thing as the \f(CW\*(C`stat\*(C'\fR function (including setting the
2876 special \f(CW\*(C`_\*(C'\fR filehandle) but stats a symbolic link instead of the file
2877 the symbolic link points to.  If symbolic links are unimplemented on
2878 your system, a normal \f(CW\*(C`stat\*(C'\fR is done.  For much more detailed
2879 information, please see the documentation for \*(L"stat\*(R".
2881 If \s-1EXPR\s0 is omitted, stats \f(CW$_\fR.
2882 .IP "m//" 8
2883 .IX Item "m//"
2884 The match operator.  See perlop.
2885 .IP "map \s-1BLOCK\s0 \s-1LIST\s0" 8
2886 .IX Item "map BLOCK LIST"
2887 .PD 0
2888 .IP "map \s-1EXPR\s0,LIST" 8
2889 .IX Item "map EXPR,LIST"
2891 Evaluates the \s-1BLOCK\s0 or \s-1EXPR\s0 for each element of \s-1LIST\s0 (locally setting
2892 \&\f(CW$_\fR to each element) and returns the list value composed of the
2893 results of each such evaluation.  In scalar context, returns the
2894 total number of elements so generated.  Evaluates \s-1BLOCK\s0 or \s-1EXPR\s0 in
2895 list context, so each element of \s-1LIST\s0 may produce zero, one, or
2896 more elements in the returned value.
2898 .Vb 1
2899 \&    @chars = map(chr, @nums);
2902 translates a list of numbers to the corresponding characters.  And
2904 .Vb 1
2905 \&    %hash = map { getkey($_) => $_ } @array;
2908 is just a funny way to write
2910 .Vb 4
2911 \&    %hash = ();
2912 \&    foreach $_ (@array) {
2913 \&        $hash{getkey($_)} = $_;
2914 \&    }
2917 Note that \f(CW$_\fR is an alias to the list value, so it can be used to
2918 modify the elements of the \s-1LIST\s0.  While this is useful and supported,
2919 it can cause bizarre results if the elements of \s-1LIST\s0 are not variables.
2920 Using a regular \f(CW\*(C`foreach\*(C'\fR loop for this purpose would be clearer in
2921 most cases.  See also \*(L"grep\*(R" for an array composed of those items of
2922 the original list for which the \s-1BLOCK\s0 or \s-1EXPR\s0 evaluates to true.
2924 \&\f(CW\*(C`{\*(C'\fR starts both hash references and blocks, so \f(CW\*(C`map { ...\*(C'\fR could be either
2925 the start of map \s-1BLOCK\s0 \s-1LIST\s0 or map \s-1EXPR\s0, \s-1LIST\s0. Because perl doesn't look
2926 ahead for the closing \f(CW\*(C`}\*(C'\fR it has to take a guess at which its dealing with
2927 based what it finds just after the \f(CW\*(C`{\*(C'\fR. Usually it gets it right, but if it
2928 doesn't it won't realize something is wrong until it gets to the \f(CW\*(C`}\*(C'\fR and
2929 encounters the missing (or unexpected) comma. The syntax error will be
2930 reported close to the \f(CW\*(C`}\*(C'\fR but you'll need to change something near the \f(CW\*(C`{\*(C'\fR
2931 such as using a unary \f(CW\*(C`+\*(C'\fR to give perl some help:
2933 .Vb 5
2934 \&    %hash = map {  "\eL$_", 1  } @array  # perl guesses EXPR.  wrong
2935 \&    %hash = map { +"\eL$_", 1  } @array  # perl guesses BLOCK. right
2936 \&    %hash = map { ("\eL$_", 1) } @array  # this also works
2937 \&    %hash = map {  lc($_), 1  } @array  # as does this.
2938 \&    %hash = map +( lc($_), 1 ), @array  # this is EXPR and works!
2941 .Vb 1
2942 \&    %hash = map  ( lc($_), 1 ), @array  # evaluates to (1, @array)
2945 or to force an anon hash constructor use \f(CW\*(C`+{\*(C'\fR
2947 .Vb 1
2948 \&   @hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end
2951 and you get list of anonymous hashes each with only 1 entry.
2952 .IP "mkdir \s-1FILENAME\s0,MASK" 8
2953 .IX Item "mkdir FILENAME,MASK"
2954 .PD 0
2955 .IP "mkdir \s-1FILENAME\s0" 8
2956 .IX Item "mkdir FILENAME"
2958 Creates the directory specified by \s-1FILENAME\s0, with permissions
2959 specified by \s-1MASK\s0 (as modified by \f(CW\*(C`umask\*(C'\fR).  If it succeeds it
2960 returns true, otherwise it returns false and sets \f(CW$!\fR (errno).
2961 If omitted, \s-1MASK\s0 defaults to 0777.
2963 In general, it is better to create directories with permissive \s-1MASK\s0,
2964 and let the user modify that with their \f(CW\*(C`umask\*(C'\fR, than it is to supply
2965 a restrictive \s-1MASK\s0 and give the user no way to be more permissive.
2966 The exceptions to this rule are when the file or directory should be
2967 kept private (mail files, for instance).  The \fIperlfunc\fR\|(1) entry on
2968 \&\f(CW\*(C`umask\*(C'\fR discusses the choice of \s-1MASK\s0 in more detail.
2970 Note that according to the \s-1POSIX\s0 1003.1\-1996 the \s-1FILENAME\s0 may have any
2971 number of trailing slashes.  Some operating and filesystems do not get
2972 this right, so Perl automatically removes all trailing slashes to keep
2973 everyone happy.
2974 .IP "msgctl \s-1ID\s0,CMD,ARG" 8
2975 .IX Item "msgctl ID,CMD,ARG"
2976 Calls the System V \s-1IPC\s0 function \fImsgctl\fR\|(2).  You'll probably have to say
2978 .Vb 1
2979 \&    use IPC::SysV;
2982 first to get the correct constant definitions.  If \s-1CMD\s0 is \f(CW\*(C`IPC_STAT\*(C'\fR,
2983 then \s-1ARG\s0 must be a variable which will hold the returned \f(CW\*(C`msqid_ds\*(C'\fR
2984 structure.  Returns like \f(CW\*(C`ioctl\*(C'\fR: the undefined value for error,
2985 \&\f(CW"0 but true"\fR for zero, or the actual return value otherwise.  See also
2986 \&\*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, and \f(CW\*(C`IPC::Semaphore\*(C'\fR documentation.
2987 .IP "msgget \s-1KEY\s0,FLAGS" 8
2988 .IX Item "msgget KEY,FLAGS"
2989 Calls the System V \s-1IPC\s0 function \fImsgget\fR\|(2).  Returns the message queue
2990 id, or the undefined value if there is an error.  See also
2991 \&\*(L"SysV \s-1IPC\s0\*(R" in perlipc and \f(CW\*(C`IPC::SysV\*(C'\fR and \f(CW\*(C`IPC::Msg\*(C'\fR documentation.
2992 .IP "msgrcv \s-1ID\s0,VAR,SIZE,TYPE,FLAGS" 8
2993 .IX Item "msgrcv ID,VAR,SIZE,TYPE,FLAGS"
2994 Calls the System V \s-1IPC\s0 function msgrcv to receive a message from
2995 message queue \s-1ID\s0 into variable \s-1VAR\s0 with a maximum message size of
2996 \&\s-1SIZE\s0.  Note that when a message is received, the message type as a
2997 native long integer will be the first thing in \s-1VAR\s0, followed by the
2998 actual message.  This packing may be opened with \f(CW\*(C`unpack("l! a*")\*(C'\fR.
2999 Taints the variable.  Returns true if successful, or false if there is
3000 an error.  See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, and
3001 \&\f(CW\*(C`IPC::SysV::Msg\*(C'\fR documentation.
3002 .IP "msgsnd \s-1ID\s0,MSG,FLAGS" 8
3003 .IX Item "msgsnd ID,MSG,FLAGS"
3004 Calls the System V \s-1IPC\s0 function msgsnd to send the message \s-1MSG\s0 to the
3005 message queue \s-1ID\s0.  \s-1MSG\s0 must begin with the native long integer message
3006 type, and be followed by the length of the actual message, and finally
3007 the message itself.  This kind of packing can be achieved with
3008 \&\f(CW\*(C`pack("l! a*", $type, $message)\*(C'\fR.  Returns true if successful,
3009 or false if there is an error.  See also \f(CW\*(C`IPC::SysV\*(C'\fR
3010 and \f(CW\*(C`IPC::SysV::Msg\*(C'\fR documentation.
3011 .IP "my \s-1EXPR\s0" 8
3012 .IX Item "my EXPR"
3013 .PD 0
3014 .IP "my \s-1TYPE\s0 \s-1EXPR\s0" 8
3015 .IX Item "my TYPE EXPR"
3016 .IP "my \s-1EXPR\s0 : \s-1ATTRS\s0" 8
3017 .IX Item "my EXPR : ATTRS"
3018 .IP "my \s-1TYPE\s0 \s-1EXPR\s0 : \s-1ATTRS\s0" 8
3019 .IX Item "my TYPE EXPR : ATTRS"
3021 A \f(CW\*(C`my\*(C'\fR declares the listed variables to be local (lexically) to the
3022 enclosing block, file, or \f(CW\*(C`eval\*(C'\fR.  If more than one value is listed,
3023 the list must be placed in parentheses.
3025 The exact semantics and interface of \s-1TYPE\s0 and \s-1ATTRS\s0 are still
3026 evolving.  \s-1TYPE\s0 is currently bound to the use of \f(CW\*(C`fields\*(C'\fR pragma,
3027 and attributes are handled using the \f(CW\*(C`attributes\*(C'\fR pragma, or starting
3028 from Perl 5.8.0 also via the \f(CW\*(C`Attribute::Handlers\*(C'\fR module.  See
3029 \&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub for details, and fields,
3030 attributes, and Attribute::Handlers.
3031 .IP "next \s-1LABEL\s0" 8
3032 .IX Item "next LABEL"
3033 .PD 0
3034 .IP "next" 8
3035 .IX Item "next"
3037 The \f(CW\*(C`next\*(C'\fR command is like the \f(CW\*(C`continue\*(C'\fR statement in C; it starts
3038 the next iteration of the loop:
3040 .Vb 4
3041 \&    LINE: while (<STDIN>) {
3042 \&        next LINE if /^#/;      # discard comments
3043 \&        #...
3044 \&    }
3047 Note that if there were a \f(CW\*(C`continue\*(C'\fR block on the above, it would get
3048 executed even on discarded lines.  If the \s-1LABEL\s0 is omitted, the command
3049 refers to the innermost enclosing loop.
3051 \&\f(CW\*(C`next\*(C'\fR cannot be used to exit a block which returns a value such as
3052 \&\f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR or \f(CW\*(C`do {}\*(C'\fR, and should not be used to exit
3053 a \fIgrep()\fR or \fImap()\fR operation.
3055 Note that a block by itself is semantically identical to a loop
3056 that executes once.  Thus \f(CW\*(C`next\*(C'\fR will exit such a block early.
3058 See also \*(L"continue\*(R" for an illustration of how \f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
3059 \&\f(CW\*(C`redo\*(C'\fR work.
3060 .IP "no Module \s-1VERSION\s0 \s-1LIST\s0" 8
3061 .IX Item "no Module VERSION LIST"
3062 .PD 0
3063 .IP "no Module \s-1VERSION\s0" 8
3064 .IX Item "no Module VERSION"
3065 .IP "no Module \s-1LIST\s0" 8
3066 .IX Item "no Module LIST"
3067 .IP "no Module" 8
3068 .IX Item "no Module"
3070 See the \f(CW\*(C`use\*(C'\fR function, which \f(CW\*(C`no\*(C'\fR is the opposite of.
3071 .IP "oct \s-1EXPR\s0" 8
3072 .IX Item "oct EXPR"
3073 .PD 0
3074 .IP "oct" 8
3075 .IX Item "oct"
3077 Interprets \s-1EXPR\s0 as an octal string and returns the corresponding
3078 value.  (If \s-1EXPR\s0 happens to start off with \f(CW\*(C`0x\*(C'\fR, interprets it as a
3079 hex string.  If \s-1EXPR\s0 starts off with \f(CW\*(C`0b\*(C'\fR, it is interpreted as a
3080 binary string.  Leading whitespace is ignored in all three cases.)
3081 The following will handle decimal, binary, octal, and hex in the standard
3082 Perl or C notation:
3084 .Vb 1
3085 \&    $val = oct($val) if $val =~ /^0/;
3088 If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.   To go the other way (produce a number
3089 in octal), use \fIsprintf()\fR or \fIprintf()\fR:
3091 .Vb 2
3092 \&    $perms = (stat("filename"))[2] & 07777;
3093 \&    $oct_perms = sprintf "%lo", $perms;
3096 The \fIoct()\fR function is commonly used when a string such as \f(CW644\fR needs
3097 to be converted into a file mode, for example. (Although perl will
3098 automatically convert strings into numbers as needed, this automatic
3099 conversion assumes base 10.)
3100 .IP "open \s-1FILEHANDLE\s0,EXPR" 8
3101 .IX Item "open FILEHANDLE,EXPR"
3102 .PD 0
3103 .IP "open \s-1FILEHANDLE\s0,MODE,EXPR" 8
3104 .IX Item "open FILEHANDLE,MODE,EXPR"
3105 .IP "open \s-1FILEHANDLE\s0,MODE,EXPR,LIST" 8
3106 .IX Item "open FILEHANDLE,MODE,EXPR,LIST"
3107 .IP "open \s-1FILEHANDLE\s0,MODE,REFERENCE" 8
3108 .IX Item "open FILEHANDLE,MODE,REFERENCE"
3109 .IP "open \s-1FILEHANDLE\s0" 8
3110 .IX Item "open FILEHANDLE"
3112 Opens the file whose filename is given by \s-1EXPR\s0, and associates it with
3113 \&\s-1FILEHANDLE\s0.
3115 (The following is a comprehensive reference to \fIopen()\fR: for a gentler
3116 introduction you may consider perlopentut.)
3118 If \s-1FILEHANDLE\s0 is an undefined scalar variable (or array or hash element)
3119 the variable is assigned a reference to a new anonymous filehandle,
3120 otherwise if \s-1FILEHANDLE\s0 is an expression, its value is used as the name of
3121 the real filehandle wanted.  (This is considered a symbolic reference, so
3122 \&\f(CW\*(C`use strict 'refs'\*(C'\fR should \fInot\fR be in effect.)
3124 If \s-1EXPR\s0 is omitted, the scalar variable of the same name as the
3125 \&\s-1FILEHANDLE\s0 contains the filename.  (Note that lexical variables\*(--those
3126 declared with \f(CW\*(C`my\*(C'\fR\-\-will not work for this purpose; so if you're
3127 using \f(CW\*(C`my\*(C'\fR, specify \s-1EXPR\s0 in your call to open.)
3129 If three or more arguments are specified then the mode of opening and
3130 the file name are separate. If \s-1MODE\s0 is \f(CW'<'\fR or nothing, the file
3131 is opened for input.  If \s-1MODE\s0 is \f(CW'>'\fR, the file is truncated and
3132 opened for output, being created if necessary.  If \s-1MODE\s0 is \f(CW'>>'\fR,
3133 the file is opened for appending, again being created if necessary.
3135 You can put a \f(CW'+'\fR in front of the \f(CW'>'\fR or \f(CW'<'\fR to
3136 indicate that you want both read and write access to the file; thus
3137 \&\f(CW'+<'\fR is almost always preferred for read/write updates\*(--the \f(CW'+>'\fR mode would clobber the file first.  You can't usually use
3138 either read-write mode for updating textfiles, since they have
3139 variable length records.  See the \fB\-i\fR switch in perlrun for a
3140 better approach.  The file is created with permissions of \f(CW0666\fR
3141 modified by the process' \f(CW\*(C`umask\*(C'\fR value.
3143 These various prefixes correspond to the \fIfopen\fR\|(3) modes of \f(CW'r'\fR,
3144 \&\f(CW'r+'\fR, \f(CW'w'\fR, \f(CW'w+'\fR, \f(CW'a'\fR, and \f(CW'a+'\fR.
3146 In the 2\-arguments (and 1\-argument) form of the call the mode and
3147 filename should be concatenated (in this order), possibly separated by
3148 spaces.  It is possible to omit the mode in these forms if the mode is
3149 \&\f(CW'<'\fR.
3151 If the filename begins with \f(CW'|'\fR, the filename is interpreted as a
3152 command to which output is to be piped, and if the filename ends with a
3153 \&\f(CW'|'\fR, the filename is interpreted as a command which pipes output to
3154 us.  See \*(L"Using \fIopen()\fR for \s-1IPC\s0\*(R" in perlipc
3155 for more examples of this.  (You are not allowed to \f(CW\*(C`open\*(C'\fR to a command
3156 that pipes both in \fIand\fR out, but see IPC::Open2, IPC::Open3,
3157 and \*(L"Bidirectional Communication with Another Process\*(R" in perlipc
3158 for alternatives.)
3160 For three or more arguments if \s-1MODE\s0 is \f(CW'|\-'\fR, the filename is
3161 interpreted as a command to which output is to be piped, and if \s-1MODE\s0
3162 is \f(CW'\-|'\fR, the filename is interpreted as a command which pipes
3163 output to us.  In the 2\-arguments (and 1\-argument) form one should
3164 replace dash (\f(CW'\-'\fR) with the command.
3165 See \*(L"Using \fIopen()\fR for \s-1IPC\s0\*(R" in perlipc for more examples of this.
3166 (You are not allowed to \f(CW\*(C`open\*(C'\fR to a command that pipes both in \fIand\fR
3167 out, but see IPC::Open2, IPC::Open3, and
3168 \&\*(L"Bidirectional Communication\*(R" in perlipc for alternatives.)
3170 In the three-or-more argument form of pipe opens, if \s-1LIST\s0 is specified
3171 (extra arguments after the command name) then \s-1LIST\s0 becomes arguments
3172 to the command invoked if the platform supports it.  The meaning of
3173 \&\f(CW\*(C`open\*(C'\fR with more than three arguments for non-pipe modes is not yet
3174 specified. Experimental \*(L"layers\*(R" may give extra \s-1LIST\s0 arguments
3175 meaning.
3177 In the 2\-arguments (and 1\-argument) form opening \f(CW'\-'\fR opens \s-1STDIN\s0
3178 and opening \f(CW'>\-'\fR opens \s-1STDOUT\s0.
3180 You may use the three-argument form of open to specify \s-1IO\s0 \*(L"layers\*(R"
3181 (sometimes also referred to as \*(L"disciplines\*(R") to be applied to the handle
3182 that affect how the input and output are processed (see open and
3183 PerlIO for more details). For example
3185 .Vb 1
3186 \&  open(FH, "<:utf8", "file")
3189 will open the \s-1UTF\-8\s0 encoded file containing Unicode characters,
3190 see perluniintro. (Note that if layers are specified in the
3191 three-arg form then default layers set by the \f(CW\*(C`open\*(C'\fR pragma are
3192 ignored.)
3194 Open returns nonzero upon success, the undefined value otherwise.  If
3195 the \f(CW\*(C`open\*(C'\fR involved a pipe, the return value happens to be the pid of
3196 the subprocess.
3198 If you're running Perl on a system that distinguishes between text
3199 files and binary files, then you should check out \*(L"binmode\*(R" for tips
3200 for dealing with this.  The key distinction between systems that need
3201 \&\f(CW\*(C`binmode\*(C'\fR and those that don't is their text file formats.  Systems
3202 like Unix, Mac \s-1OS\s0, and Plan 9, which delimit lines with a single
3203 character, and which encode that character in C as \f(CW"\en"\fR, do not
3204 need \f(CW\*(C`binmode\*(C'\fR.  The rest need it.
3206 When opening a file, it's usually a bad idea to continue normal execution
3207 if the request failed, so \f(CW\*(C`open\*(C'\fR is frequently used in connection with
3208 \&\f(CW\*(C`die\*(C'\fR.  Even if \f(CW\*(C`die\*(C'\fR won't do what you want (say, in a \s-1CGI\s0 script,
3209 where you want to make a nicely formatted error message (but there are
3210 modules that can help with that problem)) you should always check
3211 the return value from opening a file.  The infrequent exception is when
3212 working with an unopened filehandle is actually what you want to do.
3214 As a special case the 3 arg form with a read/write mode and the third
3215 argument being \f(CW\*(C`undef\*(C'\fR:
3217 .Vb 1
3218 \&    open(TMP, "+>", undef) or die ...
3221 opens a filehandle to an anonymous temporary file.  Also using \*(L"+<\*(R"
3222 works for symmetry, but you really should consider writing something
3223 to the temporary file first.  You will need to \fIseek()\fR to do the
3224 reading.
3226 File handles can be opened to \*(L"in memory\*(R" files held in Perl scalars via:
3228 .Vb 1
3229 \&    open($fh, '>', \e$variable) || ..
3232 Though if you try to re-open \f(CW\*(C`STDOUT\*(C'\fR or \f(CW\*(C`STDERR\*(C'\fR as an \*(L"in memory\*(R"
3233 file, you have to close it first:
3235 .Vb 2
3236 \&    close STDOUT;
3237 \&    open STDOUT, '>', \e$variable or die "Can't open STDOUT: $!";
3240 Examples:
3242 .Vb 3
3243 \&    $ARTICLE = 100;
3244 \&    open ARTICLE or die "Can't find article $ARTICLE: $!\en";
3245 \&    while (<ARTICLE>) {...
3248 .Vb 2
3249 \&    open(LOG, '>>/usr/spool/news/twitlog');     # (log is reserved)
3250 \&    # if the open fails, output is discarded
3253 .Vb 2
3254 \&    open(DBASE, '+<', 'dbase.mine')             # open for update
3255 \&        or die "Can't open 'dbase.mine' for update: $!";
3258 .Vb 2
3259 \&    open(DBASE, '+<dbase.mine')                 # ditto
3260 \&        or die "Can't open 'dbase.mine' for update: $!";
3263 .Vb 2
3264 \&    open(ARTICLE, '-|', "caesar <$article")     # decrypt article
3265 \&        or die "Can't start caesar: $!";
3268 .Vb 2
3269 \&    open(ARTICLE, "caesar <$article |")         # ditto
3270 \&        or die "Can't start caesar: $!";
3273 .Vb 2
3274 \&    open(EXTRACT, "|sort >/tmp/Tmp$$")          # $$ is our process id
3275 \&        or die "Can't start sort: $!";
3278 .Vb 4
3279 \&    # in memory files
3280 \&    open(MEMORY,'>', \e$var)
3281 \&        or die "Can't open memory file: $!";
3282 \&    print MEMORY "foo!\en";                      # output will end up in $var
3285 .Vb 1
3286 \&    # process argument list of files along with any includes
3289 .Vb 3
3290 \&    foreach $file (@ARGV) {
3291 \&        process($file, 'fh00');
3292 \&    }
3295 .Vb 7
3296 \&    sub process {
3297 \&        my($filename, $input) = @_;
3298 \&        $input++;               # this is a string increment
3299 \&        unless (open($input, $filename)) {
3300 \&            print STDERR "Can't open $filename: $!\en";
3301 \&            return;
3302 \&        }
3305 .Vb 9
3306 \&        local $_;
3307 \&        while (<$input>) {              # note use of indirection
3308 \&            if (/^#include "(.*)"/) {
3309 \&                process($1, $input);
3310 \&                next;
3311 \&            }
3312 \&            #...                # whatever
3313 \&        }
3314 \&    }
3317 You may also, in the Bourne shell tradition, specify an \s-1EXPR\s0 beginning
3318 with \f(CW'>&'\fR, in which case the rest of the string is interpreted
3319 as the name of a filehandle (or file descriptor, if numeric) to be
3320 duped (as \fIdup\fR\|(2)) and opened.  You may use \f(CW\*(C`&\*(C'\fR after \f(CW\*(C`>\*(C'\fR,
3321 \&\f(CW\*(C`>>\*(C'\fR, \f(CW\*(C`<\*(C'\fR, \f(CW\*(C`+>\*(C'\fR, \f(CW\*(C`+>>\*(C'\fR, and \f(CW\*(C`+<\*(C'\fR.
3322 The mode you specify should match the mode of the original filehandle.
3323 (Duping a filehandle does not take into account any existing contents
3324 of \s-1IO\s0 buffers.) If you use the 3 arg form then you can pass either a
3325 number, the name of a filehandle or the normal \*(L"reference to a glob\*(R".
3327 Here is a script that saves, redirects, and restores \f(CW\*(C`STDOUT\*(C'\fR and
3328 \&\f(CW\*(C`STDERR\*(C'\fR using various methods:
3330 .Vb 3
3331 \&    #!/usr/bin/perl
3332 \&    open my $oldout, ">&STDOUT"     or die "Can't dup STDOUT: $!";
3333 \&    open OLDERR,     ">&", \e*STDERR or die "Can't dup STDERR: $!";
3336 .Vb 2
3337 \&    open STDOUT, '>', "foo.out" or die "Can't redirect STDOUT: $!";
3338 \&    open STDERR, ">&STDOUT"     or die "Can't dup STDOUT: $!";
3341 .Vb 2
3342 \&    select STDERR; $| = 1;      # make unbuffered
3343 \&    select STDOUT; $| = 1;      # make unbuffered
3346 .Vb 2
3347 \&    print STDOUT "stdout 1\en";  # this works for
3348 \&    print STDERR "stderr 1\en";  # subprocesses too
3351 .Vb 2
3352 \&    close STDOUT;
3353 \&    close STDERR;
3356 .Vb 2
3357 \&    open STDOUT, ">&", $oldout or die "Can't dup \e$oldout: $!";
3358 \&    open STDERR, ">&OLDERR"    or die "Can't dup OLDERR: $!";
3361 .Vb 2
3362 \&    print STDOUT "stdout 2\en";
3363 \&    print STDERR "stderr 2\en";
3366 If you specify \f(CW'<&=X'\fR, where \f(CW\*(C`X\*(C'\fR is a file descriptor number
3367 or a filehandle, then Perl will do an equivalent of C's \f(CW\*(C`fdopen\*(C'\fR of
3368 that file descriptor (and not call \fIdup\fR\|(2)); this is more
3369 parsimonious of file descriptors.  For example:
3371 .Vb 2
3372 \&    # open for input, reusing the fileno of $fd
3373 \&    open(FILEHANDLE, "<&=$fd")
3378 .Vb 1
3379 \&    open(FILEHANDLE, "<&=", $fd)
3384 .Vb 2
3385 \&    # open for append, using the fileno of OLDFH
3386 \&    open(FH, ">>&=", OLDFH)
3391 .Vb 1
3392 \&    open(FH, ">>&=OLDFH")
3395 Being parsimonious on filehandles is also useful (besides being
3396 parsimonious) for example when something is dependent on file
3397 descriptors, like for example locking using \fIflock()\fR.  If you do just
3398 \&\f(CW\*(C`open(A, '>>&B')\*(C'\fR, the filehandle A will not have the same file
3399 descriptor as B, and therefore flock(A) will not flock(B), and vice
3400 versa.  But with \f(CW\*(C`open(A, '>>&=B')\*(C'\fR the filehandles will share
3401 the same file descriptor.
3403 Note that if you are using Perls older than 5.8.0, Perl will be using
3404 the standard C libraries' \fIfdopen()\fR to implement the \*(L"=\*(R" functionality.
3405 On many \s-1UNIX\s0 systems \fIfdopen()\fR fails when file descriptors exceed a
3406 certain value, typically 255.  For Perls 5.8.0 and later, PerlIO is
3407 most often the default.
3409 You can see whether Perl has been compiled with PerlIO or not by
3410 running \f(CW\*(C`perl \-V\*(C'\fR and looking for \f(CW\*(C`useperlio=\*(C'\fR line.  If \f(CW\*(C`useperlio\*(C'\fR
3411 is \f(CW\*(C`define\*(C'\fR, you have PerlIO, otherwise you don't.
3413 If you open a pipe on the command \f(CW'\-'\fR, i.e., either \f(CW'|\-'\fR or \f(CW'\-|'\fR
3414 with 2\-arguments (or 1\-argument) form of \fIopen()\fR, then
3415 there is an implicit fork done, and the return value of open is the pid
3416 of the child within the parent process, and \f(CW0\fR within the child
3417 process.  (Use \f(CW\*(C`defined($pid)\*(C'\fR to determine whether the open was successful.)
3418 The filehandle behaves normally for the parent, but i/o to that
3419 filehandle is piped from/to the \s-1STDOUT/STDIN\s0 of the child process.
3420 In the child process the filehandle isn't opened\*(--i/o happens from/to
3421 the new \s-1STDOUT\s0 or \s-1STDIN\s0.  Typically this is used like the normal
3422 piped open when you want to exercise more control over just how the
3423 pipe command gets executed, such as when you are running setuid, and
3424 don't want to have to scan shell commands for metacharacters.
3425 The following triples are more or less equivalent:
3427 .Vb 4
3428 \&    open(FOO, "|tr '[a-z]' '[A-Z]'");
3429 \&    open(FOO, '|-', "tr '[a-z]' '[A-Z]'");
3430 \&    open(FOO, '|-') || exec 'tr', '[a-z]', '[A-Z]';
3431 \&    open(FOO, '|-', "tr", '[a-z]', '[A-Z]');
3434 .Vb 4
3435 \&    open(FOO, "cat -n '$file'|");
3436 \&    open(FOO, '-|', "cat -n '$file'");
3437 \&    open(FOO, '-|') || exec 'cat', '-n', $file;
3438 \&    open(FOO, '-|', "cat", '-n', $file);
3441 The last example in each block shows the pipe as \*(L"list form\*(R", which is
3442 not yet supported on all platforms.  A good rule of thumb is that if
3443 your platform has true \f(CW\*(C`fork()\*(C'\fR (in other words, if your platform is
3444 \&\s-1UNIX\s0) you can use the list form.
3446 See \*(L"Safe Pipe Opens\*(R" in perlipc for more examples of this.
3448 Beginning with v5.6.0, Perl will attempt to flush all files opened for
3449 output before any operation that may do a fork, but this may not be
3450 supported on some platforms (see perlport).  To be safe, you may need
3451 to set \f(CW$|\fR ($AUTOFLUSH in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method
3452 of \f(CW\*(C`IO::Handle\*(C'\fR on any open handles.
3454 On systems that support a close-on-exec flag on files, the flag will
3455 be set for the newly opened file descriptor as determined by the value
3456 of $^F.  See \*(L"$^F\*(R" in perlvar.
3458 Closing any piped filehandle causes the parent process to wait for the
3459 child to finish, and returns the status value in \f(CW$?\fR.
3461 The filename passed to 2\-argument (or 1\-argument) form of \fIopen()\fR will
3462 have leading and trailing whitespace deleted, and the normal
3463 redirection characters honored.  This property, known as \*(L"magic open\*(R",
3464 can often be used to good effect.  A user could specify a filename of
3465 \&\fI\*(L"rsh cat file |\*(R"\fR, or you could change certain filenames as needed:
3467 .Vb 2
3468 \&    $filename =~ s/(.*\e.gz)\es*$/gzip -dc < $1|/;
3469 \&    open(FH, $filename) or die "Can't open $filename: $!";
3472 Use 3\-argument form to open a file with arbitrary weird characters in it,
3474 .Vb 1
3475 \&    open(FOO, '<', $file);
3478 otherwise it's necessary to protect any leading and trailing whitespace:
3480 .Vb 2
3481 \&    $file =~ s#^(\es)#./$1#;
3482 \&    open(FOO, "< $file\e0");
3485 (this may not work on some bizarre filesystems).  One should
3486 conscientiously choose between the \fImagic\fR and 3\-arguments form
3487 of \fIopen()\fR:
3489 .Vb 1
3490 \&    open IN, $ARGV[0];
3493 will allow the user to specify an argument of the form \f(CW"rsh cat file |"\fR,
3494 but will not work on a filename which happens to have a trailing space, while
3496 .Vb 1
3497 \&    open IN, '<', $ARGV[0];
3500 will have exactly the opposite restrictions.
3502 If you want a \*(L"real\*(R" C \f(CW\*(C`open\*(C'\fR (see \fIopen\fR\|(2) on your system), then you
3503 should use the \f(CW\*(C`sysopen\*(C'\fR function, which involves no such magic (but
3504 may use subtly different filemodes than Perl \fIopen()\fR, which is mapped
3505 to C \fIfopen()\fR).  This is
3506 another way to protect your filenames from interpretation.  For example:
3508 .Vb 7
3509 \&    use IO::Handle;
3510 \&    sysopen(HANDLE, $path, O_RDWR|O_CREAT|O_EXCL)
3511 \&        or die "sysopen $path: $!";
3512 \&    $oldfh = select(HANDLE); $| = 1; select($oldfh);
3513 \&    print HANDLE "stuff $$\en";
3514 \&    seek(HANDLE, 0, 0);
3515 \&    print "File contains: ", <HANDLE>;
3518 Using the constructor from the \f(CW\*(C`IO::Handle\*(C'\fR package (or one of its
3519 subclasses, such as \f(CW\*(C`IO::File\*(C'\fR or \f(CW\*(C`IO::Socket\*(C'\fR), you can generate anonymous
3520 filehandles that have the scope of whatever variables hold references to
3521 them, and automatically close whenever and however you leave that scope:
3523 .Vb 12
3524 \&    use IO::File;
3525 \&    #...
3526 \&    sub read_myfile_munged {
3527 \&        my $ALL = shift;
3528 \&        my $handle = new IO::File;
3529 \&        open($handle, "myfile") or die "myfile: $!";
3530 \&        $first = <$handle>
3531 \&            or return ();     # Automatically closed here.
3532 \&        mung $first or die "mung failed";       # Or here.
3533 \&        return $first, <$handle> if $ALL;       # Or here.
3534 \&        $first;                                 # Or here.
3535 \&    }
3538 See \*(L"seek\*(R" for some details about mixing reading and writing.
3539 .IP "opendir \s-1DIRHANDLE\s0,EXPR" 8
3540 .IX Item "opendir DIRHANDLE,EXPR"
3541 Opens a directory named \s-1EXPR\s0 for processing by \f(CW\*(C`readdir\*(C'\fR, \f(CW\*(C`telldir\*(C'\fR,
3542 \&\f(CW\*(C`seekdir\*(C'\fR, \f(CW\*(C`rewinddir\*(C'\fR, and \f(CW\*(C`closedir\*(C'\fR.  Returns true if successful.
3543 \&\s-1DIRHANDLE\s0 may be an expression whose value can be used as an indirect
3544 dirhandle, usually the real dirhandle name.  If \s-1DIRHANDLE\s0 is an undefined
3545 scalar variable (or array or hash element), the variable is assigned a
3546 reference to a new anonymous dirhandle.
3547 DIRHANDLEs have their own namespace separate from FILEHANDLEs.
3548 .IP "ord \s-1EXPR\s0" 8
3549 .IX Item "ord EXPR"
3550 .PD 0
3551 .IP "ord" 8
3552 .IX Item "ord"
3554 Returns the numeric (the native 8\-bit encoding, like \s-1ASCII\s0 or \s-1EBCDIC\s0,
3555 or Unicode) value of the first character of \s-1EXPR\s0.  If \s-1EXPR\s0 is omitted,
3556 uses \f(CW$_\fR.
3558 For the reverse, see \*(L"chr\*(R".
3559 See perlunicode and encoding for more about Unicode.
3560 .IP "our \s-1EXPR\s0" 8
3561 .IX Item "our EXPR"
3562 .PD 0
3563 .IP "our \s-1EXPR\s0 \s-1TYPE\s0" 8
3564 .IX Item "our EXPR TYPE"
3565 .IP "our \s-1EXPR\s0 : \s-1ATTRS\s0" 8
3566 .IX Item "our EXPR : ATTRS"
3567 .IP "our \s-1TYPE\s0 \s-1EXPR\s0 : \s-1ATTRS\s0" 8
3568 .IX Item "our TYPE EXPR : ATTRS"
3570 An \f(CW\*(C`our\*(C'\fR declares the listed variables to be valid globals within
3571 the enclosing block, file, or \f(CW\*(C`eval\*(C'\fR.  That is, it has the same
3572 scoping rules as a \*(L"my\*(R" declaration, but does not create a local
3573 variable.  If more than one value is listed, the list must be placed
3574 in parentheses.  The \f(CW\*(C`our\*(C'\fR declaration has no semantic effect unless
3575 \&\*(L"use strict vars\*(R" is in effect, in which case it lets you use the
3576 declared global variable without qualifying it with a package name.
3577 (But only within the lexical scope of the \f(CW\*(C`our\*(C'\fR declaration.  In this
3578 it differs from \*(L"use vars\*(R", which is package scoped.)
3580 An \f(CW\*(C`our\*(C'\fR declaration declares a global variable that will be visible
3581 across its entire lexical scope, even across package boundaries.  The
3582 package in which the variable is entered is determined at the point
3583 of the declaration, not at the point of use.  This means the following
3584 behavior holds:
3586 .Vb 3
3587 \&    package Foo;
3588 \&    our $bar;           # declares $Foo::bar for rest of lexical scope
3589 \&    $bar = 20;
3592 .Vb 2
3593 \&    package Bar;
3594 \&    print $bar;         # prints 20
3597 Multiple \f(CW\*(C`our\*(C'\fR declarations in the same lexical scope are allowed
3598 if they are in different packages.  If they happened to be in the same
3599 package, Perl will emit warnings if you have asked for them.
3601 .Vb 4
3602 \&    use warnings;
3603 \&    package Foo;
3604 \&    our $bar;           # declares $Foo::bar for rest of lexical scope
3605 \&    $bar = 20;
3608 .Vb 3
3609 \&    package Bar;
3610 \&    our $bar = 30;      # declares $Bar::bar for rest of lexical scope
3611 \&    print $bar;         # prints 30
3614 .Vb 1
3615 \&    our $bar;           # emits warning
3618 An \f(CW\*(C`our\*(C'\fR declaration may also have a list of attributes associated
3619 with it.
3621 The exact semantics and interface of \s-1TYPE\s0 and \s-1ATTRS\s0 are still
3622 evolving.  \s-1TYPE\s0 is currently bound to the use of \f(CW\*(C`fields\*(C'\fR pragma,
3623 and attributes are handled using the \f(CW\*(C`attributes\*(C'\fR pragma, or starting
3624 from Perl 5.8.0 also via the \f(CW\*(C`Attribute::Handlers\*(C'\fR module.  See
3625 \&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub for details, and fields,
3626 attributes, and Attribute::Handlers.
3628 The only currently recognized \f(CW\*(C`our()\*(C'\fR attribute is \f(CW\*(C`unique\*(C'\fR which
3629 indicates that a single copy of the global is to be used by all
3630 interpreters should the program happen to be running in a
3631 multi-interpreter environment. (The default behaviour would be for
3632 each interpreter to have its own copy of the global.)  Examples:
3634 .Vb 3
3635 \&    our @EXPORT : unique = qw(foo);
3636 \&    our %EXPORT_TAGS : unique = (bar => [qw(aa bb cc)]);
3637 \&    our $VERSION : unique = "1.00";
3640 Note that this attribute also has the effect of making the global
3641 readonly when the first new interpreter is cloned (for example,
3642 when the first new thread is created).
3644 Multi-interpreter environments can come to being either through the
3645 \&\fIfork()\fR emulation on Windows platforms, or by embedding perl in a
3646 multi-threaded application.  The \f(CW\*(C`unique\*(C'\fR attribute does nothing in
3647 all other environments.
3648 .IP "pack \s-1TEMPLATE\s0,LIST" 8
3649 .IX Item "pack TEMPLATE,LIST"
3650 Takes a \s-1LIST\s0 of values and converts it into a string using the rules
3651 given by the \s-1TEMPLATE\s0.  The resulting string is the concatenation of
3652 the converted values.  Typically, each converted value looks
3653 like its machine-level representation.  For example, on 32\-bit machines
3654 a converted integer may be represented by a sequence of 4 bytes.
3656 The \s-1TEMPLATE\s0 is a sequence of characters that give the order and type
3657 of values, as follows:
3659 .Vb 3
3660 \&    a   A string with arbitrary binary data, will be null padded.
3661 \&    A   A text (ASCII) string, will be space padded.
3662 \&    Z   A null terminated (ASCIZ) string, will be null padded.
3665 .Vb 4
3666 \&    b   A bit string (ascending bit order inside each byte, like vec()).
3667 \&    B   A bit string (descending bit order inside each byte).
3668 \&    h   A hex string (low nybble first).
3669 \&    H   A hex string (high nybble first).
3672 .Vb 2
3673 \&    c   A signed char value.
3674 \&    C   An unsigned char value.  Only does bytes.  See U for Unicode.
3677 .Vb 5
3678 \&    s   A signed short value.
3679 \&    S   An unsigned short value.
3680 \&          (This 'short' is _exactly_ 16 bits, which may differ from
3681 \&           what a local C compiler calls 'short'.  If you want
3682 \&           native-length shorts, use the '!' suffix.)
3685 .Vb 6
3686 \&    i   A signed integer value.
3687 \&    I   An unsigned integer value.
3688 \&          (This 'integer' is _at_least_ 32 bits wide.  Its exact
3689 \&           size depends on what a local C compiler calls 'int',
3690 \&           and may even be larger than the 'long' described in
3691 \&           the next item.)
3694 .Vb 5
3695 \&    l   A signed long value.
3696 \&    L   An unsigned long value.
3697 \&          (This 'long' is _exactly_ 32 bits, which may differ from
3698 \&           what a local C compiler calls 'long'.  If you want
3699 \&           native-length longs, use the '!' suffix.)
3702 .Vb 6
3703 \&    n   An unsigned short in "network" (big-endian) order.
3704 \&    N   An unsigned long in "network" (big-endian) order.
3705 \&    v   An unsigned short in "VAX" (little-endian) order.
3706 \&    V   An unsigned long in "VAX" (little-endian) order.
3707 \&          (These 'shorts' and 'longs' are _exactly_ 16 bits and
3708 \&           _exactly_ 32 bits, respectively.)
3711 .Vb 5
3712 \&    q   A signed quad (64-bit) value.
3713 \&    Q   An unsigned quad value.
3714 \&          (Quads are available only if your system supports 64-bit
3715 \&           integer values _and_ if Perl has been compiled to support those.
3716 \&           Causes a fatal error otherwise.)
3719 .Vb 2
3720 \&    j   A signed integer value (a Perl internal integer, IV).
3721 \&    J   An unsigned integer value (a Perl internal unsigned integer, UV).
3724 .Vb 2
3725 \&    f   A single-precision float in the native format.
3726 \&    d   A double-precision float in the native format.
3729 .Vb 6
3730 \&    F   A floating point value in the native native format
3731 \&           (a Perl internal floating point value, NV).
3732 \&    D   A long double-precision float in the native format.
3733 \&          (Long doubles are available only if your system supports long
3734 \&           double values _and_ if Perl has been compiled to support those.
3735 \&           Causes a fatal error otherwise.)
3738 .Vb 2
3739 \&    p   A pointer to a null-terminated string.
3740 \&    P   A pointer to a structure (fixed-length string).
3743 .Vb 3
3744 \&    u   A uuencoded string.
3745 \&    U   A Unicode character number.  Encodes to UTF-8 internally
3746 \&        (or UTF-EBCDIC in EBCDIC platforms).
3749 .Vb 4
3750 \&    w   A BER compressed integer.  Its bytes represent an unsigned
3751 \&        integer in base 128, most significant digit first, with as
3752 \&        few digits as possible.  Bit eight (the high bit) is set
3753 \&        on each byte except the last.
3756 .Vb 5
3757 \&    x   A null byte.
3758 \&    X   Back up a byte.
3759 \&    @   Null fill to absolute position, counted from the start of
3760 \&        the innermost ()-group.
3761 \&    (   Start of a ()-group.
3764 The following rules apply:
3765 .RS 8
3766 .IP "*" 8
3767 Each letter may optionally be followed by a number giving a repeat
3768 count.  With all types except \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`A\*(C'\fR, \f(CW\*(C`Z\*(C'\fR, \f(CW\*(C`b\*(C'\fR, \f(CW\*(C`B\*(C'\fR, \f(CW\*(C`h\*(C'\fR,
3769 \&\f(CW\*(C`H\*(C'\fR, \f(CW\*(C`@\*(C'\fR, \f(CW\*(C`x\*(C'\fR, \f(CW\*(C`X\*(C'\fR and \f(CW\*(C`P\*(C'\fR the pack function will gobble up that
3770 many values from the \s-1LIST\s0.  A \f(CW\*(C`*\*(C'\fR for the repeat count means to use
3771 however many items are left, except for \f(CW\*(C`@\*(C'\fR, \f(CW\*(C`x\*(C'\fR, \f(CW\*(C`X\*(C'\fR, where it is
3772 equivalent to \f(CW0\fR, and \f(CW\*(C`u\*(C'\fR, where it is equivalent to 1 (or 45, what
3773 is the same).  A numeric repeat count may optionally be enclosed in
3774 brackets, as in \f(CW\*(C`pack 'C[80]', @arr\*(C'\fR.
3776 One can replace the numeric repeat count by a template enclosed in brackets;
3777 then the packed length of this template in bytes is used as a count.
3778 For example, \f(CW\*(C`x[L]\*(C'\fR skips a long (it skips the number of bytes in a long);
3779 the template \f(CW\*(C`$t X[$t] $t\*(C'\fR \fIunpack()\fRs twice what \f(CW$t\fR unpacks.
3780 If the template in brackets contains alignment commands (such as \f(CW\*(C`x![d]\*(C'\fR),
3781 its packed length is calculated as if the start of the template has the maximal
3782 possible alignment.
3784 When used with \f(CW\*(C`Z\*(C'\fR, \f(CW\*(C`*\*(C'\fR results in the addition of a trailing null
3785 byte (so the packed result will be one longer than the byte \f(CW\*(C`length\*(C'\fR
3786 of the item).
3788 The repeat count for \f(CW\*(C`u\*(C'\fR is interpreted as the maximal number of bytes
3789 to encode per line of output, with 0 and 1 replaced by 45.
3790 .IP "*" 8
3791 The \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`A\*(C'\fR, and \f(CW\*(C`Z\*(C'\fR types gobble just one value, but pack it as a
3792 string of length count, padding with nulls or spaces as necessary.  When
3793 unpacking, \f(CW\*(C`A\*(C'\fR strips trailing spaces and nulls, \f(CW\*(C`Z\*(C'\fR strips everything
3794 after the first null, and \f(CW\*(C`a\*(C'\fR returns data verbatim.  When packing,
3795 \&\f(CW\*(C`a\*(C'\fR, and \f(CW\*(C`Z\*(C'\fR are equivalent.
3797 If the value-to-pack is too long, it is truncated.  If too long and an
3798 explicit count is provided, \f(CW\*(C`Z\*(C'\fR packs only \f(CW\*(C`$count\-1\*(C'\fR bytes, followed
3799 by a null byte.  Thus \f(CW\*(C`Z\*(C'\fR always packs a trailing null byte under
3800 all circumstances.
3801 .IP "*" 8
3802 Likewise, the \f(CW\*(C`b\*(C'\fR and \f(CW\*(C`B\*(C'\fR fields pack a string that many bits long.
3803 Each byte of the input field of \fIpack()\fR generates 1 bit of the result.
3804 Each result bit is based on the least-significant bit of the corresponding
3805 input byte, i.e., on \f(CW\*(C`ord($byte)%2\*(C'\fR.  In particular, bytes \f(CW"0"\fR and
3806 \&\f(CW"1"\fR generate bits 0 and 1, as do bytes \f(CW"\e0"\fR and \f(CW"\e1"\fR.
3808 Starting from the beginning of the input string of \fIpack()\fR, each 8\-tuple
3809 of bytes is converted to 1 byte of output.  With format \f(CW\*(C`b\*(C'\fR
3810 the first byte of the 8\-tuple determines the least-significant bit of a
3811 byte, and with format \f(CW\*(C`B\*(C'\fR it determines the most-significant bit of
3812 a byte.
3814 If the length of the input string is not exactly divisible by 8, the
3815 remainder is packed as if the input string were padded by null bytes
3816 at the end.  Similarly, during \fIunpack()\fRing the \*(L"extra\*(R" bits are ignored.
3818 If the input string of \fIpack()\fR is longer than needed, extra bytes are ignored.
3819 A \f(CW\*(C`*\*(C'\fR for the repeat count of \fIpack()\fR means to use all the bytes of
3820 the input field.  On \fIunpack()\fRing the bits are converted to a string
3821 of \f(CW"0"\fRs and \f(CW"1"\fRs.
3822 .IP "*" 8
3823 The \f(CW\*(C`h\*(C'\fR and \f(CW\*(C`H\*(C'\fR fields pack a string that many nybbles (4\-bit groups,
3824 representable as hexadecimal digits, 0\-9a\-f) long.
3826 Each byte of the input field of \fIpack()\fR generates 4 bits of the result.
3827 For non-alphabetical bytes the result is based on the 4 least-significant
3828 bits of the input byte, i.e., on \f(CW\*(C`ord($byte)%16\*(C'\fR.  In particular,
3829 bytes \f(CW"0"\fR and \f(CW"1"\fR generate nybbles 0 and 1, as do bytes
3830 \&\f(CW"\e0"\fR and \f(CW"\e1"\fR.  For bytes \f(CW"a".."f"\fR and \f(CW"A".."F"\fR the result
3831 is compatible with the usual hexadecimal digits, so that \f(CW"a"\fR and
3832 \&\f(CW"A"\fR both generate the nybble \f(CW\*(C`0xa==10\*(C'\fR.  The result for bytes
3833 \&\f(CW"g".."z"\fR and \f(CW"G".."Z"\fR is not well\-defined.
3835 Starting from the beginning of the input string of \fIpack()\fR, each pair
3836 of bytes is converted to 1 byte of output.  With format \f(CW\*(C`h\*(C'\fR the
3837 first byte of the pair determines the least-significant nybble of the
3838 output byte, and with format \f(CW\*(C`H\*(C'\fR it determines the most-significant
3839 nybble.
3841 If the length of the input string is not even, it behaves as if padded
3842 by a null byte at the end.  Similarly, during \fIunpack()\fRing the \*(L"extra\*(R"
3843 nybbles are ignored.
3845 If the input string of \fIpack()\fR is longer than needed, extra bytes are ignored.
3846 A \f(CW\*(C`*\*(C'\fR for the repeat count of \fIpack()\fR means to use all the bytes of
3847 the input field.  On \fIunpack()\fRing the bits are converted to a string
3848 of hexadecimal digits.
3849 .IP "*" 8
3850 The \f(CW\*(C`p\*(C'\fR type packs a pointer to a null-terminated string.  You are
3851 responsible for ensuring the string is not a temporary value (which can
3852 potentially get deallocated before you get around to using the packed result).
3853 The \f(CW\*(C`P\*(C'\fR type packs a pointer to a structure of the size indicated by the
3854 length.  A \s-1NULL\s0 pointer is created if the corresponding value for \f(CW\*(C`p\*(C'\fR or
3855 \&\f(CW\*(C`P\*(C'\fR is \f(CW\*(C`undef\*(C'\fR, similarly for \fIunpack()\fR.
3856 .IP "*" 8
3857 The \f(CW\*(C`/\*(C'\fR template character allows packing and unpacking of strings where
3858 the packed structure contains a byte count followed by the string itself.
3859 You write \fIlength-item\fR\f(CW\*(C`/\*(C'\fR\fIstring-item\fR.
3861 The \fIlength-item\fR can be any \f(CW\*(C`pack\*(C'\fR template letter, and describes
3862 how the length value is packed.  The ones likely to be of most use are
3863 integer-packing ones like \f(CW\*(C`n\*(C'\fR (for Java strings), \f(CW\*(C`w\*(C'\fR (for \s-1ASN\s0.1 or
3864 \&\s-1SNMP\s0) and \f(CW\*(C`N\*(C'\fR (for Sun \s-1XDR\s0).
3866 For \f(CW\*(C`pack\*(C'\fR, the \fIstring-item\fR must, at present, be \f(CW"A*"\fR, \f(CW"a*"\fR or
3867 \&\f(CW"Z*"\fR. For \f(CW\*(C`unpack\*(C'\fR the length of the string is obtained from the
3868 \&\fIlength-item\fR, but if you put in the '*' it will be ignored. For all other
3869 codes, \f(CW\*(C`unpack\*(C'\fR applies the length value to the next item, which must not
3870 have a repeat count.
3872 .Vb 3
3873 \&    unpack 'C/a', "\e04Gurusamy";        gives 'Guru'
3874 \&    unpack 'a3/A* A*', '007 Bond  J ';  gives (' Bond','J')
3875 \&    pack 'n/a* w/a*','hello,','world';  gives "\e000\e006hello,\e005world"
3878 The \fIlength-item\fR is not returned explicitly from \f(CW\*(C`unpack\*(C'\fR.
3880 Adding a count to the \fIlength-item\fR letter is unlikely to do anything
3881 useful, unless that letter is \f(CW\*(C`A\*(C'\fR, \f(CW\*(C`a\*(C'\fR or \f(CW\*(C`Z\*(C'\fR.  Packing with a
3882 \&\fIlength-item\fR of \f(CW\*(C`a\*(C'\fR or \f(CW\*(C`Z\*(C'\fR may introduce \f(CW"\e000"\fR characters,
3883 which Perl does not regard as legal in numeric strings.
3884 .IP "*" 8
3885 The integer types \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`S\*(C'\fR, \f(CW\*(C`l\*(C'\fR, and \f(CW\*(C`L\*(C'\fR may be
3886 immediately followed by a \f(CW\*(C`!\*(C'\fR suffix to signify native shorts or
3887 longs\*(--as you can see from above for example a bare \f(CW\*(C`l\*(C'\fR does mean
3888 exactly 32 bits, the native \f(CW\*(C`long\*(C'\fR (as seen by the local C compiler)
3889 may be larger.  This is an issue mainly in 64\-bit platforms.  You can
3890 see whether using \f(CW\*(C`!\*(C'\fR makes any difference by
3892 .Vb 2
3893 \&        print length(pack("s")), " ", length(pack("s!")), "\en";
3894 \&        print length(pack("l")), " ", length(pack("l!")), "\en";
3897 \&\f(CW\*(C`i!\*(C'\fR and \f(CW\*(C`I!\*(C'\fR also work but only because of completeness;
3898 they are identical to \f(CW\*(C`i\*(C'\fR and \f(CW\*(C`I\*(C'\fR.
3900 The actual sizes (in bytes) of native shorts, ints, longs, and long
3901 longs on the platform where Perl was built are also available via
3902 Config:
3904 .Vb 5
3905 \&       use Config;
3906 \&       print $Config{shortsize},    "\en";
3907 \&       print $Config{intsize},      "\en";
3908 \&       print $Config{longsize},     "\en";
3909 \&       print $Config{longlongsize}, "\en";
3912 (The \f(CW$Config{longlongsize}\fR will be undefined if your system does
3913 not support long longs.)
3914 .IP "*" 8
3915 The integer formats \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`S\*(C'\fR, \f(CW\*(C`i\*(C'\fR, \f(CW\*(C`I\*(C'\fR, \f(CW\*(C`l\*(C'\fR, \f(CW\*(C`L\*(C'\fR, \f(CW\*(C`j\*(C'\fR, and \f(CW\*(C`J\*(C'\fR
3916 are inherently non-portable between processors and operating systems
3917 because they obey the native byteorder and endianness.  For example a
3918 4\-byte integer 0x12345678 (305419896 decimal) would be ordered natively
3919 (arranged in and handled by the \s-1CPU\s0 registers) into bytes as
3921 .Vb 2
3922 \&        0x12 0x34 0x56 0x78     # big-endian
3923 \&        0x78 0x56 0x34 0x12     # little-endian
3926 Basically, the Intel and \s-1VAX\s0 CPUs are little\-endian, while everybody
3927 else, for example Motorola m68k/88k, \s-1PPC\s0, Sparc, \s-1HP\s0 \s-1PA\s0, Power, and
3928 Cray are big\-endian.  Alpha and \s-1MIPS\s0 can be either: Digital/Compaq
3929 used/uses them in little-endian mode; SGI/Cray uses them in big-endian
3930 mode.
3932 The names `big\-endian' and `little\-endian' are comic references to
3933 the classic \*(L"Gulliver's Travels\*(R" (via the paper \*(L"On Holy Wars and a
3934 Plea for Peace\*(R" by Danny Cohen, \s-1USC/ISI\s0 \s-1IEN\s0 137, April 1, 1980) and
3935 the egg-eating habits of the Lilliputians.
3937 Some systems may have even weirder byte orders such as
3939 .Vb 2
3940 \&        0x56 0x78 0x12 0x34
3941 \&        0x34 0x12 0x78 0x56
3944 You can see your system's preference with
3946 .Vb 2
3947 \&        print join(" ", map { sprintf "%#02x", $_ }
3948 \&                            unpack("C*",pack("L",0x12345678))), "\en";
3951 The byteorder on the platform where Perl was built is also available
3952 via Config:
3954 .Vb 2
3955 \&        use Config;
3956 \&        print $Config{byteorder}, "\en";
3959 Byteorders \f(CW'1234'\fR and \f(CW'12345678'\fR are little\-endian, \f(CW'4321'\fR
3960 and \f(CW'87654321'\fR are big\-endian.
3962 If you want portable packed integers use the formats \f(CW\*(C`n\*(C'\fR, \f(CW\*(C`N\*(C'\fR,
3963 \&\f(CW\*(C`v\*(C'\fR, and \f(CW\*(C`V\*(C'\fR, their byte endianness and size are known.
3964 See also perlport.
3965 .IP "*" 8
3966 Real numbers (floats and doubles) are in the native machine format only;
3967 due to the multiplicity of floating formats around, and the lack of a
3968 standard \*(L"network\*(R" representation, no facility for interchange has been
3969 made.  This means that packed floating point data written on one machine
3970 may not be readable on another \- even if both use \s-1IEEE\s0 floating point
3971 arithmetic (as the endian-ness of the memory representation is not part
3972 of the \s-1IEEE\s0 spec).  See also perlport.
3974 Note that Perl uses doubles internally for all numeric calculation, and
3975 converting from double into float and thence back to double again will
3976 lose precision (i.e., \f(CW\*(C`unpack("f", pack("f", $foo)\*(C'\fR) will not in general
3977 equal \f(CW$foo\fR).
3978 .IP "*" 8
3979 If the pattern begins with a \f(CW\*(C`U\*(C'\fR, the resulting string will be
3980 treated as UTF\-8\-encoded Unicode. You can force \s-1UTF\-8\s0 encoding on in a
3981 string with an initial \f(CW\*(C`U0\*(C'\fR, and the bytes that follow will be
3982 interpreted as Unicode characters. If you don't want this to happen,
3983 you can begin your pattern with \f(CW\*(C`C0\*(C'\fR (or anything else) to force Perl
3984 not to \s-1UTF\-8\s0 encode your string, and then follow this with a \f(CW\*(C`U*\*(C'\fR
3985 somewhere in your pattern.
3986 .IP "*" 8
3987 You must yourself do any alignment or padding by inserting for example
3988 enough \f(CW'x'\fRes while packing.  There is no way to \fIpack()\fR and \fIunpack()\fR
3989 could know where the bytes are going to or coming from.  Therefore
3990 \&\f(CW\*(C`pack\*(C'\fR (and \f(CW\*(C`unpack\*(C'\fR) handle their output and input as flat
3991 sequences of bytes.
3992 .IP "*" 8
3993 A ()\-group is a sub-TEMPLATE enclosed in parentheses.  A group may
3994 take a repeat count, both as postfix, and for \fIunpack()\fR also via the \f(CW\*(C`/\*(C'\fR
3995 template character. Within each repetition of a group, positioning with
3996 \&\f(CW\*(C`@\*(C'\fR starts again at 0. Therefore, the result of
3998 .Vb 1
3999 \&    pack( '@1A((@2A)@3A)', 'a', 'b', 'c' )
4002 is the string \*(L"\e0a\e0\e0bc\*(R".
4003 .IP "*" 8
4004 \&\f(CW\*(C`x\*(C'\fR and \f(CW\*(C`X\*(C'\fR accept \f(CW\*(C`!\*(C'\fR modifier.  In this case they act as
4005 alignment commands: they jump forward/back to the closest position
4006 aligned at a multiple of \f(CW\*(C`count\*(C'\fR bytes.  For example, to \fIpack()\fR or
4007 \&\fIunpack()\fR C's \f(CW\*(C`struct {char c; double d; char cc[2]}\*(C'\fR one may need to
4008 use the template \f(CW\*(C`C x![d] d C[2]\*(C'\fR; this assumes that doubles must be
4009 aligned on the double's size.
4011 For alignment commands \f(CW\*(C`count\*(C'\fR of 0 is equivalent to \f(CW\*(C`count\*(C'\fR of 1;
4012 both result in no\-ops.
4013 .IP "*" 8
4014 A comment in a \s-1TEMPLATE\s0 starts with \f(CW\*(C`#\*(C'\fR and goes to the end of line.
4015 White space may be used to separate pack codes from each other, but
4016 a \f(CW\*(C`!\*(C'\fR modifier and a repeat count must follow immediately.
4017 .IP "*" 8
4018 If \s-1TEMPLATE\s0 requires more arguments to \fIpack()\fR than actually given, \fIpack()\fR
4019 assumes additional \f(CW""\fR arguments.  If \s-1TEMPLATE\s0 requires less arguments
4020 to \fIpack()\fR than actually given, extra arguments are ignored.
4022 .RS 8
4024 Examples:
4026 .Vb 6
4027 \&    $foo = pack("CCCC",65,66,67,68);
4028 \&    # foo eq "ABCD"
4029 \&    $foo = pack("C4",65,66,67,68);
4030 \&    # same thing
4031 \&    $foo = pack("U4",0x24b6,0x24b7,0x24b8,0x24b9);
4032 \&    # same thing with Unicode circled letters
4035 .Vb 2
4036 \&    $foo = pack("ccxxcc",65,66,67,68);
4037 \&    # foo eq "AB\e0\e0CD"
4040 .Vb 4
4041 \&    # note: the above examples featuring "C" and "c" are true
4042 \&    # only on ASCII and ASCII-derived systems such as ISO Latin 1
4043 \&    # and UTF-8.  In EBCDIC the first example would be
4044 \&    # $foo = pack("CCCC",193,194,195,196);
4047 .Vb 3
4048 \&    $foo = pack("s2",1,2);
4049 \&    # "\e1\e0\e2\e0" on little-endian
4050 \&    # "\e0\e1\e0\e2" on big-endian
4053 .Vb 2
4054 \&    $foo = pack("a4","abcd","x","y","z");
4055 \&    # "abcd"
4058 .Vb 2
4059 \&    $foo = pack("aaaa","abcd","x","y","z");
4060 \&    # "axyz"
4063 .Vb 2
4064 \&    $foo = pack("a14","abcdefg");
4065 \&    # "abcdefg\e0\e0\e0\e0\e0\e0\e0"
4068 .Vb 2
4069 \&    $foo = pack("i9pl", gmtime);
4070 \&    # a real struct tm (on my system anyway)
4073 .Vb 3
4074 \&    $utmp_template = "Z8 Z8 Z16 L";
4075 \&    $utmp = pack($utmp_template, @utmp1);
4076 \&    # a struct utmp (BSDish)
4079 .Vb 2
4080 \&    @utmp2 = unpack($utmp_template, $utmp);
4081 \&    # "@utmp1" eq "@utmp2"
4084 .Vb 3
4085 \&    sub bintodec {
4086 \&        unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
4087 \&    }
4090 .Vb 5
4091 \&    $foo = pack('sx2l', 12, 34);
4092 \&    # short 12, two zero bytes padding, long 34
4093 \&    $bar = pack('s@4l', 12, 34);
4094 \&    # short 12, zero fill to position 4, long 34
4095 \&    # $foo eq $bar
4098 The same template may generally also be used in \fIunpack()\fR.
4100 .IP "package \s-1NAMESPACE\s0" 8
4101 .IX Item "package NAMESPACE"
4102 .PD 0
4103 .IP "package" 8
4104 .IX Item "package"
4106 Declares the compilation unit as being in the given namespace.  The scope
4107 of the package declaration is from the declaration itself through the end
4108 of the enclosing block, file, or eval (the same as the \f(CW\*(C`my\*(C'\fR operator).
4109 All further unqualified dynamic identifiers will be in this namespace.
4110 A package statement affects only dynamic variables\*(--including those
4111 you've used \f(CW\*(C`local\*(C'\fR on\*(--but \fInot\fR lexical variables, which are created
4112 with \f(CW\*(C`my\*(C'\fR.  Typically it would be the first declaration in a file to
4113 be included by the \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR operator.  You can switch into a
4114 package in more than one place; it merely influences which symbol table
4115 is used by the compiler for the rest of that block.  You can refer to
4116 variables and filehandles in other packages by prefixing the identifier
4117 with the package name and a double colon:  \f(CW$Package::Variable\fR.
4118 If the package name is null, the \f(CW\*(C`main\*(C'\fR package as assumed.  That is,
4119 \&\f(CW$::sail\fR is equivalent to \f(CW$main::sail\fR (as well as to \f(CW$main'sail\fR,
4120 still seen in older code).
4122 If \s-1NAMESPACE\s0 is omitted, then there is no current package, and all
4123 identifiers must be fully qualified or lexicals.  However, you are
4124 strongly advised not to make use of this feature. Its use can cause
4125 unexpected behaviour, even crashing some versions of Perl. It is
4126 deprecated, and will be removed from a future release.
4128 See \*(L"Packages\*(R" in perlmod for more information about packages, modules,
4129 and classes.  See perlsub for other scoping issues.
4130 .IP "pipe \s-1READHANDLE\s0,WRITEHANDLE" 8
4131 .IX Item "pipe READHANDLE,WRITEHANDLE"
4132 Opens a pair of connected pipes like the corresponding system call.
4133 Note that if you set up a loop of piped processes, deadlock can occur
4134 unless you are very careful.  In addition, note that Perl's pipes use
4135 \&\s-1IO\s0 buffering, so you may need to set \f(CW$|\fR to flush your \s-1WRITEHANDLE\s0
4136 after each command, depending on the application.
4138 See IPC::Open2, IPC::Open3, and \*(L"Bidirectional Communication\*(R" in perlipc
4139 for examples of such things.
4141 On systems that support a close-on-exec flag on files, the flag will be set
4142 for the newly opened file descriptors as determined by the value of $^F.
4143 See \*(L"$^F\*(R" in perlvar.
4144 .IP "pop \s-1ARRAY\s0" 8
4145 .IX Item "pop ARRAY"
4146 .PD 0
4147 .IP "pop" 8
4148 .IX Item "pop"
4150 Pops and returns the last value of the array, shortening the array by
4151 one element.  Has an effect similar to
4153 .Vb 1
4154 \&    $ARRAY[$#ARRAY--]
4157 If there are no elements in the array, returns the undefined value
4158 (although this may happen at other times as well).  If \s-1ARRAY\s0 is
4159 omitted, pops the \f(CW@ARGV\fR array in the main program, and the \f(CW@_\fR
4160 array in subroutines, just like \f(CW\*(C`shift\*(C'\fR.
4161 .IP "pos \s-1SCALAR\s0" 8
4162 .IX Item "pos SCALAR"
4163 .PD 0
4164 .IP "pos" 8
4165 .IX Item "pos"
4167 Returns the offset of where the last \f(CW\*(C`m//g\*(C'\fR search left off for the variable
4168 in question (\f(CW$_\fR is used when the variable is not specified).  May be
4169 modified to change that offset.  Such modification will also influence
4170 the \f(CW\*(C`\eG\*(C'\fR zero-width assertion in regular expressions.  See perlre and
4171 perlop.
4172 .IP "print \s-1FILEHANDLE\s0 \s-1LIST\s0" 8
4173 .IX Item "print FILEHANDLE LIST"
4174 .PD 0
4175 .IP "print \s-1LIST\s0" 8
4176 .IX Item "print LIST"
4177 .IP "print" 8
4178 .IX Item "print"
4180 Prints a string or a list of strings.  Returns true if successful.
4181 \&\s-1FILEHANDLE\s0 may be a scalar variable name, in which case the variable
4182 contains the name of or a reference to the filehandle, thus introducing
4183 one level of indirection.  (\s-1NOTE:\s0 If \s-1FILEHANDLE\s0 is a variable and
4184 the next token is a term, it may be misinterpreted as an operator
4185 unless you interpose a \f(CW\*(C`+\*(C'\fR or put parentheses around the arguments.)
4186 If \s-1FILEHANDLE\s0 is omitted, prints by default to standard output (or
4187 to the last selected output channel\*(--see \*(L"select\*(R").  If \s-1LIST\s0 is
4188 also omitted, prints \f(CW$_\fR to the currently selected output channel.
4189 To set the default output channel to something other than \s-1STDOUT\s0
4190 use the select operation.  The current value of \f(CW$,\fR (if any) is
4191 printed between each \s-1LIST\s0 item.  The current value of \f(CW\*(C`$\e\*(C'\fR (if
4192 any) is printed after the entire \s-1LIST\s0 has been printed.  Because
4193 print takes a \s-1LIST\s0, anything in the \s-1LIST\s0 is evaluated in list
4194 context, and any subroutine that you call will have one or more of
4195 its expressions evaluated in list context.  Also be careful not to
4196 follow the print keyword with a left parenthesis unless you want
4197 the corresponding right parenthesis to terminate the arguments to
4198 the print\*(--interpose a \f(CW\*(C`+\*(C'\fR or put parentheses around all the
4199 arguments.
4201 Note that if you're storing \s-1FILEHANDLES\s0 in an array or other expression,
4202 you will have to use a block returning its value instead:
4204 .Vb 2
4205 \&    print { $files[$i] } "stuff\en";
4206 \&    print { $OK ? STDOUT : STDERR } "stuff\en";
4208 .IP "printf \s-1FILEHANDLE\s0 \s-1FORMAT\s0, \s-1LIST\s0" 8
4209 .IX Item "printf FILEHANDLE FORMAT, LIST"
4210 .PD 0
4211 .IP "printf \s-1FORMAT\s0, \s-1LIST\s0" 8
4212 .IX Item "printf FORMAT, LIST"
4214 Equivalent to \f(CW\*(C`print FILEHANDLE sprintf(FORMAT, LIST)\*(C'\fR, except that \f(CW\*(C`$\e\*(C'\fR
4215 (the output record separator) is not appended.  The first argument
4216 of the list will be interpreted as the \f(CW\*(C`printf\*(C'\fR format. See \f(CW\*(C`sprintf\*(C'\fR
4217 for an explanation of the format argument. If \f(CW\*(C`use locale\*(C'\fR is in effect,
4218 the character used for the decimal point in formatted real numbers is
4219 affected by the \s-1LC_NUMERIC\s0 locale.  See perllocale.
4221 Don't fall into the trap of using a \f(CW\*(C`printf\*(C'\fR when a simple
4222 \&\f(CW\*(C`print\*(C'\fR would do.  The \f(CW\*(C`print\*(C'\fR is more efficient and less
4223 error prone.
4224 .IP "prototype \s-1FUNCTION\s0" 8
4225 .IX Item "prototype FUNCTION"
4226 Returns the prototype of a function as a string (or \f(CW\*(C`undef\*(C'\fR if the
4227 function has no prototype).  \s-1FUNCTION\s0 is a reference to, or the name of,
4228 the function whose prototype you want to retrieve.
4230 If \s-1FUNCTION\s0 is a string starting with \f(CW\*(C`CORE::\*(C'\fR, the rest is taken as a
4231 name for Perl builtin.  If the builtin is not \fIoverridable\fR (such as
4232 \&\f(CW\*(C`qw//\*(C'\fR) or its arguments cannot be expressed by a prototype (such as
4233 \&\f(CW\*(C`system\*(C'\fR) returns \f(CW\*(C`undef\*(C'\fR because the builtin does not really behave
4234 like a Perl function.  Otherwise, the string describing the equivalent
4235 prototype is returned.
4236 .IP "push \s-1ARRAY\s0,LIST" 8
4237 .IX Item "push ARRAY,LIST"
4238 Treats \s-1ARRAY\s0 as a stack, and pushes the values of \s-1LIST\s0
4239 onto the end of \s-1ARRAY\s0.  The length of \s-1ARRAY\s0 increases by the length of
4240 \&\s-1LIST\s0.  Has the same effect as
4242 .Vb 3
4243 \&    for $value (LIST) {
4244 \&        $ARRAY[++$#ARRAY] = $value;
4245 \&    }
4248 but is more efficient.  Returns the new number of elements in the array.
4249 .IP "q/STRING/" 8
4250 .IX Item "q/STRING/"
4251 .PD 0
4252 .IP "qq/STRING/" 8
4253 .IX Item "qq/STRING/"
4254 .IP "qr/STRING/" 8
4255 .IX Item "qr/STRING/"
4256 .IP "qx/STRING/" 8
4257 .IX Item "qx/STRING/"
4258 .IP "qw/STRING/" 8
4259 .IX Item "qw/STRING/"
4261 Generalized quotes.  See \*(L"Regexp Quote-Like Operators\*(R" in perlop.
4262 .IP "quotemeta \s-1EXPR\s0" 8
4263 .IX Item "quotemeta EXPR"
4264 .PD 0
4265 .IP "quotemeta" 8
4266 .IX Item "quotemeta"
4268 Returns the value of \s-1EXPR\s0 with all non\-\*(L"word\*(R"
4269 characters backslashed.  (That is, all characters not matching
4270 \&\f(CW\*(C`/[A\-Za\-z_0\-9]/\*(C'\fR will be preceded by a backslash in the
4271 returned string, regardless of any locale settings.)
4272 This is the internal function implementing
4273 the \f(CW\*(C`\eQ\*(C'\fR escape in double-quoted strings.
4275 If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
4276 .IP "rand \s-1EXPR\s0" 8
4277 .IX Item "rand EXPR"
4278 .PD 0
4279 .IP "rand" 8
4280 .IX Item "rand"
4282 Returns a random fractional number greater than or equal to \f(CW0\fR and less
4283 than the value of \s-1EXPR\s0.  (\s-1EXPR\s0 should be positive.)  If \s-1EXPR\s0 is
4284 omitted, the value \f(CW1\fR is used.  Currently \s-1EXPR\s0 with the value \f(CW0\fR is
4285 also special-cased as \f(CW1\fR \- this has not been documented before perl 5.8.0
4286 and is subject to change in future versions of perl.  Automatically calls
4287 \&\f(CW\*(C`srand\*(C'\fR unless \f(CW\*(C`srand\*(C'\fR has already been called.  See also \f(CW\*(C`srand\*(C'\fR.
4289 Apply \f(CW\*(C`int()\*(C'\fR to the value returned by \f(CW\*(C`rand()\*(C'\fR if you want random
4290 integers instead of random fractional numbers.  For example,
4292 .Vb 1
4293 \&    int(rand(10))
4296 returns a random integer between \f(CW0\fR and \f(CW9\fR, inclusive.
4298 (Note: If your rand function consistently returns numbers that are too
4299 large or too small, then your version of Perl was probably compiled
4300 with the wrong number of \s-1RANDBITS\s0.)
4301 .IP "read \s-1FILEHANDLE\s0,SCALAR,LENGTH,OFFSET" 8
4302 .IX Item "read FILEHANDLE,SCALAR,LENGTH,OFFSET"
4303 .PD 0
4304 .IP "read \s-1FILEHANDLE\s0,SCALAR,LENGTH" 8
4305 .IX Item "read FILEHANDLE,SCALAR,LENGTH"
4307 Attempts to read \s-1LENGTH\s0 \fIcharacters\fR of data into variable \s-1SCALAR\s0
4308 from the specified \s-1FILEHANDLE\s0.  Returns the number of characters
4309 actually read, \f(CW0\fR at end of file, or undef if there was an error (in
4310 the latter case \f(CW$!\fR is also set).  \s-1SCALAR\s0 will be grown or shrunk 
4311 so that the last character actually read is the last character of the
4312 scalar after the read.
4314 An \s-1OFFSET\s0 may be specified to place the read data at some place in the
4315 string other than the beginning.  A negative \s-1OFFSET\s0 specifies
4316 placement at that many characters counting backwards from the end of
4317 the string.  A positive \s-1OFFSET\s0 greater than the length of \s-1SCALAR\s0
4318 results in the string being padded to the required size with \f(CW"\e0"\fR
4319 bytes before the result of the read is appended.
4321 The call is actually implemented in terms of either Perl's or system's
4322 \&\fIfread()\fR call.  To get a true \fIread\fR\|(2) system call, see \f(CW\*(C`sysread\*(C'\fR.
4324 Note the \fIcharacters\fR: depending on the status of the filehandle,
4325 either (8\-bit) bytes or characters are read.  By default all
4326 filehandles operate on bytes, but for example if the filehandle has
4327 been opened with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", and the \f(CW\*(C`open\*(C'\fR
4328 pragma, open), the I/O will operate on \s-1UTF\-8\s0 encoded Unicode
4329 characters, not bytes.  Similarly for the \f(CW\*(C`:encoding\*(C'\fR pragma:
4330 in that case pretty much any characters can be read.
4331 .IP "readdir \s-1DIRHANDLE\s0" 8
4332 .IX Item "readdir DIRHANDLE"
4333 Returns the next directory entry for a directory opened by \f(CW\*(C`opendir\*(C'\fR.
4334 If used in list context, returns all the rest of the entries in the
4335 directory.  If there are no more entries, returns an undefined value in
4336 scalar context or a null list in list context.
4338 If you're planning to filetest the return values out of a \f(CW\*(C`readdir\*(C'\fR, you'd
4339 better prepend the directory in question.  Otherwise, because we didn't
4340 \&\f(CW\*(C`chdir\*(C'\fR there, it would have been testing the wrong file.
4342 .Vb 3
4343 \&    opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
4344 \&    @dots = grep { /^\e./ && -f "$some_dir/$_" } readdir(DIR);
4345 \&    closedir DIR;
4347 .IP "readline \s-1EXPR\s0" 8
4348 .IX Item "readline EXPR"
4349 Reads from the filehandle whose typeglob is contained in \s-1EXPR\s0.  In scalar
4350 context, each call reads and returns the next line, until end-of-file is
4351 reached, whereupon the subsequent call returns undef.  In list context,
4352 reads until end-of-file is reached and returns a list of lines.  Note that
4353 the notion of \*(L"line\*(R" used here is however you may have defined it
4354 with \f(CW$/\fR or \f(CW$INPUT_RECORD_SEPARATOR\fR).  See \*(L"$/\*(R" in perlvar.
4356 When \f(CW$/\fR is set to \f(CW\*(C`undef\*(C'\fR, when \fIreadline()\fR is in scalar
4357 context (i.e. file slurp mode), and when an empty file is read, it
4358 returns \f(CW''\fR the first time, followed by \f(CW\*(C`undef\*(C'\fR subsequently.
4360 This is the internal function implementing the \f(CW\*(C`<EXPR>\*(C'\fR
4361 operator, but you can use it directly.  The \f(CW\*(C`<EXPR>\*(C'\fR
4362 operator is discussed in more detail in \*(L"I/O Operators\*(R" in perlop.
4364 .Vb 2
4365 \&    $line = <STDIN>;
4366 \&    $line = readline(*STDIN);           # same thing
4369 If readline encounters an operating system error, \f(CW$!\fR will be set with the
4370 corresponding error message.  It can be helpful to check \f(CW$!\fR when you are
4371 reading from filehandles you don't trust, such as a tty or a socket.  The
4372 following example uses the operator form of \f(CW\*(C`readline\*(C'\fR, and takes the necessary
4373 steps to ensure that \f(CW\*(C`readline\*(C'\fR was successful.
4375 .Vb 8
4376 \&    for (;;) {
4377 \&        undef $!;
4378 \&        unless (defined( $line = <> )) {
4379 \&            die $! if $!;
4380 \&            last; # reached EOF
4381 \&        }
4382 \&        # ...
4383 \&    }
4385 .IP "readlink \s-1EXPR\s0" 8
4386 .IX Item "readlink EXPR"
4387 .PD 0
4388 .IP "readlink" 8
4389 .IX Item "readlink"
4391 Returns the value of a symbolic link, if symbolic links are
4392 implemented.  If not, gives a fatal error.  If there is some system
4393 error, returns the undefined value and sets \f(CW$!\fR (errno).  If \s-1EXPR\s0 is
4394 omitted, uses \f(CW$_\fR.
4395 .IP "readpipe \s-1EXPR\s0" 8
4396 .IX Item "readpipe EXPR"
4397 \&\s-1EXPR\s0 is executed as a system command.
4398 The collected standard output of the command is returned.
4399 In scalar context, it comes back as a single (potentially
4400 multi\-line) string.  In list context, returns a list of lines
4401 (however you've defined lines with \f(CW$/\fR or \f(CW$INPUT_RECORD_SEPARATOR\fR).
4402 This is the internal function implementing the \f(CW\*(C`qx/EXPR/\*(C'\fR
4403 operator, but you can use it directly.  The \f(CW\*(C`qx/EXPR/\*(C'\fR
4404 operator is discussed in more detail in \*(L"I/O Operators\*(R" in perlop.
4405 .IP "recv \s-1SOCKET\s0,SCALAR,LENGTH,FLAGS" 8
4406 .IX Item "recv SOCKET,SCALAR,LENGTH,FLAGS"
4407 Receives a message on a socket.  Attempts to receive \s-1LENGTH\s0 characters
4408 of data into variable \s-1SCALAR\s0 from the specified \s-1SOCKET\s0 filehandle.
4409 \&\s-1SCALAR\s0 will be grown or shrunk to the length actually read.  Takes the
4410 same flags as the system call of the same name.  Returns the address
4411 of the sender if \s-1SOCKET\s0's protocol supports this; returns an empty
4412 string otherwise.  If there's an error, returns the undefined value.
4413 This call is actually implemented in terms of \fIrecvfrom\fR\|(2) system call.
4414 See \*(L"\s-1UDP:\s0 Message Passing\*(R" in perlipc for examples.
4416 Note the \fIcharacters\fR: depending on the status of the socket, either
4417 (8\-bit) bytes or characters are received.  By default all sockets
4418 operate on bytes, but for example if the socket has been changed using
4419 \&\fIbinmode()\fR to operate with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see the \f(CW\*(C`open\*(C'\fR
4420 pragma, open), the I/O will operate on \s-1UTF\-8\s0 encoded Unicode
4421 characters, not bytes.  Similarly for the \f(CW\*(C`:encoding\*(C'\fR pragma:
4422 in that case pretty much any characters can be read.
4423 .IP "redo \s-1LABEL\s0" 8
4424 .IX Item "redo LABEL"
4425 .PD 0
4426 .IP "redo" 8
4427 .IX Item "redo"
4429 The \f(CW\*(C`redo\*(C'\fR command restarts the loop block without evaluating the
4430 conditional again.  The \f(CW\*(C`continue\*(C'\fR block, if any, is not executed.  If
4431 the \s-1LABEL\s0 is omitted, the command refers to the innermost enclosing
4432 loop.  This command is normally used by programs that want to lie to
4433 themselves about what was just input:
4435 .Vb 16
4436 \&    # a simpleminded Pascal comment stripper
4437 \&    # (warning: assumes no { or } in strings)
4438 \&    LINE: while (<STDIN>) {
4439 \&        while (s|({.*}.*){.*}|$1 |) {}
4440 \&        s|{.*}| |;
4441 \&        if (s|{.*| |) {
4442 \&            $front = $_;
4443 \&            while (<STDIN>) {
4444 \&                if (/}/) {      # end of comment?
4445 \&                    s|^|$front\e{|;
4446 \&                    redo LINE;
4447 \&                }
4448 \&            }
4449 \&        }
4450 \&        print;
4451 \&    }
4454 \&\f(CW\*(C`redo\*(C'\fR cannot be used to retry a block which returns a value such as
4455 \&\f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR or \f(CW\*(C`do {}\*(C'\fR, and should not be used to exit
4456 a \fIgrep()\fR or \fImap()\fR operation.
4458 Note that a block by itself is semantically identical to a loop
4459 that executes once.  Thus \f(CW\*(C`redo\*(C'\fR inside such a block will effectively
4460 turn it into a looping construct.
4462 See also \*(L"continue\*(R" for an illustration of how \f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
4463 \&\f(CW\*(C`redo\*(C'\fR work.
4464 .IP "ref \s-1EXPR\s0" 8
4465 .IX Item "ref EXPR"
4466 .PD 0
4467 .IP "ref" 8
4468 .IX Item "ref"
4470 Returns a true value if \s-1EXPR\s0 is a reference, false otherwise.  If \s-1EXPR\s0
4471 is not specified, \f(CW$_\fR will be used.  The value returned depends on the
4472 type of thing the reference is a reference to.
4473 Builtin types include:
4475 .Vb 7
4476 \&    SCALAR
4477 \&    ARRAY
4478 \&    HASH
4479 \&    CODE
4480 \&    REF
4481 \&    GLOB
4482 \&    LVALUE
4485 If the referenced object has been blessed into a package, then that package
4486 name is returned instead.  You can think of \f(CW\*(C`ref\*(C'\fR as a \f(CW\*(C`typeof\*(C'\fR operator.
4488 .Vb 9
4489 \&    if (ref($r) eq "HASH") {
4490 \&        print "r is a reference to a hash.\en";
4491 \&    }
4492 \&    unless (ref($r)) {
4493 \&        print "r is not a reference at all.\en";
4494 \&    }
4495 \&    if (UNIVERSAL::isa($r, "HASH")) {  # for subclassing
4496 \&        print "r is a reference to something that isa hash.\en";
4497 \&    }
4500 See also perlref.
4501 .IP "rename \s-1OLDNAME\s0,NEWNAME" 8
4502 .IX Item "rename OLDNAME,NEWNAME"
4503 Changes the name of a file; an existing file \s-1NEWNAME\s0 will be
4504 clobbered.  Returns true for success, false otherwise.
4506 Behavior of this function varies wildly depending on your system
4507 implementation.  For example, it will usually not work across file system
4508 boundaries, even though the system \fImv\fR command sometimes compensates
4509 for this.  Other restrictions include whether it works on directories,
4510 open files, or pre-existing files.  Check perlport and either the
4511 \&\fIrename\fR\|(2) manpage or equivalent system documentation for details.
4512 .IP "require \s-1VERSION\s0" 8
4513 .IX Item "require VERSION"
4514 .PD 0
4515 .IP "require \s-1EXPR\s0" 8
4516 .IX Item "require EXPR"
4517 .IP "require" 8
4518 .IX Item "require"
4520 Demands a version of Perl specified by \s-1VERSION\s0, or demands some semantics
4521 specified by \s-1EXPR\s0 or by \f(CW$_\fR if \s-1EXPR\s0 is not supplied.
4523 \&\s-1VERSION\s0 may be either a numeric argument such as 5.006, which will be
4524 compared to \f(CW$]\fR, or a literal of the form v5.6.1, which will be compared
4525 to \f(CW$^V\fR (aka \f(CW$PERL_VERSION\fR).  A fatal error is produced at run time if
4526 \&\s-1VERSION\s0 is greater than the version of the current Perl interpreter.
4527 Compare with \*(L"use\*(R", which can do a similar check at compile time.
4529 Specifying \s-1VERSION\s0 as a literal of the form v5.6.1 should generally be
4530 avoided, because it leads to misleading error messages under earlier
4531 versions of Perl which do not support this syntax.  The equivalent numeric
4532 version should be used instead.
4534 .Vb 3
4535 \&    require v5.6.1;     # run time version check
4536 \&    require 5.6.1;      # ditto
4537 \&    require 5.006_001;  # ditto; preferred for backwards compatibility
4540 Otherwise, demands that a library file be included if it hasn't already
4541 been included.  The file is included via the do-FILE mechanism, which is
4542 essentially just a variety of \f(CW\*(C`eval\*(C'\fR.  Has semantics similar to the following
4543 subroutine:
4545 .Vb 20
4546 \&    sub require {
4547 \&        my($filename) = @_;
4548 \&        return 1 if $INC{$filename};
4549 \&        my($realfilename,$result);
4550 \&        ITER: {
4551 \&            foreach $prefix (@INC) {
4552 \&                $realfilename = "$prefix/$filename";
4553 \&                if (-f $realfilename) {
4554 \&                    $INC{$filename} = $realfilename;
4555 \&                    $result = do $realfilename;
4556 \&                    last ITER;
4557 \&                }
4558 \&            }
4559 \&            die "Can't find $filename in \e@INC";
4560 \&        }
4561 \&        delete $INC{$filename} if $@ || !$result;
4562 \&        die $@ if $@;
4563 \&        die "$filename did not return true value" unless $result;
4564 \&        return $result;
4565 \&    }
4568 Note that the file will not be included twice under the same specified
4569 name.  The file must return true as the last statement to indicate
4570 successful execution of any initialization code, so it's customary to
4571 end such a file with \f(CW\*(C`1;\*(C'\fR unless you're sure it'll return true
4572 otherwise.  But it's better just to put the \f(CW\*(C`1;\*(C'\fR, in case you add more
4573 statements.
4575 If \s-1EXPR\s0 is a bareword, the require assumes a "\fI.pm\fR\*(L" extension and
4576 replaces \*(R"\fI::\fR\*(L" with \*(R"\fI/\fR" in the filename for you,
4577 to make it easy to load standard modules.  This form of loading of
4578 modules does not risk altering your namespace.
4580 In other words, if you try this:
4582 .Vb 1
4583 \&        require Foo::Bar;    # a splendid bareword
4586 The require function will actually look for the "\fIFoo/Bar.pm\fR" file in the
4587 directories specified in the \f(CW@INC\fR array.
4589 But if you try this:
4591 .Vb 4
4592 \&        $class = 'Foo::Bar';
4593 \&        require $class;      # $class is not a bareword
4594 \&    #or
4595 \&        require "Foo::Bar";  # not a bareword because of the ""
4598 The require function will look for the "\fIFoo::Bar\fR\*(L" file in the \f(CW@INC\fR array and
4599 will complain about not finding \*(R"\fIFoo::Bar\fR" there.  In this case you can do:
4601 .Vb 1
4602 \&        eval "require $class";
4605 Now that you understand how \f(CW\*(C`require\*(C'\fR looks for files in the case of
4606 a bareword argument, there is a little extra functionality going on
4607 behind the scenes.  Before \f(CW\*(C`require\*(C'\fR looks for a "\fI.pm\fR\*(L" extension,
4608 it will first look for a filename with a \*(R"\fI.pmc\fR" extension.  A file
4609 with this extension is assumed to be Perl bytecode generated by
4610 B::Bytecode.  If this file is found, and it's modification
4611 time is newer than a coinciding "\fI.pm\fR\*(L" non-compiled file, it will be
4612 loaded in place of that non-compiled file ending in a \*(R"\fI.pm\fR" extension.
4614 You can also insert hooks into the import facility, by putting directly
4615 Perl code into the \f(CW@INC\fR array.  There are three forms of hooks: subroutine
4616 references, array references and blessed objects.
4618 Subroutine references are the simplest case.  When the inclusion system
4619 walks through \f(CW@INC\fR and encounters a subroutine, this subroutine gets
4620 called with two parameters, the first being a reference to itself, and the
4621 second the name of the file to be included (e.g. "\fIFoo/Bar.pm\fR").  The
4622 subroutine should return \f(CW\*(C`undef\*(C'\fR or a filehandle, from which the file to
4623 include will be read.  If \f(CW\*(C`undef\*(C'\fR is returned, \f(CW\*(C`require\*(C'\fR will look at
4624 the remaining elements of \f(CW@INC\fR.
4626 If the hook is an array reference, its first element must be a subroutine
4627 reference.  This subroutine is called as above, but the first parameter is
4628 the array reference.  This enables to pass indirectly some arguments to
4629 the subroutine.
4631 In other words, you can write:
4633 .Vb 5
4634 \&    push @INC, \e&my_sub;
4635 \&    sub my_sub {
4636 \&        my ($coderef, $filename) = @_;  # $coderef is \e&my_sub
4637 \&        ...
4638 \&    }
4643 .Vb 7
4644 \&    push @INC, [ \e&my_sub, $x, $y, ... ];
4645 \&    sub my_sub {
4646 \&        my ($arrayref, $filename) = @_;
4647 \&        # Retrieve $x, $y, ...
4648 \&        my @parameters = @$arrayref[1..$#$arrayref];
4649 \&        ...
4650 \&    }
4653 If the hook is an object, it must provide an \s-1INC\s0 method, that will be
4654 called as above, the first parameter being the object itself.  (Note that
4655 you must fully qualify the sub's name, as it is always forced into package
4656 \&\f(CW\*(C`main\*(C'\fR.)  Here is a typical code layout:
4658 .Vb 7
4659 \&    # In Foo.pm
4660 \&    package Foo;
4661 \&    sub new { ... }
4662 \&    sub Foo::INC {
4663 \&        my ($self, $filename) = @_;
4664 \&        ...
4665 \&    }
4668 .Vb 2
4669 \&    # In the main program
4670 \&    push @INC, new Foo(...);
4673 Note that these hooks are also permitted to set the \f(CW%INC\fR entry
4674 corresponding to the files they have loaded. See \*(L"%INC\*(R" in perlvar.
4676 For a yet-more-powerful import facility, see \*(L"use\*(R" and perlmod.
4677 .IP "reset \s-1EXPR\s0" 8
4678 .IX Item "reset EXPR"
4679 .PD 0
4680 .IP "reset" 8
4681 .IX Item "reset"
4683 Generally used in a \f(CW\*(C`continue\*(C'\fR block at the end of a loop to clear
4684 variables and reset \f(CW\*(C`??\*(C'\fR searches so that they work again.  The
4685 expression is interpreted as a list of single characters (hyphens
4686 allowed for ranges).  All variables and arrays beginning with one of
4687 those letters are reset to their pristine state.  If the expression is
4688 omitted, one-match searches (\f(CW\*(C`?pattern?\*(C'\fR) are reset to match again.  Resets
4689 only variables or searches in the current package.  Always returns
4690 1.  Examples:
4692 .Vb 3
4693 \&    reset 'X';          # reset all X variables
4694 \&    reset 'a-z';        # reset lower case variables
4695 \&    reset;              # just reset ?one-time? searches
4698 Resetting \f(CW"A\-Z"\fR is not recommended because you'll wipe out your
4699 \&\f(CW@ARGV\fR and \f(CW@INC\fR arrays and your \f(CW%ENV\fR hash.  Resets only package
4700 variables\*(--lexical variables are unaffected, but they clean themselves
4701 up on scope exit anyway, so you'll probably want to use them instead.
4702 See \*(L"my\*(R".
4703 .IP "return \s-1EXPR\s0" 8
4704 .IX Item "return EXPR"
4705 .PD 0
4706 .IP "return" 8
4707 .IX Item "return"
4709 Returns from a subroutine, \f(CW\*(C`eval\*(C'\fR, or \f(CW\*(C`do FILE\*(C'\fR with the value
4710 given in \s-1EXPR\s0.  Evaluation of \s-1EXPR\s0 may be in list, scalar, or void
4711 context, depending on how the return value will be used, and the context
4712 may vary from one execution to the next (see \f(CW\*(C`wantarray\*(C'\fR).  If no \s-1EXPR\s0
4713 is given, returns an empty list in list context, the undefined value in
4714 scalar context, and (of course) nothing at all in a void context.
4716 (Note that in the absence of an explicit \f(CW\*(C`return\*(C'\fR, a subroutine, eval,
4717 or do \s-1FILE\s0 will automatically return the value of the last expression
4718 evaluated.)
4719 .IP "reverse \s-1LIST\s0" 8
4720 .IX Item "reverse LIST"
4721 In list context, returns a list value consisting of the elements
4722 of \s-1LIST\s0 in the opposite order.  In scalar context, concatenates the
4723 elements of \s-1LIST\s0 and returns a string value with all characters
4724 in the opposite order.
4726 .Vb 1
4727 \&    print reverse <>;           # line tac, last line first
4730 .Vb 2
4731 \&    undef $/;                   # for efficiency of <>
4732 \&    print scalar reverse <>;    # character tac, last line tsrif
4735 This operator is also handy for inverting a hash, although there are some
4736 caveats.  If a value is duplicated in the original hash, only one of those
4737 can be represented as a key in the inverted hash.  Also, this has to
4738 unwind one hash and build a whole new one, which may take some time
4739 on a large hash, such as from a \s-1DBM\s0 file.
4741 .Vb 1
4742 \&    %by_name = reverse %by_address;     # Invert the hash
4744 .IP "rewinddir \s-1DIRHANDLE\s0" 8
4745 .IX Item "rewinddir DIRHANDLE"
4746 Sets the current position to the beginning of the directory for the
4747 \&\f(CW\*(C`readdir\*(C'\fR routine on \s-1DIRHANDLE\s0.
4748 .IP "rindex \s-1STR\s0,SUBSTR,POSITION" 8
4749 .IX Item "rindex STR,SUBSTR,POSITION"
4750 .PD 0
4751 .IP "rindex \s-1STR\s0,SUBSTR" 8
4752 .IX Item "rindex STR,SUBSTR"
4754 Works just like \fIindex()\fR except that it returns the position of the \s-1LAST\s0
4755 occurrence of \s-1SUBSTR\s0 in \s-1STR\s0.  If \s-1POSITION\s0 is specified, returns the
4756 last occurrence at or before that position.
4757 .IP "rmdir \s-1FILENAME\s0" 8
4758 .IX Item "rmdir FILENAME"
4759 .PD 0
4760 .IP "rmdir" 8
4761 .IX Item "rmdir"
4763 Deletes the directory specified by \s-1FILENAME\s0 if that directory is
4764 empty.  If it succeeds it returns true, otherwise it returns false and
4765 sets \f(CW$!\fR (errno).  If \s-1FILENAME\s0 is omitted, uses \f(CW$_\fR.
4766 .IP "s///" 8
4767 .IX Item "s///"
4768 The substitution operator.  See perlop.
4769 .IP "scalar \s-1EXPR\s0" 8
4770 .IX Item "scalar EXPR"
4771 Forces \s-1EXPR\s0 to be interpreted in scalar context and returns the value
4772 of \s-1EXPR\s0.
4774 .Vb 1
4775 \&    @counts = ( scalar @a, scalar @b, scalar @c );
4778 There is no equivalent operator to force an expression to
4779 be interpolated in list context because in practice, this is never
4780 needed.  If you really wanted to do so, however, you could use
4781 the construction \f(CW\*(C`@{[ (some expression) ]}\*(C'\fR, but usually a simple
4782 \&\f(CW\*(C`(some expression)\*(C'\fR suffices.
4784 Because \f(CW\*(C`scalar\*(C'\fR is unary operator, if you accidentally use for \s-1EXPR\s0 a
4785 parenthesized list, this behaves as a scalar comma expression, evaluating
4786 all but the last element in void context and returning the final element
4787 evaluated in scalar context.  This is seldom what you want.
4789 The following single statement:
4791 .Vb 1
4792 \&        print uc(scalar(&foo,$bar)),$baz;
4795 is the moral equivalent of these two:
4797 .Vb 2
4798 \&        &foo;
4799 \&        print(uc($bar),$baz);
4802 See perlop for more details on unary operators and the comma operator.
4803 .IP "seek \s-1FILEHANDLE\s0,POSITION,WHENCE" 8
4804 .IX Item "seek FILEHANDLE,POSITION,WHENCE"
4805 Sets \s-1FILEHANDLE\s0's position, just like the \f(CW\*(C`fseek\*(C'\fR call of \f(CW\*(C`stdio\*(C'\fR.
4806 \&\s-1FILEHANDLE\s0 may be an expression whose value gives the name of the
4807 filehandle.  The values for \s-1WHENCE\s0 are \f(CW0\fR to set the new position
4808 \&\fIin bytes\fR to \s-1POSITION\s0, \f(CW1\fR to set it to the current position plus
4809 \&\s-1POSITION\s0, and \f(CW2\fR to set it to \s-1EOF\s0 plus \s-1POSITION\s0 (typically
4810 negative).  For \s-1WHENCE\s0 you may use the constants \f(CW\*(C`SEEK_SET\*(C'\fR,
4811 \&\f(CW\*(C`SEEK_CUR\*(C'\fR, and \f(CW\*(C`SEEK_END\*(C'\fR (start of the file, current position, end
4812 of the file) from the Fcntl module.  Returns \f(CW1\fR upon success, \f(CW0\fR
4813 otherwise.
4815 Note the \fIin bytes\fR: even if the filehandle has been set to
4816 operate on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR open
4817 layer), \fItell()\fR will return byte offsets, not character offsets
4818 (because implementing that would render \fIseek()\fR and \fItell()\fR rather slow).
4820 If you want to position file for \f(CW\*(C`sysread\*(C'\fR or \f(CW\*(C`syswrite\*(C'\fR, don't use
4821 \&\f(CW\*(C`seek\*(C'\fR\-\-buffering makes its effect on the file's system position
4822 unpredictable and non\-portable.  Use \f(CW\*(C`sysseek\*(C'\fR instead.
4824 Due to the rules and rigors of \s-1ANSI\s0 C, on some systems you have to do a
4825 seek whenever you switch between reading and writing.  Amongst other
4826 things, this may have the effect of calling stdio's \fIclearerr\fR\|(3).
4827 A \s-1WHENCE\s0 of \f(CW1\fR (\f(CW\*(C`SEEK_CUR\*(C'\fR) is useful for not moving the file position:
4829 .Vb 1
4830 \&    seek(TEST,0,1);
4833 This is also useful for applications emulating \f(CW\*(C`tail \-f\*(C'\fR.  Once you hit
4834 \&\s-1EOF\s0 on your read, and then sleep for a while, you might have to stick in a
4835 \&\fIseek()\fR to reset things.  The \f(CW\*(C`seek\*(C'\fR doesn't change the current position,
4836 but it \fIdoes\fR clear the end-of-file condition on the handle, so that the
4837 next \f(CW\*(C`<FILE>\*(C'\fR makes Perl try again to read something.  We hope.
4839 If that doesn't work (some \s-1IO\s0 implementations are particularly
4840 cantankerous), then you may need something more like this:
4842 .Vb 8
4843 \&    for (;;) {
4844 \&        for ($curpos = tell(FILE); $_ = <FILE>;
4845 \&             $curpos = tell(FILE)) {
4846 \&            # search for some stuff and put it into files
4847 \&        }
4848 \&        sleep($for_a_while);
4849 \&        seek(FILE, $curpos, 0);
4850 \&    }
4852 .IP "seekdir \s-1DIRHANDLE\s0,POS" 8
4853 .IX Item "seekdir DIRHANDLE,POS"
4854 Sets the current position for the \f(CW\*(C`readdir\*(C'\fR routine on \s-1DIRHANDLE\s0.  \s-1POS\s0
4855 must be a value returned by \f(CW\*(C`telldir\*(C'\fR.  Has the same caveats about
4856 possible directory compaction as the corresponding system library
4857 routine.
4858 .IP "select \s-1FILEHANDLE\s0" 8
4859 .IX Item "select FILEHANDLE"
4860 .PD 0
4861 .IP "select" 8
4862 .IX Item "select"
4864 Returns the currently selected filehandle.  Sets the current default
4865 filehandle for output, if \s-1FILEHANDLE\s0 is supplied.  This has two
4866 effects: first, a \f(CW\*(C`write\*(C'\fR or a \f(CW\*(C`print\*(C'\fR without a filehandle will
4867 default to this \s-1FILEHANDLE\s0.  Second, references to variables related to
4868 output will refer to this output channel.  For example, if you have to
4869 set the top of form format for more than one output channel, you might
4870 do the following:
4872 .Vb 4
4873 \&    select(REPORT1);
4874 \&    $^ = 'report1_top';
4875 \&    select(REPORT2);
4876 \&    $^ = 'report2_top';
4879 \&\s-1FILEHANDLE\s0 may be an expression whose value gives the name of the
4880 actual filehandle.  Thus:
4882 .Vb 1
4883 \&    $oldfh = select(STDERR); $| = 1; select($oldfh);
4886 Some programmers may prefer to think of filehandles as objects with
4887 methods, preferring to write the last example as:
4889 .Vb 2
4890 \&    use IO::Handle;
4891 \&    STDERR->autoflush(1);
4893 .IP "select \s-1RBITS\s0,WBITS,EBITS,TIMEOUT" 8
4894 .IX Item "select RBITS,WBITS,EBITS,TIMEOUT"
4895 This calls the \fIselect\fR\|(2) system call with the bit masks specified, which
4896 can be constructed using \f(CW\*(C`fileno\*(C'\fR and \f(CW\*(C`vec\*(C'\fR, along these lines:
4898 .Vb 4
4899 \&    $rin = $win = $ein = '';
4900 \&    vec($rin,fileno(STDIN),1) = 1;
4901 \&    vec($win,fileno(STDOUT),1) = 1;
4902 \&    $ein = $rin | $win;
4905 If you want to select on many filehandles you might wish to write a
4906 subroutine:
4908 .Vb 9
4909 \&    sub fhbits {
4910 \&        my(@fhlist) = split(' ',$_[0]);
4911 \&        my($bits);
4912 \&        for (@fhlist) {
4913 \&            vec($bits,fileno($_),1) = 1;
4914 \&        }
4915 \&        $bits;
4916 \&    }
4917 \&    $rin = fhbits('STDIN TTY SOCK');
4920 The usual idiom is:
4922 .Vb 2
4923 \&    ($nfound,$timeleft) =
4924 \&      select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
4927 or to block until something becomes ready just do this
4929 .Vb 1
4930 \&    $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
4933 Most systems do not bother to return anything useful in \f(CW$timeleft\fR, so
4934 calling \fIselect()\fR in scalar context just returns \f(CW$nfound\fR.
4936 Any of the bit masks can also be undef.  The timeout, if specified, is
4937 in seconds, which may be fractional.  Note: not all implementations are
4938 capable of returning the \f(CW$timeleft\fR.  If not, they always return
4939 \&\f(CW$timeleft\fR equal to the supplied \f(CW$timeout\fR.
4941 You can effect a sleep of 250 milliseconds this way:
4943 .Vb 1
4944 \&    select(undef, undef, undef, 0.25);
4947 Note that whether \f(CW\*(C`select\*(C'\fR gets restarted after signals (say, \s-1SIGALRM\s0)
4948 is implementation\-dependent.
4950 \&\fB\s-1WARNING\s0\fR: One should not attempt to mix buffered I/O (like \f(CW\*(C`read\*(C'\fR
4951 or <\s-1FH\s0>) with \f(CW\*(C`select\*(C'\fR, except as permitted by \s-1POSIX\s0, and even
4952 then only on \s-1POSIX\s0 systems.  You have to use \f(CW\*(C`sysread\*(C'\fR instead.
4953 .IP "semctl \s-1ID\s0,SEMNUM,CMD,ARG" 8
4954 .IX Item "semctl ID,SEMNUM,CMD,ARG"
4955 Calls the System V \s-1IPC\s0 function \f(CW\*(C`semctl\*(C'\fR.  You'll probably have to say
4957 .Vb 1
4958 \&    use IPC::SysV;
4961 first to get the correct constant definitions.  If \s-1CMD\s0 is \s-1IPC_STAT\s0 or
4962 \&\s-1GETALL\s0, then \s-1ARG\s0 must be a variable which will hold the returned
4963 semid_ds structure or semaphore value array.  Returns like \f(CW\*(C`ioctl\*(C'\fR:
4964 the undefined value for error, "\f(CW\*(C`0 but true\*(C'\fR" for zero, or the actual
4965 return value otherwise.  The \s-1ARG\s0 must consist of a vector of native
4966 short integers, which may be created with \f(CW\*(C`pack("s!",(0)x$nsem)\*(C'\fR.
4967 See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, \f(CW\*(C`IPC::Semaphore\*(C'\fR
4968 documentation.
4969 .IP "semget \s-1KEY\s0,NSEMS,FLAGS" 8
4970 .IX Item "semget KEY,NSEMS,FLAGS"
4971 Calls the System V \s-1IPC\s0 function semget.  Returns the semaphore id, or
4972 the undefined value if there is an error.  See also
4973 \&\*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, \f(CW\*(C`IPC::SysV::Semaphore\*(C'\fR
4974 documentation.
4975 .IP "semop \s-1KEY\s0,OPSTRING" 8
4976 .IX Item "semop KEY,OPSTRING"
4977 Calls the System V \s-1IPC\s0 function semop to perform semaphore operations
4978 such as signalling and waiting.  \s-1OPSTRING\s0 must be a packed array of
4979 semop structures.  Each semop structure can be generated with
4980 \&\f(CW\*(C`pack("s!3", $semnum, $semop, $semflag)\*(C'\fR.  The number of semaphore
4981 operations is implied by the length of \s-1OPSTRING\s0.  Returns true if
4982 successful, or false if there is an error.  As an example, the
4983 following code waits on semaphore \f(CW$semnum\fR of semaphore id \f(CW$semid:\fR
4985 .Vb 2
4986 \&    $semop = pack("s!3", $semnum, -1, 0);
4987 \&    die "Semaphore trouble: $!\en" unless semop($semid, $semop);
4990 To signal the semaphore, replace \f(CW\*(C`\-1\*(C'\fR with \f(CW1\fR.  See also
4991 \&\*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, and \f(CW\*(C`IPC::SysV::Semaphore\*(C'\fR
4992 documentation.
4993 .IP "send \s-1SOCKET\s0,MSG,FLAGS,TO" 8
4994 .IX Item "send SOCKET,MSG,FLAGS,TO"
4995 .PD 0
4996 .IP "send \s-1SOCKET\s0,MSG,FLAGS" 8
4997 .IX Item "send SOCKET,MSG,FLAGS"
4999 Sends a message on a socket.  Attempts to send the scalar \s-1MSG\s0 to the
5000 \&\s-1SOCKET\s0 filehandle.  Takes the same flags as the system call of the
5001 same name.  On unconnected sockets you must specify a destination to
5002 send \s-1TO\s0, in which case it does a C \f(CW\*(C`sendto\*(C'\fR.  Returns the number of
5003 characters sent, or the undefined value if there is an error.  The C
5004 system call \fIsendmsg\fR\|(2) is currently unimplemented.  See
5005 \&\*(L"\s-1UDP:\s0 Message Passing\*(R" in perlipc for examples.
5007 Note the \fIcharacters\fR: depending on the status of the socket, either
5008 (8\-bit) bytes or characters are sent.  By default all sockets operate
5009 on bytes, but for example if the socket has been changed using
5010 \&\fIbinmode()\fR to operate with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", or the
5011 \&\f(CW\*(C`open\*(C'\fR pragma, open), the I/O will operate on \s-1UTF\-8\s0 encoded
5012 Unicode characters, not bytes.  Similarly for the \f(CW\*(C`:encoding\*(C'\fR pragma:
5013 in that case pretty much any characters can be sent.
5014 .IP "setpgrp \s-1PID\s0,PGRP" 8
5015 .IX Item "setpgrp PID,PGRP"
5016 Sets the current process group for the specified \s-1PID\s0, \f(CW0\fR for the current
5017 process.  Will produce a fatal error if used on a machine that doesn't
5018 implement \s-1POSIX\s0 \fIsetpgid\fR\|(2) or \s-1BSD\s0 \fIsetpgrp\fR\|(2).  If the arguments are omitted,
5019 it defaults to \f(CW\*(C`0,0\*(C'\fR.  Note that the \s-1BSD\s0 4.2 version of \f(CW\*(C`setpgrp\*(C'\fR does not
5020 accept any arguments, so only \f(CW\*(C`setpgrp(0,0)\*(C'\fR is portable.  See also
5021 \&\f(CW\*(C`POSIX::setsid()\*(C'\fR.
5022 .IP "setpriority \s-1WHICH\s0,WHO,PRIORITY" 8
5023 .IX Item "setpriority WHICH,WHO,PRIORITY"
5024 Sets the current priority for a process, a process group, or a user.
5025 (See \fIsetpriority\fR\|(2).)  Will produce a fatal error if used on a machine
5026 that doesn't implement \fIsetpriority\fR\|(2).
5027 .IP "setsockopt \s-1SOCKET\s0,LEVEL,OPTNAME,OPTVAL" 8
5028 .IX Item "setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL"
5029 Sets the socket option requested.  Returns undefined if there is an
5030 error.  \s-1OPTVAL\s0 may be specified as \f(CW\*(C`undef\*(C'\fR if you don't want to pass an
5031 argument.
5032 .IP "shift \s-1ARRAY\s0" 8
5033 .IX Item "shift ARRAY"
5034 .PD 0
5035 .IP "shift" 8
5036 .IX Item "shift"
5038 Shifts the first value of the array off and returns it, shortening the
5039 array by 1 and moving everything down.  If there are no elements in the
5040 array, returns the undefined value.  If \s-1ARRAY\s0 is omitted, shifts the
5041 \&\f(CW@_\fR array within the lexical scope of subroutines and formats, and the
5042 \&\f(CW@ARGV\fR array at file scopes or within the lexical scopes established by
5043 the \f(CW\*(C`eval ''\*(C'\fR, \f(CW\*(C`BEGIN {}\*(C'\fR, \f(CW\*(C`INIT {}\*(C'\fR, \f(CW\*(C`CHECK {}\*(C'\fR, and \f(CW\*(C`END {}\*(C'\fR
5044 constructs.
5046 See also \f(CW\*(C`unshift\*(C'\fR, \f(CW\*(C`push\*(C'\fR, and \f(CW\*(C`pop\*(C'\fR.  \f(CW\*(C`shift\*(C'\fR and \f(CW\*(C`unshift\*(C'\fR do the
5047 same thing to the left end of an array that \f(CW\*(C`pop\*(C'\fR and \f(CW\*(C`push\*(C'\fR do to the
5048 right end.
5049 .IP "shmctl \s-1ID\s0,CMD,ARG" 8
5050 .IX Item "shmctl ID,CMD,ARG"
5051 Calls the System V \s-1IPC\s0 function shmctl.  You'll probably have to say
5053 .Vb 1
5054 \&    use IPC::SysV;
5057 first to get the correct constant definitions.  If \s-1CMD\s0 is \f(CW\*(C`IPC_STAT\*(C'\fR,
5058 then \s-1ARG\s0 must be a variable which will hold the returned \f(CW\*(C`shmid_ds\*(C'\fR
5059 structure.  Returns like ioctl: the undefined value for error, "\f(CW0\fR but
5060 true" for zero, or the actual return value otherwise.
5061 See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc and \f(CW\*(C`IPC::SysV\*(C'\fR documentation.
5062 .IP "shmget \s-1KEY\s0,SIZE,FLAGS" 8
5063 .IX Item "shmget KEY,SIZE,FLAGS"
5064 Calls the System V \s-1IPC\s0 function shmget.  Returns the shared memory
5065 segment id, or the undefined value if there is an error.
5066 See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc and \f(CW\*(C`IPC::SysV\*(C'\fR documentation.
5067 .IP "shmread \s-1ID\s0,VAR,POS,SIZE" 8
5068 .IX Item "shmread ID,VAR,POS,SIZE"
5069 .PD 0
5070 .IP "shmwrite \s-1ID\s0,STRING,POS,SIZE" 8
5071 .IX Item "shmwrite ID,STRING,POS,SIZE"
5073 Reads or writes the System V shared memory segment \s-1ID\s0 starting at
5074 position \s-1POS\s0 for size \s-1SIZE\s0 by attaching to it, copying in/out, and
5075 detaching from it.  When reading, \s-1VAR\s0 must be a variable that will
5076 hold the data read.  When writing, if \s-1STRING\s0 is too long, only \s-1SIZE\s0
5077 bytes are used; if \s-1STRING\s0 is too short, nulls are written to fill out
5078 \&\s-1SIZE\s0 bytes.  Return true if successful, or false if there is an error.
5079 \&\fIshmread()\fR taints the variable. See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc,
5080 \&\f(CW\*(C`IPC::SysV\*(C'\fR documentation, and the \f(CW\*(C`IPC::Shareable\*(C'\fR module from \s-1CPAN\s0.
5081 .IP "shutdown \s-1SOCKET\s0,HOW" 8
5082 .IX Item "shutdown SOCKET,HOW"
5083 Shuts down a socket connection in the manner indicated by \s-1HOW\s0, which
5084 has the same interpretation as in the system call of the same name.
5086 .Vb 3
5087 \&    shutdown(SOCKET, 0);    # I/we have stopped reading data
5088 \&    shutdown(SOCKET, 1);    # I/we have stopped writing data
5089 \&    shutdown(SOCKET, 2);    # I/we have stopped using this socket
5092 This is useful with sockets when you want to tell the other
5093 side you're done writing but not done reading, or vice versa.
5094 It's also a more insistent form of close because it also
5095 disables the file descriptor in any forked copies in other
5096 processes.
5097 .IP "sin \s-1EXPR\s0" 8
5098 .IX Item "sin EXPR"
5099 .PD 0
5100 .IP "sin" 8
5101 .IX Item "sin"
5103 Returns the sine of \s-1EXPR\s0 (expressed in radians).  If \s-1EXPR\s0 is omitted,
5104 returns sine of \f(CW$_\fR.
5106 For the inverse sine operation, you may use the \f(CW\*(C`Math::Trig::asin\*(C'\fR
5107 function, or use this relation:
5109 .Vb 1
5110 \&    sub asin { atan2($_[0], sqrt(1 - $_[0] * $_[0])) }
5112 .IP "sleep \s-1EXPR\s0" 8
5113 .IX Item "sleep EXPR"
5114 .PD 0
5115 .IP "sleep" 8
5116 .IX Item "sleep"
5118 Causes the script to sleep for \s-1EXPR\s0 seconds, or forever if no \s-1EXPR\s0.
5119 May be interrupted if the process receives a signal such as \f(CW\*(C`SIGALRM\*(C'\fR.
5120 Returns the number of seconds actually slept.  You probably cannot
5121 mix \f(CW\*(C`alarm\*(C'\fR and \f(CW\*(C`sleep\*(C'\fR calls, because \f(CW\*(C`sleep\*(C'\fR is often implemented
5122 using \f(CW\*(C`alarm\*(C'\fR.
5124 On some older systems, it may sleep up to a full second less than what
5125 you requested, depending on how it counts seconds.  Most modern systems
5126 always sleep the full amount.  They may appear to sleep longer than that,
5127 however, because your process might not be scheduled right away in a
5128 busy multitasking system.
5130 For delays of finer granularity than one second, you may use Perl's
5131 \&\f(CW\*(C`syscall\*(C'\fR interface to access \fIsetitimer\fR\|(2) if your system supports
5132 it, or else see \*(L"select\*(R" above.  The Time::HiRes module (from \s-1CPAN\s0,
5133 and starting from Perl 5.8 part of the standard distribution) may also
5134 help.
5136 See also the \s-1POSIX\s0 module's \f(CW\*(C`pause\*(C'\fR function.
5137 .IP "socket \s-1SOCKET\s0,DOMAIN,TYPE,PROTOCOL" 8
5138 .IX Item "socket SOCKET,DOMAIN,TYPE,PROTOCOL"
5139 Opens a socket of the specified kind and attaches it to filehandle
5140 \&\s-1SOCKET\s0.  \s-1DOMAIN\s0, \s-1TYPE\s0, and \s-1PROTOCOL\s0 are specified the same as for
5141 the system call of the same name.  You should \f(CW\*(C`use Socket\*(C'\fR first
5142 to get the proper definitions imported.  See the examples in
5143 \&\*(L"Sockets: Client/Server Communication\*(R" in perlipc.
5145 On systems that support a close-on-exec flag on files, the flag will
5146 be set for the newly opened file descriptor, as determined by the
5147 value of $^F.  See \*(L"$^F\*(R" in perlvar.
5148 .IP "socketpair \s-1SOCKET1\s0,SOCKET2,DOMAIN,TYPE,PROTOCOL" 8
5149 .IX Item "socketpair SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL"
5150 Creates an unnamed pair of sockets in the specified domain, of the
5151 specified type.  \s-1DOMAIN\s0, \s-1TYPE\s0, and \s-1PROTOCOL\s0 are specified the same as
5152 for the system call of the same name.  If unimplemented, yields a fatal
5153 error.  Returns true if successful.
5155 On systems that support a close-on-exec flag on files, the flag will
5156 be set for the newly opened file descriptors, as determined by the value
5157 of $^F.  See \*(L"$^F\*(R" in perlvar.
5159 Some systems defined \f(CW\*(C`pipe\*(C'\fR in terms of \f(CW\*(C`socketpair\*(C'\fR, in which a call
5160 to \f(CW\*(C`pipe(Rdr, Wtr)\*(C'\fR is essentially:
5162 .Vb 4
5163 \&    use Socket;
5164 \&    socketpair(Rdr, Wtr, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
5165 \&    shutdown(Rdr, 1);        # no more writing for reader
5166 \&    shutdown(Wtr, 0);        # no more reading for writer
5169 See perlipc for an example of socketpair use.  Perl 5.8 and later will
5170 emulate socketpair using \s-1IP\s0 sockets to localhost if your system implements
5171 sockets but not socketpair.
5172 .IP "sort \s-1SUBNAME\s0 \s-1LIST\s0" 8
5173 .IX Item "sort SUBNAME LIST"
5174 .PD 0
5175 .IP "sort \s-1BLOCK\s0 \s-1LIST\s0" 8
5176 .IX Item "sort BLOCK LIST"
5177 .IP "sort \s-1LIST\s0" 8
5178 .IX Item "sort LIST"
5180 In list context, this sorts the \s-1LIST\s0 and returns the sorted list value.
5181 In scalar context, the behaviour of \f(CW\*(C`sort()\*(C'\fR is undefined.
5183 If \s-1SUBNAME\s0 or \s-1BLOCK\s0 is omitted, \f(CW\*(C`sort\*(C'\fRs in standard string comparison
5184 order.  If \s-1SUBNAME\s0 is specified, it gives the name of a subroutine
5185 that returns an integer less than, equal to, or greater than \f(CW0\fR,
5186 depending on how the elements of the list are to be ordered.  (The \f(CW\*(C`<=>\*(C'\fR and \f(CW\*(C`cmp\*(C'\fR operators are extremely useful in such routines.)
5187 \&\s-1SUBNAME\s0 may be a scalar variable name (unsubscripted), in which case
5188 the value provides the name of (or a reference to) the actual
5189 subroutine to use.  In place of a \s-1SUBNAME\s0, you can provide a \s-1BLOCK\s0 as
5190 an anonymous, in-line sort subroutine.
5192 If the subroutine's prototype is \f(CW\*(C`($$)\*(C'\fR, the elements to be compared
5193 are passed by reference in \f(CW@_\fR, as for a normal subroutine.  This is
5194 slower than unprototyped subroutines, where the elements to be
5195 compared are passed into the subroutine
5196 as the package global variables \f(CW$a\fR and \f(CW$b\fR (see example below).  Note that
5197 in the latter case, it is usually counter-productive to declare \f(CW$a\fR and
5198 \&\f(CW$b\fR as lexicals.
5200 In either case, the subroutine may not be recursive.  The values to be
5201 compared are always passed by reference, so don't modify them.
5203 You also cannot exit out of the sort block or subroutine using any of the
5204 loop control operators described in perlsyn or with \f(CW\*(C`goto\*(C'\fR.
5206 When \f(CW\*(C`use locale\*(C'\fR is in effect, \f(CW\*(C`sort LIST\*(C'\fR sorts \s-1LIST\s0 according to the
5207 current collation locale.  See perllocale.
5209 Perl 5.6 and earlier used a quicksort algorithm to implement sort.
5210 That algorithm was not stable, and \fIcould\fR go quadratic.  (A \fIstable\fR sort
5211 preserves the input order of elements that compare equal.  Although
5212 quicksort's run time is O(NlogN) when averaged over all arrays of
5213 length N, the time can be O(N**2), \fIquadratic\fR behavior, for some
5214 inputs.)  In 5.7, the quicksort implementation was replaced with
5215 a stable mergesort algorithm whose worst case behavior is O(NlogN).
5216 But benchmarks indicated that for some inputs, on some platforms,
5217 the original quicksort was faster.  5.8 has a sort pragma for
5218 limited control of the sort.  Its rather blunt control of the
5219 underlying algorithm may not persist into future perls, but the
5220 ability to characterize the input or output in implementation
5221 independent ways quite probably will.  See sort.
5223 Examples:
5225 .Vb 2
5226 \&    # sort lexically
5227 \&    @articles = sort @files;
5230 .Vb 2
5231 \&    # same thing, but with explicit sort routine
5232 \&    @articles = sort {$a cmp $b} @files;
5235 .Vb 2
5236 \&    # now case-insensitively
5237 \&    @articles = sort {uc($a) cmp uc($b)} @files;
5240 .Vb 2
5241 \&    # same thing in reversed order
5242 \&    @articles = sort {$b cmp $a} @files;
5245 .Vb 2
5246 \&    # sort numerically ascending
5247 \&    @articles = sort {$a <=> $b} @files;
5250 .Vb 2
5251 \&    # sort numerically descending
5252 \&    @articles = sort {$b <=> $a} @files;
5255 .Vb 3
5256 \&    # this sorts the %age hash by value instead of key
5257 \&    # using an in-line function
5258 \&    @eldest = sort { $age{$b} <=> $age{$a} } keys %age;
5261 .Vb 5
5262 \&    # sort using explicit subroutine name
5263 \&    sub byage {
5264 \&        $age{$a} <=> $age{$b};  # presuming numeric
5265 \&    }
5266 \&    @sortedclass = sort byage @class;
5269 .Vb 9
5270 \&    sub backwards { $b cmp $a }
5271 \&    @harry  = qw(dog cat x Cain Abel);
5272 \&    @george = qw(gone chased yz Punished Axed);
5273 \&    print sort @harry;
5274 \&            # prints AbelCaincatdogx
5275 \&    print sort backwards @harry;
5276 \&            # prints xdogcatCainAbel
5277 \&    print sort @george, 'to', @harry;
5278 \&            # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
5281 .Vb 3
5282 \&    # inefficiently sort by descending numeric compare using
5283 \&    # the first integer after the first = sign, or the
5284 \&    # whole record case-insensitively otherwise
5287 .Vb 5
5288 \&    @new = sort {
5289 \&        ($b =~ /=(\ed+)/)[0] <=> ($a =~ /=(\ed+)/)[0]
5290 \&                            ||
5291 \&                    uc($a)  cmp  uc($b)
5292 \&    } @old;
5295 .Vb 8
5296 \&    # same thing, but much more efficiently;
5297 \&    # we'll build auxiliary indices instead
5298 \&    # for speed
5299 \&    @nums = @caps = ();
5300 \&    for (@old) {
5301 \&        push @nums, /=(\ed+)/;
5302 \&        push @caps, uc($_);
5303 \&    }
5306 .Vb 6
5307 \&    @new = @old[ sort {
5308 \&                        $nums[$b] <=> $nums[$a]
5309 \&                                 ||
5310 \&                        $caps[$a] cmp $caps[$b]
5311 \&                       } 0..$#old
5312 \&               ];
5315 .Vb 6
5316 \&    # same thing, but without any temps
5317 \&    @new = map { $_->[0] }
5318 \&           sort { $b->[1] <=> $a->[1]
5319 \&                           ||
5320 \&                  $a->[2] cmp $b->[2]
5321 \&           } map { [$_, /=(\ed+)/, uc($_)] } @old;
5324 .Vb 4
5325 \&    # using a prototype allows you to use any comparison subroutine
5326 \&    # as a sort subroutine (including other package's subroutines)
5327 \&    package other;
5328 \&    sub backwards ($$) { $_[1] cmp $_[0]; }     # $a and $b are not set here
5331 .Vb 2
5332 \&    package main;
5333 \&    @new = sort other::backwards @old;
5336 .Vb 3
5337 \&    # guarantee stability, regardless of algorithm
5338 \&    use sort 'stable';
5339 \&    @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
5342 .Vb 3
5343 \&    # force use of mergesort (not portable outside Perl 5.8)
5344 \&    use sort '_mergesort';  # note discouraging _
5345 \&    @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
5348 If you're using strict, you \fImust not\fR declare \f(CW$a\fR
5349 and \f(CW$b\fR as lexicals.  They are package globals.  That means
5350 if you're in the \f(CW\*(C`main\*(C'\fR package and type
5352 .Vb 1
5353 \&    @articles = sort {$b <=> $a} @files;
5356 then \f(CW$a\fR and \f(CW$b\fR are \f(CW$main::a\fR and \f(CW$main::b\fR (or \f(CW$::a\fR and \f(CW$::b\fR),
5357 but if you're in the \f(CW\*(C`FooPack\*(C'\fR package, it's the same as typing
5359 .Vb 1
5360 \&    @articles = sort {$FooPack::b <=> $FooPack::a} @files;
5363 The comparison function is required to behave.  If it returns
5364 inconsistent results (sometimes saying \f(CW$x[1]\fR is less than \f(CW$x[2]\fR and
5365 sometimes saying the opposite, for example) the results are not
5366 well\-defined.
5368 Because \f(CW\*(C`<=>\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR when either operand is \f(CW\*(C`NaN\*(C'\fR
5369 (not\-a\-number), and because \f(CW\*(C`sort\*(C'\fR will trigger a fatal error unless the
5370 result of a comparison is defined, when sorting with a comparison function
5371 like \f(CW\*(C`$a <=> $b\*(C'\fR, be careful about lists that might contain a \f(CW\*(C`NaN\*(C'\fR.
5372 The following example takes advantage of the fact that \f(CW\*(C`NaN != NaN\*(C'\fR to
5373 eliminate any \f(CW\*(C`NaN\*(C'\fRs from the input.
5375 .Vb 1
5376 \&    @result = sort { $a <=> $b } grep { $_ == $_ } @input;
5378 .IP "splice \s-1ARRAY\s0,OFFSET,LENGTH,LIST" 8
5379 .IX Item "splice ARRAY,OFFSET,LENGTH,LIST"
5380 .PD 0
5381 .IP "splice \s-1ARRAY\s0,OFFSET,LENGTH" 8
5382 .IX Item "splice ARRAY,OFFSET,LENGTH"
5383 .IP "splice \s-1ARRAY\s0,OFFSET" 8
5384 .IX Item "splice ARRAY,OFFSET"
5385 .IP "splice \s-1ARRAY\s0" 8
5386 .IX Item "splice ARRAY"
5388 Removes the elements designated by \s-1OFFSET\s0 and \s-1LENGTH\s0 from an array, and
5389 replaces them with the elements of \s-1LIST\s0, if any.  In list context,
5390 returns the elements removed from the array.  In scalar context,
5391 returns the last element removed, or \f(CW\*(C`undef\*(C'\fR if no elements are
5392 removed.  The array grows or shrinks as necessary.
5393 If \s-1OFFSET\s0 is negative then it starts that far from the end of the array.
5394 If \s-1LENGTH\s0 is omitted, removes everything from \s-1OFFSET\s0 onward.
5395 If \s-1LENGTH\s0 is negative, removes the elements from \s-1OFFSET\s0 onward
5396 except for \-LENGTH elements at the end of the array.
5397 If both \s-1OFFSET\s0 and \s-1LENGTH\s0 are omitted, removes everything. If \s-1OFFSET\s0 is
5398 past the end of the array, perl issues a warning, and splices at the
5399 end of the array.
5401 The following equivalences hold (assuming \f(CW\*(C`$[ == 0 and $#a >= $i\*(C'\fR )
5403 .Vb 5
5404 \&    push(@a,$x,$y)      splice(@a,@a,0,$x,$y)
5405 \&    pop(@a)             splice(@a,-1)
5406 \&    shift(@a)           splice(@a,0,1)
5407 \&    unshift(@a,$x,$y)   splice(@a,0,0,$x,$y)
5408 \&    $a[$i] = $y         splice(@a,$i,1,$y)
5411 Example, assuming array lengths are passed before arrays:
5413 .Vb 10
5414 \&    sub aeq {   # compare two list values
5415 \&        my(@a) = splice(@_,0,shift);
5416 \&        my(@b) = splice(@_,0,shift);
5417 \&        return 0 unless @a == @b;       # same len?
5418 \&        while (@a) {
5419 \&            return 0 if pop(@a) ne pop(@b);
5420 \&        }
5421 \&        return 1;
5422 \&    }
5423 \&    if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }
5425 .IP "split /PATTERN/,EXPR,LIMIT" 8
5426 .IX Item "split /PATTERN/,EXPR,LIMIT"
5427 .PD 0
5428 .IP "split /PATTERN/,EXPR" 8
5429 .IX Item "split /PATTERN/,EXPR"
5430 .IP "split /PATTERN/" 8
5431 .IX Item "split /PATTERN/"
5432 .IP "split" 8
5433 .IX Item "split"
5435 Splits a string into a list of strings and returns that list.  By default,
5436 empty leading fields are preserved, and empty trailing ones are deleted.
5438 In scalar context, returns the number of fields found and splits into
5439 the \f(CW@_\fR array.  Use of split in scalar context is deprecated, however,
5440 because it clobbers your subroutine arguments.
5442 If \s-1EXPR\s0 is omitted, splits the \f(CW$_\fR string.  If \s-1PATTERN\s0 is also omitted,
5443 splits on whitespace (after skipping any leading whitespace).  Anything
5444 matching \s-1PATTERN\s0 is taken to be a delimiter separating the fields.  (Note
5445 that the delimiter may be longer than one character.)
5447 If \s-1LIMIT\s0 is specified and positive, it represents the maximum number
5448 of fields the \s-1EXPR\s0 will be split into, though the actual number of
5449 fields returned depends on the number of times \s-1PATTERN\s0 matches within
5450 \&\s-1EXPR\s0.  If \s-1LIMIT\s0 is unspecified or zero, trailing null fields are
5451 stripped (which potential users of \f(CW\*(C`pop\*(C'\fR would do well to remember).
5452 If \s-1LIMIT\s0 is negative, it is treated as if an arbitrarily large \s-1LIMIT\s0
5453 had been specified.  Note that splitting an \s-1EXPR\s0 that evaluates to the
5454 empty string always returns the empty list, regardless of the \s-1LIMIT\s0
5455 specified.
5457 A pattern matching the null string (not to be confused with
5458 a null pattern \f(CW\*(C`//\*(C'\fR, which is just one member of the set of patterns
5459 matching a null string) will split the value of \s-1EXPR\s0 into separate
5460 characters at each point it matches that way.  For example:
5462 .Vb 1
5463 \&    print join(':', split(/ */, 'hi there'));
5466 produces the output 'h:i:t:h:e:r:e'.
5468 Using the empty pattern \f(CW\*(C`//\*(C'\fR specifically matches the null string, and is
5469 not be confused with the use of \f(CW\*(C`//\*(C'\fR to mean \*(L"the last successful pattern
5470 match\*(R".
5472 Empty leading (or trailing) fields are produced when there are positive width
5473 matches at the beginning (or end) of the string; a zero-width match at the
5474 beginning (or end) of the string does not produce an empty field.  For
5475 example:
5477 .Vb 1
5478 \&   print join(':', split(/(?=\ew)/, 'hi there!'));
5481 produces the output 'h:i :t:h:e:r:e!'.
5483 The \s-1LIMIT\s0 parameter can be used to split a line partially
5485 .Vb 1
5486 \&    ($login, $passwd, $remainder) = split(/:/, $_, 3);
5489 When assigning to a list, if \s-1LIMIT\s0 is omitted, or zero, Perl supplies
5490 a \s-1LIMIT\s0 one larger than the number of variables in the list, to avoid
5491 unnecessary work.  For the list above \s-1LIMIT\s0 would have been 4 by
5492 default.  In time critical applications it behooves you not to split
5493 into more fields than you really need.
5495 If the \s-1PATTERN\s0 contains parentheses, additional list elements are
5496 created from each matching substring in the delimiter.
5498 .Vb 1
5499 \&    split(/([,-])/, "1-10,20", 3);
5502 produces the list value
5504 .Vb 1
5505 \&    (1, '-', 10, ',', 20)
5508 If you had the entire header of a normal Unix email message in \f(CW$header\fR,
5509 you could split it up into fields and their values this way:
5511 .Vb 2
5512 \&    $header =~ s/\en\es+/ /g;  # fix continuation lines
5513 \&    %hdrs   =  (UNIX_FROM => split /^(\eS*?):\es*/m, $header);
5516 The pattern \f(CW\*(C`/PATTERN/\*(C'\fR may be replaced with an expression to specify
5517 patterns that vary at runtime.  (To do runtime compilation only once,
5518 use \f(CW\*(C`/$variable/o\*(C'\fR.)
5520 As a special case, specifying a \s-1PATTERN\s0 of space (\f(CW'\ '\fR) will split on
5521 white space just as \f(CW\*(C`split\*(C'\fR with no arguments does.  Thus, \f(CW\*(C`split('\ ')\*(C'\fR can
5522 be used to emulate \fBawk\fR's default behavior, whereas \f(CW\*(C`split(/\ /)\*(C'\fR
5523 will give you as many null initial fields as there are leading spaces.
5524 A \f(CW\*(C`split\*(C'\fR on \f(CW\*(C`/\es+/\*(C'\fR is like a \f(CW\*(C`split('\ ')\*(C'\fR except that any leading
5525 whitespace produces a null first field.  A \f(CW\*(C`split\*(C'\fR with no arguments
5526 really does a \f(CW\*(C`split('\ ',\ $_)\*(C'\fR internally.
5528 A \s-1PATTERN\s0 of \f(CW\*(C`/^/\*(C'\fR is treated as if it were \f(CW\*(C`/^/m\*(C'\fR, since it isn't
5529 much use otherwise.
5531 Example:
5533 .Vb 7
5534 \&    open(PASSWD, '/etc/passwd');
5535 \&    while (<PASSWD>) {
5536 \&        chomp;
5537 \&        ($login, $passwd, $uid, $gid,
5538 \&         $gcos, $home, $shell) = split(/:/);
5539 \&        #...
5540 \&    }
5543 As with regular pattern matching, any capturing parentheses that are not
5544 matched in a \f(CW\*(C`split()\*(C'\fR will be set to \f(CW\*(C`undef\*(C'\fR when returned:
5546 .Vb 2
5547 \&    @fields = split /(A)|B/, "1A2B3";
5548 \&    # @fields is (1, 'A', 2, undef, 3)
5550 .IP "sprintf \s-1FORMAT\s0, \s-1LIST\s0" 8
5551 .IX Item "sprintf FORMAT, LIST"
5552 Returns a string formatted by the usual \f(CW\*(C`printf\*(C'\fR conventions of the C
5553 library function \f(CW\*(C`sprintf\*(C'\fR.  See below for more details
5554 and see \fIsprintf\fR\|(3) or \fIprintf\fR\|(3) on your system for an explanation of
5555 the general principles.
5557 For example:
5559 .Vb 2
5560 \&        # Format number with up to 8 leading zeroes
5561 \&        $result = sprintf("%08d", $number);
5564 .Vb 2
5565 \&        # Round number to 3 digits after decimal point
5566 \&        $rounded = sprintf("%.3f", $number);
5569 Perl does its own \f(CW\*(C`sprintf\*(C'\fR formatting\*(--it emulates the C
5570 function \f(CW\*(C`sprintf\*(C'\fR, but it doesn't use it (except for floating-point
5571 numbers, and even then only the standard modifiers are allowed).  As a
5572 result, any non-standard extensions in your local \f(CW\*(C`sprintf\*(C'\fR are not
5573 available from Perl.
5575 Unlike \f(CW\*(C`printf\*(C'\fR, \f(CW\*(C`sprintf\*(C'\fR does not do what you probably mean when you
5576 pass it an array as your first argument. The array is given scalar context,
5577 and instead of using the 0th element of the array as the format, Perl will
5578 use the count of elements in the array as the format, which is almost never
5579 useful.
5581 Perl's \f(CW\*(C`sprintf\*(C'\fR permits the following universally-known conversions:
5583 .Vb 10
5584 \&   %%   a percent sign
5585 \&   %c   a character with the given number
5586 \&   %s   a string
5587 \&   %d   a signed integer, in decimal
5588 \&   %u   an unsigned integer, in decimal
5589 \&   %o   an unsigned integer, in octal
5590 \&   %x   an unsigned integer, in hexadecimal
5591 \&   %e   a floating-point number, in scientific notation
5592 \&   %f   a floating-point number, in fixed decimal notation
5593 \&   %g   a floating-point number, in %e or %f notation
5596 In addition, Perl permits the following widely-supported conversions:
5598 .Vb 7
5599 \&   %X   like %x, but using upper-case letters
5600 \&   %E   like %e, but using an upper-case "E"
5601 \&   %G   like %g, but with an upper-case "E" (if applicable)
5602 \&   %b   an unsigned integer, in binary
5603 \&   %p   a pointer (outputs the Perl value's address in hexadecimal)
5604 \&   %n   special: *stores* the number of characters output so far
5605 \&        into the next variable in the parameter list
5608 Finally, for backward (and we do mean \*(L"backward\*(R") compatibility, Perl
5609 permits these unnecessary but widely-supported conversions:
5611 .Vb 5
5612 \&   %i   a synonym for %d
5613 \&   %D   a synonym for %ld
5614 \&   %U   a synonym for %lu
5615 \&   %O   a synonym for %lo
5616 \&   %F   a synonym for %f
5619 Note that the number of exponent digits in the scientific notation produced
5620 by \f(CW%e\fR, \f(CW%E\fR, \f(CW%g\fR and \f(CW%G\fR for numbers with the modulus of the
5621 exponent less than 100 is system\-dependent: it may be three or less
5622 (zero\-padded as necessary).  In other words, 1.23 times ten to the
5623 99th may be either \*(L"1.23e99\*(R" or \*(L"1.23e099\*(R".
5625 Between the \f(CW\*(C`%\*(C'\fR and the format letter, you may specify a number of
5626 additional attributes controlling the interpretation of the format.
5627 In order, these are:
5628 .RS 8
5629 .IP "format parameter index" 4
5630 .IX Item "format parameter index"
5631 An explicit format parameter index, such as \f(CW\*(C`2$\*(C'\fR. By default sprintf
5632 will format the next unused argument in the list, but this allows you
5633 to take the arguments out of order. Eg:
5635 .Vb 2
5636 \&  printf '%2$d %1$d', 12, 34;      # prints "34 12"
5637 \&  printf '%3$d %d %1$d', 1, 2, 3;  # prints "3 1 1"
5639 .IP "flags" 4
5640 .IX Item "flags"
5641 one or more of:
5642    space   prefix positive number with a space
5643    +       prefix positive number with a plus sign
5644    \-       left-justify within the field
5645    0       use zeros, not spaces, to right-justify
5646    #       prefix non-zero octal with \*(L"0\*(R", non-zero hex with \*(L"0x\*(R",
5647            non-zero binary with \*(L"0b\*(R"
5649 For example:
5651 .Vb 6
5652 \&  printf '<% d>', 12;   # prints "< 12>"
5653 \&  printf '<%+d>', 12;   # prints "<+12>"
5654 \&  printf '<%6s>', 12;   # prints "<    12>"
5655 \&  printf '<%-6s>', 12;  # prints "<12    >"
5656 \&  printf '<%06s>', 12;  # prints "<000012>"
5657 \&  printf '<%#x>', 12;   # prints "<0xc>"
5659 .IP "vector flag" 4
5660 .IX Item "vector flag"
5661 The vector flag \f(CW\*(C`v\*(C'\fR, optionally specifying the join string to use.
5662 This flag tells perl to interpret the supplied string as a vector
5663 of integers, one for each character in the string, separated by
5664 a given string (a dot \f(CW\*(C`.\*(C'\fR by default). This can be useful for
5665 displaying ordinal values of characters in arbitrary strings:
5667 .Vb 1
5668 \&  printf "version is v%vd\en", $^V;     # Perl's version
5671 Put an asterisk \f(CW\*(C`*\*(C'\fR before the \f(CW\*(C`v\*(C'\fR to override the string to
5672 use to separate the numbers:
5674 .Vb 2
5675 \&  printf "address is %*vX\en", ":", $addr;   # IPv6 address
5676 \&  printf "bits are %0*v8b\en", " ", $bits;   # random bitstring
5679 You can also explicitly specify the argument number to use for
5680 the join string using eg \f(CW\*(C`*2$v\*(C'\fR:
5682 .Vb 1
5683 \&  printf '%*4$vX %*4$vX %*4$vX', @addr[1..3], ":";   # 3 IPv6 addresses
5685 .IP "(minimum) width" 4
5686 .IX Item "(minimum) width"
5687 Arguments are usually formatted to be only as wide as required to
5688 display the given value. You can override the width by putting
5689 a number here, or get the width from the next argument (with \f(CW\*(C`*\*(C'\fR)
5690 or from a specified argument (with eg \f(CW\*(C`*2$\*(C'\fR):
5692 .Vb 5
5693 \&  printf '<%s>', "a";       # prints "<a>"
5694 \&  printf '<%6s>', "a";      # prints "<     a>"
5695 \&  printf '<%*s>', 6, "a";   # prints "<     a>"
5696 \&  printf '<%*2$s>', "a", 6; # prints "<     a>"
5697 \&  printf '<%2s>', "long";   # prints "<long>" (does not truncate)
5700 If a field width obtained through \f(CW\*(C`*\*(C'\fR is negative, it has the same
5701 effect as the \f(CW\*(C`\-\*(C'\fR flag: left\-justification.
5702 .IP "precision, or maximum width" 4
5703 .IX Item "precision, or maximum width"
5704 You can specify a precision (for numeric conversions) or a maximum
5705 width (for string conversions) by specifying a \f(CW\*(C`.\*(C'\fR followed by a number.
5706 For floating point formats, with the exception of 'g' and 'G', this specifies
5707 the number of decimal places to show (the default being 6), eg:
5709 .Vb 6
5710 \&  # these examples are subject to system-specific variation
5711 \&  printf '<%f>', 1;    # prints "<1.000000>"
5712 \&  printf '<%.1f>', 1;  # prints "<1.0>"
5713 \&  printf '<%.0f>', 1;  # prints "<1>"
5714 \&  printf '<%e>', 10;   # prints "<1.000000e+01>"
5715 \&  printf '<%.1e>', 10; # prints "<1.0e+01>"
5718 For 'g' and 'G', this specifies the maximum number of digits to show,
5719 including prior to the decimal point as well as after it, eg:
5721 .Vb 8
5722 \&  # these examples are subject to system-specific variation
5723 \&  printf '<%g>', 1;        # prints "<1>"
5724 \&  printf '<%.10g>', 1;     # prints "<1>"
5725 \&  printf '<%g>', 100;      # prints "<100>"
5726 \&  printf '<%.1g>', 100;    # prints "<1e+02>"
5727 \&  printf '<%.2g>', 100.01; # prints "<1e+02>"
5728 \&  printf '<%.5g>', 100.01; # prints "<100.01>"
5729 \&  printf '<%.4g>', 100.01; # prints "<100>"
5732 For integer conversions, specifying a precision implies that the
5733 output of the number itself should be zero-padded to this width:
5735 .Vb 3
5736 \&  printf '<%.6x>', 1;      # prints "<000001>"
5737 \&  printf '<%#.6x>', 1;     # prints "<0x000001>"
5738 \&  printf '<%-10.6x>', 1;   # prints "<000001    >"
5741 For string conversions, specifying a precision truncates the string
5742 to fit in the specified width:
5744 .Vb 2
5745 \&  printf '<%.5s>', "truncated";   # prints "<trunc>"
5746 \&  printf '<%10.5s>', "truncated"; # prints "<     trunc>"
5749 You can also get the precision from the next argument using \f(CW\*(C`.*\*(C'\fR:
5751 .Vb 2
5752 \&  printf '<%.6x>', 1;       # prints "<000001>"
5753 \&  printf '<%.*x>', 6, 1;    # prints "<000001>"
5756 You cannot currently get the precision from a specified number,
5757 but it is intended that this will be possible in the future using
5758 eg \f(CW\*(C`.*2$\*(C'\fR:
5760 .Vb 1
5761 \&  printf '<%.*2$x>', 1, 6;   # INVALID, but in future will print "<000001>"
5763 .IP "size" 4
5764 .IX Item "size"
5765 For numeric conversions, you can specify the size to interpret the
5766 number as using \f(CW\*(C`l\*(C'\fR, \f(CW\*(C`h\*(C'\fR, \f(CW\*(C`V\*(C'\fR, \f(CW\*(C`q\*(C'\fR, \f(CW\*(C`L\*(C'\fR, or \f(CW\*(C`ll\*(C'\fR. For integer
5767 conversions (\f(CW\*(C`d u o x X b i D U O\*(C'\fR), numbers are usually assumed to be
5768 whatever the default integer size is on your platform (usually 32 or 64
5769 bits), but you can override this to use instead one of the standard C types,
5770 as supported by the compiler used to build Perl:
5772 .Vb 4
5773 \&   l           interpret integer as C type "long" or "unsigned long"
5774 \&   h           interpret integer as C type "short" or "unsigned short"
5775 \&   q, L or ll  interpret integer as C type "long long", "unsigned long long".
5776 \&               or "quads" (typically 64-bit integers)
5779 The last will produce errors if Perl does not understand \*(L"quads\*(R" in your
5780 installation. (This requires that either the platform natively supports quads
5781 or Perl was specifically compiled to support quads.) You can find out
5782 whether your Perl supports quads via Config:
5784 .Vb 3
5785 \&        use Config;
5786 \&        ($Config{use64bitint} eq 'define' || $Config{longsize} >= 8) &&
5787 \&                print "quads\en";
5790 For floating point conversions (\f(CW\*(C`e f g E F G\*(C'\fR), numbers are usually assumed
5791 to be the default floating point size on your platform (double or long double),
5792 but you can force 'long double' with \f(CW\*(C`q\*(C'\fR, \f(CW\*(C`L\*(C'\fR, or \f(CW\*(C`ll\*(C'\fR if your
5793 platform supports them. You can find out whether your Perl supports long
5794 doubles via Config:
5796 .Vb 2
5797 \&        use Config;
5798 \&        $Config{d_longdbl} eq 'define' && print "long doubles\en";
5801 You can find out whether Perl considers 'long double' to be the default
5802 floating point size to use on your platform via Config:
5804 .Vb 3
5805 \&        use Config;
5806 \&        ($Config{uselongdouble} eq 'define') &&
5807 \&                print "long doubles by default\en";
5810 It can also be the case that long doubles and doubles are the same thing:
5812 .Vb 3
5813 \&        use Config;
5814 \&        ($Config{doublesize} == $Config{longdblsize}) &&
5815 \&                print "doubles are long doubles\en";
5818 The size specifier \f(CW\*(C`V\*(C'\fR has no effect for Perl code, but it is supported
5819 for compatibility with \s-1XS\s0 code; it means 'use the standard size for
5820 a Perl integer (or floating-point number)', which is already the
5821 default for Perl code.
5822 .IP "order of arguments" 4
5823 .IX Item "order of arguments"
5824 Normally, sprintf takes the next unused argument as the value to
5825 format for each format specification. If the format specification
5826 uses \f(CW\*(C`*\*(C'\fR to require additional arguments, these are consumed from
5827 the argument list in the order in which they appear in the format
5828 specification \fIbefore\fR the value to format. Where an argument is
5829 specified using an explicit index, this does not affect the normal
5830 order for the arguments (even when the explicitly specified index
5831 would have been the next argument in any case).
5835 .Vb 1
5836 \&  printf '<%*.*s>', $a, $b, $c;
5839 would use \f(CW$a\fR for the width, \f(CW$b\fR for the precision and \f(CW$c\fR
5840 as the value to format, while:
5842 .Vb 1
5843 \&  print '<%*1$.*s>', $a, $b;
5846 would use \f(CW$a\fR for the width and the precision, and \f(CW$b\fR as the
5847 value to format.
5849 Here are some more examples \- beware that when using an explicit
5850 index, the \f(CW\*(C`$\*(C'\fR may need to be escaped:
5852 .Vb 4
5853 \&  printf "%2\e$d %d\en",    12, 34;               # will print "34 12\en"
5854 \&  printf "%2\e$d %d %d\en", 12, 34;               # will print "34 12 34\en"
5855 \&  printf "%3\e$d %d %d\en", 12, 34, 56;           # will print "56 12 34\en"
5856 \&  printf "%2\e$*3\e$d %d\en", 12, 34, 3;           # will print " 34 12\en"
5859 .RS 8
5861 If \f(CW\*(C`use locale\*(C'\fR is in effect, the character used for the decimal
5862 point in formatted real numbers is affected by the \s-1LC_NUMERIC\s0 locale.
5863 See perllocale.
5865 .IP "sqrt \s-1EXPR\s0" 8
5866 .IX Item "sqrt EXPR"
5867 .PD 0
5868 .IP "sqrt" 8
5869 .IX Item "sqrt"
5871 Return the square root of \s-1EXPR\s0.  If \s-1EXPR\s0 is omitted, returns square
5872 root of \f(CW$_\fR.  Only works on non-negative operands, unless you've
5873 loaded the standard Math::Complex module.
5875 .Vb 2
5876 \&    use Math::Complex;
5877 \&    print sqrt(-2);    # prints 1.4142135623731i
5879 .IP "srand \s-1EXPR\s0" 8
5880 .IX Item "srand EXPR"
5881 .PD 0
5882 .IP "srand" 8
5883 .IX Item "srand"
5885 Sets the random number seed for the \f(CW\*(C`rand\*(C'\fR operator.
5887 The point of the function is to \*(L"seed\*(R" the \f(CW\*(C`rand\*(C'\fR function so that
5888 \&\f(CW\*(C`rand\*(C'\fR can produce a different sequence each time you run your
5889 program.
5891 If \fIsrand()\fR is not called explicitly, it is called implicitly at the
5892 first use of the \f(CW\*(C`rand\*(C'\fR operator.  However, this was not the case in
5893 versions of Perl before 5.004, so if your script will run under older
5894 Perl versions, it should call \f(CW\*(C`srand\*(C'\fR.
5896 Most programs won't even call \fIsrand()\fR at all, except those that
5897 need a cryptographically-strong starting point rather than the
5898 generally acceptable default, which is based on time of day,
5899 process \s-1ID\s0, and memory allocation, or the \fI/dev/urandom\fR device,
5900 if available.
5902 You can call srand($seed) with the same \f(CW$seed\fR to reproduce the
5903 \&\fIsame\fR sequence from \fIrand()\fR, but this is usually reserved for
5904 generating predictable results for testing or debugging.
5905 Otherwise, don't call \fIsrand()\fR more than once in your program.
5907 Do \fBnot\fR call \fIsrand()\fR (i.e. without an argument) more than once in
5908 a script.  The internal state of the random number generator should
5909 contain more entropy than can be provided by any seed, so calling
5910 \&\fIsrand()\fR again actually \fIloses\fR randomness.
5912 Most implementations of \f(CW\*(C`srand\*(C'\fR take an integer and will silently
5913 truncate decimal numbers.  This means \f(CW\*(C`srand(42)\*(C'\fR will usually
5914 produce the same results as \f(CW\*(C`srand(42.1)\*(C'\fR.  To be safe, always pass
5915 \&\f(CW\*(C`srand\*(C'\fR an integer.
5917 In versions of Perl prior to 5.004 the default seed was just the
5918 current \f(CW\*(C`time\*(C'\fR.  This isn't a particularly good seed, so many old
5919 programs supply their own seed value (often \f(CW\*(C`time ^ $$\*(C'\fR or \f(CW\*(C`time ^
5920 ($$ + ($$ << 15))\*(C'\fR), but that isn't necessary any more.
5922 Note that you need something much more random than the default seed for
5923 cryptographic purposes.  Checksumming the compressed output of one or more
5924 rapidly changing operating system status programs is the usual method.  For
5925 example:
5927 .Vb 1
5928 \&    srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
5931 If you're particularly concerned with this, see the \f(CW\*(C`Math::TrulyRandom\*(C'\fR
5932 module in \s-1CPAN\s0.
5934 Frequently called programs (like \s-1CGI\s0 scripts) that simply use
5936 .Vb 1
5937 \&    time ^ $$
5940 for a seed can fall prey to the mathematical property that
5942 .Vb 1
5943 \&    a^b == (a+1)^(b+1)
5946 one-third of the time.  So don't do that.
5947 .IP "stat \s-1FILEHANDLE\s0" 8
5948 .IX Item "stat FILEHANDLE"
5949 .PD 0
5950 .IP "stat \s-1EXPR\s0" 8
5951 .IX Item "stat EXPR"
5952 .IP "stat" 8
5953 .IX Item "stat"
5955 Returns a 13\-element list giving the status info for a file, either
5956 the file opened via \s-1FILEHANDLE\s0, or named by \s-1EXPR\s0.  If \s-1EXPR\s0 is omitted,
5957 it stats \f(CW$_\fR.  Returns a null list if the stat fails.  Typically used
5958 as follows:
5960 .Vb 3
5961 \&    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
5962 \&       $atime,$mtime,$ctime,$blksize,$blocks)
5963 \&           = stat($filename);
5966 Not all fields are supported on all filesystem types.  Here are the
5967 meaning of the fields:
5969 .Vb 13
5970 \&  0 dev      device number of filesystem
5971 \&  1 ino      inode number
5972 \&  2 mode     file mode  (type and permissions)
5973 \&  3 nlink    number of (hard) links to the file
5974 \&  4 uid      numeric user ID of file's owner
5975 \&  5 gid      numeric group ID of file's owner
5976 \&  6 rdev     the device identifier (special files only)
5977 \&  7 size     total size of file, in bytes
5978 \&  8 atime    last access time in seconds since the epoch
5979 \&  9 mtime    last modify time in seconds since the epoch
5980 \& 10 ctime    inode change time in seconds since the epoch (*)
5981 \& 11 blksize  preferred block size for file system I/O
5982 \& 12 blocks   actual number of blocks allocated
5985 (The epoch was at 00:00 January 1, 1970 \s-1GMT\s0.)
5987 (*) The ctime field is non\-portable, in particular you cannot expect
5988 it to be a \*(L"creation time\*(R", see \*(L"Files and Filesystems\*(R" in perlport
5989 for details.
5991 If stat is passed the special filehandle consisting of an underline, no
5992 stat is done, but the current contents of the stat structure from the
5993 last stat or filetest are returned.  Example:
5995 .Vb 3
5996 \&    if (-x $file && (($d) = stat(_)) && $d < 0) {
5997 \&        print "$file is executable NFS file\en";
5998 \&    }
6001 (This works on machines only for which the device number is negative
6002 under \s-1NFS\s0.)
6004 Because the mode contains both the file type and its permissions, you
6005 should mask off the file type portion and (s)printf using a \f(CW"%o"\fR
6006 if you want to see the real permissions.
6008 .Vb 2
6009 \&    $mode = (stat($filename))[2];
6010 \&    printf "Permissions are %04o\en", $mode & 07777;
6013 In scalar context, \f(CW\*(C`stat\*(C'\fR returns a boolean value indicating success
6014 or failure, and, if successful, sets the information associated with
6015 the special filehandle \f(CW\*(C`_\*(C'\fR.
6017 The File::stat module provides a convenient, by-name access mechanism:
6019 .Vb 5
6020 \&    use File::stat;
6021 \&    $sb = stat($filename);
6022 \&    printf "File is %s, size is %s, perm %04o, mtime %s\en",
6023 \&        $filename, $sb->size, $sb->mode & 07777,
6024 \&        scalar localtime $sb->mtime;
6027 You can import symbolic mode constants (\f(CW\*(C`S_IF*\*(C'\fR) and functions
6028 (\f(CW\*(C`S_IS*\*(C'\fR) from the Fcntl module:
6030 .Vb 1
6031 \&    use Fcntl ':mode';
6034 .Vb 1
6035 \&    $mode = (stat($filename))[2];
6038 .Vb 3
6039 \&    $user_rwx      = ($mode & S_IRWXU) >> 6;
6040 \&    $group_read    = ($mode & S_IRGRP) >> 3;
6041 \&    $other_execute =  $mode & S_IXOTH;
6044 .Vb 1
6045 \&    printf "Permissions are %04o\en", S_IMODE($mode), "\en";
6048 .Vb 2
6049 \&    $is_setuid     =  $mode & S_ISUID;
6050 \&    $is_setgid     =  S_ISDIR($mode);
6053 You could write the last two using the \f(CW\*(C`\-u\*(C'\fR and \f(CW\*(C`\-d\*(C'\fR operators.
6054 The commonly available S_IF* constants are
6056 .Vb 1
6057 \&    # Permissions: read, write, execute, for user, group, others.
6060 .Vb 3
6061 \&    S_IRWXU S_IRUSR S_IWUSR S_IXUSR
6062 \&    S_IRWXG S_IRGRP S_IWGRP S_IXGRP
6063 \&    S_IRWXO S_IROTH S_IWOTH S_IXOTH
6066 .Vb 2
6067 \&    # Setuid/Setgid/Stickiness/SaveText.
6068 \&    # Note that the exact meaning of these is system dependent.
6071 .Vb 1
6072 \&    S_ISUID S_ISGID S_ISVTX S_ISTXT
6075 .Vb 1
6076 \&    # File types.  Not necessarily all are available on your system.
6079 .Vb 1
6080 \&    S_IFREG S_IFDIR S_IFLNK S_IFBLK S_ISCHR S_IFIFO S_IFSOCK S_IFWHT S_ENFMT
6083 .Vb 1
6084 \&    # The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR.
6087 .Vb 1
6088 \&    S_IREAD S_IWRITE S_IEXEC
6091 and the S_IF* functions are
6093 .Vb 2
6094 \&    S_IMODE($mode)      the part of $mode containing the permission bits
6095 \&                        and the setuid/setgid/sticky bits
6098 .Vb 3
6099 \&    S_IFMT($mode)       the part of $mode containing the file type
6100 \&                        which can be bit-anded with e.g. S_IFREG
6101 \&                        or with the following functions
6104 .Vb 1
6105 \&    # The operators -f, -d, -l, -b, -c, -p, and -s.
6108 .Vb 2
6109 \&    S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)
6110 \&    S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)
6113 .Vb 3
6114 \&    # No direct -X operator counterpart, but for the first one
6115 \&    # the -g operator is often equivalent.  The ENFMT stands for
6116 \&    # record flocking enforcement, a platform-dependent feature.
6119 .Vb 1
6120 \&    S_ISENFMT($mode) S_ISWHT($mode)
6123 See your native \fIchmod\fR\|(2) and \fIstat\fR\|(2) documentation for more details
6124 about the S_* constants.
6126 To get status info for a symbolic link instead of the target file
6127 behind the link, use the \f(CW\*(C`lstat\*(C'\fR function, see \*(L"stat\*(R".
6128 .IP "study \s-1SCALAR\s0" 8
6129 .IX Item "study SCALAR"
6130 .PD 0
6131 .IP "study" 8
6132 .IX Item "study"
6134 Takes extra time to study \s-1SCALAR\s0 (\f(CW$_\fR if unspecified) in anticipation of
6135 doing many pattern matches on the string before it is next modified.
6136 This may or may not save time, depending on the nature and number of
6137 patterns you are searching on, and on the distribution of character
6138 frequencies in the string to be searched\*(--you probably want to compare
6139 run times with and without it to see which runs faster.  Those loops
6140 which scan for many short constant strings (including the constant
6141 parts of more complex patterns) will benefit most.  You may have only
6142 one \f(CW\*(C`study\*(C'\fR active at a time\*(--if you study a different scalar the first
6143 is \*(L"unstudied\*(R".  (The way \f(CW\*(C`study\*(C'\fR works is this: a linked list of every
6144 character in the string to be searched is made, so we know, for
6145 example, where all the \f(CW'k'\fR characters are.  From each search string,
6146 the rarest character is selected, based on some static frequency tables
6147 constructed from some C programs and English text.  Only those places
6148 that contain this \*(L"rarest\*(R" character are examined.)
6150 For example, here is a loop that inserts index producing entries
6151 before any line containing a certain pattern:
6153 .Vb 8
6154 \&    while (<>) {
6155 \&        study;
6156 \&        print ".IX foo\en"       if /\ebfoo\eb/;
6157 \&        print ".IX bar\en"       if /\ebbar\eb/;
6158 \&        print ".IX blurfl\en"    if /\ebblurfl\eb/;
6159 \&        # ...
6160 \&        print;
6161 \&    }
6164 In searching for \f(CW\*(C`/\ebfoo\eb/\*(C'\fR, only those locations in \f(CW$_\fR that contain \f(CW\*(C`f\*(C'\fR
6165 will be looked at, because \f(CW\*(C`f\*(C'\fR is rarer than \f(CW\*(C`o\*(C'\fR.  In general, this is
6166 a big win except in pathological cases.  The only question is whether
6167 it saves you more time than it took to build the linked list in the
6168 first place.
6170 Note that if you have to look for strings that you don't know till
6171 runtime, you can build an entire loop as a string and \f(CW\*(C`eval\*(C'\fR that to
6172 avoid recompiling all your patterns all the time.  Together with
6173 undefining \f(CW$/\fR to input entire files as one record, this can be very
6174 fast, often faster than specialized programs like \fIfgrep\fR\|(1).  The following
6175 scans a list of files (\f(CW@files\fR) for a list of words (\f(CW@words\fR), and prints
6176 out the names of those files that contain a match:
6178 .Vb 12
6179 \&    $search = 'while (<>) { study;';
6180 \&    foreach $word (@words) {
6181 \&        $search .= "++\e$seen{\e$ARGV} if /\e\eb$word\e\eb/;\en";
6182 \&    }
6183 \&    $search .= "}";
6184 \&    @ARGV = @files;
6185 \&    undef $/;
6186 \&    eval $search;               # this screams
6187 \&    $/ = "\en";          # put back to normal input delimiter
6188 \&    foreach $file (sort keys(%seen)) {
6189 \&        print $file, "\en";
6190 \&    }
6192 .IP "sub \s-1NAME\s0 \s-1BLOCK\s0" 8
6193 .IX Item "sub NAME BLOCK"
6194 .PD 0
6195 .IP "sub \s-1NAME\s0 (\s-1PROTO\s0) \s-1BLOCK\s0" 8
6196 .IX Item "sub NAME (PROTO) BLOCK"
6197 .IP "sub \s-1NAME\s0 : \s-1ATTRS\s0 \s-1BLOCK\s0" 8
6198 .IX Item "sub NAME : ATTRS BLOCK"
6199 .IP "sub \s-1NAME\s0 (\s-1PROTO\s0) : \s-1ATTRS\s0 \s-1BLOCK\s0" 8
6200 .IX Item "sub NAME (PROTO) : ATTRS BLOCK"
6202 This is subroutine definition, not a real function \fIper se\fR.
6203 Without a \s-1BLOCK\s0 it's just a forward declaration.  Without a \s-1NAME\s0,
6204 it's an anonymous function declaration, and does actually return
6205 a value: the \s-1CODE\s0 ref of the closure you just created.
6207 See perlsub and perlref for details about subroutines and
6208 references, and attributes and Attribute::Handlers for more
6209 information about attributes.
6210 .IP "substr \s-1EXPR\s0,OFFSET,LENGTH,REPLACEMENT" 8
6211 .IX Item "substr EXPR,OFFSET,LENGTH,REPLACEMENT"
6212 .PD 0
6213 .IP "substr \s-1EXPR\s0,OFFSET,LENGTH" 8
6214 .IX Item "substr EXPR,OFFSET,LENGTH"
6215 .IP "substr \s-1EXPR\s0,OFFSET" 8
6216 .IX Item "substr EXPR,OFFSET"
6218 Extracts a substring out of \s-1EXPR\s0 and returns it.  First character is at
6219 offset \f(CW0\fR, or whatever you've set \f(CW$[\fR to (but don't do that).
6220 If \s-1OFFSET\s0 is negative (or more precisely, less than \f(CW$[\fR), starts
6221 that far from the end of the string.  If \s-1LENGTH\s0 is omitted, returns
6222 everything to the end of the string.  If \s-1LENGTH\s0 is negative, leaves that
6223 many characters off the end of the string.
6225 You can use the \fIsubstr()\fR function as an lvalue, in which case \s-1EXPR\s0
6226 must itself be an lvalue.  If you assign something shorter than \s-1LENGTH\s0,
6227 the string will shrink, and if you assign something longer than \s-1LENGTH\s0,
6228 the string will grow to accommodate it.  To keep the string the same
6229 length you may need to pad or chop your value using \f(CW\*(C`sprintf\*(C'\fR.
6231 If \s-1OFFSET\s0 and \s-1LENGTH\s0 specify a substring that is partly outside the
6232 string, only the part within the string is returned.  If the substring
6233 is beyond either end of the string, \fIsubstr()\fR returns the undefined
6234 value and produces a warning.  When used as an lvalue, specifying a
6235 substring that is entirely outside the string is a fatal error.
6236 Here's an example showing the behavior for boundary cases:
6238 .Vb 5
6239 \&    my $name = 'fred';
6240 \&    substr($name, 4) = 'dy';            # $name is now 'freddy'
6241 \&    my $null = substr $name, 6, 2;      # returns '' (no warning)
6242 \&    my $oops = substr $name, 7;         # returns undef, with warning
6243 \&    substr($name, 7) = 'gap';           # fatal error
6246 An alternative to using \fIsubstr()\fR as an lvalue is to specify the
6247 replacement string as the 4th argument.  This allows you to replace
6248 parts of the \s-1EXPR\s0 and return what was there before in one operation,
6249 just as you can with \fIsplice()\fR.
6251 If the lvalue returned by substr is used after the \s-1EXPR\s0 is changed in
6252 any way, the behaviour may not be as expected and is subject to change.
6253 This caveat includes code such as \f(CW\*(C`print(substr($foo,$a,$b)=$bar)\*(C'\fR or
6254 \&\f(CW\*(C`(substr($foo,$a,$b)=$bar)=$fud\*(C'\fR (where \f(CW$foo\fR is changed via the
6255 substring assignment, and then the substr is used again), or where a
6256 \&\fIsubstr()\fR is aliased via a \f(CW\*(C`foreach\*(C'\fR loop or passed as a parameter or
6257 a reference to it is taken and then the alias, parameter, or deref'd
6258 reference either is used after the original \s-1EXPR\s0 has been changed or
6259 is assigned to and then used a second time.
6260 .IP "symlink \s-1OLDFILE\s0,NEWFILE" 8
6261 .IX Item "symlink OLDFILE,NEWFILE"
6262 Creates a new filename symbolically linked to the old filename.
6263 Returns \f(CW1\fR for success, \f(CW0\fR otherwise.  On systems that don't support
6264 symbolic links, produces a fatal error at run time.  To check for that,
6265 use eval:
6267 .Vb 1
6268 \&    $symlink_exists = eval { symlink("",""); 1 };
6270 .IP "syscall \s-1NUMBER\s0, \s-1LIST\s0" 8
6271 .IX Item "syscall NUMBER, LIST"
6272 Calls the system call specified as the first element of the list,
6273 passing the remaining elements as arguments to the system call.  If
6274 unimplemented, produces a fatal error.  The arguments are interpreted
6275 as follows: if a given argument is numeric, the argument is passed as
6276 an int.  If not, the pointer to the string value is passed.  You are
6277 responsible to make sure a string is pre-extended long enough to
6278 receive any result that might be written into a string.  You can't use a
6279 string literal (or other read-only string) as an argument to \f(CW\*(C`syscall\*(C'\fR
6280 because Perl has to assume that any string pointer might be written
6281 through.  If your
6282 integer arguments are not literals and have never been interpreted in a
6283 numeric context, you may need to add \f(CW0\fR to them to force them to look
6284 like numbers.  This emulates the \f(CW\*(C`syswrite\*(C'\fR function (or vice versa):
6286 .Vb 3
6287 \&    require 'syscall.ph';               # may need to run h2ph
6288 \&    $s = "hi there\en";
6289 \&    syscall(&SYS_write, fileno(STDOUT), $s, length $s);
6292 Note that Perl supports passing of up to only 14 arguments to your system call,
6293 which in practice should usually suffice.
6295 Syscall returns whatever value returned by the system call it calls.
6296 If the system call fails, \f(CW\*(C`syscall\*(C'\fR returns \f(CW\*(C`\-1\*(C'\fR and sets \f(CW$!\fR (errno).
6297 Note that some system calls can legitimately return \f(CW\*(C`\-1\*(C'\fR.  The proper
6298 way to handle such calls is to assign \f(CW\*(C`$!=0;\*(C'\fR before the call and
6299 check the value of \f(CW$!\fR if syscall returns \f(CW\*(C`\-1\*(C'\fR.
6301 There's a problem with \f(CW\*(C`syscall(&SYS_pipe)\*(C'\fR: it returns the file
6302 number of the read end of the pipe it creates.  There is no way
6303 to retrieve the file number of the other end.  You can avoid this
6304 problem by using \f(CW\*(C`pipe\*(C'\fR instead.
6305 .IP "sysopen \s-1FILEHANDLE\s0,FILENAME,MODE" 8
6306 .IX Item "sysopen FILEHANDLE,FILENAME,MODE"
6307 .PD 0
6308 .IP "sysopen \s-1FILEHANDLE\s0,FILENAME,MODE,PERMS" 8
6309 .IX Item "sysopen FILEHANDLE,FILENAME,MODE,PERMS"
6311 Opens the file whose filename is given by \s-1FILENAME\s0, and associates it
6312 with \s-1FILEHANDLE\s0.  If \s-1FILEHANDLE\s0 is an expression, its value is used as
6313 the name of the real filehandle wanted.  This function calls the
6314 underlying operating system's \f(CW\*(C`open\*(C'\fR function with the parameters
6315 \&\s-1FILENAME\s0, \s-1MODE\s0, \s-1PERMS\s0.
6317 The possible values and flag bits of the \s-1MODE\s0 parameter are
6318 system\-dependent; they are available via the standard module \f(CW\*(C`Fcntl\*(C'\fR.
6319 See the documentation of your operating system's \f(CW\*(C`open\*(C'\fR to see which
6320 values and flag bits are available.  You may combine several flags
6321 using the \f(CW\*(C`|\*(C'\fR\-operator.
6323 Some of the most common values are \f(CW\*(C`O_RDONLY\*(C'\fR for opening the file in
6324 read-only mode, \f(CW\*(C`O_WRONLY\*(C'\fR for opening the file in write-only mode,
6325 and \f(CW\*(C`O_RDWR\*(C'\fR for opening the file in read-write mode, and.
6327 For historical reasons, some values work on almost every system
6328 supported by perl: zero means read\-only, one means write\-only, and two
6329 means read/write.  We know that these values do \fInot\fR work under
6330 \&\s-1OS/390\s0 & \s-1VM/ESA\s0 Unix and on the Macintosh; you probably don't want to
6331 use them in new code.
6333 If the file named by \s-1FILENAME\s0 does not exist and the \f(CW\*(C`open\*(C'\fR call creates
6334 it (typically because \s-1MODE\s0 includes the \f(CW\*(C`O_CREAT\*(C'\fR flag), then the value of
6335 \&\s-1PERMS\s0 specifies the permissions of the newly created file.  If you omit
6336 the \s-1PERMS\s0 argument to \f(CW\*(C`sysopen\*(C'\fR, Perl uses the octal value \f(CW0666\fR.
6337 These permission values need to be in octal, and are modified by your
6338 process's current \f(CW\*(C`umask\*(C'\fR.
6340 In many systems the \f(CW\*(C`O_EXCL\*(C'\fR flag is available for opening files in
6341 exclusive mode.  This is \fBnot\fR locking: exclusiveness means here that
6342 if the file already exists, \fIsysopen()\fR fails.  The \f(CW\*(C`O_EXCL\*(C'\fR wins
6343 \&\f(CW\*(C`O_TRUNC\*(C'\fR.
6345 Sometimes you may want to truncate an already-existing file: \f(CW\*(C`O_TRUNC\*(C'\fR.
6347 You should seldom if ever use \f(CW0644\fR as argument to \f(CW\*(C`sysopen\*(C'\fR, because
6348 that takes away the user's option to have a more permissive umask.
6349 Better to omit it.  See the \fIperlfunc\fR\|(1) entry on \f(CW\*(C`umask\*(C'\fR for more
6350 on this.
6352 Note that \f(CW\*(C`sysopen\*(C'\fR depends on the \fIfdopen()\fR C library function.
6353 On many \s-1UNIX\s0 systems, \fIfdopen()\fR is known to fail when file descriptors
6354 exceed a certain value, typically 255. If you need more file
6355 descriptors than that, consider rebuilding Perl to use the \f(CW\*(C`sfio\*(C'\fR
6356 library, or perhaps using the \fIPOSIX::open()\fR function.
6358 See perlopentut for a kinder, gentler explanation of opening files.
6359 .IP "sysread \s-1FILEHANDLE\s0,SCALAR,LENGTH,OFFSET" 8
6360 .IX Item "sysread FILEHANDLE,SCALAR,LENGTH,OFFSET"
6361 .PD 0
6362 .IP "sysread \s-1FILEHANDLE\s0,SCALAR,LENGTH" 8
6363 .IX Item "sysread FILEHANDLE,SCALAR,LENGTH"
6365 Attempts to read \s-1LENGTH\s0 bytes of data into variable \s-1SCALAR\s0 from the
6366 specified \s-1FILEHANDLE\s0, using the system call \fIread\fR\|(2).  It bypasses
6367 buffered \s-1IO\s0, so mixing this with other kinds of reads, \f(CW\*(C`print\*(C'\fR,
6368 \&\f(CW\*(C`write\*(C'\fR, \f(CW\*(C`seek\*(C'\fR, \f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR can cause confusion because the
6369 perlio or stdio layers usually buffers data.  Returns the number of
6370 bytes actually read, \f(CW0\fR at end of file, or undef if there was an
6371 error (in the latter case \f(CW$!\fR is also set).  \s-1SCALAR\s0 will be grown or
6372 shrunk so that the last byte actually read is the last byte of the
6373 scalar after the read.
6375 An \s-1OFFSET\s0 may be specified to place the read data at some place in the
6376 string other than the beginning.  A negative \s-1OFFSET\s0 specifies
6377 placement at that many characters counting backwards from the end of
6378 the string.  A positive \s-1OFFSET\s0 greater than the length of \s-1SCALAR\s0
6379 results in the string being padded to the required size with \f(CW"\e0"\fR
6380 bytes before the result of the read is appended.
6382 There is no \fIsyseof()\fR function, which is ok, since \fIeof()\fR doesn't work
6383 very well on device files (like ttys) anyway.  Use \fIsysread()\fR and check
6384 for a return value for 0 to decide whether you're done.
6386 Note that if the filehandle has been marked as \f(CW\*(C`:utf8\*(C'\fR Unicode
6387 characters are read instead of bytes (the \s-1LENGTH\s0, \s-1OFFSET\s0, and the
6388 return value of \fIsysread()\fR are in Unicode characters).
6389 The \f(CW\*(C`:encoding(...)\*(C'\fR layer implicitly introduces the \f(CW\*(C`:utf8\*(C'\fR layer.
6390 See \*(L"binmode\*(R", \*(L"open\*(R", and the \f(CW\*(C`open\*(C'\fR pragma, open.
6391 .IP "sysseek \s-1FILEHANDLE\s0,POSITION,WHENCE" 8
6392 .IX Item "sysseek FILEHANDLE,POSITION,WHENCE"
6393 Sets \s-1FILEHANDLE\s0's system position in bytes using the system call
6394 \&\fIlseek\fR\|(2).  \s-1FILEHANDLE\s0 may be an expression whose value gives the name
6395 of the filehandle.  The values for \s-1WHENCE\s0 are \f(CW0\fR to set the new
6396 position to \s-1POSITION\s0, \f(CW1\fR to set the it to the current position plus
6397 \&\s-1POSITION\s0, and \f(CW2\fR to set it to \s-1EOF\s0 plus \s-1POSITION\s0 (typically
6398 negative).
6400 Note the \fIin bytes\fR: even if the filehandle has been set to operate
6401 on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR I/O layer), \fItell()\fR
6402 will return byte offsets, not character offsets (because implementing
6403 that would render \fIsysseek()\fR very slow).
6405 \&\fIsysseek()\fR bypasses normal buffered \s-1IO\s0, so mixing this with reads (other
6406 than \f(CW\*(C`sysread\*(C'\fR, for example &gt;&lt or \fIread()\fR) \f(CW\*(C`print\*(C'\fR, \f(CW\*(C`write\*(C'\fR,
6407 \&\f(CW\*(C`seek\*(C'\fR, \f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR may cause confusion.
6409 For \s-1WHENCE\s0, you may also use the constants \f(CW\*(C`SEEK_SET\*(C'\fR, \f(CW\*(C`SEEK_CUR\*(C'\fR,
6410 and \f(CW\*(C`SEEK_END\*(C'\fR (start of the file, current position, end of the file)
6411 from the Fcntl module.  Use of the constants is also more portable
6412 than relying on 0, 1, and 2.  For example to define a \*(L"systell\*(R" function:
6414 .Vb 2
6415 \&        use Fcntl 'SEEK_CUR';
6416 \&        sub systell { sysseek($_[0], 0, SEEK_CUR) }
6419 Returns the new position, or the undefined value on failure.  A position
6420 of zero is returned as the string \f(CW"0 but true"\fR; thus \f(CW\*(C`sysseek\*(C'\fR returns
6421 true on success and false on failure, yet you can still easily determine
6422 the new position.
6423 .IP "system \s-1LIST\s0" 8
6424 .IX Item "system LIST"
6425 .PD 0
6426 .IP "system \s-1PROGRAM\s0 \s-1LIST\s0" 8
6427 .IX Item "system PROGRAM LIST"
6429 Does exactly the same thing as \f(CW\*(C`exec LIST\*(C'\fR, except that a fork is
6430 done first, and the parent process waits for the child process to
6431 complete.  Note that argument processing varies depending on the
6432 number of arguments.  If there is more than one argument in \s-1LIST\s0,
6433 or if \s-1LIST\s0 is an array with more than one value, starts the program
6434 given by the first element of the list with arguments given by the
6435 rest of the list.  If there is only one scalar argument, the argument
6436 is checked for shell metacharacters, and if there are any, the
6437 entire argument is passed to the system's command shell for parsing
6438 (this is \f(CW\*(C`/bin/sh \-c\*(C'\fR on Unix platforms, but varies on other
6439 platforms).  If there are no shell metacharacters in the argument,
6440 it is split into words and passed directly to \f(CW\*(C`execvp\*(C'\fR, which is
6441 more efficient.
6443 Beginning with v5.6.0, Perl will attempt to flush all files opened for
6444 output before any operation that may do a fork, but this may not be
6445 supported on some platforms (see perlport).  To be safe, you may need
6446 to set \f(CW$|\fR ($AUTOFLUSH in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method
6447 of \f(CW\*(C`IO::Handle\*(C'\fR on any open handles.
6449 The return value is the exit status of the program as returned by the
6450 \&\f(CW\*(C`wait\*(C'\fR call.  To get the actual exit value shift right by eight (see below).
6451 See also \*(L"exec\*(R".  This is \fInot\fR what you want to use to capture
6452 the output from a command, for that you should use merely backticks or
6453 \&\f(CW\*(C`qx//\*(C'\fR, as described in \*(L"`STRING`\*(R" in perlop.  Return value of \-1
6454 indicates a failure to start the program (inspect $! for the reason).
6456 Like \f(CW\*(C`exec\*(C'\fR, \f(CW\*(C`system\*(C'\fR allows you to lie to a program about its name if
6457 you use the \f(CW\*(C`system PROGRAM LIST\*(C'\fR syntax.  Again, see \*(L"exec\*(R".
6459 Because \f(CW\*(C`system\*(C'\fR and backticks block \f(CW\*(C`SIGINT\*(C'\fR and \f(CW\*(C`SIGQUIT\*(C'\fR,
6460 killing the program they're running doesn't actually interrupt
6461 your program.
6463 .Vb 3
6464 \&    @args = ("command", "arg1", "arg2");
6465 \&    system(@args) == 0
6466 \&         or die "system @args failed: $?"
6469 You can check all the failure possibilities by inspecting
6470 \&\f(CW$?\fR like this:
6472 .Vb 10
6473 \&    if ($? == -1) {
6474 \&        print "failed to execute: $!\en";
6475 \&    }
6476 \&    elsif ($? & 127) {
6477 \&        printf "child died with signal %d, %s coredump\en",
6478 \&            ($? & 127),  ($? & 128) ? 'with' : 'without';
6479 \&    }
6480 \&    else {
6481 \&        printf "child exited with value %d\en", $? >> 8;
6482 \&    }
6485 or more portably by using the W*() calls of the \s-1POSIX\s0 extension;
6486 see perlport for more information.
6488 When the arguments get executed via the system shell, results
6489 and return codes will be subject to its quirks and capabilities.
6490 See \*(L"`STRING`\*(R" in perlop and \*(L"exec\*(R" for details.
6491 .IP "syswrite \s-1FILEHANDLE\s0,SCALAR,LENGTH,OFFSET" 8
6492 .IX Item "syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET"
6493 .PD 0
6494 .IP "syswrite \s-1FILEHANDLE\s0,SCALAR,LENGTH" 8
6495 .IX Item "syswrite FILEHANDLE,SCALAR,LENGTH"
6496 .IP "syswrite \s-1FILEHANDLE\s0,SCALAR" 8
6497 .IX Item "syswrite FILEHANDLE,SCALAR"
6499 Attempts to write \s-1LENGTH\s0 bytes of data from variable \s-1SCALAR\s0 to the
6500 specified \s-1FILEHANDLE\s0, using the system call \fIwrite\fR\|(2).  If \s-1LENGTH\s0 is
6501 not specified, writes whole \s-1SCALAR\s0.  It bypasses buffered \s-1IO\s0, so
6502 mixing this with reads (other than \f(CWsysread())\fR, \f(CW\*(C`print\*(C'\fR, \f(CW\*(C`write\*(C'\fR,
6503 \&\f(CW\*(C`seek\*(C'\fR, \f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR may cause confusion because the perlio and
6504 stdio layers usually buffers data.  Returns the number of bytes
6505 actually written, or \f(CW\*(C`undef\*(C'\fR if there was an error (in this case the
6506 errno variable \f(CW$!\fR is also set).  If the \s-1LENGTH\s0 is greater than the
6507 available data in the \s-1SCALAR\s0 after the \s-1OFFSET\s0, only as much data as is
6508 available will be written.
6510 An \s-1OFFSET\s0 may be specified to write the data from some part of the
6511 string other than the beginning.  A negative \s-1OFFSET\s0 specifies writing
6512 that many characters counting backwards from the end of the string.
6513 In the case the \s-1SCALAR\s0 is empty you can use \s-1OFFSET\s0 but only zero offset.
6515 Note that if the filehandle has been marked as \f(CW\*(C`:utf8\*(C'\fR, Unicode
6516 characters are written instead of bytes (the \s-1LENGTH\s0, \s-1OFFSET\s0, and the
6517 return value of \fIsyswrite()\fR are in \s-1UTF\-8\s0 encoded Unicode characters).
6518 The \f(CW\*(C`:encoding(...)\*(C'\fR layer implicitly introduces the \f(CW\*(C`:utf8\*(C'\fR layer.
6519 See \*(L"binmode\*(R", \*(L"open\*(R", and the \f(CW\*(C`open\*(C'\fR pragma, open.
6520 .IP "tell \s-1FILEHANDLE\s0" 8
6521 .IX Item "tell FILEHANDLE"
6522 .PD 0
6523 .IP "tell" 8
6524 .IX Item "tell"
6526 Returns the current position \fIin bytes\fR for \s-1FILEHANDLE\s0, or \-1 on
6527 error.  \s-1FILEHANDLE\s0 may be an expression whose value gives the name of
6528 the actual filehandle.  If \s-1FILEHANDLE\s0 is omitted, assumes the file
6529 last read.
6531 Note the \fIin bytes\fR: even if the filehandle has been set to
6532 operate on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR open
6533 layer), \fItell()\fR will return byte offsets, not character offsets
6534 (because that would render \fIseek()\fR and \fItell()\fR rather slow).
6536 The return value of \fItell()\fR for the standard streams like the \s-1STDIN\s0
6537 depends on the operating system: it may return \-1 or something else.
6538 \&\fItell()\fR on pipes, fifos, and sockets usually returns \-1.
6540 There is no \f(CW\*(C`systell\*(C'\fR function.  Use \f(CW\*(C`sysseek(FH, 0, 1)\*(C'\fR for that.
6542 Do not use \fItell()\fR on a filehandle that has been opened using
6543 \&\fIsysopen()\fR, use \fIsysseek()\fR for that as described above.  Why?  Because
6544 \&\fIsysopen()\fR creates unbuffered, \*(L"raw\*(R", filehandles, while \fIopen()\fR creates
6545 buffered filehandles.  \fIsysseek()\fR make sense only on the first kind,
6546 \&\fItell()\fR only makes sense on the second kind.
6547 .IP "telldir \s-1DIRHANDLE\s0" 8
6548 .IX Item "telldir DIRHANDLE"
6549 Returns the current position of the \f(CW\*(C`readdir\*(C'\fR routines on \s-1DIRHANDLE\s0.
6550 Value may be given to \f(CW\*(C`seekdir\*(C'\fR to access a particular location in a
6551 directory.  Has the same caveats about possible directory compaction as
6552 the corresponding system library routine.
6553 .IP "tie \s-1VARIABLE\s0,CLASSNAME,LIST" 8
6554 .IX Item "tie VARIABLE,CLASSNAME,LIST"
6555 This function binds a variable to a package class that will provide the
6556 implementation for the variable.  \s-1VARIABLE\s0 is the name of the variable
6557 to be enchanted.  \s-1CLASSNAME\s0 is the name of a class implementing objects
6558 of correct type.  Any additional arguments are passed to the \f(CW\*(C`new\*(C'\fR
6559 method of the class (meaning \f(CW\*(C`TIESCALAR\*(C'\fR, \f(CW\*(C`TIEHANDLE\*(C'\fR, \f(CW\*(C`TIEARRAY\*(C'\fR,
6560 or \f(CW\*(C`TIEHASH\*(C'\fR).  Typically these are arguments such as might be passed
6561 to the \f(CW\*(C`dbm_open()\*(C'\fR function of C.  The object returned by the \f(CW\*(C`new\*(C'\fR
6562 method is also returned by the \f(CW\*(C`tie\*(C'\fR function, which would be useful
6563 if you want to access other methods in \s-1CLASSNAME\s0.
6565 Note that functions such as \f(CW\*(C`keys\*(C'\fR and \f(CW\*(C`values\*(C'\fR may return huge lists
6566 when used on large objects, like \s-1DBM\s0 files.  You may prefer to use the
6567 \&\f(CW\*(C`each\*(C'\fR function to iterate over such.  Example:
6569 .Vb 7
6570 \&    # print out history file offsets
6571 \&    use NDBM_File;
6572 \&    tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
6573 \&    while (($key,$val) = each %HIST) {
6574 \&        print $key, ' = ', unpack('L',$val), "\en";
6575 \&    }
6576 \&    untie(%HIST);
6579 A class implementing a hash should have the following methods:
6581 .Vb 10
6582 \&    TIEHASH classname, LIST
6583 \&    FETCH this, key
6584 \&    STORE this, key, value
6585 \&    DELETE this, key
6586 \&    CLEAR this
6587 \&    EXISTS this, key
6588 \&    FIRSTKEY this
6589 \&    NEXTKEY this, lastkey
6590 \&    DESTROY this
6591 \&    UNTIE this
6594 A class implementing an ordinary array should have the following methods:
6596 .Vb 14
6597 \&    TIEARRAY classname, LIST
6598 \&    FETCH this, key
6599 \&    STORE this, key, value
6600 \&    FETCHSIZE this
6601 \&    STORESIZE this, count
6602 \&    CLEAR this
6603 \&    PUSH this, LIST
6604 \&    POP this
6605 \&    SHIFT this
6606 \&    UNSHIFT this, LIST
6607 \&    SPLICE this, offset, length, LIST
6608 \&    EXTEND this, count
6609 \&    DESTROY this
6610 \&    UNTIE this
6613 A class implementing a file handle should have the following methods:
6615 .Vb 16
6616 \&    TIEHANDLE classname, LIST
6617 \&    READ this, scalar, length, offset
6618 \&    READLINE this
6619 \&    GETC this
6620 \&    WRITE this, scalar, length, offset
6621 \&    PRINT this, LIST
6622 \&    PRINTF this, format, LIST
6623 \&    BINMODE this
6624 \&    EOF this
6625 \&    FILENO this
6626 \&    SEEK this, position, whence
6627 \&    TELL this
6628 \&    OPEN this, mode, LIST
6629 \&    CLOSE this
6630 \&    DESTROY this
6631 \&    UNTIE this
6634 A class implementing a scalar should have the following methods:
6636 .Vb 5
6637 \&    TIESCALAR classname, LIST
6638 \&    FETCH this,
6639 \&    STORE this, value
6640 \&    DESTROY this
6641 \&    UNTIE this
6644 Not all methods indicated above need be implemented.  See perltie,
6645 Tie::Hash, Tie::Array, Tie::Scalar, and Tie::Handle.
6647 Unlike \f(CW\*(C`dbmopen\*(C'\fR, the \f(CW\*(C`tie\*(C'\fR function will not use or require a module
6648 for you\*(--you need to do that explicitly yourself.  See DB_File
6649 or the \fIConfig\fR module for interesting \f(CW\*(C`tie\*(C'\fR implementations.
6651 For further details see perltie, \*(L"tied \s-1VARIABLE\s0\*(R".
6652 .IP "tied \s-1VARIABLE\s0" 8
6653 .IX Item "tied VARIABLE"
6654 Returns a reference to the object underlying \s-1VARIABLE\s0 (the same value
6655 that was originally returned by the \f(CW\*(C`tie\*(C'\fR call that bound the variable
6656 to a package.)  Returns the undefined value if \s-1VARIABLE\s0 isn't tied to a
6657 package.
6658 .IP "time" 8
6659 .IX Item "time"
6660 Returns the number of non-leap seconds since whatever time the system
6661 considers to be the epoch (that's 00:00:00, January 1, 1904 for Mac \s-1OS\s0,
6662 and 00:00:00 \s-1UTC\s0, January 1, 1970 for most other systems).
6663 Suitable for feeding to \f(CW\*(C`gmtime\*(C'\fR and \f(CW\*(C`localtime\*(C'\fR.
6665 For measuring time in better granularity than one second,
6666 you may use either the Time::HiRes module (from \s-1CPAN\s0, and starting from
6667 Perl 5.8 part of the standard distribution), or if you have
6668 \&\fIgettimeofday\fR\|(2), you may be able to use the \f(CW\*(C`syscall\*(C'\fR interface of Perl.
6669 See perlfaq8 for details.
6670 .IP "times" 8
6671 .IX Item "times"
6672 Returns a four-element list giving the user and system times, in
6673 seconds, for this process and the children of this process.
6675 .Vb 1
6676 \&    ($user,$system,$cuser,$csystem) = times;
6679 In scalar context, \f(CW\*(C`times\*(C'\fR returns \f(CW$user\fR.
6680 .IP "tr///" 8
6681 .IX Item "tr///"
6682 The transliteration operator.  Same as \f(CW\*(C`y///\*(C'\fR.  See perlop.
6683 .IP "truncate \s-1FILEHANDLE\s0,LENGTH" 8
6684 .IX Item "truncate FILEHANDLE,LENGTH"
6685 .PD 0
6686 .IP "truncate \s-1EXPR\s0,LENGTH" 8
6687 .IX Item "truncate EXPR,LENGTH"
6689 Truncates the file opened on \s-1FILEHANDLE\s0, or named by \s-1EXPR\s0, to the
6690 specified length.  Produces a fatal error if truncate isn't implemented
6691 on your system.  Returns true if successful, the undefined value
6692 otherwise.
6694 The behavior is undefined if \s-1LENGTH\s0 is greater than the length of the
6695 file.
6696 .IP "uc \s-1EXPR\s0" 8
6697 .IX Item "uc EXPR"
6698 .PD 0
6699 .IP "uc" 8
6700 .IX Item "uc"
6702 Returns an uppercased version of \s-1EXPR\s0.  This is the internal function
6703 implementing the \f(CW\*(C`\eU\*(C'\fR escape in double-quoted strings.  Respects
6704 current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use locale\*(C'\fR in force.  See perllocale
6705 and perlunicode for more details about locale and Unicode support.
6706 It does not attempt to do titlecase mapping on initial letters.  See
6707 \&\f(CW\*(C`ucfirst\*(C'\fR for that.
6709 If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
6710 .IP "ucfirst \s-1EXPR\s0" 8
6711 .IX Item "ucfirst EXPR"
6712 .PD 0
6713 .IP "ucfirst" 8
6714 .IX Item "ucfirst"
6716 Returns the value of \s-1EXPR\s0 with the first character in uppercase
6717 (titlecase in Unicode).  This is the internal function implementing
6718 the \f(CW\*(C`\eu\*(C'\fR escape in double-quoted strings.  Respects current \s-1LC_CTYPE\s0
6719 locale if \f(CW\*(C`use locale\*(C'\fR in force.  See perllocale and perlunicode
6720 for more details about locale and Unicode support.
6722 If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
6723 .IP "umask \s-1EXPR\s0" 8
6724 .IX Item "umask EXPR"
6725 .PD 0
6726 .IP "umask" 8
6727 .IX Item "umask"
6729 Sets the umask for the process to \s-1EXPR\s0 and returns the previous value.
6730 If \s-1EXPR\s0 is omitted, merely returns the current umask.
6732 The Unix permission \f(CW\*(C`rwxr\-x\-\-\-\*(C'\fR is represented as three sets of three
6733 bits, or three octal digits: \f(CW0750\fR (the leading 0 indicates octal
6734 and isn't one of the digits).  The \f(CW\*(C`umask\*(C'\fR value is such a number
6735 representing disabled permissions bits.  The permission (or \*(L"mode\*(R")
6736 values you pass \f(CW\*(C`mkdir\*(C'\fR or \f(CW\*(C`sysopen\*(C'\fR are modified by your umask, so
6737 even if you tell \f(CW\*(C`sysopen\*(C'\fR to create a file with permissions \f(CW0777\fR,
6738 if your umask is \f(CW0022\fR then the file will actually be created with
6739 permissions \f(CW0755\fR.  If your \f(CW\*(C`umask\*(C'\fR were \f(CW0027\fR (group can't
6740 write; others can't read, write, or execute), then passing
6741 \&\f(CW\*(C`sysopen\*(C'\fR \f(CW0666\fR would create a file with mode \f(CW0640\fR (\f(CW\*(C`0666 &~
6742 027\*(C'\fR is \f(CW0640\fR).
6744 Here's some advice: supply a creation mode of \f(CW0666\fR for regular
6745 files (in \f(CW\*(C`sysopen\*(C'\fR) and one of \f(CW0777\fR for directories (in
6746 \&\f(CW\*(C`mkdir\*(C'\fR) and executable files.  This gives users the freedom of
6747 choice: if they want protected files, they might choose process umasks
6748 of \f(CW022\fR, \f(CW027\fR, or even the particularly antisocial mask of \f(CW077\fR.
6749 Programs should rarely if ever make policy decisions better left to
6750 the user.  The exception to this is when writing files that should be
6751 kept private: mail files, web browser cookies, \fI.rhosts\fR files, and
6752 so on.
6754 If \fIumask\fR\|(2) is not implemented on your system and you are trying to
6755 restrict access for \fIyourself\fR (i.e., (\s-1EXPR\s0 & 0700) > 0), produces a
6756 fatal error at run time.  If \fIumask\fR\|(2) is not implemented and you are
6757 not trying to restrict access for yourself, returns \f(CW\*(C`undef\*(C'\fR.
6759 Remember that a umask is a number, usually given in octal; it is \fInot\fR a
6760 string of octal digits.  See also \*(L"oct\*(R", if all you have is a string.
6761 .IP "undef \s-1EXPR\s0" 8
6762 .IX Item "undef EXPR"
6763 .PD 0
6764 .IP "undef" 8
6765 .IX Item "undef"
6767 Undefines the value of \s-1EXPR\s0, which must be an lvalue.  Use only on a
6768 scalar value, an array (using \f(CW\*(C`@\*(C'\fR), a hash (using \f(CW\*(C`%\*(C'\fR), a subroutine
6769 (using \f(CW\*(C`&\*(C'\fR), or a typeglob (using \f(CW\*(C`*\*(C'\fR).  (Saying \f(CW\*(C`undef $hash{$key}\*(C'\fR
6770 will probably not do what you expect on most predefined variables or
6771 \&\s-1DBM\s0 list values, so don't do that; see delete.)  Always returns the
6772 undefined value.  You can omit the \s-1EXPR\s0, in which case nothing is
6773 undefined, but you still get an undefined value that you could, for
6774 instance, return from a subroutine, assign to a variable or pass as a
6775 parameter.  Examples:
6777 .Vb 9
6778 \&    undef $foo;
6779 \&    undef $bar{'blurfl'};      # Compare to: delete $bar{'blurfl'};
6780 \&    undef @ary;
6781 \&    undef %hash;
6782 \&    undef &mysub;
6783 \&    undef *xyz;       # destroys $xyz, @xyz, %xyz, &xyz, etc.
6784 \&    return (wantarray ? (undef, $errmsg) : undef) if $they_blew_it;
6785 \&    select undef, undef, undef, 0.25;
6786 \&    ($a, $b, undef, $c) = &foo;       # Ignore third value returned
6789 Note that this is a unary operator, not a list operator.
6790 .IP "unlink \s-1LIST\s0" 8
6791 .IX Item "unlink LIST"
6792 .PD 0
6793 .IP "unlink" 8
6794 .IX Item "unlink"
6796 Deletes a list of files.  Returns the number of files successfully
6797 deleted.
6799 .Vb 3
6800 \&    $cnt = unlink 'a', 'b', 'c';
6801 \&    unlink @goners;
6802 \&    unlink <*.bak>;
6805 Note: \f(CW\*(C`unlink\*(C'\fR will not delete directories unless you are superuser and
6806 the \fB\-U\fR flag is supplied to Perl.  Even if these conditions are
6807 met, be warned that unlinking a directory can inflict damage on your
6808 filesystem.  Use \f(CW\*(C`rmdir\*(C'\fR instead.
6810 If \s-1LIST\s0 is omitted, uses \f(CW$_\fR.
6811 .IP "unpack \s-1TEMPLATE\s0,EXPR" 8
6812 .IX Item "unpack TEMPLATE,EXPR"
6813 \&\f(CW\*(C`unpack\*(C'\fR does the reverse of \f(CW\*(C`pack\*(C'\fR: it takes a string
6814 and expands it out into a list of values.
6815 (In scalar context, it returns merely the first value produced.)
6817 The string is broken into chunks described by the \s-1TEMPLATE\s0.  Each chunk
6818 is converted separately to a value.  Typically, either the string is a result
6819 of \f(CW\*(C`pack\*(C'\fR, or the bytes of the string represent a C structure of some
6820 kind.
6822 The \s-1TEMPLATE\s0 has the same format as in the \f(CW\*(C`pack\*(C'\fR function.
6823 Here's a subroutine that does substring:
6825 .Vb 4
6826 \&    sub substr {
6827 \&        my($what,$where,$howmuch) = @_;
6828 \&        unpack("x$where a$howmuch", $what);
6829 \&    }
6832 and then there's
6834 .Vb 1
6835 \&    sub ordinal { unpack("c",$_[0]); } # same as ord()
6838 In addition to fields allowed in \fIpack()\fR, you may prefix a field with
6839 a %<number> to indicate that
6840 you want a <number>\-bit checksum of the items instead of the items
6841 themselves.  Default is a 16\-bit checksum.  Checksum is calculated by
6842 summing numeric values of expanded values (for string fields the sum of
6843 \&\f(CW\*(C`ord($char)\*(C'\fR is taken, for bit fields the sum of zeroes and ones).
6845 For example, the following
6846 computes the same number as the System V sum program:
6848 .Vb 4
6849 \&    $checksum = do {
6850 \&        local $/;  # slurp!
6851 \&        unpack("%32C*",<>) % 65535;
6852 \&    };
6855 The following efficiently counts the number of set bits in a bit vector:
6857 .Vb 1
6858 \&    $setbits = unpack("%32b*", $selectmask);
6861 The \f(CW\*(C`p\*(C'\fR and \f(CW\*(C`P\*(C'\fR formats should be used with care.  Since Perl
6862 has no way of checking whether the value passed to \f(CW\*(C`unpack()\*(C'\fR
6863 corresponds to a valid memory location, passing a pointer value that's
6864 not known to be valid is likely to have disastrous consequences.
6866 If there are more pack codes or if the repeat count of a field or a group
6867 is larger than what the remainder of the input string allows, the result
6868 is not well defined: in some cases, the repeat count is decreased, or
6869 \&\f(CW\*(C`unpack()\*(C'\fR will produce null strings or zeroes, or terminate with an
6870 error. If the input string is longer than one described by the \s-1TEMPLATE\s0,
6871 the rest is ignored.
6873 See \*(L"pack\*(R" for more examples and notes.
6874 .IP "untie \s-1VARIABLE\s0" 8
6875 .IX Item "untie VARIABLE"
6876 Breaks the binding between a variable and a package.  (See \f(CW\*(C`tie\*(C'\fR.)
6877 Has no effect if the variable is not tied.
6878 .IP "unshift \s-1ARRAY\s0,LIST" 8
6879 .IX Item "unshift ARRAY,LIST"
6880 Does the opposite of a \f(CW\*(C`shift\*(C'\fR.  Or the opposite of a \f(CW\*(C`push\*(C'\fR,
6881 depending on how you look at it.  Prepends list to the front of the
6882 array, and returns the new number of elements in the array.
6884 .Vb 1
6885 \&    unshift(@ARGV, '-e') unless $ARGV[0] =~ /^-/;
6888 Note the \s-1LIST\s0 is prepended whole, not one element at a time, so the
6889 prepended elements stay in the same order.  Use \f(CW\*(C`reverse\*(C'\fR to do the
6890 reverse.
6891 .IP "use Module \s-1VERSION\s0 \s-1LIST\s0" 8
6892 .IX Item "use Module VERSION LIST"
6893 .PD 0
6894 .IP "use Module \s-1VERSION\s0" 8
6895 .IX Item "use Module VERSION"
6896 .IP "use Module \s-1LIST\s0" 8
6897 .IX Item "use Module LIST"
6898 .IP "use Module" 8
6899 .IX Item "use Module"
6900 .IP "use \s-1VERSION\s0" 8
6901 .IX Item "use VERSION"
6903 Imports some semantics into the current package from the named module,
6904 generally by aliasing certain subroutine or variable names into your
6905 package.  It is exactly equivalent to
6907 .Vb 1
6908 \&    BEGIN { require Module; import Module LIST; }
6911 except that Module \fImust\fR be a bareword.
6913 \&\s-1VERSION\s0 may be either a numeric argument such as 5.006, which will be
6914 compared to \f(CW$]\fR, or a literal of the form v5.6.1, which will be compared
6915 to \f(CW$^V\fR (aka \f(CW$PERL_VERSION\fR.  A fatal error is produced if \s-1VERSION\s0 is
6916 greater than the version of the current Perl interpreter; Perl will not
6917 attempt to parse the rest of the file.  Compare with \*(L"require\*(R", which can
6918 do a similar check at run time.
6920 Specifying \s-1VERSION\s0 as a literal of the form v5.6.1 should generally be
6921 avoided, because it leads to misleading error messages under earlier
6922 versions of Perl which do not support this syntax.  The equivalent numeric
6923 version should be used instead.
6925 .Vb 3
6926 \&    use v5.6.1;         # compile time version check
6927 \&    use 5.6.1;          # ditto
6928 \&    use 5.006_001;      # ditto; preferred for backwards compatibility
6931 This is often useful if you need to check the current Perl version before
6932 \&\f(CW\*(C`use\*(C'\fRing library modules that have changed in incompatible ways from
6933 older versions of Perl.  (We try not to do this more than we have to.)
6935 The \f(CW\*(C`BEGIN\*(C'\fR forces the \f(CW\*(C`require\*(C'\fR and \f(CW\*(C`import\*(C'\fR to happen at compile time.  The
6936 \&\f(CW\*(C`require\*(C'\fR makes sure the module is loaded into memory if it hasn't been
6937 yet.  The \f(CW\*(C`import\*(C'\fR is not a builtin\*(--it's just an ordinary static method
6938 call into the \f(CW\*(C`Module\*(C'\fR package to tell the module to import the list of
6939 features back into the current package.  The module can implement its
6940 \&\f(CW\*(C`import\*(C'\fR method any way it likes, though most modules just choose to
6941 derive their \f(CW\*(C`import\*(C'\fR method via inheritance from the \f(CW\*(C`Exporter\*(C'\fR class that
6942 is defined in the \f(CW\*(C`Exporter\*(C'\fR module.  See Exporter.  If no \f(CW\*(C`import\*(C'\fR
6943 method can be found then the call is skipped.
6945 If you do not want to call the package's \f(CW\*(C`import\*(C'\fR method (for instance,
6946 to stop your namespace from being altered), explicitly supply the empty list:
6948 .Vb 1
6949 \&    use Module ();
6952 That is exactly equivalent to
6954 .Vb 1
6955 \&    BEGIN { require Module }
6958 If the \s-1VERSION\s0 argument is present between Module and \s-1LIST\s0, then the
6959 \&\f(CW\*(C`use\*(C'\fR will call the \s-1VERSION\s0 method in class Module with the given
6960 version as an argument.  The default \s-1VERSION\s0 method, inherited from
6961 the \s-1UNIVERSAL\s0 class, croaks if the given version is larger than the
6962 value of the variable \f(CW$Module::VERSION\fR.
6964 Again, there is a distinction between omitting \s-1LIST\s0 (\f(CW\*(C`import\*(C'\fR called
6965 with no arguments) and an explicit empty \s-1LIST\s0 \f(CW\*(C`()\*(C'\fR (\f(CW\*(C`import\*(C'\fR not
6966 called).  Note that there is no comma after \s-1VERSION\s0!
6968 Because this is a wide-open interface, pragmas (compiler directives)
6969 are also implemented this way.  Currently implemented pragmas are:
6971 .Vb 8
6972 \&    use constant;
6973 \&    use diagnostics;
6974 \&    use integer;
6975 \&    use sigtrap  qw(SEGV BUS);
6976 \&    use strict   qw(subs vars refs);
6977 \&    use subs     qw(afunc blurfl);
6978 \&    use warnings qw(all);
6979 \&    use sort     qw(stable _quicksort _mergesort);
6982 Some of these pseudo-modules import semantics into the current
6983 block scope (like \f(CW\*(C`strict\*(C'\fR or \f(CW\*(C`integer\*(C'\fR, unlike ordinary modules,
6984 which import symbols into the current package (which are effective
6985 through the end of the file).
6987 There's a corresponding \f(CW\*(C`no\*(C'\fR command that unimports meanings imported
6988 by \f(CW\*(C`use\*(C'\fR, i.e., it calls \f(CW\*(C`unimport Module LIST\*(C'\fR instead of \f(CW\*(C`import\*(C'\fR.
6990 .Vb 3
6991 \&    no integer;
6992 \&    no strict 'refs';
6993 \&    no warnings;
6996 See perlmodlib for a list of standard modules and pragmas.  See perlrun
6997 for the \f(CW\*(C`\-M\*(C'\fR and \f(CW\*(C`\-m\*(C'\fR command-line options to perl that give \f(CW\*(C`use\*(C'\fR
6998 functionality from the command\-line.
6999 .IP "utime \s-1LIST\s0" 8
7000 .IX Item "utime LIST"
7001 Changes the access and modification times on each file of a list of
7002 files.  The first two elements of the list must be the \s-1NUMERICAL\s0 access
7003 and modification times, in that order.  Returns the number of files
7004 successfully changed.  The inode change time of each file is set
7005 to the current time.  For example, this code has the same effect as the
7006 Unix \fItouch\fR\|(1) command when the files \fIalready exist\fR.
7008 .Vb 3
7009 \&    #!/usr/bin/perl
7010 \&    $now = time;
7011 \&    utime $now, $now, @ARGV;
7014 \&\fBNote:\fR  Under \s-1NFS\s0, \fItouch\fR\|(1) uses the time of the \s-1NFS\s0 server, not
7015 the time of the local machine.  If there is a time synchronization
7016 problem, the \s-1NFS\s0 server and local machine will have different times.
7018 Since perl 5.7.2, if the first two elements of the list are \f(CW\*(C`undef\*(C'\fR, then
7019 the \fIutime\fR\|(2) function in the C library will be called with a null second
7020 argument. On most systems, this will set the file's access and
7021 modification times to the current time (i.e. equivalent to the example
7022 above.)
7024 .Vb 1
7025 \&    utime undef, undef, @ARGV;
7027 .IP "values \s-1HASH\s0" 8
7028 .IX Item "values HASH"
7029 Returns a list consisting of all the values of the named hash.
7030 (In a scalar context, returns the number of values.)
7032 The values are returned in an apparently random order.  The actual
7033 random order is subject to change in future versions of perl, but it
7034 is guaranteed to be the same order as either the \f(CW\*(C`keys\*(C'\fR or \f(CW\*(C`each\*(C'\fR
7035 function would produce on the same (unmodified) hash.  Since Perl
7036 5.8.1 the ordering is different even between different runs of Perl
7037 for security reasons (see \*(L"Algorithmic Complexity Attacks\*(R" in perlsec).
7039 As a side effect, calling \fIvalues()\fR resets the \s-1HASH\s0's internal iterator,
7040 see \*(L"each\*(R".
7042 Note that the values are not copied, which means modifying them will
7043 modify the contents of the hash:
7045 .Vb 2
7046 \&    for (values %hash)      { s/foo/bar/g }   # modifies %hash values
7047 \&    for (@hash{keys %hash}) { s/foo/bar/g }   # same
7050 See also \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`each\*(C'\fR, and \f(CW\*(C`sort\*(C'\fR.
7051 .IP "vec \s-1EXPR\s0,OFFSET,BITS" 8
7052 .IX Item "vec EXPR,OFFSET,BITS"
7053 Treats the string in \s-1EXPR\s0 as a bit vector made up of elements of
7054 width \s-1BITS\s0, and returns the value of the element specified by \s-1OFFSET\s0
7055 as an unsigned integer.  \s-1BITS\s0 therefore specifies the number of bits
7056 that are reserved for each element in the bit vector.  This must
7057 be a power of two from 1 to 32 (or 64, if your platform supports
7058 that).
7060 If \s-1BITS\s0 is 8, \*(L"elements\*(R" coincide with bytes of the input string.
7062 If \s-1BITS\s0 is 16 or more, bytes of the input string are grouped into chunks
7063 of size \s-1BITS/8\s0, and each group is converted to a number as with
7064 \&\fIpack()\fR/\fIunpack()\fR with big-endian formats \f(CW\*(C`n\*(C'\fR/\f(CW\*(C`N\*(C'\fR (and analogously
7065 for BITS==64).  See \*(L"pack\*(R" for details.
7067 If bits is 4 or less, the string is broken into bytes, then the bits
7068 of each byte are broken into 8/BITS groups.  Bits of a byte are
7069 numbered in a little-endian-ish way, as in \f(CW0x01\fR, \f(CW0x02\fR,
7070 \&\f(CW0x04\fR, \f(CW0x08\fR, \f(CW0x10\fR, \f(CW0x20\fR, \f(CW0x40\fR, \f(CW0x80\fR.  For example,
7071 breaking the single input byte \f(CW\*(C`chr(0x36)\*(C'\fR into two groups gives a list
7072 \&\f(CW\*(C`(0x6, 0x3)\*(C'\fR; breaking it into 4 groups gives \f(CW\*(C`(0x2, 0x1, 0x3, 0x0)\*(C'\fR.
7074 \&\f(CW\*(C`vec\*(C'\fR may also be assigned to, in which case parentheses are needed
7075 to give the expression the correct precedence as in
7077 .Vb 1
7078 \&    vec($image, $max_x * $x + $y, 8) = 3;
7081 If the selected element is outside the string, the value 0 is returned.
7082 If an element off the end of the string is written to, Perl will first
7083 extend the string with sufficiently many zero bytes.   It is an error
7084 to try to write off the beginning of the string (i.e. negative \s-1OFFSET\s0).
7086 The string should not contain any character with the value > 255 (which
7087 can only happen if you're using \s-1UTF\-8\s0 encoding).  If it does, it will be
7088 treated as something which is not \s-1UTF\-8\s0 encoded.  When the \f(CW\*(C`vec\*(C'\fR was
7089 assigned to, other parts of your program will also no longer consider the
7090 string to be \s-1UTF\-8\s0 encoded.  In other words, if you do have such characters
7091 in your string, \fIvec()\fR will operate on the actual byte string, and not the
7092 conceptual character string.
7094 Strings created with \f(CW\*(C`vec\*(C'\fR can also be manipulated with the logical
7095 operators \f(CW\*(C`|\*(C'\fR, \f(CW\*(C`&\*(C'\fR, \f(CW\*(C`^\*(C'\fR, and \f(CW\*(C`~\*(C'\fR.  These operators will assume a bit
7096 vector operation is desired when both operands are strings.
7097 See \*(L"Bitwise String Operators\*(R" in perlop.
7099 The following code will build up an \s-1ASCII\s0 string saying \f(CW'PerlPerlPerl'\fR.
7100 The comments show the string after each step.  Note that this code works
7101 in the same way on big-endian or little-endian machines.
7103 .Vb 2
7104 \&    my $foo = '';
7105 \&    vec($foo,  0, 32) = 0x5065726C;     # 'Perl'
7108 .Vb 2
7109 \&    # $foo eq "Perl" eq "\ex50\ex65\ex72\ex6C", 32 bits
7110 \&    print vec($foo, 0, 8);              # prints 80 == 0x50 == ord('P')
7113 .Vb 11
7114 \&    vec($foo,  2, 16) = 0x5065;         # 'PerlPe'
7115 \&    vec($foo,  3, 16) = 0x726C;         # 'PerlPerl'
7116 \&    vec($foo,  8,  8) = 0x50;           # 'PerlPerlP'
7117 \&    vec($foo,  9,  8) = 0x65;           # 'PerlPerlPe'
7118 \&    vec($foo, 20,  4) = 2;              # 'PerlPerlPe'   . "\ex02"
7119 \&    vec($foo, 21,  4) = 7;              # 'PerlPerlPer'
7120 \&                                        # 'r' is "\ex72"
7121 \&    vec($foo, 45,  2) = 3;              # 'PerlPerlPer'  . "\ex0c"
7122 \&    vec($foo, 93,  1) = 1;              # 'PerlPerlPer'  . "\ex2c"
7123 \&    vec($foo, 94,  1) = 1;              # 'PerlPerlPerl'
7124 \&                                        # 'l' is "\ex6c"
7127 To transform a bit vector into a string or list of 0's and 1's, use these:
7129 .Vb 2
7130 \&    $bits = unpack("b*", $vector);
7131 \&    @bits = split(//, unpack("b*", $vector));
7134 If you know the exact length in bits, it can be used in place of the \f(CW\*(C`*\*(C'\fR.
7136 Here is an example to illustrate how the bits actually fall in place:
7138 .Vb 1
7139 \&    #!/usr/bin/perl -wl
7142 .Vb 5
7143 \&    print <<'EOT';
7144 \&                                      0         1         2         3
7145 \&                       unpack("V",$_) 01234567890123456789012345678901
7146 \&    ------------------------------------------------------------------
7147 \&    EOT
7150 .Vb 13
7151 \&    for $w (0..3) {
7152 \&        $width = 2**$w;
7153 \&        for ($shift=0; $shift < $width; ++$shift) {
7154 \&            for ($off=0; $off < 32/$width; ++$off) {
7155 \&                $str = pack("B*", "0"x32);
7156 \&                $bits = (1<<$shift);
7157 \&                vec($str, $off, $width) = $bits;
7158 \&                $res = unpack("b*",$str);
7159 \&                $val = unpack("V", $str);
7160 \&                write;
7161 \&            }
7162 \&        }
7163 \&    }
7166 .Vb 5
7167 \&    format STDOUT =
7168 \&    vec($_,@#,@#) = @<< == @######### @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
7169 \&    $off, $width, $bits, $val, $res
7170 \&    .
7171 \&    __END__
7174 Regardless of the machine architecture on which it is run, the above
7175 example should print the following table:
7177 .Vb 131
7178 \&                                      0         1         2         3
7179 \&                       unpack("V",$_) 01234567890123456789012345678901
7180 \&    ------------------------------------------------------------------
7181 \&    vec($_, 0, 1) = 1   ==          1 10000000000000000000000000000000
7182 \&    vec($_, 1, 1) = 1   ==          2 01000000000000000000000000000000
7183 \&    vec($_, 2, 1) = 1   ==          4 00100000000000000000000000000000
7184 \&    vec($_, 3, 1) = 1   ==          8 00010000000000000000000000000000
7185 \&    vec($_, 4, 1) = 1   ==         16 00001000000000000000000000000000
7186 \&    vec($_, 5, 1) = 1   ==         32 00000100000000000000000000000000
7187 \&    vec($_, 6, 1) = 1   ==         64 00000010000000000000000000000000
7188 \&    vec($_, 7, 1) = 1   ==        128 00000001000000000000000000000000
7189 \&    vec($_, 8, 1) = 1   ==        256 00000000100000000000000000000000
7190 \&    vec($_, 9, 1) = 1   ==        512 00000000010000000000000000000000
7191 \&    vec($_,10, 1) = 1   ==       1024 00000000001000000000000000000000
7192 \&    vec($_,11, 1) = 1   ==       2048 00000000000100000000000000000000
7193 \&    vec($_,12, 1) = 1   ==       4096 00000000000010000000000000000000
7194 \&    vec($_,13, 1) = 1   ==       8192 00000000000001000000000000000000
7195 \&    vec($_,14, 1) = 1   ==      16384 00000000000000100000000000000000
7196 \&    vec($_,15, 1) = 1   ==      32768 00000000000000010000000000000000
7197 \&    vec($_,16, 1) = 1   ==      65536 00000000000000001000000000000000
7198 \&    vec($_,17, 1) = 1   ==     131072 00000000000000000100000000000000
7199 \&    vec($_,18, 1) = 1   ==     262144 00000000000000000010000000000000
7200 \&    vec($_,19, 1) = 1   ==     524288 00000000000000000001000000000000
7201 \&    vec($_,20, 1) = 1   ==    1048576 00000000000000000000100000000000
7202 \&    vec($_,21, 1) = 1   ==    2097152 00000000000000000000010000000000
7203 \&    vec($_,22, 1) = 1   ==    4194304 00000000000000000000001000000000
7204 \&    vec($_,23, 1) = 1   ==    8388608 00000000000000000000000100000000
7205 \&    vec($_,24, 1) = 1   ==   16777216 00000000000000000000000010000000
7206 \&    vec($_,25, 1) = 1   ==   33554432 00000000000000000000000001000000
7207 \&    vec($_,26, 1) = 1   ==   67108864 00000000000000000000000000100000
7208 \&    vec($_,27, 1) = 1   ==  134217728 00000000000000000000000000010000
7209 \&    vec($_,28, 1) = 1   ==  268435456 00000000000000000000000000001000
7210 \&    vec($_,29, 1) = 1   ==  536870912 00000000000000000000000000000100
7211 \&    vec($_,30, 1) = 1   == 1073741824 00000000000000000000000000000010
7212 \&    vec($_,31, 1) = 1   == 2147483648 00000000000000000000000000000001
7213 \&    vec($_, 0, 2) = 1   ==          1 10000000000000000000000000000000
7214 \&    vec($_, 1, 2) = 1   ==          4 00100000000000000000000000000000
7215 \&    vec($_, 2, 2) = 1   ==         16 00001000000000000000000000000000
7216 \&    vec($_, 3, 2) = 1   ==         64 00000010000000000000000000000000
7217 \&    vec($_, 4, 2) = 1   ==        256 00000000100000000000000000000000
7218 \&    vec($_, 5, 2) = 1   ==       1024 00000000001000000000000000000000
7219 \&    vec($_, 6, 2) = 1   ==       4096 00000000000010000000000000000000
7220 \&    vec($_, 7, 2) = 1   ==      16384 00000000000000100000000000000000
7221 \&    vec($_, 8, 2) = 1   ==      65536 00000000000000001000000000000000
7222 \&    vec($_, 9, 2) = 1   ==     262144 00000000000000000010000000000000
7223 \&    vec($_,10, 2) = 1   ==    1048576 00000000000000000000100000000000
7224 \&    vec($_,11, 2) = 1   ==    4194304 00000000000000000000001000000000
7225 \&    vec($_,12, 2) = 1   ==   16777216 00000000000000000000000010000000
7226 \&    vec($_,13, 2) = 1   ==   67108864 00000000000000000000000000100000
7227 \&    vec($_,14, 2) = 1   ==  268435456 00000000000000000000000000001000
7228 \&    vec($_,15, 2) = 1   == 1073741824 00000000000000000000000000000010
7229 \&    vec($_, 0, 2) = 2   ==          2 01000000000000000000000000000000
7230 \&    vec($_, 1, 2) = 2   ==          8 00010000000000000000000000000000
7231 \&    vec($_, 2, 2) = 2   ==         32 00000100000000000000000000000000
7232 \&    vec($_, 3, 2) = 2   ==        128 00000001000000000000000000000000
7233 \&    vec($_, 4, 2) = 2   ==        512 00000000010000000000000000000000
7234 \&    vec($_, 5, 2) = 2   ==       2048 00000000000100000000000000000000
7235 \&    vec($_, 6, 2) = 2   ==       8192 00000000000001000000000000000000
7236 \&    vec($_, 7, 2) = 2   ==      32768 00000000000000010000000000000000
7237 \&    vec($_, 8, 2) = 2   ==     131072 00000000000000000100000000000000
7238 \&    vec($_, 9, 2) = 2   ==     524288 00000000000000000001000000000000
7239 \&    vec($_,10, 2) = 2   ==    2097152 00000000000000000000010000000000
7240 \&    vec($_,11, 2) = 2   ==    8388608 00000000000000000000000100000000
7241 \&    vec($_,12, 2) = 2   ==   33554432 00000000000000000000000001000000
7242 \&    vec($_,13, 2) = 2   ==  134217728 00000000000000000000000000010000
7243 \&    vec($_,14, 2) = 2   ==  536870912 00000000000000000000000000000100
7244 \&    vec($_,15, 2) = 2   == 2147483648 00000000000000000000000000000001
7245 \&    vec($_, 0, 4) = 1   ==          1 10000000000000000000000000000000
7246 \&    vec($_, 1, 4) = 1   ==         16 00001000000000000000000000000000
7247 \&    vec($_, 2, 4) = 1   ==        256 00000000100000000000000000000000
7248 \&    vec($_, 3, 4) = 1   ==       4096 00000000000010000000000000000000
7249 \&    vec($_, 4, 4) = 1   ==      65536 00000000000000001000000000000000
7250 \&    vec($_, 5, 4) = 1   ==    1048576 00000000000000000000100000000000
7251 \&    vec($_, 6, 4) = 1   ==   16777216 00000000000000000000000010000000
7252 \&    vec($_, 7, 4) = 1   ==  268435456 00000000000000000000000000001000
7253 \&    vec($_, 0, 4) = 2   ==          2 01000000000000000000000000000000
7254 \&    vec($_, 1, 4) = 2   ==         32 00000100000000000000000000000000
7255 \&    vec($_, 2, 4) = 2   ==        512 00000000010000000000000000000000
7256 \&    vec($_, 3, 4) = 2   ==       8192 00000000000001000000000000000000
7257 \&    vec($_, 4, 4) = 2   ==     131072 00000000000000000100000000000000
7258 \&    vec($_, 5, 4) = 2   ==    2097152 00000000000000000000010000000000
7259 \&    vec($_, 6, 4) = 2   ==   33554432 00000000000000000000000001000000
7260 \&    vec($_, 7, 4) = 2   ==  536870912 00000000000000000000000000000100
7261 \&    vec($_, 0, 4) = 4   ==          4 00100000000000000000000000000000
7262 \&    vec($_, 1, 4) = 4   ==         64 00000010000000000000000000000000
7263 \&    vec($_, 2, 4) = 4   ==       1024 00000000001000000000000000000000
7264 \&    vec($_, 3, 4) = 4   ==      16384 00000000000000100000000000000000
7265 \&    vec($_, 4, 4) = 4   ==     262144 00000000000000000010000000000000
7266 \&    vec($_, 5, 4) = 4   ==    4194304 00000000000000000000001000000000
7267 \&    vec($_, 6, 4) = 4   ==   67108864 00000000000000000000000000100000
7268 \&    vec($_, 7, 4) = 4   == 1073741824 00000000000000000000000000000010
7269 \&    vec($_, 0, 4) = 8   ==          8 00010000000000000000000000000000
7270 \&    vec($_, 1, 4) = 8   ==        128 00000001000000000000000000000000
7271 \&    vec($_, 2, 4) = 8   ==       2048 00000000000100000000000000000000
7272 \&    vec($_, 3, 4) = 8   ==      32768 00000000000000010000000000000000
7273 \&    vec($_, 4, 4) = 8   ==     524288 00000000000000000001000000000000
7274 \&    vec($_, 5, 4) = 8   ==    8388608 00000000000000000000000100000000
7275 \&    vec($_, 6, 4) = 8   ==  134217728 00000000000000000000000000010000
7276 \&    vec($_, 7, 4) = 8   == 2147483648 00000000000000000000000000000001
7277 \&    vec($_, 0, 8) = 1   ==          1 10000000000000000000000000000000
7278 \&    vec($_, 1, 8) = 1   ==        256 00000000100000000000000000000000
7279 \&    vec($_, 2, 8) = 1   ==      65536 00000000000000001000000000000000
7280 \&    vec($_, 3, 8) = 1   ==   16777216 00000000000000000000000010000000
7281 \&    vec($_, 0, 8) = 2   ==          2 01000000000000000000000000000000
7282 \&    vec($_, 1, 8) = 2   ==        512 00000000010000000000000000000000
7283 \&    vec($_, 2, 8) = 2   ==     131072 00000000000000000100000000000000
7284 \&    vec($_, 3, 8) = 2   ==   33554432 00000000000000000000000001000000
7285 \&    vec($_, 0, 8) = 4   ==          4 00100000000000000000000000000000
7286 \&    vec($_, 1, 8) = 4   ==       1024 00000000001000000000000000000000
7287 \&    vec($_, 2, 8) = 4   ==     262144 00000000000000000010000000000000
7288 \&    vec($_, 3, 8) = 4   ==   67108864 00000000000000000000000000100000
7289 \&    vec($_, 0, 8) = 8   ==          8 00010000000000000000000000000000
7290 \&    vec($_, 1, 8) = 8   ==       2048 00000000000100000000000000000000
7291 \&    vec($_, 2, 8) = 8   ==     524288 00000000000000000001000000000000
7292 \&    vec($_, 3, 8) = 8   ==  134217728 00000000000000000000000000010000
7293 \&    vec($_, 0, 8) = 16  ==         16 00001000000000000000000000000000
7294 \&    vec($_, 1, 8) = 16  ==       4096 00000000000010000000000000000000
7295 \&    vec($_, 2, 8) = 16  ==    1048576 00000000000000000000100000000000
7296 \&    vec($_, 3, 8) = 16  ==  268435456 00000000000000000000000000001000
7297 \&    vec($_, 0, 8) = 32  ==         32 00000100000000000000000000000000
7298 \&    vec($_, 1, 8) = 32  ==       8192 00000000000001000000000000000000
7299 \&    vec($_, 2, 8) = 32  ==    2097152 00000000000000000000010000000000
7300 \&    vec($_, 3, 8) = 32  ==  536870912 00000000000000000000000000000100
7301 \&    vec($_, 0, 8) = 64  ==         64 00000010000000000000000000000000
7302 \&    vec($_, 1, 8) = 64  ==      16384 00000000000000100000000000000000
7303 \&    vec($_, 2, 8) = 64  ==    4194304 00000000000000000000001000000000
7304 \&    vec($_, 3, 8) = 64  == 1073741824 00000000000000000000000000000010
7305 \&    vec($_, 0, 8) = 128 ==        128 00000001000000000000000000000000
7306 \&    vec($_, 1, 8) = 128 ==      32768 00000000000000010000000000000000
7307 \&    vec($_, 2, 8) = 128 ==    8388608 00000000000000000000000100000000
7308 \&    vec($_, 3, 8) = 128 == 2147483648 00000000000000000000000000000001
7310 .IP "wait" 8
7311 .IX Item "wait"
7312 Behaves like the \fIwait\fR\|(2) system call on your system: it waits for a child
7313 process to terminate and returns the pid of the deceased process, or
7314 \&\f(CW\*(C`\-1\*(C'\fR if there are no child processes.  The status is returned in \f(CW$?\fR.
7315 Note that a return value of \f(CW\*(C`\-1\*(C'\fR could mean that child processes are
7316 being automatically reaped, as described in perlipc.
7317 .IP "waitpid \s-1PID\s0,FLAGS" 8
7318 .IX Item "waitpid PID,FLAGS"
7319 Waits for a particular child process to terminate and returns the pid of
7320 the deceased process, or \f(CW\*(C`\-1\*(C'\fR if there is no such child process.  On some
7321 systems, a value of 0 indicates that there are processes still running.
7322 The status is returned in \f(CW$?\fR.  If you say
7324 .Vb 5
7325 \&    use POSIX ":sys_wait_h";
7326 \&    #...
7327 \&    do {
7328 \&        $kid = waitpid(-1, WNOHANG);
7329 \&    } until $kid > 0;
7332 then you can do a non-blocking wait for all pending zombie processes.
7333 Non-blocking wait is available on machines supporting either the
7334 \&\fIwaitpid\fR\|(2) or \fIwait4\fR\|(2) system calls.  However, waiting for a particular
7335 pid with \s-1FLAGS\s0 of \f(CW0\fR is implemented everywhere.  (Perl emulates the
7336 system call by remembering the status values of processes that have
7337 exited but have not been harvested by the Perl script yet.)
7339 Note that on some systems, a return value of \f(CW\*(C`\-1\*(C'\fR could mean that child
7340 processes are being automatically reaped.  See perlipc for details,
7341 and for other examples.
7342 .IP "wantarray" 8
7343 .IX Item "wantarray"
7344 Returns true if the context of the currently executing subroutine is
7345 looking for a list value.  Returns false if the context is looking
7346 for a scalar.  Returns the undefined value if the context is looking
7347 for no value (void context).
7349 .Vb 3
7350 \&    return unless defined wantarray;    # don't bother doing more
7351 \&    my @a = complex_calculation();
7352 \&    return wantarray ? @a : "@a";
7355 This function should have been named \fIwantlist()\fR instead.
7356 .IP "warn \s-1LIST\s0" 8
7357 .IX Item "warn LIST"
7358 Produces a message on \s-1STDERR\s0 just like \f(CW\*(C`die\*(C'\fR, but doesn't exit or throw
7359 an exception.
7361 If \s-1LIST\s0 is empty and \f(CW$@\fR already contains a value (typically from a
7362 previous eval) that value is used after appending \f(CW"\et...caught"\fR
7363 to \f(CW$@\fR.  This is useful for staying almost, but not entirely similar to
7364 \&\f(CW\*(C`die\*(C'\fR.
7366 If \f(CW$@\fR is empty then the string \f(CW"Warning: Something's wrong"\fR is used.
7368 No message is printed if there is a \f(CW$SIG{_\|_WARN_\|_}\fR handler
7369 installed.  It is the handler's responsibility to deal with the message
7370 as it sees fit (like, for instance, converting it into a \f(CW\*(C`die\*(C'\fR).  Most
7371 handlers must therefore make arrangements to actually display the
7372 warnings that they are not prepared to deal with, by calling \f(CW\*(C`warn\*(C'\fR
7373 again in the handler.  Note that this is quite safe and will not
7374 produce an endless loop, since \f(CW\*(C`_\|_WARN_\|_\*(C'\fR hooks are not called from
7375 inside one.
7377 You will find this behavior is slightly different from that of
7378 \&\f(CW$SIG{_\|_DIE_\|_}\fR handlers (which don't suppress the error text, but can
7379 instead call \f(CW\*(C`die\*(C'\fR again to change it).
7381 Using a \f(CW\*(C`_\|_WARN_\|_\*(C'\fR handler provides a powerful way to silence all
7382 warnings (even the so-called mandatory ones).  An example:
7384 .Vb 7
7385 \&    # wipe out *all* compile-time warnings
7386 \&    BEGIN { $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN } }
7387 \&    my $foo = 10;
7388 \&    my $foo = 20;          # no warning about duplicate my $foo,
7389 \&                           # but hey, you asked for it!
7390 \&    # no compile-time or run-time warnings before here
7391 \&    $DOWARN = 1;
7394 .Vb 2
7395 \&    # run-time warnings enabled after here
7396 \&    warn "\e$foo is alive and $foo!";     # does show up
7399 See perlvar for details on setting \f(CW%SIG\fR entries, and for more
7400 examples.  See the Carp module for other kinds of warnings using its
7401 \&\fIcarp()\fR and \fIcluck()\fR functions.
7402 .IP "write \s-1FILEHANDLE\s0" 8
7403 .IX Item "write FILEHANDLE"
7404 .PD 0
7405 .IP "write \s-1EXPR\s0" 8
7406 .IX Item "write EXPR"
7407 .IP "write" 8
7408 .IX Item "write"
7410 Writes a formatted record (possibly multi\-line) to the specified \s-1FILEHANDLE\s0,
7411 using the format associated with that file.  By default the format for
7412 a file is the one having the same name as the filehandle, but the
7413 format for the current output channel (see the \f(CW\*(C`select\*(C'\fR function) may be set
7414 explicitly by assigning the name of the format to the \f(CW$~\fR variable.
7416 Top of form processing is handled automatically:  if there is
7417 insufficient room on the current page for the formatted record, the
7418 page is advanced by writing a form feed, a special top-of-page format
7419 is used to format the new page header, and then the record is written.
7420 By default the top-of-page format is the name of the filehandle with
7421 \&\*(L"_TOP\*(R" appended, but it may be dynamically set to the format of your
7422 choice by assigning the name to the \f(CW$^\fR variable while the filehandle is
7423 selected.  The number of lines remaining on the current page is in
7424 variable \f(CW\*(C`$\-\*(C'\fR, which can be set to \f(CW0\fR to force a new page.
7426 If \s-1FILEHANDLE\s0 is unspecified, output goes to the current default output
7427 channel, which starts out as \s-1STDOUT\s0 but may be changed by the
7428 \&\f(CW\*(C`select\*(C'\fR operator.  If the \s-1FILEHANDLE\s0 is an \s-1EXPR\s0, then the expression
7429 is evaluated and the resulting string is used to look up the name of
7430 the \s-1FILEHANDLE\s0 at run time.  For more on formats, see perlform.
7432 Note that write is \fInot\fR the opposite of \f(CW\*(C`read\*(C'\fR.  Unfortunately.
7433 .IP "y///" 8
7434 .IX Item "y///"
7435 The transliteration operator.  Same as \f(CW\*(C`tr///\*(C'\fR.  See perlop.