Debian Codegraph @

upstream/debian-unstable/man1/perlfunc.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "PERLFUNC 1"
58 .TH PERLFUNC 1 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 perlfunc \- Perl builtin functions
65 .IX Xref "function"
66 .SH DESCRIPTION
67 .IX Header "DESCRIPTION"
68 The functions in this section can serve as terms in an expression.
69 They fall into two major categories: list operators and named unary
70 operators. These differ in their precedence relationship with a
71 following comma. (See the precedence table in perlop.) List
72 operators take more than one argument, while unary operators can never
73 take more than one argument. Thus, a comma terminates the argument of
74 a unary operator, but merely separates the arguments of a list
75 operator. A unary operator generally provides scalar context to its
76 argument, while a list operator may provide either scalar or list
77 contexts for its arguments. If it does both, scalar arguments
78 come first and list argument follow, and there can only ever
79 be one such list argument. For instance,
80 \&\f(CW\*(C`splice\*(C'\fR has three scalar arguments
81 followed by a list, whereas \f(CW\*(C`gethostbyname\*(C'\fR has
82 four scalar arguments.
83 .PP
84 In the syntax descriptions that follow, list operators that expect a
85 list (and provide list context for elements of the list) are shown
86 with LIST as an argument. Such a list may consist of any combination
87 of scalar arguments or list values; the list values will be included
88 in the list as if each individual element were interpolated at that
89 point in the list, forming a longer single-dimensional list value.
90 Commas should separate literal elements of the LIST.
91 .PP
92 Any function in the list below may be used either with or without
93 parentheses around its arguments. (The syntax descriptions omit the
94 parentheses.) If you use parentheses, the simple but occasionally
95 surprising rule is this: It \fIlooks\fR like a function, therefore it \fIis\fR a
96 function, and precedence doesn't matter. Otherwise it's a list
97 operator or unary operator, and precedence does matter. Whitespace
98 between the function and left parenthesis doesn't count, so sometimes
99 you need to be careful:
100 .PP
101 .Vb 5
102 \& print 1+2+4; # Prints 7.
103 \& print(1+2) + 4; # Prints 3.
104 \& print (1+2)+4; # Also prints 3!
105 \& print +(1+2)+4; # Prints 7.
106 \& print ((1+2)+4); # Prints 7.
107 .Ve
108 .PP
109 If you run Perl with the \f(CW\*(C`use warnings\*(C'\fR pragma, it can warn
110 you about this. For example, the third line above produces:
111 .PP
112 .Vb 2
113 \& print (...) interpreted as function at \- line 1.
114 \& Useless use of integer addition in void context at \- line 1.
115 .Ve
116 .PP
117 A few functions take no arguments at all, and therefore work as neither
118 unary nor list operators. These include such functions as
119 \&\f(CW\*(C`time\*(C'\fR and \f(CW\*(C`endpwent\*(C'\fR. For example,
120 \&\f(CW\*(C`time+86_400\*(C'\fR always means \f(CW\*(C`time() + 86_400\*(C'\fR.
121 .PP
122 For functions that can be used in either a scalar or list context,
123 nonabortive failure is generally indicated in scalar context by
124 returning the undefined value, and in list context by returning the
125 empty list.
126 .PP
127 Remember the following important rule: There is \fBno rule\fR that relates
128 the behavior of an expression in list context to its behavior in scalar
129 context, or vice versa. It might do two totally different things.
130 Each operator and function decides which sort of value would be most
131 appropriate to return in scalar context. Some operators return the
132 length of the list that would have been returned in list context. Some
133 operators return the first value in the list. Some operators return the
134 last value in the list. Some operators return a count of successful
135 operations. In general, they do what you want, unless you want
136 consistency.
137 .IX Xref "context"
138 .PP
139 A named array in scalar context is quite different from what would at
140 first glance appear to be a list in scalar context. You can't get a list
141 like \f(CW\*(C`(1,2,3)\*(C'\fR into being in scalar context, because the compiler knows
142 the context at compile time. It would generate the scalar comma operator
143 there, not the list concatenation version of the comma. That means it
144 was never a list to start with.
145 .PP
146 In general, functions in Perl that serve as wrappers for system calls
147 ("syscalls") of the same name (like \fBchown\fR\|(2), \fBfork\fR\|(2),
148 \&\fBclosedir\fR\|(2), etc.) return true when they succeed and
149 \&\f(CW\*(C`undef\*(C'\fR otherwise, as is usually mentioned in the
150 descriptions below. This is different from the C interfaces, which
151 return \f(CW\-1\fR on failure. Exceptions to this rule include
152 \&\f(CW\*(C`wait\*(C'\fR, \f(CW\*(C`waitpid\*(C'\fR, and
153 \&\f(CW\*(C`syscall\*(C'\fR. System calls also set the special
154 \&\f(CW$!\fR variable on failure. Other functions do not, except
155 accidentally.
156 .PP
157 Extension modules can also hook into the Perl parser to define new
158 kinds of keyword-headed expression. These may look like functions, but
159 may also look completely different. The syntax following the keyword
160 is defined entirely by the extension. If you are an implementor, see
161 "PL_keyword_plugin" in perlapi for the mechanism. If you are using such
162 a module, see the module's documentation for details of the syntax that
163 it defines.
164 .SS "Perl Functions by Category"
165 .IX Xref "function"
166 .IX Subsection "Perl Functions by Category"
167 Here are Perl's functions (including things that look like
168 functions, like some keywords and named operators)
169 arranged by category. Some functions appear in more
170 than one place. Any warnings, including those produced by
171 keywords, are described in perldiag and warnings.
172 .IP "Functions for SCALARs or strings" 4
173 .IX Xref "scalar string character"
174 .IX Item "Functions for SCALARs or strings"
175 \&\f(CW\*(C`chomp\*(C'\fR, \f(CW\*(C`chop\*(C'\fR,
176 \&\f(CW\*(C`chr\*(C'\fR, \f(CW\*(C`crypt\*(C'\fR,
177 \&\f(CW\*(C`fc\*(C'\fR, \f(CW\*(C`hex\*(C'\fR,
178 \&\f(CW\*(C`index\*(C'\fR, \f(CW\*(C`lc\*(C'\fR,
179 \&\f(CW\*(C`lcfirst\*(C'\fR, \f(CW\*(C`length\*(C'\fR,
180 \&\f(CW\*(C`oct\*(C'\fR, \f(CW\*(C`ord\*(C'\fR,
181 \&\f(CW\*(C`pack\*(C'\fR,
182 \&\f(CW\*(C`q//\*(C'\fR,
183 \&\f(CW\*(C`qq//\*(C'\fR, \f(CW\*(C`reverse\*(C'\fR,
184 \&\f(CW\*(C`rindex\*(C'\fR,
185 \&\f(CW\*(C`sprintf\*(C'\fR,
186 \&\f(CW\*(C`substr\*(C'\fR,
187 \&\f(CW\*(C`tr///\*(C'\fR, \f(CW\*(C`uc\*(C'\fR,
188 \&\f(CW\*(C`ucfirst\*(C'\fR,
189 \&\f(CW\*(C`y///\*(C'\fR
190 .Sp
191 \&\f(CW\*(C`fc\*(C'\fR is available only if the
192 \&\f(CW"fc"\fR feature is enabled or if it is
193 prefixed with \f(CW\*(C`CORE::\*(C'\fR. The
194 \&\f(CW"fc"\fR feature is enabled automatically
195 with a \f(CW\*(C`use v5.16\*(C'\fR (or higher) declaration in the current scope.
196 .IP "Regular expressions and pattern matching" 4
197 .IX Xref "regular expression regex regexp"
198 .IX Item "Regular expressions and pattern matching"
199 \&\f(CW\*(C`m//\*(C'\fR, \f(CW\*(C`pos\*(C'\fR,
200 \&\f(CW\*(C`qr//\*(C'\fR,
201 \&\f(CW\*(C`quotemeta\*(C'\fR,
202 \&\f(CW\*(C`s///\*(C'\fR,
203 \&\f(CW\*(C`split\*(C'\fR,
204 \&\f(CW\*(C`study\*(C'\fR
205 .IP "Numeric functions" 4
206 .IX Xref "numeric number trigonometric trigonometry"
207 .IX Item "Numeric functions"
208 \&\f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`atan2\*(C'\fR, \f(CW\*(C`cos\*(C'\fR,
209 \&\f(CW\*(C`exp\*(C'\fR, \f(CW\*(C`hex\*(C'\fR, \f(CW\*(C`int\*(C'\fR,
210 \&\f(CW\*(C`log\*(C'\fR, \f(CW\*(C`oct\*(C'\fR, \f(CW\*(C`rand\*(C'\fR,
211 \&\f(CW\*(C`sin\*(C'\fR, \f(CW\*(C`sqrt\*(C'\fR, \f(CW\*(C`srand\*(C'\fR
212 .ie n .IP "Functions for real @ARRAYs" 4
213 .el .IP "Functions for real \f(CW@ARRAYs\fR" 4
214 .IX Xref "array"
215 .IX Item "Functions for real @ARRAYs"
216 \&\f(CW\*(C`each\*(C'\fR, \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`pop\*(C'\fR,
217 \&\f(CW\*(C`push\*(C'\fR, \f(CW\*(C`shift\*(C'\fR,
218 \&\f(CW\*(C`splice\*(C'\fR,
219 \&\f(CW\*(C`unshift\*(C'\fR, \f(CW\*(C`values\*(C'\fR
220 .IP "Functions for list data" 4
221 .IX Xref "list"
222 .IX Item "Functions for list data"
223 \&\f(CW\*(C`grep\*(C'\fR, \f(CW\*(C`join\*(C'\fR,
224 \&\f(CW\*(C`map\*(C'\fR, \f(CW\*(C`qw//\*(C'\fR,
225 \&\f(CW\*(C`reverse\*(C'\fR, \f(CW\*(C`sort\*(C'\fR,
226 \&\f(CW\*(C`unpack\*(C'\fR
227 .ie n .IP "Functions for real %HASHes" 4
228 .el .IP "Functions for real \f(CW%HASHes\fR" 4
229 .IX Xref "hash"
230 .IX Item "Functions for real %HASHes"
231 \&\f(CW\*(C`delete\*(C'\fR, \f(CW\*(C`each\*(C'\fR,
232 \&\f(CW\*(C`exists\*(C'\fR, \f(CW\*(C`keys\*(C'\fR,
233 \&\f(CW\*(C`values\*(C'\fR
234 .IP "Input and output functions" 4
235 .IX Xref "I O input output dbm"
236 .IX Item "Input and output functions"
237 \&\f(CW\*(C`binmode\*(C'\fR, \f(CW\*(C`close\*(C'\fR,
238 \&\f(CW\*(C`closedir\*(C'\fR, \f(CW\*(C`dbmclose\*(C'\fR,
239 \&\f(CW\*(C`dbmopen\*(C'\fR, \f(CW\*(C`die\*(C'\fR,
240 \&\f(CW\*(C`eof\*(C'\fR, \f(CW\*(C`fileno\*(C'\fR,
241 \&\f(CW\*(C`flock\*(C'\fR, \f(CW\*(C`format\*(C'\fR,
242 \&\f(CW\*(C`getc\*(C'\fR, \f(CW\*(C`print\*(C'\fR,
243 \&\f(CW\*(C`printf\*(C'\fR,
244 \&\f(CW\*(C`read\*(C'\fR,
245 \&\f(CW\*(C`readdir\*(C'\fR, \f(CW\*(C`readline\*(C'\fR,
246 \&\f(CW\*(C`rewinddir\*(C'\fR, \f(CW\*(C`say\*(C'\fR,
247 \&\f(CW\*(C`seek\*(C'\fR,
248 \&\f(CW\*(C`seekdir\*(C'\fR,
249 \&\f(CW\*(C`select\*(C'\fR,
250 \&\f(CW\*(C`syscall\*(C'\fR,
251 \&\f(CW\*(C`sysread\*(C'\fR,
252 \&\f(CW\*(C`sysseek\*(C'\fR,
253 \&\f(CW\*(C`syswrite\*(C'\fR,
254 \&\f(CW\*(C`tell\*(C'\fR, \f(CW\*(C`telldir\*(C'\fR,
255 \&\f(CW\*(C`truncate\*(C'\fR, \f(CW\*(C`warn\*(C'\fR,
256 \&\f(CW\*(C`write\*(C'\fR
257 .Sp
258 \&\f(CW\*(C`say\*(C'\fR is available only if the
259 \&\f(CW"say"\fR feature is enabled or if it is
260 prefixed with \f(CW\*(C`CORE::\*(C'\fR. The
261 \&\f(CW"say"\fR feature is enabled automatically
262 with a \f(CW\*(C`use v5.10\*(C'\fR (or higher) declaration in the current scope.
263 .IP "Functions for fixed-length data or records" 4
264 .IX Item "Functions for fixed-length data or records"
265 \&\f(CW\*(C`pack\*(C'\fR,
266 \&\f(CW\*(C`read\*(C'\fR,
267 \&\f(CW\*(C`syscall\*(C'\fR,
268 \&\f(CW\*(C`sysread\*(C'\fR,
269 \&\f(CW\*(C`sysseek\*(C'\fR,
270 \&\f(CW\*(C`syswrite\*(C'\fR,
271 \&\f(CW\*(C`unpack\*(C'\fR, \f(CW\*(C`vec\*(C'\fR
272 .IP "Functions for filehandles, files, or directories" 4
273 .IX Xref "file filehandle directory pipe link symlink"
274 .IX Item "Functions for filehandles, files, or directories"
275 \&\f(CW\*(C`\-\fR\f(CIX\fR\f(CW\*(C'\fR, \f(CW\*(C`chdir\*(C'\fR,
276 \&\f(CW\*(C`chmod\*(C'\fR, \f(CW\*(C`chown\*(C'\fR,
277 \&\f(CW\*(C`chroot\*(C'\fR,
278 \&\f(CW\*(C`fcntl\*(C'\fR, \f(CW\*(C`glob\*(C'\fR,
279 \&\f(CW\*(C`ioctl\*(C'\fR,
280 \&\f(CW\*(C`link\*(C'\fR, \f(CW\*(C`lstat\*(C'\fR,
281 \&\f(CW\*(C`mkdir\*(C'\fR, \f(CW\*(C`open\*(C'\fR,
282 \&\f(CW\*(C`opendir\*(C'\fR, \f(CW\*(C`readlink\*(C'\fR,
283 \&\f(CW\*(C`rename\*(C'\fR, \f(CW\*(C`rmdir\*(C'\fR,
284 \&\f(CW\*(C`select\*(C'\fR, \f(CW\*(C`stat\*(C'\fR,
285 \&\f(CW\*(C`symlink\*(C'\fR,
286 \&\f(CW\*(C`sysopen\*(C'\fR,
287 \&\f(CW\*(C`umask\*(C'\fR, \f(CW\*(C`unlink\*(C'\fR,
288 \&\f(CW\*(C`utime\*(C'\fR
289 .IP "Keywords related to the control flow of your Perl program" 4
290 .IX Xref "control flow"
291 .IX Item "Keywords related to the control flow of your Perl program"
292 \&\f(CW\*(C`break\*(C'\fR, \f(CW\*(C`caller\*(C'\fR,
293 \&\f(CW\*(C`continue\*(C'\fR, \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`do\*(C'\fR,
294 \&\f(CW\*(C`dump\*(C'\fR, \f(CW\*(C`eval\*(C'\fR,
295 \&\f(CW\*(C`evalbytes\*(C'\fR, \f(CW\*(C`exit\*(C'\fR,
296 \&\f(CW\*(C`_\|_FILE_\|_\*(C'\fR, \f(CW\*(C`goto\*(C'\fR,
297 \&\f(CW\*(C`last\*(C'\fR, \f(CW\*(C`_\|_LINE_\|_\*(C'\fR,
298 \&\f(CW\*(C`method\*(C'\fR,
299 \&\f(CW\*(C`next\*(C'\fR, \f(CW\*(C`_\|_PACKAGE_\|_\*(C'\fR,
300 \&\f(CW\*(C`redo\*(C'\fR, \f(CW\*(C`return\*(C'\fR,
301 \&\f(CW\*(C`sub\*(C'\fR, \f(CW\*(C`_\|_SUB_\|_\*(C'\fR,
302 \&\f(CW\*(C`wantarray\*(C'\fR
303 .Sp
304 \&\f(CW\*(C`break\*(C'\fR is available only if you enable the experimental
305 \&\f(CW"switch"\fR feature or use the \f(CW\*(C`CORE::\*(C'\fR
306 prefix. The \f(CW"switch"\fR feature also
307 enables the \f(CW\*(C`default\*(C'\fR, \f(CW\*(C`given\*(C'\fR and \f(CW\*(C`when\*(C'\fR statements, which are
308 documented in "Switch Statements" in perlsyn.
309 The \f(CW"switch"\fR feature is enabled
310 automatically with a \f(CW\*(C`use v5.10\*(C'\fR (or higher) declaration in the current
311 scope. In Perl v5.14 and earlier, \f(CW\*(C`continue\*(C'\fR
312 required the \f(CW"switch"\fR feature, like
313 the other keywords.
314 .Sp
315 \&\f(CW\*(C`evalbytes\*(C'\fR is only available with the
316 \&\f(CW"evalbytes"\fR feature
317 (see feature) or if prefixed with \f(CW\*(C`CORE::\*(C'\fR. \f(CW\*(C`_\|_SUB_\|_\*(C'\fR
318 is only available with the
319 \&\f(CW"current_sub"\fR feature or if
320 prefixed with \f(CW\*(C`CORE::\*(C'\fR. Both the
321 \&\f(CW"evalbytes"\fR
322 and \f(CW"current_sub"\fR features are
323 enabled automatically with a \f(CW\*(C`use v5.16\*(C'\fR (or higher) declaration in the
324 current scope.
325 .IP "Keywords related to scoping" 4
326 .IX Item "Keywords related to scoping"
327 \&\f(CW\*(C`caller\*(C'\fR,
328 \&\f(CW\*(C`class\*(C'\fR,
329 \&\f(CW\*(C`field\*(C'\fR,
330 \&\f(CW\*(C`import\*(C'\fR,
331 \&\f(CW\*(C`local\*(C'\fR,
332 \&\f(CW\*(C`my\*(C'\fR,
333 \&\f(CW\*(C`our\*(C'\fR,
334 \&\f(CW\*(C`package\*(C'\fR,
335 \&\f(CW\*(C`state\*(C'\fR,
336 \&\f(CW\*(C`use\*(C'\fR
337 .Sp
338 \&\f(CW\*(C`state\*(C'\fR is available only if the
339 \&\f(CW"state"\fR feature is enabled or if it is
340 prefixed with \f(CW\*(C`CORE::\*(C'\fR. The
341 \&\f(CW"state"\fR feature is enabled
342 automatically with a \f(CW\*(C`use v5.10\*(C'\fR (or higher) declaration in the current
343 scope.
344 .IP "Miscellaneous functions" 4
345 .IX Item "Miscellaneous functions"
346 \&\f(CW\*(C`defined\*(C'\fR, \f(CW\*(C`formline\*(C'\fR,
347 \&\f(CW\*(C`lock\*(C'\fR, \f(CW\*(C`prototype\*(C'\fR,
348 \&\f(CW\*(C`reset\*(C'\fR, \f(CW\*(C`scalar\*(C'\fR,
349 \&\f(CW\*(C`undef\*(C'\fR
350 .IP "Functions for processes and process groups" 4
351 .IX Xref "process pid process id"
352 .IX Item "Functions for processes and process groups"
353 \&\f(CW\*(C`alarm\*(C'\fR, \f(CW\*(C`exec\*(C'\fR, \f(CW\*(C`fork\*(C'\fR,
354 \&\f(CW\*(C`getpgrp\*(C'\fR, \f(CW\*(C`getppid\*(C'\fR,
355 \&\f(CW\*(C`getpriority\*(C'\fR, \f(CW\*(C`kill\*(C'\fR,
356 \&\f(CW\*(C`pipe\*(C'\fR,
357 \&\f(CW\*(C`qx//\*(C'\fR,
358 \&\f(CW\*(C`readpipe\*(C'\fR, \f(CW\*(C`setpgrp\*(C'\fR,
359 \&\f(CW\*(C`setpriority\*(C'\fR,
360 \&\f(CW\*(C`sleep\*(C'\fR, \f(CW\*(C`system\*(C'\fR, \f(CW\*(C`times\*(C'\fR,
361 \&\f(CW\*(C`wait\*(C'\fR, \f(CW\*(C`waitpid\*(C'\fR
362 .IP "Keywords related to Perl modules" 4
363 .IX Xref "module"
364 .IX Item "Keywords related to Perl modules"
365 \&\f(CW\*(C`do\*(C'\fR, \f(CW\*(C`import\*(C'\fR,
366 \&\f(CW\*(C`no\*(C'\fR, \f(CW\*(C`package\*(C'\fR,
367 \&\f(CW\*(C`require\*(C'\fR, \f(CW\*(C`use\*(C'\fR
368 .IP "Keywords related to classes and object-orientation" 4
369 .IX Xref "object class package"
370 .IX Item "Keywords related to classes and object-orientation"
371 \&\f(CW\*(C`bless\*(C'\fR,
372 \&\f(CW\*(C`class\*(C'\fR,
373 \&\f(CW\*(C`_\|_CLASS_\|_\*(C'\fR,
374 \&\f(CW\*(C`dbmclose\*(C'\fR,
375 \&\f(CW\*(C`dbmopen\*(C'\fR,
376 \&\f(CW\*(C`field\*(C'\fR,
377 \&\f(CW\*(C`method\*(C'\fR,
378 \&\f(CW\*(C`package\*(C'\fR,
379 \&\f(CW\*(C`ref\*(C'\fR,
380 \&\f(CW\*(C`tie\*(C'\fR,
381 \&\f(CW\*(C`tied\*(C'\fR,
382 \&\f(CW\*(C`untie\*(C'\fR,
383 \&\f(CW\*(C`use\*(C'\fR
384 .IP "Low-level socket functions" 4
385 .IX Xref "socket sock"
386 .IX Item "Low-level socket functions"
387 \&\f(CW\*(C`accept\*(C'\fR,
388 \&\f(CW\*(C`bind\*(C'\fR, \f(CW\*(C`connect\*(C'\fR,
389 \&\f(CW\*(C`getpeername\*(C'\fR,
390 \&\f(CW\*(C`getsockname\*(C'\fR,
391 \&\f(CW\*(C`getsockopt\*(C'\fR,
392 \&\f(CW\*(C`listen\*(C'\fR,
393 \&\f(CW\*(C`recv\*(C'\fR,
394 \&\f(CW\*(C`send\*(C'\fR,
395 \&\f(CW\*(C`setsockopt\*(C'\fR,
396 \&\f(CW\*(C`shutdown\*(C'\fR,
397 \&\f(CW\*(C`socket\*(C'\fR,
398 \&\f(CW\*(C`socketpair\*(C'\fR
399 .IP "System V interprocess communication functions" 4
400 .IX Xref "IPC System V semaphore shared memory memory message"
401 .IX Item "System V interprocess communication functions"
402 \&\f(CW\*(C`msgctl\*(C'\fR, \f(CW\*(C`msgget\*(C'\fR,
403 \&\f(CW\*(C`msgrcv\*(C'\fR,
404 \&\f(CW\*(C`msgsnd\*(C'\fR,
405 \&\f(CW\*(C`semctl\*(C'\fR,
406 \&\f(CW\*(C`semget\*(C'\fR, \f(CW\*(C`semop\*(C'\fR,
407 \&\f(CW\*(C`shmctl\*(C'\fR, \f(CW\*(C`shmget\*(C'\fR,
408 \&\f(CW\*(C`shmread\*(C'\fR,
409 \&\f(CW\*(C`shmwrite\*(C'\fR
410 .IP "Fetching user and group info" 4
411 .IX Xref "user group password uid gid passwd etc passwd"
412 .IX Item "Fetching user and group info"
413 \&\f(CW\*(C`endgrent\*(C'\fR, \f(CW\*(C`endhostent\*(C'\fR,
414 \&\f(CW\*(C`endnetent\*(C'\fR, \f(CW\*(C`endpwent\*(C'\fR,
415 \&\f(CW\*(C`getgrent\*(C'\fR, \f(CW\*(C`getgrgid\*(C'\fR,
416 \&\f(CW\*(C`getgrnam\*(C'\fR, \f(CW\*(C`getlogin\*(C'\fR,
417 \&\f(CW\*(C`getpwent\*(C'\fR, \f(CW\*(C`getpwnam\*(C'\fR,
418 \&\f(CW\*(C`getpwuid\*(C'\fR, \f(CW\*(C`setgrent\*(C'\fR,
419 \&\f(CW\*(C`setpwent\*(C'\fR
420 .IP "Fetching network info" 4
421 .IX Xref "network protocol host hostname IP address service"
422 .IX Item "Fetching network info"
423 \&\f(CW\*(C`endprotoent\*(C'\fR, \f(CW\*(C`endservent\*(C'\fR,
424 \&\f(CW\*(C`gethostbyaddr\*(C'\fR,
425 \&\f(CW\*(C`gethostbyname\*(C'\fR, \f(CW\*(C`gethostent\*(C'\fR,
426 \&\f(CW\*(C`getnetbyaddr\*(C'\fR,
427 \&\f(CW\*(C`getnetbyname\*(C'\fR, \f(CW\*(C`getnetent\*(C'\fR,
428 \&\f(CW\*(C`getprotobyname\*(C'\fR,
429 \&\f(CW\*(C`getprotobynumber\*(C'\fR,
430 \&\f(CW\*(C`getprotoent\*(C'\fR,
431 \&\f(CW\*(C`getservbyname\*(C'\fR,
432 \&\f(CW\*(C`getservbyport\*(C'\fR,
433 \&\f(CW\*(C`getservent\*(C'\fR, \f(CW\*(C`sethostent\*(C'\fR,
434 \&\f(CW\*(C`setnetent\*(C'\fR,
435 \&\f(CW\*(C`setprotoent\*(C'\fR,
436 \&\f(CW\*(C`setservent\*(C'\fR
437 .IP "Time-related functions" 4
438 .IX Xref "time date"
439 .IX Item "Time-related functions"
440 \&\f(CW\*(C`gmtime\*(C'\fR, \f(CW\*(C`localtime\*(C'\fR,
441 \&\f(CW\*(C`time\*(C'\fR, \f(CW\*(C`times\*(C'\fR
442 .IP "Non-function keywords" 4
443 .IX Item "Non-function keywords"
444 \&\f(CW\*(C`ADJUST\*(C'\fR,
445 \&\f(CW\*(C`and\*(C'\fR,
446 \&\f(CW\*(C`AUTOLOAD\*(C'\fR,
447 \&\f(CW\*(C`BEGIN\*(C'\fR,
448 \&\f(CW\*(C`catch\*(C'\fR,
449 \&\f(CW\*(C`CHECK\*(C'\fR,
450 \&\f(CW\*(C`cmp\*(C'\fR,
451 \&\f(CW\*(C`CORE\*(C'\fR,
452 \&\f(CW\*(C`_\|_DATA_\|_\*(C'\fR,
453 \&\f(CW\*(C`default\*(C'\fR,
454 \&\f(CW\*(C`defer\*(C'\fR,
455 \&\f(CW\*(C`DESTROY\*(C'\fR,
456 \&\f(CW\*(C`else\*(C'\fR,
457 \&\f(CW\*(C`elseif\*(C'\fR,
458 \&\f(CW\*(C`elsif\*(C'\fR,
459 \&\f(CW\*(C`END\*(C'\fR,
460 \&\f(CW\*(C`_\|_END_\|_\*(C'\fR,
461 \&\f(CW\*(C`eq\*(C'\fR,
462 \&\f(CW\*(C`finally\*(C'\fR,
463 \&\f(CW\*(C`for\*(C'\fR,
464 \&\f(CW\*(C`foreach\*(C'\fR,
465 \&\f(CW\*(C`ge\*(C'\fR,
466 \&\f(CW\*(C`given\*(C'\fR,
467 \&\f(CW\*(C`gt\*(C'\fR,
468 \&\f(CW\*(C`if\*(C'\fR,
469 \&\f(CW\*(C`INIT\*(C'\fR,
470 \&\f(CW\*(C`isa\*(C'\fR,
471 \&\f(CW\*(C`le\*(C'\fR,
472 \&\f(CW\*(C`lt\*(C'\fR,
473 \&\f(CW\*(C`ne\*(C'\fR,
474 \&\f(CW\*(C`not\*(C'\fR,
475 \&\f(CW\*(C`or\*(C'\fR,
476 \&\f(CW\*(C`try\*(C'\fR,
477 \&\f(CW\*(C`UNITCHECK\*(C'\fR,
478 \&\f(CW\*(C`unless\*(C'\fR,
479 \&\f(CW\*(C`until\*(C'\fR,
480 \&\f(CW\*(C`when\*(C'\fR,
481 \&\f(CW\*(C`while\*(C'\fR,
482 \&\f(CW\*(C`x\*(C'\fR,
483 \&\f(CW\*(C`xor\*(C'\fR
484 .SS Portability
485 .IX Xref "portability Unix portable"
486 .IX Subsection "Portability"
487 Perl was born in Unix and can therefore access all common Unix
488 system calls. In non-Unix environments, the functionality of some
489 Unix system calls may not be available or details of the available
490 functionality may differ slightly. The Perl functions affected
491 by this are:
492 .PP
493 \&\f(CW\*(C`\-\fR\f(CIX\fR\f(CW\*(C'\fR, \f(CW\*(C`binmode\*(C'\fR,
494 \&\f(CW\*(C`chmod\*(C'\fR, \f(CW\*(C`chown\*(C'\fR,
495 \&\f(CW\*(C`chroot\*(C'\fR, \f(CW\*(C`crypt\*(C'\fR,
496 \&\f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR,
497 \&\f(CW\*(C`dump\*(C'\fR, \f(CW\*(C`endgrent\*(C'\fR,
498 \&\f(CW\*(C`endhostent\*(C'\fR, \f(CW\*(C`endnetent\*(C'\fR,
499 \&\f(CW\*(C`endprotoent\*(C'\fR, \f(CW\*(C`endpwent\*(C'\fR,
500 \&\f(CW\*(C`endservent\*(C'\fR, \f(CW\*(C`exec\*(C'\fR,
501 \&\f(CW\*(C`fcntl\*(C'\fR,
502 \&\f(CW\*(C`flock\*(C'\fR, \f(CW\*(C`fork\*(C'\fR,
503 \&\f(CW\*(C`getgrent\*(C'\fR, \f(CW\*(C`getgrgid\*(C'\fR,
504 \&\f(CW\*(C`gethostbyname\*(C'\fR, \f(CW\*(C`gethostent\*(C'\fR,
505 \&\f(CW\*(C`getlogin\*(C'\fR,
506 \&\f(CW\*(C`getnetbyaddr\*(C'\fR,
507 \&\f(CW\*(C`getnetbyname\*(C'\fR, \f(CW\*(C`getnetent\*(C'\fR,
508 \&\f(CW\*(C`getppid\*(C'\fR, \f(CW\*(C`getpgrp\*(C'\fR,
509 \&\f(CW\*(C`getpriority\*(C'\fR,
510 \&\f(CW\*(C`getprotobynumber\*(C'\fR,
511 \&\f(CW\*(C`getprotoent\*(C'\fR, \f(CW\*(C`getpwent\*(C'\fR,
512 \&\f(CW\*(C`getpwnam\*(C'\fR, \f(CW\*(C`getpwuid\*(C'\fR,
513 \&\f(CW\*(C`getservbyport\*(C'\fR,
514 \&\f(CW\*(C`getservent\*(C'\fR,
515 \&\f(CW\*(C`getsockopt\*(C'\fR,
516 \&\f(CW\*(C`glob\*(C'\fR, \f(CW\*(C`ioctl\*(C'\fR,
517 \&\f(CW\*(C`kill\*(C'\fR, \f(CW\*(C`link\*(C'\fR,
518 \&\f(CW\*(C`lstat\*(C'\fR, \f(CW\*(C`msgctl\*(C'\fR,
519 \&\f(CW\*(C`msgget\*(C'\fR,
520 \&\f(CW\*(C`msgrcv\*(C'\fR,
521 \&\f(CW\*(C`msgsnd\*(C'\fR, \f(CW\*(C`open\*(C'\fR,
522 \&\f(CW\*(C`pipe\*(C'\fR, \f(CW\*(C`readlink\*(C'\fR,
523 \&\f(CW\*(C`rename\*(C'\fR,
524 \&\f(CW\*(C`select\*(C'\fR,
525 \&\f(CW\*(C`semctl\*(C'\fR,
526 \&\f(CW\*(C`semget\*(C'\fR, \f(CW\*(C`semop\*(C'\fR,
527 \&\f(CW\*(C`setgrent\*(C'\fR, \f(CW\*(C`sethostent\*(C'\fR,
528 \&\f(CW\*(C`setnetent\*(C'\fR, \f(CW\*(C`setpgrp\*(C'\fR,
529 \&\f(CW\*(C`setpriority\*(C'\fR,
530 \&\f(CW\*(C`setprotoent\*(C'\fR, \f(CW\*(C`setpwent\*(C'\fR,
531 \&\f(CW\*(C`setservent\*(C'\fR,
532 \&\f(CW\*(C`setsockopt\*(C'\fR,
533 \&\f(CW\*(C`shmctl\*(C'\fR, \f(CW\*(C`shmget\*(C'\fR,
534 \&\f(CW\*(C`shmread\*(C'\fR,
535 \&\f(CW\*(C`shmwrite\*(C'\fR,
536 \&\f(CW\*(C`socket\*(C'\fR,
537 \&\f(CW\*(C`socketpair\*(C'\fR,
538 \&\f(CW\*(C`stat\*(C'\fR, \f(CW\*(C`symlink\*(C'\fR,
539 \&\f(CW\*(C`syscall\*(C'\fR,
540 \&\f(CW\*(C`sysopen\*(C'\fR,
541 \&\f(CW\*(C`system\*(C'\fR, \f(CW\*(C`times\*(C'\fR,
542 \&\f(CW\*(C`truncate\*(C'\fR, \f(CW\*(C`umask\*(C'\fR,
543 \&\f(CW\*(C`unlink\*(C'\fR, \f(CW\*(C`utime\*(C'\fR, \f(CW\*(C`wait\*(C'\fR,
544 \&\f(CW\*(C`waitpid\*(C'\fR
545 .PP
546 For more information about the portability of these functions, see
547 perlport and other available platform-specific documentation.
548 .SS "Alphabetical Listing of Perl Functions"
549 .IX Subsection "Alphabetical Listing of Perl Functions"
550 .IP "\-X FILEHANDLE" 4
551 .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"
552 .IX Item "-X FILEHANDLE"
553 .PD 0
554 .IP "\-X EXPR" 4
555 .IX Item "-X EXPR"
556 .IP "\-X DIRHANDLE" 4
557 .IX Item "-X DIRHANDLE"
558 .IP \-X 4
559 .IX Item "-X"
560 .PD
561 A file test, where X is one of the letters listed below. This unary
562 operator takes one argument, either a filename, a filehandle, or a dirhandle,
563 and tests the associated file to see if something is true about it. If the
564 argument is omitted, tests \f(CW$_\fR, except for \f(CW\*(C`\-t\*(C'\fR, which
565 tests STDIN. Unless otherwise documented, it returns \f(CW1\fR for true and
566 \&\f(CW\*(Aq\*(Aq\fR for false. If the file doesn't exist or can't be examined, it
567 returns \f(CW\*(C`undef\*(C'\fR and sets \f(CW$!\fR (errno).
568 With the exception of the \f(CW\*(C`\-l\*(C'\fR test they all follow symbolic links
569 because they use \f(CWstat()\fR and not \f(CWlstat()\fR (so dangling symlinks can't
570 be examined and will therefore report failure).
571 .Sp
572 Despite the funny names, precedence is the same as any other named unary
573 operator. The operator may be any of:
574 .Sp
575 .Vb 4
576 \& \-r File is readable by effective uid/gid.
577 \& \-w File is writable by effective uid/gid.
578 \& \-x File is executable by effective uid/gid.
579 \& \-o File is owned by effective uid.
580 \&
581 \& \-R File is readable by real uid/gid.
582 \& \-W File is writable by real uid/gid.
583 \& \-X File is executable by real uid/gid.
584 \& \-O File is owned by real uid.
585 \&
586 \& \-e File exists.
587 \& \-z File has zero size (is empty).
588 \& \-s File has nonzero size (returns size in bytes).
589 \&
590 \& \-f File is a plain file.
591 \& \-d File is a directory.
592 \& \-l File is a symbolic link (false if symlinks aren\*(Aqt
593 \& supported by the file system).
594 \& \-p File is a named pipe (FIFO), or Filehandle is a pipe.
595 \& \-S File is a socket.
596 \& \-b File is a block special file.
597 \& \-c File is a character special file.
598 \& \-t Filehandle is opened to a tty.
599 \&
600 \& \-u File has setuid bit set.
601 \& \-g File has setgid bit set.
602 \& \-k File has sticky bit set.
603 \&
604 \& \-T File is an ASCII or UTF\-8 text file (heuristic guess).
605 \& \-B File is a "binary" file (opposite of \-T).
606 \&
607 \& \-M Script start time minus file modification time, in days.
608 \& \-A Same for access time.
609 \& \-C Same for inode change time (Unix, may differ for other
610 \& platforms)
611 .Ve
612 .Sp
613 Example:
614 .Sp
615 .Vb 5
616 \& while (<>) {
617 \& chomp;
618 \& next unless \-f $_; # ignore specials
619 \& #...
620 \& }
621 .Ve
622 .Sp
623 Note that \f(CW\*(C`\-s/a/b/\*(C'\fR does not do a negated substitution. Saying
624 \&\f(CW\*(C`\-exp($foo)\*(C'\fR still works as expected, however: only single letters
625 following a minus are interpreted as file tests.
626 .Sp
627 These operators are exempt from the "looks like a function rule" described
628 above. That is, an opening parenthesis after the operator does not affect
629 how much of the following code constitutes the argument. Put the opening
630 parentheses before the operator to separate it from code that follows (this
631 applies only to operators with higher precedence than unary operators, of
632 course):
633 .Sp
634 .Vb 2
635 \& \-s($file) + 1024 # probably wrong; same as \-s($file + 1024)
636 \& (\-s $file) + 1024 # correct
637 .Ve
638 .Sp
639 The interpretation of the file permission operators \f(CW\*(C`\-r\*(C'\fR, \f(CW\*(C`\-R\*(C'\fR,
640 \&\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
641 of the file and the uids and gids of the user. There may be other
642 reasons you can't actually read, write, or execute the file: for
643 example network filesystem access controls, ACLs (access control lists),
644 read-only filesystems, and unrecognized executable formats. Note
645 that the use of these six specific operators to verify if some operation
646 is possible is usually a mistake, because it may be open to race
647 conditions.
648 .Sp
649 Also note that, for the superuser on the local filesystems, the \f(CW\*(C`\-r\*(C'\fR,
650 \&\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
651 if any execute bit is set in the mode. Scripts run by the superuser
652 may thus need to do a \f(CW\*(C`stat\*(C'\fR to determine the
653 actual mode of the file, or temporarily set their effective uid to
654 something else.
655 .Sp
656 If you are using ACLs, there is a pragma called \f(CW\*(C`filetest\*(C'\fR
657 that may produce more accurate results than the bare
658 \&\f(CW\*(C`stat\*(C'\fR mode bits.
659 When under \f(CW\*(C`use filetest \*(Aqaccess\*(Aq\*(C'\fR, the above-mentioned filetests
660 test whether the permission can(not) be granted using the \fBaccess\fR\|(2)
661 family of system calls. Also note that the \f(CW\*(C`\-x\*(C'\fR and \f(CW\*(C`\-X\*(C'\fR tests may
662 under this pragma return true even if there are no execute permission
663 bits set (nor any extra execute permission ACLs). This strangeness is
664 due to the underlying system calls' definitions. Note also that, due to
665 the implementation of \f(CW\*(C`use filetest \*(Aqaccess\*(Aq\*(C'\fR, the \f(CW\*(C`_\*(C'\fR special
666 filehandle won't cache the results of the file tests when this pragma is
667 in effect. Read the documentation for the \f(CW\*(C`filetest\*(C'\fR
668 pragma for more information.
669 .Sp
670 The \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR tests work as follows. The first block or so of
671 the file is examined to see if it is valid UTF\-8 that includes non-ASCII
672 characters. If so, it's a \f(CW\*(C`\-T\*(C'\fR file. Otherwise, that same portion of
673 the file is examined for odd characters such as strange control codes or
674 characters with the high bit set. If more than a third of the
675 characters are strange, it's a \f(CW\*(C`\-B\*(C'\fR file; otherwise it's a \f(CW\*(C`\-T\*(C'\fR file.
676 Also, any file containing a zero byte in the examined portion is
677 considered a binary file. (If executed within the scope of a use\ locale which includes \f(CW\*(C`LC_CTYPE\*(C'\fR, odd characters are
678 anything that isn't a printable nor space in the current locale.) If
679 \&\f(CW\*(C`\-T\*(C'\fR or \f(CW\*(C`\-B\*(C'\fR is used on a filehandle, the current IO buffer is
680 examined
681 rather than the first block. Both \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR return true on an empty
682 file, or a file at EOF when testing a filehandle. Because you have to
683 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
684 against the file first, as in \f(CW\*(C`next unless \-f $file && \-T $file\*(C'\fR.
685 .Sp
686 If any of the file tests (or either the \f(CW\*(C`stat\*(C'\fR or
687 \&\f(CW\*(C`lstat\*(C'\fR operator) is given the special filehandle
688 consisting of a solitary underline, then the stat structure of the
689 previous file test (or \f(CW\*(C`stat\*(C'\fR operator) is used,
690 saving a system call. (This doesn't work with \f(CW\*(C`\-t\*(C'\fR, and you need to
691 remember that \f(CW\*(C`lstat\*(C'\fR and \f(CW\*(C`\-l\*(C'\fR leave values in
692 the stat structure for the symbolic link, not the real file.) (Also, if
693 the stat buffer was filled by an \f(CW\*(C`lstat\*(C'\fR call,
694 \&\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).
695 Example:
696 .Sp
697 .Vb 1
698 \& print "Can do.\en" if \-r $x || \-w _ || \-x _;
699 \&
700 \& stat($filename);
701 \& print "Readable\en" if \-r _;
702 \& print "Writable\en" if \-w _;
703 \& print "Executable\en" if \-x _;
704 \& print "Setuid\en" if \-u _;
705 \& print "Setgid\en" if \-g _;
706 \& print "Sticky\en" if \-k _;
707 \& print "Text\en" if \-T _;
708 \& print "Binary\en" if \-B _;
709 .Ve
710 .Sp
711 As of Perl 5.10.0, as a form of purely syntactic sugar, you can stack file
712 test operators, in a way that \f(CW\*(C`\-f \-w \-x $file\*(C'\fR is equivalent to
713 \&\f(CW\*(C`\-x $file && \-w _ && \-f _\*(C'\fR. (This is only fancy syntax: if you use
714 the return value of \f(CW\*(C`\-f $file\*(C'\fR as an argument to another filetest
715 operator, no special magic will happen.)
716 .Sp
717 Portability issues: "\-X" in perlport.
718 .Sp
719 To avoid confusing would-be users of your code with mysterious
720 syntax errors, put something like this at the top of your script:
721 .Sp
722 .Vb 1
723 \& use v5.10; # so filetest ops can stack
724 .Ve
725 .IP "abs VALUE" 4
726 .IX Xref "abs absolute"
727 .IX Item "abs VALUE"
728 .PD 0
729 .IP abs 4
730 .IX Item "abs"
731 .PD
732 Returns the absolute value of its argument.
733 If VALUE is omitted, uses \f(CW$_\fR.
734 .IP "accept NEWSOCKET,GENERICSOCKET" 4
735 .IX Xref "accept"
736 .IX Item "accept NEWSOCKET,GENERICSOCKET"
737 Accepts an incoming socket connect, just as \fBaccept\fR\|(2)
738 does. Returns the packed address if it succeeded, false otherwise.
739 See the example in "Sockets: Client/Server Communication" in perlipc.
740 .Sp
741 On systems that support a close-on-exec flag on files, the flag will
742 be set for the newly opened file descriptor, as determined by the
743 value of \f(CW$^F\fR. See "$^F" in perlvar.
744 .IP "alarm SECONDS" 4
745 .IX Xref "alarm SIGALRM timer"
746 .IX Item "alarm SECONDS"
747 .PD 0
748 .IP alarm 4
749 .IX Item "alarm"
750 .PD
751 Arranges to have a SIGALRM delivered to this process after the
752 specified number of wallclock seconds has elapsed. If SECONDS is not
753 specified, the value stored in \f(CW$_\fR is used. (On some
754 machines, unfortunately, the elapsed time may be up to one second less
755 or more than you specified because of how seconds are counted, and
756 process scheduling may delay the delivery of the signal even further.)
757 .Sp
758 Only one timer may be counting at once. Each call disables the
759 previous timer, and an argument of \f(CW0\fR may be supplied to cancel the
760 previous timer without starting a new one. The returned value is the
761 amount of time remaining on the previous timer.
762 .Sp
763 For delays of finer granularity than one second, the Time::HiRes module
764 (from CPAN, and starting from Perl 5.8 part of the standard
765 distribution) provides
766 \&\f(CW\*(C`ualarm\*(C'\fR.
767 You may also use Perl's four-argument version of
768 \&\f(CW\*(C`select\*(C'\fR leaving the first three
769 arguments undefined, or you might be able to use the
770 \&\f(CW\*(C`syscall\*(C'\fR interface to access \fBsetitimer\fR\|(2)
771 if your system supports it. See perlfaq8 for details.
772 .Sp
773 It is usually a mistake to intermix \f(CW\*(C`alarm\*(C'\fR and
774 \&\f(CW\*(C`sleep\*(C'\fR calls, because \f(CW\*(C`sleep\*(C'\fR may be
775 internally implemented on your system with \f(CW\*(C`alarm\*(C'\fR.
776 .Sp
777 If you want to use \f(CW\*(C`alarm\*(C'\fR to time out a system call
778 you need to use an \f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR pair. You
779 can't rely on the alarm causing the system call to fail with
780 \&\f(CW$!\fR set to \f(CW\*(C`EINTR\*(C'\fR because Perl sets up signal handlers
781 to restart system calls on some systems. Using
782 \&\f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR always works, modulo the
783 caveats given in "Signals" in perlipc.
784 .Sp
785 .Vb 10
786 \& eval {
787 \& local $SIG{ALRM} = sub { die "alarm\en" }; # NB: \en required
788 \& alarm $timeout;
789 \& my $nread = sysread $socket, $buffer, $size;
790 \& alarm 0;
791 \& };
792 \& if ($@) {
793 \& die unless $@ eq "alarm\en"; # propagate unexpected errors
794 \& # timed out
795 \& }
796 \& else {
797 \& # didn\*(Aqt
798 \& }
799 .Ve
800 .Sp
801 For more information see perlipc.
802 .Sp
803 Portability issues: "alarm" in perlport.
804 .IP "atan2 Y,X" 4
805 .IX Xref "atan2 arctangent tan tangent"
806 .IX Item "atan2 Y,X"
807 Returns the arctangent of Y/X in the range \-PI to PI.
808 .Sp
809 For the tangent operation, you may use the
810 \&\f(CW\*(C`Math::Trig::tan\*(C'\fR function, or use the familiar
811 relation:
812 .Sp
813 .Vb 1
814 \& sub tan { sin($_[0]) / cos($_[0]) }
815 .Ve
816 .Sp
817 The return value for \f(CW\*(C`atan2(0,0)\*(C'\fR is implementation-defined; consult
818 your \fBatan2\fR\|(3) manpage for more information.
819 .Sp
820 Portability issues: "atan2" in perlport.
821 .IP "bind SOCKET,NAME" 4
822 .IX Xref "bind"
823 .IX Item "bind SOCKET,NAME"
824 Binds a network address to a socket, just as \fBbind\fR\|(2)
825 does. Returns true if it succeeded, false otherwise. NAME should be a
826 packed address of the appropriate type for the socket. See the examples in
827 "Sockets: Client/Server Communication" in perlipc.
828 .IP "binmode FILEHANDLE, LAYER" 4
829 .IX Xref "binmode binary text DOS Windows"
830 .IX Item "binmode FILEHANDLE, LAYER"
831 .PD 0
832 .IP "binmode FILEHANDLE" 4
833 .IX Item "binmode FILEHANDLE"
834 .PD
835 Arranges for FILEHANDLE to be read or written in "binary" or "text"
836 mode on systems where the run-time libraries distinguish between
837 binary and text files. If FILEHANDLE is an expression, the value is
838 taken as the name of the filehandle. Returns true on success,
839 otherwise it returns \f(CW\*(C`undef\*(C'\fR and sets
840 \&\f(CW$!\fR (errno).
841 .Sp
842 On some systems (in general, DOS\- and Windows-based systems)
843 \&\f(CW\*(C`binmode\*(C'\fR is necessary when you're not
844 working with a text file. For the sake of portability it is a good idea
845 always to use it when appropriate, and never to use it when it isn't
846 appropriate. Also, people can set their I/O to be by default
847 UTF8\-encoded Unicode, not bytes.
848 .Sp
849 In other words: regardless of platform, use
850 \&\f(CW\*(C`binmode\*(C'\fR on binary data, like images,
851 for example.
852 .Sp
853 If LAYER is present it is a single string, but may contain multiple
854 directives. The directives alter the behaviour of the filehandle.
855 When LAYER is present, using binmode on a text file makes sense.
856 .Sp
857 If LAYER is omitted or specified as \f(CW\*(C`:raw\*(C'\fR the filehandle is made
858 suitable for passing binary data. This includes turning off possible CRLF
859 translation and marking it as bytes (as opposed to Unicode characters).
860 Note that, despite what may be implied in \fI"Programming Perl"\fR (the
861 Camel, 3rd edition) or elsewhere, \f(CW\*(C`:raw\*(C'\fR is \fInot\fR simply the inverse of \f(CW\*(C`:crlf\*(C'\fR.
862 Other layers that would affect the binary nature of the stream are
863 \&\fIalso\fR disabled. See PerlIO, and the discussion about the PERLIO
864 environment variable in perlrun.
865 .Sp
866 The \f(CW\*(C`:bytes\*(C'\fR, \f(CW\*(C`:crlf\*(C'\fR, \f(CW\*(C`:utf8\*(C'\fR, and any other directives of the
867 form \f(CW\*(C`:...\*(C'\fR, are called I/O \fIlayers\fR. The open pragma can be used to
868 establish default I/O layers.
869 .Sp
870 \&\fIThe LAYER parameter of the \fR\f(CI\*(C`binmode\*(C'\fR\fI
871 function is described as "DISCIPLINE" in "Programming Perl, 3rd
872 Edition". However, since the publishing of this book, by many known as
873 "Camel III", the consensus of the naming of this functionality has moved
874 from "discipline" to "layer". All documentation of this version of Perl
875 therefore refers to "layers" rather than to "disciplines". Now back to
876 the regularly scheduled documentation...\fR
877 .Sp
878 To mark FILEHANDLE as UTF\-8, use \f(CW\*(C`:utf8\*(C'\fR or \f(CW:encoding(UTF\-8)\fR.
879 \&\f(CW\*(C`:utf8\*(C'\fR just marks the data as UTF\-8 without further checking,
880 while \f(CW:encoding(UTF\-8)\fR checks the data for actually being valid
881 UTF\-8. More details can be found in PerlIO::encoding.
882 .Sp
883 In general, \f(CW\*(C`binmode\*(C'\fR should be called
884 after \f(CW\*(C`open\*(C'\fR but before any I/O is done on the
885 filehandle. Calling \f(CW\*(C`binmode\*(C'\fR normally
886 flushes any pending buffered output data (and perhaps pending input
887 data) on the handle. An exception to this is the \f(CW\*(C`:encoding\*(C'\fR layer
888 that changes the default character encoding of the handle.
889 The \f(CW\*(C`:encoding\*(C'\fR layer sometimes needs to be called in
890 mid-stream, and it doesn't flush the stream. \f(CW\*(C`:encoding\*(C'\fR
891 also implicitly pushes on top of itself the \f(CW\*(C`:utf8\*(C'\fR layer because
892 internally Perl operates on UTF8\-encoded Unicode characters.
893 .Sp
894 The operating system, device drivers, C libraries, and Perl run-time
895 system all conspire to let the programmer treat a single
896 character (\f(CW\*(C`\en\*(C'\fR) as the line terminator, irrespective of external
897 representation. On many operating systems, the native text file
898 representation matches the internal representation, but on some
899 platforms the external representation of \f(CW\*(C`\en\*(C'\fR is made up of more than
900 one character.
901 .Sp
902 All variants of Unix, Mac OS (old and new), and Stream_LF files on VMS use
903 a single character to end each line in the external representation of text
904 (even though that single character is CARRIAGE RETURN on old, pre-Darwin
905 flavors of Mac OS, and is LINE FEED on Unix and most VMS files). In other
906 systems like OS/2, DOS, and the various flavors of MS-Windows, your program
907 sees a \f(CW\*(C`\en\*(C'\fR as a simple \f(CW\*(C`\ecJ\*(C'\fR, but what's stored in text files are the
908 two characters \f(CW\*(C`\ecM\ecJ\*(C'\fR. That means that if you don't use
909 \&\f(CW\*(C`binmode\*(C'\fR on these systems, \f(CW\*(C`\ecM\ecJ\*(C'\fR
910 sequences on disk will be converted to \f(CW\*(C`\en\*(C'\fR on input, and any \f(CW\*(C`\en\*(C'\fR in
911 your program will be converted back to \f(CW\*(C`\ecM\ecJ\*(C'\fR on output. This is
912 what you want for text files, but it can be disastrous for binary files.
913 .Sp
914 Another consequence of using \f(CW\*(C`binmode\*(C'\fR
915 (on some systems) is that special end-of-file markers will be seen as
916 part of the data stream. For systems from the Microsoft family this
917 means that, if your binary data contain \f(CW\*(C`\ecZ\*(C'\fR, the I/O subsystem will
918 regard it as the end of the file, unless you use
919 \&\f(CW\*(C`binmode\*(C'\fR.
920 .Sp
921 \&\f(CW\*(C`binmode\*(C'\fR is important not only for
922 \&\f(CW\*(C`readline\*(C'\fR and \f(CW\*(C`print\*(C'\fR
923 operations, but also when using
924 \&\f(CW\*(C`read\*(C'\fR,
925 \&\f(CW\*(C`seek\*(C'\fR,
926 \&\f(CW\*(C`sysread\*(C'\fR,
927 \&\f(CW\*(C`syswrite\*(C'\fR and
928 \&\f(CW\*(C`tell\*(C'\fR (see perlport for more details). See the
929 \&\f(CW$/\fR and \f(CW\*(C`$\e\*(C'\fR variables in
930 perlvar for how to manually set your input and output
931 line-termination sequences.
932 .Sp
933 Portability issues: "binmode" in perlport.
934 .IP "bless REF,CLASSNAME" 4
935 .IX Xref "bless"
936 .IX Item "bless REF,CLASSNAME"
937 .PD 0
938 .IP "bless REF" 4
939 .IX Item "bless REF"
940 .PD
941 \&\f(CW\*(C`bless\*(C'\fR tells Perl to mark the item referred to by \f(CW\*(C`REF\*(C'\fR as an
942 object in a package. The two-argument version of \f(CW\*(C`bless\*(C'\fR is
943 always preferable unless there is a specific reason to \fInot\fR
944 use it.
945 .RS 4
946 .IP \(bu 4
947 Bless the referred-to item into a specific package
948 (recommended form):
949 .Sp
950 .Vb 1
951 \& bless $ref, $package;
952 .Ve
953 .Sp
954 The two-argument form adds the object to the package specified
955 as the second argument.
956 .IP \(bu 4
957 Bless the referred-to item into package \f(CW\*(C`main\*(C'\fR:
958 .Sp
959 .Vb 1
960 \& bless $ref, "";
961 .Ve
962 .Sp
963 If the second argument is an empty string, \f(CW\*(C`bless\*(C'\fR adds the
964 object to package \f(CW\*(C`main\*(C'\fR.
965 .IP \(bu 4
966 Bless the referred-to item into the current package (not
967 inheritable):
968 .Sp
969 .Vb 1
970 \& bless $ref;
971 .Ve
972 .Sp
973 If \f(CW\*(C`bless\*(C'\fR is used without its second argument, the object is
974 created in the current package. The second argument should
975 always be supplied if a derived class might inherit a method
976 executing \f(CW\*(C`bless\*(C'\fR. Because it is a potential source of bugs,
977 one-argument \f(CW\*(C`bless\*(C'\fR is discouraged.
978 .RE
979 .RS 4
980 .Sp
981 See perlobj for more about the blessing (and blessings) of
982 objects.
983 .Sp
984 \&\f(CW\*(C`bless\*(C'\fR returns its first argument, the
985 supplied reference, as the value of the function; since \f(CW\*(C`bless\*(C'\fR
986 is commonly the last thing executed in constructors, this means
987 that the reference to the object is returned as the
988 constructor's value and allows the caller to immediately use
989 this returned object in method calls.
990 .Sp
991 \&\f(CW\*(C`CLASSNAME\*(C'\fR should always be a mixed-case name, as
992 all-uppercase and all-lowercase names are meant to be used only
993 for Perl builtin types and pragmas, respectively. Avoid creating
994 all-uppercase or all-lowercase package names to prevent
995 confusion.
996 .Sp
997 Also avoid \f(CW\*(C`bless\*(C'\fRing things into the class name \f(CW0\fR; this
998 will cause code which (erroneously) checks the result of
999 \&\f(CW\*(C`ref\*(C'\fR to see if a reference is \f(CW\*(C`bless\*(C'\fRed to fail,
1000 as "0", a falsy value, is returned.
1001 .Sp
1002 See "Perl Modules" in perlmod for more details.
1003 .RE
1004 .IP break 4
1005 .IX Item "break"
1006 Break out of a \f(CW\*(C`given\*(C'\fR block.
1007 .Sp
1008 \&\f(CW\*(C`break\*(C'\fR is available only if the
1009 \&\f(CW"switch"\fR feature is enabled or if it
1010 is prefixed with \f(CW\*(C`CORE::\*(C'\fR. The
1011 \&\f(CW"switch"\fR feature is enabled
1012 automatically with a \f(CW\*(C`use v5.10\*(C'\fR (or higher) declaration in the current
1013 scope.
1014 .IP "caller EXPR" 4
1015 .IX Xref "caller call stack stack stack trace"
1016 .IX Item "caller EXPR"
1017 .PD 0
1018 .IP caller 4
1019 .IX Item "caller"
1020 .PD
1021 Returns the context of the current pure perl subroutine call. In scalar
1022 context, returns the caller's package name if there \fIis\fR a caller (that is, if
1023 we're in a subroutine or \f(CW\*(C`eval\*(C'\fR or
1024 \&\f(CW\*(C`require\*(C'\fR) and the undefined value otherwise.
1025 \&\f(CW\*(C`caller\*(C'\fR never returns XS subs and they are skipped. The next pure perl
1026 sub will appear instead of the XS sub in caller's return values. In
1027 list context, caller returns
1028 .Sp
1029 .Vb 2
1030 \& # 0 1 2
1031 \& my ($package, $filename, $line) = caller;
1032 .Ve
1033 .Sp
1034 Like \f(CW\*(C`_\|_FILE_\|_\*(C'\fR and \f(CW\*(C`_\|_LINE_\|_\*(C'\fR, the filename and
1035 line number returned here may be altered by the mechanism described at
1036 "Plain Old Comments (Not!)" in perlsyn.
1037 .Sp
1038 With EXPR, it returns some extra information that the debugger uses to
1039 print a stack trace. The value of EXPR indicates how many call frames
1040 to go back before the current one.
1041 .Sp
1042 .Vb 2
1043 \& # 0 1 2 3 4
1044 \& my ($package, $filename, $line, $subroutine, $hasargs,
1045 \&
1046 \& # 5 6 7 8 9 10
1047 \& $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
1048 \& = caller($i);
1049 .Ve
1050 .Sp
1051 Here, \f(CW$subroutine\fR is the function that the caller called (rather than the
1052 function containing the caller). Note that \f(CW$subroutine\fR may be \f(CW\*(C`(eval)\*(C'\fR if
1053 the frame is not a subroutine call, but an \f(CW\*(C`eval\*(C'\fR. In
1054 such a case additional elements \f(CW$evaltext\fR and \f(CW$is_require\fR are set:
1055 \&\f(CW$is_require\fR is true if the frame is created by a
1056 \&\f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR
1057 statement, \f(CW$evaltext\fR contains the text of the \f(CW\*(C`eval EXPR\*(C'\fR statement.
1058 In particular, for an \f(CW\*(C`eval BLOCK\*(C'\fR statement, \f(CW$subroutine\fR is \f(CW\*(C`(eval)\*(C'\fR,
1059 but \f(CW$evaltext\fR is undefined. (Note also that each
1060 \&\f(CW\*(C`use\*(C'\fR statement creates a
1061 \&\f(CW\*(C`require\*(C'\fR frame inside an \f(CW\*(C`eval EXPR\*(C'\fR frame.)
1062 \&\f(CW$subroutine\fR may also be \f(CW\*(C`(unknown)\*(C'\fR if this particular subroutine
1063 happens to have been deleted from the symbol table. \f(CW$hasargs\fR is true
1064 if a new instance of \f(CW@_\fR was set up for the frame.
1065 \&\f(CW$hints\fR and \f(CW$bitmask\fR contain pragmatic hints that the caller was
1066 compiled with. \f(CW$hints\fR corresponds to \f(CW$^H\fR, and
1067 \&\f(CW$bitmask\fR corresponds to
1068 \&\f(CW\*(C`${^WARNING_BITS}\*(C'\fR. The \f(CW$hints\fR and
1069 \&\f(CW$bitmask\fR values are subject to change between versions of Perl, and
1070 are not meant for external use.
1071 .Sp
1072 \&\f(CW$hinthash\fR is a reference to a hash containing the value of
1073 \&\f(CW\*(C`%^H\*(C'\fR when the caller was compiled, or
1074 \&\f(CW\*(C`undef\*(C'\fR if \f(CW\*(C`%^H\*(C'\fR was empty. Do not
1075 modify the values of this hash, as they are the actual values stored in
1076 the optree.
1077 .Sp
1078 Note that the only types of call frames that are visible are subroutine
1079 calls and \f(CW\*(C`eval\*(C'\fR. Other forms of context, such as \f(CW\*(C`while\*(C'\fR or \f(CW\*(C`foreach\*(C'\fR
1080 loops or \f(CW\*(C`try\*(C'\fR blocks are not considered interesting to \f(CW\*(C`caller\*(C'\fR, as they
1081 do not alter the behaviour of the \f(CW\*(C`return\*(C'\fR expression.
1082 .Sp
1083 Furthermore, when called from within the DB package in
1084 list context, and with an argument, caller returns more
1085 detailed information: it sets the list variable \f(CW@DB::args\fR to be the
1086 arguments with which the subroutine was invoked.
1087 .Sp
1088 Be aware that the optimizer might have optimized call frames away before
1089 \&\f(CW\*(C`caller\*(C'\fR had a chance to get the information. That
1090 means that \f(CWcaller(N)\fR might not return information about the call
1091 frame you expect it to, for \f(CW\*(C`N > 1\*(C'\fR. In particular, \f(CW@DB::args\fR
1092 might have information from the previous time \f(CW\*(C`caller\*(C'\fR
1093 was called.
1094 .Sp
1095 Be aware that setting \f(CW@DB::args\fR is \fIbest effort\fR, intended for
1096 debugging or generating backtraces, and should not be relied upon. In
1097 particular, as \f(CW@_\fR contains aliases to the caller's
1098 arguments, Perl does not take a copy of \f(CW@_\fR, so
1099 \&\f(CW@DB::args\fR will contain modifications the subroutine makes to
1100 \&\f(CW@_\fR or its contents, not the original values at call
1101 time. \f(CW@DB::args\fR, like \f(CW@_\fR, does not hold explicit
1102 references to its elements, so under certain cases its elements may have
1103 become freed and reallocated for other variables or temporary values.
1104 Finally, a side effect of the current implementation is that the effects
1105 of \f(CW\*(C`shift @_\*(C'\fR can \fInormally\fR be undone (but not \f(CW\*(C`pop @_\*(C'\fR or other
1106 splicing, \fIand\fR not if a reference to \f(CW@_\fR has been
1107 taken, \fIand\fR subject to the caveat about reallocated elements), so
1108 \&\f(CW@DB::args\fR is actually a hybrid of the current state and initial state
1109 of \f(CW@_\fR. Buyer beware.
1110 .IP "chdir EXPR" 4
1111 .IX Xref "chdir cd directory, change"
1112 .IX Item "chdir EXPR"
1113 .PD 0
1114 .IP "chdir FILEHANDLE" 4
1115 .IX Item "chdir FILEHANDLE"
1116 .IP "chdir DIRHANDLE" 4
1117 .IX Item "chdir DIRHANDLE"
1118 .IP chdir 4
1119 .IX Item "chdir"
1120 .PD
1121 Changes the working directory to EXPR, if possible. If EXPR is omitted,
1122 changes to the directory specified by \f(CW$ENV{HOME}\fR, if set; if not,
1123 changes to the directory specified by \f(CW$ENV{LOGDIR}\fR. (Under VMS, the
1124 variable \f(CW$ENV{\*(AqSYS$LOGIN\*(Aq}\fR is also checked, and used if it is set.) If
1125 neither is set, \f(CW\*(C`chdir\*(C'\fR does nothing and fails. It
1126 returns true on success, false otherwise. See the example under
1127 \&\f(CW\*(C`die\*(C'\fR.
1128 .Sp
1129 On systems that support \fBfchdir\fR\|(2), you may pass a filehandle or
1130 directory handle as the argument. On systems that don't support \fBfchdir\fR\|(2),
1131 passing handles raises an exception.
1132 .IP "chmod LIST" 4
1133 .IX Xref "chmod permission mode"
1134 .IX Item "chmod LIST"
1135 Changes the permissions of a list of files. The first element of the
1136 list must be the numeric mode, which should probably be an octal
1137 number, and which definitely should \fInot\fR be a string of octal digits:
1138 \&\f(CW0644\fR is okay, but \f(CW"0644"\fR is not. Returns the number of files
1139 successfully changed. See also \f(CW\*(C`oct\*(C'\fR if all you have is a
1140 string.
1141 .Sp
1142 .Vb 6
1143 \& my $cnt = chmod 0755, "foo", "bar";
1144 \& chmod 0755, @executables;
1145 \& my $mode = "0644"; chmod $mode, "foo"; # !!! sets mode to
1146 \& # \-\-w\-\-\-\-r\-T
1147 \& my $mode = "0644"; chmod oct($mode), "foo"; # this is better
1148 \& my $mode = 0644; chmod $mode, "foo"; # this is best
1149 .Ve
1150 .Sp
1151 On systems that support \fBfchmod\fR\|(2), you may pass filehandles among the
1152 files. On systems that don't support \fBfchmod\fR\|(2), passing filehandles raises
1153 an exception. Filehandles must be passed as globs or glob references to be
1154 recognized; barewords are considered filenames.
1155 .Sp
1156 .Vb 3
1157 \& open(my $fh, "<", "foo");
1158 \& my $perm = (stat $fh)[2] & 07777;
1159 \& chmod($perm | 0600, $fh);
1160 .Ve
1161 .Sp
1162 You can also import the symbolic \f(CW\*(C`S_I*\*(C'\fR constants from the
1163 \&\f(CW\*(C`Fcntl\*(C'\fR module:
1164 .Sp
1165 .Vb 3
1166 \& use Fcntl qw( :mode );
1167 \& chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables;
1168 \& # Identical to the chmod 0755 of the example above.
1169 .Ve
1170 .Sp
1171 Portability issues: "chmod" in perlport.
1172 .IP "chomp VARIABLE" 4
1173 .IX Xref "chomp INPUT_RECORD_SEPARATOR $ newline eol"
1174 .IX Item "chomp VARIABLE"
1175 .PD 0
1176 .IP "chomp( LIST )" 4
1177 .IX Item "chomp( LIST )"
1178 .IP chomp 4
1179 .IX Item "chomp"
1180 .PD
1181 This safer version of \f(CW\*(C`chop\*(C'\fR removes any trailing
1182 string that corresponds to the current value of
1183 \&\f(CW$/\fR (also known as \f(CW$INPUT_RECORD_SEPARATOR\fR
1184 in the \f(CW\*(C`English\*(C'\fR module). It returns the total
1185 number of characters removed from all its arguments. It's often used to
1186 remove the newline from the end of an input record when you're worried
1187 that the final record may be missing its newline. When in paragraph
1188 mode (\f(CW\*(C`$/ = \*(Aq\*(Aq\*(C'\fR), it removes all trailing newlines from the string.
1189 When in slurp mode (\f(CW\*(C`$/ = undef\*(C'\fR) or fixed-length record mode
1190 (\f(CW$/\fR is a reference to an integer or the like;
1191 see perlvar), \f(CW\*(C`chomp\*(C'\fR won't remove anything.
1192 If VARIABLE is omitted, it chomps \f(CW$_\fR. Example:
1193 .Sp
1194 .Vb 5
1195 \& while (<>) {
1196 \& chomp; # avoid \en on last field
1197 \& my @array = split(/:/);
1198 \& # ...
1199 \& }
1200 .Ve
1201 .Sp
1202 If VARIABLE is a hash, it chomps the hash's values, but not its keys,
1203 resetting the \f(CW\*(C`each\*(C'\fR iterator in the process.
1204 .Sp
1205 You can actually chomp anything that's an lvalue, including an assignment:
1206 .Sp
1207 .Vb 2
1208 \& chomp(my $cwd = \`pwd\`);
1209 \& chomp(my $answer = <STDIN>);
1210 .Ve
1211 .Sp
1212 If you chomp a list, each element is chomped, and the total number of
1213 characters removed is returned.
1214 .Sp
1215 Note that parentheses are necessary when you're chomping anything
1216 that is not a simple variable. This is because \f(CW\*(C`chomp $cwd = \`pwd\`;\*(C'\fR
1217 is interpreted as \f(CW\*(C`(chomp $cwd) = \`pwd\`;\*(C'\fR, rather than as
1218 \&\f(CW\*(C`chomp( $cwd = \`pwd\` )\*(C'\fR which you might expect. Similarly,
1219 \&\f(CW\*(C`chomp $x, $y\*(C'\fR is interpreted as \f(CW\*(C`chomp($x), $y\*(C'\fR rather than
1220 as \f(CW\*(C`chomp($x, $y)\*(C'\fR.
1221 .IP "chop VARIABLE" 4
1222 .IX Xref "chop"
1223 .IX Item "chop VARIABLE"
1224 .PD 0
1225 .IP "chop( LIST )" 4
1226 .IX Item "chop( LIST )"
1227 .IP chop 4
1228 .IX Item "chop"
1229 .PD
1230 Chops off the last character of a string and returns the character
1231 chopped. It is much more efficient than \f(CW\*(C`s/.$//s\*(C'\fR because it neither
1232 scans nor copies the string. If VARIABLE is omitted, chops
1233 \&\f(CW$_\fR.
1234 If VARIABLE is a hash, it chops the hash's values, but not its keys,
1235 resetting the \f(CW\*(C`each\*(C'\fR iterator in the process.
1236 .Sp
1237 You can actually chop anything that's an lvalue, including an assignment.
1238 .Sp
1239 If you chop a list, each element is chopped. Only the value of the
1240 last \f(CW\*(C`chop\*(C'\fR is returned.
1241 .Sp
1242 Note that \f(CW\*(C`chop\*(C'\fR returns the last character. To
1243 return all but the last character, use \f(CW\*(C`substr($string, 0, \-1)\*(C'\fR.
1244 .Sp
1245 See also \f(CW\*(C`chomp\*(C'\fR.
1246 .IP "chown LIST" 4
1247 .IX Xref "chown owner user group"
1248 .IX Item "chown LIST"
1249 Changes the owner (and group) of a list of files. The first two
1250 elements of the list must be the \fInumeric\fR uid and gid, in that
1251 order. A value of \-1 in either position is interpreted by most
1252 systems to leave that value unchanged. Returns the number of files
1253 successfully changed.
1254 .Sp
1255 .Vb 2
1256 \& my $cnt = chown $uid, $gid, \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq;
1257 \& chown $uid, $gid, @filenames;
1258 .Ve
1259 .Sp
1260 On systems that support \fBfchown\fR\|(2), you may pass filehandles among the
1261 files. On systems that don't support \fBfchown\fR\|(2), passing filehandles raises
1262 an exception. Filehandles must be passed as globs or glob references to be
1263 recognized; barewords are considered filenames.
1264 .Sp
1265 Here's an example that looks up nonnumeric uids in the passwd file:
1266 .Sp
1267 .Vb 4
1268 \& print "User: ";
1269 \& chomp(my $user = <STDIN>);
1270 \& print "Files: ";
1271 \& chomp(my $pattern = <STDIN>);
1272 \&
1273 \& my ($login,$pass,$uid,$gid) = getpwnam($user)
1274 \& or die "$user not in passwd file";
1275 \&
1276 \& my @ary = glob($pattern); # expand filenames
1277 \& chown $uid, $gid, @ary;
1278 .Ve
1279 .Sp
1280 On most systems, you are not allowed to change the ownership of the
1281 file unless you're the superuser, although you should be able to change
1282 the group to any of your secondary groups. On insecure systems, these
1283 restrictions may be relaxed, but this is not a portable assumption.
1284 On POSIX systems, you can detect this condition this way:
1285 .Sp
1286 .Vb 3
1287 \& use POSIX qw(pathconf _PC_CHOWN_RESTRICTED);
1288 \& my $can_chown_giveaway =
1289 \& ! pathconf($path_of_interest, _PC_CHOWN_RESTRICTED);
1290 .Ve
1291 .Sp
1292 Portability issues: "chown" in perlport.
1293 .IP "chr NUMBER" 4
1294 .IX Xref "chr character ASCII Unicode"
1295 .IX Item "chr NUMBER"
1296 .PD 0
1297 .IP chr 4
1298 .IX Item "chr"
1299 .PD
1300 Returns the character represented by that NUMBER in the character set.
1301 For example, \f(CWchr(65)\fR is \f(CW"A"\fR in either ASCII or Unicode, and
1302 \&\f(CWchr(0x263a)\fR is a Unicode smiley face.
1303 .Sp
1304 Negative values give the Unicode replacement character (\f(CWchr(0xfffd)\fR),
1305 except under the bytes pragma, where the low eight bits of the value
1306 (truncated to an integer) are used.
1307 .Sp
1308 If NUMBER is omitted, uses \f(CW$_\fR.
1309 .Sp
1310 For the reverse, use \f(CW\*(C`ord\*(C'\fR.
1311 .Sp
1312 Note that characters from 128 to 255 (inclusive) are by default
1313 internally not encoded as UTF\-8 for backward compatibility reasons.
1314 .Sp
1315 See perlunicode for more about Unicode.
1316 .IP "chroot FILENAME" 4
1317 .IX Xref "chroot root"
1318 .IX Item "chroot FILENAME"
1319 .PD 0
1320 .IP chroot 4
1321 .IX Item "chroot"
1322 .PD
1323 This function works like the system call by the same name: it makes the
1324 named directory the new root directory for all further pathnames that
1325 begin with a \f(CW\*(C`/\*(C'\fR by your process and all its children. (It doesn't
1326 change your current working directory, which is unaffected.) For security
1327 reasons, this call is restricted to the superuser. If FILENAME is
1328 omitted, does a \f(CW\*(C`chroot\*(C'\fR to \f(CW$_\fR.
1329 .Sp
1330 \&\fBNOTE:\fR It is mandatory for security to \f(CWchdir("/")\fR
1331 (\f(CW\*(C`chdir\*(C'\fR to the root directory) immediately after a
1332 \&\f(CW\*(C`chroot\*(C'\fR, otherwise the current working directory
1333 may be outside of the new root.
1334 .Sp
1335 Portability issues: "chroot" in perlport.
1336 .IP "class NAMESPACE" 4
1337 .IX Item "class NAMESPACE"
1338 .PD 0
1339 .IP "class NAMESPACE VERSION" 4
1340 .IX Item "class NAMESPACE VERSION"
1341 .IP "class NAMESPACE BLOCK" 4
1342 .IX Item "class NAMESPACE BLOCK"
1343 .IP "class NAMESPACE VERSION BLOCK" 4
1344 .IX Item "class NAMESPACE VERSION BLOCK"
1345 .PD
1346 Declares the BLOCK or the rest of the compilation unit as being in the given
1347 namespace, which implements an object class. This behaves similarly to
1348 \&\f(CW\*(C`package\*(C'\fR, except that the newly-created package behaves
1349 as a class.
1350 .IP "close FILEHANDLE" 4
1351 .IX Xref "close"
1352 .IX Item "close FILEHANDLE"
1353 .PD 0
1354 .IP close 4
1355 .IX Item "close"
1356 .PD
1357 Closes the file or pipe associated with the filehandle, flushes the IO
1358 buffers, and closes the system file descriptor. Returns true if those
1359 operations succeed, and if no error was reported by any PerlIO layer,
1360 and there was no existing error on the filehandle.
1361 .Sp
1362 If there was an existing error on the filehandle, close will return
1363 false and \f(CW$!\fR will be set to the error from the
1364 failing operation, so you can safely use its value when reporting the
1365 error.
1366 .Sp
1367 Closes the currently selected filehandle if the argument is
1368 omitted.
1369 .Sp
1370 You don't have to close FILEHANDLE if you are immediately going to do
1371 another \f(CW\*(C`open\*(C'\fR on it, because
1372 \&\f(CW\*(C`open\*(C'\fR closes it for you. (See
1373 \&\f(CW\*(C`open\*(C'\fR.) However, an explicit
1374 \&\f(CW\*(C`close\*(C'\fR on an input file resets the line counter
1375 (\f(CW$.\fR), while the implicit close done by
1376 \&\f(CW\*(C`open\*(C'\fR does not.
1377 .Sp
1378 If the filehandle came from a piped open, \f(CW\*(C`close\*(C'\fR
1379 returns false if one of the other syscalls involved fails or if its
1380 program exits with non-zero status. If the only problem was that the
1381 program exited non-zero, \f(CW$!\fR will be set to \f(CW0\fR.
1382 Closing a pipe also waits for the process executing on the pipe to
1383 exit\-\-in case you wish to look at the output of the pipe afterwards\-\-and
1384 implicitly puts the exit status value of that command into
1385 \&\f(CW$?\fR and
1386 \&\f(CW\*(C`${^CHILD_ERROR_NATIVE}\*(C'\fR.
1387 .Sp
1388 If there are multiple threads running, \f(CW\*(C`close\*(C'\fR on
1389 a filehandle from a piped open returns true without waiting for the
1390 child process to terminate, if the filehandle is still open in another
1391 thread.
1392 .Sp
1393 Closing the read end of a pipe before the process writing to it at the
1394 other end is done writing results in the writer receiving a SIGPIPE. If
1395 the other end can't handle that, be sure to read all the data before
1396 closing the pipe.
1397 .Sp
1398 Example:
1399 .Sp
1400 .Vb 8
1401 \& open(OUTPUT, \*(Aq|sort >foo\*(Aq) # pipe to sort
1402 \& or die "Can\*(Aqt start sort: $!";
1403 \& #... # print stuff to output
1404 \& close OUTPUT # wait for sort to finish
1405 \& or warn $! ? "Error closing sort pipe: $!"
1406 \& : "Exit status $? from sort";
1407 \& open(INPUT, \*(Aqfoo\*(Aq) # get sort\*(Aqs results
1408 \& or die "Can\*(Aqt open \*(Aqfoo\*(Aq for input: $!";
1409 .Ve
1410 .Sp
1411 FILEHANDLE may be an expression whose value can be used as an indirect
1412 filehandle, usually the real filehandle name or an autovivified handle.
1413 .Sp
1414 If an error occurs when perl implicitly closes a handle, perl will
1415 produce a warning. Explicitly calling close on the handle prevents that
1416 warning.
1417 .IP "closedir DIRHANDLE" 4
1418 .IX Xref "closedir"
1419 .IX Item "closedir DIRHANDLE"
1420 Closes a directory opened by \f(CW\*(C`opendir\*(C'\fR and
1421 returns the success of that system call.
1422 .IP "connect SOCKET,NAME" 4
1423 .IX Xref "connect"
1424 .IX Item "connect SOCKET,NAME"
1425 Attempts to connect to a remote socket, just like \fBconnect\fR\|(2).
1426 Returns true if it succeeded, false otherwise. NAME should be a
1427 packed address of the appropriate type for the socket. See the examples in
1428 "Sockets: Client/Server Communication" in perlipc.
1429 .IP "continue BLOCK" 4
1430 .IX Xref "continue"
1431 .IX Item "continue BLOCK"
1432 .PD 0
1433 .IP continue 4
1434 .IX Item "continue"
1435 .PD
1436 When followed by a BLOCK, \f(CW\*(C`continue\*(C'\fR is actually a
1437 flow control statement rather than a function. If there is a
1438 \&\f(CW\*(C`continue\*(C'\fR BLOCK attached to a BLOCK (typically in a
1439 \&\f(CW\*(C`while\*(C'\fR or \f(CW\*(C`foreach\*(C'\fR), it is always executed just before the
1440 conditional is about to be evaluated again, just like the third part of
1441 a \f(CW\*(C`for\*(C'\fR loop in C. Thus it can be used to increment a loop variable,
1442 even when the loop has been continued via the \f(CW\*(C`next\*(C'\fR
1443 statement (which is similar to the C \f(CW\*(C`continue\*(C'\fR
1444 statement).
1445 .Sp
1446 \&\f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, or
1447 \&\f(CW\*(C`redo\*(C'\fR may appear within a
1448 \&\f(CW\*(C`continue\*(C'\fR block; \f(CW\*(C`last\*(C'\fR and
1449 \&\f(CW\*(C`redo\*(C'\fR behave as if they had been executed within the
1450 main block. So will \f(CW\*(C`next\*(C'\fR, but since it will execute a
1451 \&\f(CW\*(C`continue\*(C'\fR block, it may be more entertaining.
1452 .Sp
1453 .Vb 9
1454 \& while (EXPR) {
1455 \& ### redo always comes here
1456 \& do_something;
1457 \& } continue {
1458 \& ### next always comes here
1459 \& do_something_else;
1460 \& # then back to the top to re\-check EXPR
1461 \& }
1462 \& ### last always comes here
1463 .Ve
1464 .Sp
1465 Omitting the \f(CW\*(C`continue\*(C'\fR section is equivalent to
1466 using an empty one, logically enough, so \f(CW\*(C`next\*(C'\fR goes
1467 directly back to check the condition at the top of the loop.
1468 .Sp
1469 When there is no BLOCK, \f(CW\*(C`continue\*(C'\fR is a function
1470 that falls through the current \f(CW\*(C`when\*(C'\fR or \f(CW\*(C`default\*(C'\fR block instead of
1471 iterating a dynamically enclosing \f(CW\*(C`foreach\*(C'\fR or exiting a lexically
1472 enclosing \f(CW\*(C`given\*(C'\fR. In Perl 5.14 and earlier, this form of
1473 \&\f(CW\*(C`continue\*(C'\fR was only available when the
1474 \&\f(CW"switch"\fR feature was enabled. See
1475 feature and "Switch Statements" in perlsyn for more information.
1476 .IP "cos EXPR" 4
1477 .IX Xref "cos cosine acos arccosine"
1478 .IX Item "cos EXPR"
1479 .PD 0
1480 .IP cos 4
1481 .IX Item "cos"
1482 .PD
1483 Returns the cosine of EXPR (expressed in radians). If EXPR is omitted,
1484 takes the cosine of \f(CW$_\fR.
1485 .Sp
1486 For the inverse cosine operation, you may use the
1487 \&\f(CW\*(C`Math::Trig::acos\*(C'\fR function, or use this relation:
1488 .Sp
1489 .Vb 1
1490 \& sub acos { atan2( sqrt(1 \- $_[0] * $_[0]), $_[0] ) }
1491 .Ve
1492 .IP "crypt PLAINTEXT,SALT" 4
1493 .IX Xref "crypt digest hash salt plaintext password decrypt cryptography passwd encrypt"
1494 .IX Item "crypt PLAINTEXT,SALT"
1495 Creates a digest string exactly like the \fBcrypt\fR\|(3) function in the C
1496 library (assuming that you actually have a version there that has not
1497 been extirpated as a potential munition).
1498 .Sp
1499 \&\f(CW\*(C`crypt\*(C'\fR is a one-way hash function. The
1500 PLAINTEXT and SALT are turned
1501 into a short string, called a digest, which is returned. The same
1502 PLAINTEXT and SALT will always return the same string, but there is no
1503 (known) way to get the original PLAINTEXT from the hash. Small
1504 changes in the PLAINTEXT or SALT will result in large changes in the
1505 digest.
1506 .Sp
1507 There is no decrypt function. This function isn't all that useful for
1508 cryptography (for that, look for \fICrypt\fR modules on your nearby CPAN
1509 mirror) and the name "crypt" is a bit of a misnomer. Instead it is
1510 primarily used to check if two pieces of text are the same without
1511 having to transmit or store the text itself. An example is checking
1512 if a correct password is given. The digest of the password is stored,
1513 not the password itself. The user types in a password that is
1514 \&\f(CW\*(C`crypt\*(C'\fR'd with the same salt as the stored
1515 digest. If the two digests match, the password is correct.
1516 .Sp
1517 When verifying an existing digest string you should use the digest as
1518 the salt (like \f(CW\*(C`crypt($plain, $digest) eq $digest\*(C'\fR). The SALT used
1519 to create the digest is visible as part of the digest. This ensures
1520 \&\f(CW\*(C`crypt\*(C'\fR will hash the new string with the same
1521 salt as the digest. This allows your code to work with the standard
1522 \&\f(CW\*(C`crypt\*(C'\fR and with more exotic implementations.
1523 In other words, assume nothing about the returned string itself nor
1524 about how many bytes of SALT may matter.
1525 .Sp
1526 Traditionally the result is a string of 13 bytes: two first bytes of
1527 the salt, followed by 11 bytes from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR, and only
1528 the first eight bytes of PLAINTEXT mattered. But alternative
1529 hashing schemes (like MD5), higher level security schemes (like C2),
1530 and implementations on non-Unix platforms may produce different
1531 strings.
1532 .Sp
1533 When choosing a new salt create a random two character string whose
1534 characters come from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR (like \f(CW\*(C`join \*(Aq\*(Aq, (\*(Aq.\*(Aq,
1535 \&\*(Aq/\*(Aq, 0..9, \*(AqA\*(Aq..\*(AqZ\*(Aq, \*(Aqa\*(Aq..\*(Aqz\*(Aq)[rand 64, rand 64]\*(C'\fR). This set of
1536 characters is just a recommendation; the characters allowed in
1537 the salt depend solely on your system's crypt library, and Perl can't
1538 restrict what salts \f(CW\*(C`crypt\*(C'\fR accepts.
1539 .Sp
1540 Here's an example that makes sure that whoever runs this program knows
1541 their password:
1542 .Sp
1543 .Vb 1
1544 \& my $pwd = (getpwuid($<))[1];
1545 \&
1546 \& system "stty \-echo";
1547 \& print "Password: ";
1548 \& chomp(my $word = <STDIN>);
1549 \& print "\en";
1550 \& system "stty echo";
1551 \&
1552 \& if (crypt($word, $pwd) ne $pwd) {
1553 \& die "Sorry...\en";
1554 \& } else {
1555 \& print "ok\en";
1556 \& }
1557 .Ve
1558 .Sp
1559 Of course, typing in your own password to whoever asks you
1560 for it is unwise.
1561 .Sp
1562 The \f(CW\*(C`crypt\*(C'\fR function is unsuitable for hashing
1563 large quantities of data, not least of all because you can't get the
1564 information back. Look at the Digest module for more robust
1565 algorithms.
1566 .Sp
1567 If using \f(CW\*(C`crypt\*(C'\fR on a Unicode string (which
1568 \&\fIpotentially\fR has characters with codepoints above 255), Perl tries to
1569 make sense of the situation by trying to downgrade (a copy of) the
1570 string back to an eight-bit byte string before calling
1571 \&\f(CW\*(C`crypt\*(C'\fR (on that copy). If that works, good.
1572 If not, \f(CW\*(C`crypt\*(C'\fR dies with
1573 \&\f(CW\*(C`Wide character in crypt\*(C'\fR.
1574 .Sp
1575 Portability issues: "crypt" in perlport.
1576 .IP "dbmclose HASH" 4
1577 .IX Xref "dbmclose"
1578 .IX Item "dbmclose HASH"
1579 [This function has been largely superseded by the
1580 \&\f(CW\*(C`untie\*(C'\fR function.]
1581 .Sp
1582 Breaks the binding between a DBM file and a hash.
1583 .Sp
1584 Portability issues: "dbmclose" in perlport.
1585 .IP "dbmopen HASH,DBNAME,MASK" 4
1586 .IX Xref "dbmopen dbm ndbm sdbm gdbm"
1587 .IX Item "dbmopen HASH,DBNAME,MASK"
1588 [This function has been largely superseded by the
1589 \&\f(CW\*(C`tie\*(C'\fR function.]
1590 .Sp
1591 This binds a \fBdbm\fR\|(3), \fBndbm\fR\|(3), \fBsdbm\fR\|(3), \fBgdbm\fR\|(3), or Berkeley
1592 DB file to a hash. HASH is the name of the hash. (Unlike normal
1593 \&\f(CW\*(C`open\*(C'\fR, the first argument is \fInot\fR a
1594 filehandle, even though it looks like one). DBNAME is the name of the
1595 database (without the \fI.dir\fR or \fI.pag\fR extension if any). If the
1596 database does not exist, it is created with protection specified by MASK
1597 (as modified by the \f(CW\*(C`umask\*(C'\fR). To prevent creation of
1598 the database if it doesn't exist, you may specify a MASK of 0, and the
1599 function will return a false value if it can't find an existing
1600 database. If your system supports only the older DBM functions, you may
1601 make only one \f(CW\*(C`dbmopen\*(C'\fR call in your
1602 program. In older versions of Perl, if your system had neither DBM nor
1603 ndbm, calling \f(CW\*(C`dbmopen\*(C'\fR produced a fatal
1604 error; it now falls back to \fBsdbm\fR\|(3).
1605 .Sp
1606 If you don't have write access to the DBM file, you can only read hash
1607 variables, not set them. If you want to test whether you can write,
1608 either use file tests or try setting a dummy hash entry inside an
1609 \&\f(CW\*(C`eval\*(C'\fR to trap the error.
1610 .Sp
1611 Note that functions such as \f(CW\*(C`keys\*(C'\fR and
1612 \&\f(CW\*(C`values\*(C'\fR may return huge lists when used on large DBM
1613 files. You may prefer to use the \f(CW\*(C`each\*(C'\fR function to
1614 iterate over large DBM files. Example:
1615 .Sp
1616 .Vb 6
1617 \& # print out history file offsets
1618 \& dbmopen(%HIST,\*(Aq/usr/lib/news/history\*(Aq,0666);
1619 \& while (($key,$val) = each %HIST) {
1620 \& print $key, \*(Aq = \*(Aq, unpack(\*(AqL\*(Aq,$val), "\en";
1621 \& }
1622 \& dbmclose(%HIST);
1623 .Ve
1624 .Sp
1625 See also AnyDBM_File for a more general description of the pros and
1626 cons of the various dbm approaches, as well as DB_File for a particularly
1627 rich implementation.
1628 .Sp
1629 You can control which DBM library you use by loading that library
1630 before you call \f(CW\*(C`dbmopen\*(C'\fR:
1631 .Sp
1632 .Vb 3
1633 \& use DB_File;
1634 \& dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")
1635 \& or die "Can\*(Aqt open netscape history file: $!";
1636 .Ve
1637 .Sp
1638 Portability issues: "dbmopen" in perlport.
1639 .IP "defined EXPR" 4
1640 .IX Xref "defined undef undefined"
1641 .IX Item "defined EXPR"
1642 .PD 0
1643 .IP defined 4
1644 .IX Item "defined"
1645 .PD
1646 Returns a Boolean value telling whether EXPR has a value other than the
1647 undefined value \f(CW\*(C`undef\*(C'\fR. If EXPR is not present,
1648 \&\f(CW$_\fR is checked.
1649 .Sp
1650 Many operations return \f(CW\*(C`undef\*(C'\fR to indicate failure, end
1651 of file, system error, uninitialized variable, and other exceptional
1652 conditions. This function allows you to distinguish
1653 \&\f(CW\*(C`undef\*(C'\fR from other values. (A simple Boolean test will
1654 not distinguish among \f(CW\*(C`undef\*(C'\fR, zero, the empty string,
1655 and \f(CW"0"\fR, which are all equally false.) Note that since
1656 \&\f(CW\*(C`undef\*(C'\fR is a valid scalar, its presence doesn't
1657 \&\fInecessarily\fR indicate an exceptional condition: \f(CW\*(C`pop\*(C'\fR
1658 returns \f(CW\*(C`undef\*(C'\fR when its argument is an empty array,
1659 \&\fIor\fR when the element to return happens to be \f(CW\*(C`undef\*(C'\fR.
1660 .Sp
1661 You may also use \f(CWdefined(&func)\fR to check whether subroutine \f(CW\*(C`func\*(C'\fR
1662 has ever been defined. The return value is unaffected by any forward
1663 declarations of \f(CW\*(C`func\*(C'\fR. A subroutine that is not defined
1664 may still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR method that
1665 makes it spring into existence the first time that it is called; see
1666 perlsub.
1667 .Sp
1668 Use of \f(CW\*(C`defined\*(C'\fR on aggregates (hashes and arrays) is
1669 no longer supported. It used to report whether memory for that
1670 aggregate had ever been allocated. You should instead use a simple
1671 test for size:
1672 .Sp
1673 .Vb 2
1674 \& if (@an_array) { print "has array elements\en" }
1675 \& if (%a_hash) { print "has hash members\en" }
1676 .Ve
1677 .Sp
1678 When used on a hash element, it tells you whether the value is defined,
1679 not whether the key exists in the hash. Use \f(CW\*(C`exists\*(C'\fR
1680 for the latter purpose.
1681 .Sp
1682 Examples:
1683 .Sp
1684 .Vb 6
1685 \& print if defined $switch{D};
1686 \& print "$val\en" while defined($val = pop(@ary));
1687 \& die "Can\*(Aqt readlink $sym: $!"
1688 \& unless defined($value = readlink $sym);
1689 \& sub foo { defined &$bar ? $bar\->(@_) : die "No bar"; }
1690 \& $debugging = 0 unless defined $debugging;
1691 .Ve
1692 .Sp
1693 Note: Many folks tend to overuse \f(CW\*(C`defined\*(C'\fR and are
1694 then surprised to discover that the number \f(CW0\fR and \f(CW""\fR (the
1695 zero-length string) are, in fact, defined values. For example, if you
1696 say
1697 .Sp
1698 .Vb 1
1699 \& "ab" =~ /a(.*)b/;
1700 .Ve
1701 .Sp
1702 The pattern match succeeds and \f(CW$1\fR is defined, although it
1703 matched "nothing". It didn't really fail to match anything. Rather, it
1704 matched something that happened to be zero characters long. This is all
1705 very above-board and honest. When a function returns an undefined value,
1706 it's an admission that it couldn't give you an honest answer. So you
1707 should use \f(CW\*(C`defined\*(C'\fR only when questioning the
1708 integrity of what you're trying to do. At other times, a simple
1709 comparison to \f(CW0\fR or \f(CW""\fR is what you want.
1710 .Sp
1711 See also \f(CW\*(C`undef\*(C'\fR, \f(CW\*(C`exists\*(C'\fR,
1712 \&\f(CW\*(C`ref\*(C'\fR.
1713 .IP "delete EXPR" 4
1714 .IX Xref "delete"
1715 .IX Item "delete EXPR"
1716 Given an expression that specifies an element or slice of a hash,
1717 \&\f(CW\*(C`delete\*(C'\fR deletes the specified elements from that hash
1718 so that \f(CW\*(C`exists\*(C'\fR on that element no longer returns
1719 true. Setting a hash element to the undefined value does not remove its
1720 key, but deleting it does; see \f(CW\*(C`exists\*(C'\fR.
1721 .Sp
1722 In list context, usually returns the value or values deleted, or the last such
1723 element in scalar context. The return list's length corresponds to that of
1724 the argument list: deleting non-existent elements returns the undefined value
1725 in their corresponding positions. Since Perl 5.28, a
1726 key/value hash slice can be passed
1727 to \f(CW\*(C`delete\*(C'\fR, and the return value is a list of key/value pairs (two elements
1728 for each item deleted from the hash).
1729 .Sp
1730 \&\f(CW\*(C`delete\*(C'\fR may also be used on arrays and array slices,
1731 but its behavior is less straightforward. Although
1732 \&\f(CW\*(C`exists\*(C'\fR will return false for deleted entries,
1733 deleting array elements never changes indices of existing values; use
1734 \&\f(CW\*(C`shift\*(C'\fR or \f(CW\*(C`splice\*(C'\fR for that. However, if any deleted elements
1735 fall at the end of an array, the array's size shrinks to the position of
1736 the highest element that still tests true for \f(CW\*(C`exists\*(C'\fR,
1737 or to 0 if none do. In other words, an array won't have trailing
1738 nonexistent elements after a delete.
1739 .Sp
1740 \&\fBWARNING:\fR Calling \f(CW\*(C`delete\*(C'\fR on array values is
1741 strongly discouraged. The
1742 notion of deleting or checking the existence of Perl array elements is not
1743 conceptually coherent, and can lead to surprising behavior.
1744 .Sp
1745 Deleting from \f(CW%ENV\fR modifies the environment.
1746 Deleting from a hash tied to a DBM file deletes the entry from the DBM
1747 file. Deleting from a \f(CW\*(C`tied\*(C'\fR hash or array may not
1748 necessarily return anything; it depends on the implementation of the
1749 \&\f(CW\*(C`tied\*(C'\fR package's DELETE method, which may do whatever
1750 it pleases.
1751 .Sp
1752 The \f(CW\*(C`delete local EXPR\*(C'\fR construct localizes the deletion to the current
1753 block at run time. Until the block exits, elements locally deleted
1754 temporarily no longer exist. See "Localized deletion of elements
1755 of composite types" in perlsub.
1756 .Sp
1757 .Vb 4
1758 \& my %hash = (foo => 11, bar => 22, baz => 33);
1759 \& my $scalar = delete $hash{foo}; # $scalar is 11
1760 \& $scalar = delete @hash{qw(foo bar)}; # $scalar is 22
1761 \& my @array = delete @hash{qw(foo baz)}; # @array is (undef,33)
1762 .Ve
1763 .Sp
1764 The following (inefficiently) deletes all the values of \f(CW%HASH\fR and \f(CW@ARRAY:\fR
1765 .Sp
1766 .Vb 3
1767 \& foreach my $key (keys %HASH) {
1768 \& delete $HASH{$key};
1769 \& }
1770 \&
1771 \& foreach my $index (0 .. $#ARRAY) {
1772 \& delete $ARRAY[$index];
1773 \& }
1774 .Ve
1775 .Sp
1776 And so do these:
1777 .Sp
1778 .Vb 1
1779 \& delete @HASH{keys %HASH};
1780 \&
1781 \& delete @ARRAY[0 .. $#ARRAY];
1782 .Ve
1783 .Sp
1784 But both are slower than assigning the empty list
1785 or undefining \f(CW%HASH\fR or \f(CW@ARRAY\fR, which is the customary
1786 way to empty out an aggregate:
1787 .Sp
1788 .Vb 2
1789 \& %HASH = (); # completely empty %HASH
1790 \& undef %HASH; # forget %HASH ever existed
1791 \&
1792 \& @ARRAY = (); # completely empty @ARRAY
1793 \& undef @ARRAY; # forget @ARRAY ever existed
1794 .Ve
1795 .Sp
1796 The EXPR can be arbitrarily complicated provided its
1797 final operation is an element or slice of an aggregate:
1798 .Sp
1799 .Vb 2
1800 \& delete $ref\->[$x][$y]{$key};
1801 \& delete $ref\->[$x][$y]\->@{$key1, $key2, @morekeys};
1802 \&
1803 \& delete $ref\->[$x][$y][$index];
1804 \& delete $ref\->[$x][$y]\->@[$index1, $index2, @moreindices];
1805 .Ve
1806 .IP "die LIST" 4
1807 .IX Xref "die throw exception raise $@ abort"
1808 .IX Item "die LIST"
1809 \&\f(CW\*(C`die\*(C'\fR raises an exception. Inside an \f(CW\*(C`eval\*(C'\fR
1810 the exception is stuffed into \f(CW$@\fR and the \f(CW\*(C`eval\*(C'\fR is terminated with the undefined value. If the exception is
1811 outside of all enclosing \f(CW\*(C`eval\*(C'\fRs, then the uncaught
1812 exception is printed to \f(CW\*(C`STDERR\*(C'\fR and perl exits with an exit code
1813 indicating failure. If you need to exit the process with a specific
1814 exit code, see \f(CW\*(C`exit\*(C'\fR.
1815 .Sp
1816 Equivalent examples:
1817 .Sp
1818 .Vb 2
1819 \& die "Can\*(Aqt cd to spool: $!\en" unless chdir \*(Aq/usr/spool/news\*(Aq;
1820 \& chdir \*(Aq/usr/spool/news\*(Aq or die "Can\*(Aqt cd to spool: $!\en"
1821 .Ve
1822 .Sp
1823 Most of the time, \f(CW\*(C`die\*(C'\fR is called with a string to use as the exception.
1824 You may either give a single non-reference operand to serve as the
1825 exception, or a list of two or more items, which will be stringified
1826 and concatenated to make the exception.
1827 .Sp
1828 If the string exception does not end in a newline, the current
1829 script line number and input line number (if any) and a newline
1830 are appended to it. Note that the "input line number" (also
1831 known as "chunk") is subject to whatever notion of "line" happens to
1832 be currently in effect, and is also available as the special variable
1833 \&\f(CW$.\fR. See "$/" in perlvar and "$." in perlvar.
1834 .Sp
1835 Hint: sometimes appending \f(CW", stopped"\fR to your message will cause it
1836 to make better sense when the string \f(CW"at foo line 123"\fR is appended.
1837 Suppose you are running script "canasta".
1838 .Sp
1839 .Vb 2
1840 \& die "/etc/games is no good";
1841 \& die "/etc/games is no good, stopped";
1842 .Ve
1843 .Sp
1844 produce, respectively
1845 .Sp
1846 .Vb 2
1847 \& /etc/games is no good at canasta line 123.
1848 \& /etc/games is no good, stopped at canasta line 123.
1849 .Ve
1850 .Sp
1851 If LIST was empty or made an empty string, and \f(CW$@\fR
1852 already contains an exception value (typically from a previous
1853 \&\f(CW\*(C`eval\*(C'\fR), then that value is reused after
1854 appending \f(CW"\et...propagated"\fR. This is useful for propagating exceptions:
1855 .Sp
1856 .Vb 2
1857 \& eval { ... };
1858 \& die unless $@ =~ /Expected exception/;
1859 .Ve
1860 .Sp
1861 If LIST was empty or made an empty string,
1862 and \f(CW$@\fR contains an object
1863 reference that has a \f(CW\*(C`PROPAGATE\*(C'\fR method, that method will be called
1864 with additional file and line number parameters. The return value
1865 replaces the value in \f(CW$@\fR; i.e., as if
1866 \&\f(CW\*(C`$@ = eval { $@\->PROPAGATE(_\|_FILE_\|_, _\|_LINE_\|_) };\*(C'\fR were called.
1867 .Sp
1868 If LIST was empty or made an empty string, and \f(CW$@\fR
1869 is also empty, then the string \f(CW"Died"\fR is used.
1870 .Sp
1871 You can also call \f(CW\*(C`die\*(C'\fR with a reference argument, and if
1872 this is trapped within an \f(CW\*(C`eval\*(C'\fR, \f(CW$@\fR
1873 contains that reference. This permits more elaborate exception handling
1874 using objects that maintain arbitrary state about the exception. Such a
1875 scheme is sometimes preferable to matching particular string values of
1876 \&\f(CW$@\fR with regular expressions.
1877 .Sp
1878 Because Perl stringifies uncaught exception messages before display,
1879 you'll probably want to overload stringification operations on
1880 exception objects. See overload for details about that.
1881 The stringified message should be non-empty, and should end in a newline,
1882 in order to fit in with the treatment of string exceptions.
1883 Also, because an exception object reference cannot be stringified
1884 without destroying it, Perl doesn't attempt to append location or other
1885 information to a reference exception. If you want location information
1886 with a complex exception object, you'll have to arrange to put the
1887 location information into the object yourself.
1888 .Sp
1889 Because \f(CW$@\fR is a global variable, be careful that
1890 analyzing an exception caught by \f(CW\*(C`eval\*(C'\fR doesn't replace the reference
1891 in the global variable. It's
1892 easiest to make a local copy of the reference before any manipulations.
1893 Here's an example:
1894 .Sp
1895 .Vb 1
1896 \& use Scalar::Util "blessed";
1897 \&
1898 \& eval { ... ; die Some::Module::Exception\->new( FOO => "bar" ) };
1899 \& if (my $ev_err = $@) {
1900 \& if (blessed($ev_err)
1901 \& && $ev_err\->isa("Some::Module::Exception")) {
1902 \& # handle Some::Module::Exception
1903 \& }
1904 \& else {
1905 \& # handle all other possible exceptions
1906 \& }
1907 \& }
1908 .Ve
1909 .Sp
1910 If an uncaught exception results in interpreter exit, the exit code is
1911 determined from the values of \f(CW$!\fR and
1912 \&\f(CW$?\fR with this pseudocode:
1913 .Sp
1914 .Vb 3
1915 \& exit $! if $!; # errno
1916 \& exit $? >> 8 if $? >> 8; # child exit status
1917 \& exit 255; # last resort
1918 .Ve
1919 .Sp
1920 As with \f(CW\*(C`exit\*(C'\fR, \f(CW$?\fR is set prior to
1921 unwinding the call stack; any \f(CW\*(C`DESTROY\*(C'\fR or \f(CW\*(C`END\*(C'\fR handlers can then
1922 alter this value, and thus Perl's exit code.
1923 .Sp
1924 The intent is to squeeze as much possible information about the likely cause
1925 into the limited space of the system exit code. However, as
1926 \&\f(CW$!\fR is the value of C's \f(CW\*(C`errno\*(C'\fR, which can be set by
1927 any system call, this means that the value of the exit code used by
1928 \&\f(CW\*(C`die\*(C'\fR can be non-predictable, so should not be relied
1929 upon, other than to be non-zero.
1930 .Sp
1931 You can arrange for a callback to be run just before the
1932 \&\f(CW\*(C`die\*(C'\fR does its deed, by setting the
1933 \&\f(CW$SIG{_\|_DIE_\|_}\fR hook. The associated handler is called
1934 with the exception as an argument, and can change the exception,
1935 if it sees fit, by
1936 calling \f(CW\*(C`die\*(C'\fR again. See "%SIG" in perlvar for details on
1937 setting \f(CW%SIG\fR entries, and \f(CW\*(C`eval\*(C'\fR for some
1938 examples. Although this feature was to be run only right before your
1939 program was to exit, this is not currently so: the
1940 \&\f(CW$SIG{_\|_DIE_\|_}\fR hook is currently called even inside
1941 \&\f(CW\*(C`eval\*(C'\fRed blocks/strings! If one wants the hook to do
1942 nothing in such situations, put
1943 .Sp
1944 .Vb 1
1945 \& die @_ if $^S;
1946 .Ve
1947 .Sp
1948 as the first line of the handler (see "$^S" in perlvar). Because
1949 this promotes strange action at a distance, this counterintuitive
1950 behavior may be fixed in a future release.
1951 .Sp
1952 See also \f(CW\*(C`exit\*(C'\fR, \f(CW\*(C`warn\*(C'\fR, and the Carp
1953 module.
1954 .IP "do BLOCK" 4
1955 .IX Xref "do block"
1956 .IX Item "do BLOCK"
1957 Not really a function. Returns the value of the last command in the
1958 sequence of commands indicated by BLOCK. When modified by the \f(CW\*(C`while\*(C'\fR or
1959 \&\f(CW\*(C`until\*(C'\fR loop modifier, executes the BLOCK once before testing the loop
1960 condition. (On other statements the loop modifiers test the conditional
1961 first.)
1962 .Sp
1963 \&\f(CW\*(C`do BLOCK\*(C'\fR does \fInot\fR count as a loop, so the loop control statements
1964 \&\f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, or
1965 \&\f(CW\*(C`redo\*(C'\fR cannot be used to leave or restart the block.
1966 See perlsyn for alternative strategies.
1967 .IP "do EXPR" 4
1968 .IX Xref "do"
1969 .IX Item "do EXPR"
1970 Uses the value of EXPR as a filename and executes the contents of the
1971 file as a Perl script:
1972 .Sp
1973 .Vb 4
1974 \& # load the exact specified file (./ and ../ special\-cased)
1975 \& do \*(Aq/foo/stat.pl\*(Aq;
1976 \& do \*(Aq./stat.pl\*(Aq;
1977 \& do \*(Aq../foo/stat.pl\*(Aq;
1978 \&
1979 \& # search for the named file within @INC
1980 \& do \*(Aqstat.pl\*(Aq;
1981 \& do \*(Aqfoo/stat.pl\*(Aq;
1982 .Ve
1983 .Sp
1984 \&\f(CW\*(C`do \*(Aq./stat.pl\*(Aq\*(C'\fR is largely like
1985 .Sp
1986 .Vb 1
1987 \& eval \`cat stat.pl\`;
1988 .Ve
1989 .Sp
1990 except that it's more concise, runs no external processes, and keeps
1991 track of the current filename for error messages. It also differs in that
1992 code evaluated with \f(CW\*(C`do FILE\*(C'\fR cannot see lexicals in the enclosing
1993 scope; \f(CW\*(C`eval STRING\*(C'\fR does. It's the same, however, in that it does
1994 reparse the file every time you call it, so you probably don't want
1995 to do this inside a loop.
1996 .Sp
1997 Using \f(CW\*(C`do\*(C'\fR with a relative path (except for \fI./\fR and \fI../\fR), like
1998 .Sp
1999 .Vb 1
2000 \& do \*(Aqfoo/stat.pl\*(Aq;
2001 .Ve
2002 .Sp
2003 will search the \f(CW@INC\fR directories, and update
2004 \&\f(CW%INC\fR if the file is found. See "@INC" in perlvar
2005 and "%INC" in perlvar for these variables. In particular, note that
2006 whilst historically \f(CW@INC\fR contained '.' (the
2007 current directory) making these two cases equivalent, that is no
2008 longer necessarily the case, as '.' is not included in \f(CW@INC\fR by default
2009 in perl versions 5.26.0 onwards. Instead, perl will now warn:
2010 .Sp
2011 .Vb 2
2012 \& do "stat.pl" failed, \*(Aq.\*(Aq is no longer in @INC;
2013 \& did you mean do "./stat.pl"?
2014 .Ve
2015 .Sp
2016 If \f(CW\*(C`do\*(C'\fR can read the file but cannot compile it, it
2017 returns \f(CW\*(C`undef\*(C'\fR and sets an error message in
2018 \&\f(CW$@\fR. If \f(CW\*(C`do\*(C'\fR cannot read the file, it
2019 returns undef and sets \f(CW$!\fR to the error. Always check
2020 \&\f(CW$@\fR first, as compilation could fail in a way that also
2021 sets \f(CW$!\fR. If the file is successfully compiled,
2022 \&\f(CW\*(C`do\*(C'\fR returns the value of the last expression evaluated.
2023 .Sp
2024 Inclusion of library modules is better done with the
2025 \&\f(CW\*(C`use\*(C'\fR and \f(CW\*(C`require\*(C'\fR
2026 operators, which also do automatic error checking and raise an exception
2027 if there's a problem.
2028 .Sp
2029 You might like to use \f(CW\*(C`do\*(C'\fR to read in a program
2030 configuration file. Manual error checking can be done this way:
2031 .Sp
2032 .Vb 11
2033 \& # Read in config files: system first, then user.
2034 \& # Beware of using relative pathnames here.
2035 \& for $file ("/share/prog/defaults.rc",
2036 \& "$ENV{HOME}/.someprogrc")
2037 \& {
2038 \& unless ($return = do $file) {
2039 \& warn "couldn\*(Aqt parse $file: $@" if $@;
2040 \& warn "couldn\*(Aqt do $file: $!" unless defined $return;
2041 \& warn "couldn\*(Aqt run $file" unless $return;
2042 \& }
2043 \& }
2044 .Ve
2045 .IP "dump LABEL" 4
2046 .IX Xref "dump core undump"
2047 .IX Item "dump LABEL"
2048 .PD 0
2049 .IP "dump EXPR" 4
2050 .IX Item "dump EXPR"
2051 .IP dump 4
2052 .IX Item "dump"
2053 .PD
2054 This function causes an immediate core dump. See also the \fB\-u\fR
2055 command-line switch in perlrun, which does the same thing.
2056 Primarily this is so that you can use the \fBundump\fR program (not
2057 supplied) to turn your core dump into an executable binary after
2058 having initialized all your variables at the beginning of the
2059 program. When the new binary is executed it will begin by executing
2060 a \f(CW\*(C`goto LABEL\*(C'\fR (with all the restrictions that \f(CW\*(C`goto\*(C'\fR
2061 suffers).
2062 Think of it as a goto with an intervening core dump and reincarnation.
2063 If \f(CW\*(C`LABEL\*(C'\fR is omitted, restarts the program from the top. The
2064 \&\f(CW\*(C`dump EXPR\*(C'\fR form, available starting in Perl 5.18.0, allows a name to be
2065 computed at run time, being otherwise identical to \f(CW\*(C`dump LABEL\*(C'\fR.
2066 .Sp
2067 \&\fBWARNING\fR: Any files opened at the time of the dump will \fInot\fR
2068 be open any more when the program is reincarnated, with possible
2069 resulting confusion by Perl.
2070 .Sp
2071 This function is now largely obsolete, mostly because it's very hard to
2072 convert a core file into an executable. As of Perl 5.30, it must be invoked
2073 as \f(CWCORE::dump()\fR.
2074 .Sp
2075 Unlike most named operators, this has the same precedence as assignment.
2076 It is also exempt from the looks-like-a-function rule, so
2077 \&\f(CW\*(C`dump ("foo")."bar"\*(C'\fR will cause "bar" to be part of the argument to
2078 \&\f(CW\*(C`dump\*(C'\fR.
2079 .Sp
2080 Portability issues: "dump" in perlport.
2081 .IP "each HASH" 4
2082 .IX Xref "each hash, iterator"
2083 .IX Item "each HASH"
2084 .PD 0
2085 .IP "each ARRAY" 4
2086 .IX Xref "array, iterator"
2087 .IX Item "each ARRAY"
2088 .PD
2089 When called on a hash in list context, returns a 2\-element list
2090 consisting of the key and value for the next element of a hash. In Perl
2091 5.12 and later only, it will also return the index and value for the next
2092 element of an array so that you can iterate over it; older Perls consider
2093 this a syntax error. When called in scalar context, returns only the key
2094 (not the value) in a hash, or the index in an array.
2095 .Sp
2096 Hash entries are returned in an apparently random order. The actual random
2097 order is specific to a given hash; the exact same series of operations
2098 on two hashes may result in a different order for each hash. Any insertion
2099 into the hash may change the order, as will any deletion, with the exception
2100 that the most recent key returned by \f(CW\*(C`each\*(C'\fR or
2101 \&\f(CW\*(C`keys\*(C'\fR may be deleted without changing the order. So
2102 long as a given hash is unmodified you may rely on
2103 \&\f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR and
2104 \&\f(CW\*(C`each\*(C'\fR to repeatedly return the same order
2105 as each other. See "Algorithmic Complexity Attacks" in perlsec for
2106 details on why hash order is randomized. Aside from the guarantees
2107 provided here the exact details of Perl's hash algorithm and the hash
2108 traversal order are subject to change in any release of Perl.
2109 .Sp
2110 After \f(CW\*(C`each\*(C'\fR has returned all entries from the hash or
2111 array, the next call to \f(CW\*(C`each\*(C'\fR returns the empty list in
2112 list context and \f(CW\*(C`undef\*(C'\fR in scalar context; the next
2113 call following \fIthat\fR one restarts iteration. Each hash or array has
2114 its own internal iterator, accessed by \f(CW\*(C`each\*(C'\fR,
2115 \&\f(CW\*(C`keys\*(C'\fR, and \f(CW\*(C`values\*(C'\fR. The iterator is
2116 implicitly reset when \f(CW\*(C`each\*(C'\fR has reached the end as just
2117 described; it can be explicitly reset by calling \f(CW\*(C`keys\*(C'\fR
2118 or \f(CW\*(C`values\*(C'\fR on the hash or array, or by referencing
2119 the hash (but not array) in list context. If you add or delete
2120 a hash's elements while iterating over it, the effect on the iterator is
2121 unspecified; for example, entries may be skipped or duplicated\-\-so don't
2122 do that. Exception: It is always safe to delete the item most recently
2123 returned by \f(CW\*(C`each\*(C'\fR, so the following code works properly:
2124 .Sp
2125 .Vb 4
2126 \& while (my ($key, $value) = each %hash) {
2127 \& print $key, "\en";
2128 \& delete $hash{$key}; # This is safe
2129 \& }
2130 .Ve
2131 .Sp
2132 Tied hashes may have a different ordering behaviour to perl's hash
2133 implementation.
2134 .Sp
2135 The iterator used by \f(CW\*(C`each\*(C'\fR is attached to the hash or array, and is
2136 shared between all iteration operations applied to the same hash or array.
2137 Thus all uses of \f(CW\*(C`each\*(C'\fR on a single hash or array advance the same
2138 iterator location. All uses of \f(CW\*(C`each\*(C'\fR are also subject to having the
2139 iterator reset by any use of \f(CW\*(C`keys\*(C'\fR or \f(CW\*(C`values\*(C'\fR on the same hash or
2140 array, or by the hash (but not array) being referenced in list context.
2141 This makes \f(CW\*(C`each\*(C'\fR\-based loops quite fragile: it is easy to arrive at
2142 such a loop with the iterator already part way through the object, or to
2143 accidentally clobber the iterator state during execution of the loop body.
2144 It's easy enough to explicitly reset the iterator before starting a loop,
2145 but there is no way to insulate the iterator state used by a loop from
2146 the iterator state used by anything else that might execute during the
2147 loop body. To avoid these problems, use a \f(CW\*(C`foreach\*(C'\fR loop rather than
2148 \&\f(CW\*(C`while\*(C'\fR\-\f(CW\*(C`each\*(C'\fR.
2149 .Sp
2150 This extends to using \f(CW\*(C`each\*(C'\fR on the result of an anonymous hash or
2151 array constructor. A new underlying array or hash is created each
2152 time so each will always start iterating from scratch, eg:
2153 .Sp
2154 .Vb 4
2155 \& # loops forever
2156 \& while (my ($key, $value) = each @{ +{ a => 1 } }) {
2157 \& print "$key=$value\en";
2158 \& }
2159 .Ve
2160 .Sp
2161 This prints out your environment like the \fBprintenv\fR\|(1) program,
2162 but in a different order:
2163 .Sp
2164 .Vb 3
2165 \& while (my ($key,$value) = each %ENV) {
2166 \& print "$key=$value\en";
2167 \& }
2168 .Ve
2169 .Sp
2170 Starting with Perl 5.14, an experimental feature allowed
2171 \&\f(CW\*(C`each\*(C'\fR to take a scalar expression. This experiment has
2172 been deemed unsuccessful, and was removed as of Perl 5.24.
2173 .Sp
2174 As of Perl 5.18 you can use a bare \f(CW\*(C`each\*(C'\fR in a \f(CW\*(C`while\*(C'\fR
2175 loop, which will set \f(CW$_\fR on every iteration.
2176 If either an \f(CW\*(C`each\*(C'\fR expression or an explicit assignment of an \f(CW\*(C`each\*(C'\fR
2177 expression to a scalar is used as a \f(CW\*(C`while\*(C'\fR/\f(CW\*(C`for\*(C'\fR condition, then
2178 the condition actually tests for definedness of the expression's value,
2179 not for its regular truth value.
2180 .Sp
2181 .Vb 3
2182 \& while (each %ENV) {
2183 \& print "$_=$ENV{$_}\en";
2184 \& }
2185 .Ve
2186 .Sp
2187 To avoid confusing would-be users of your code who are running earlier
2188 versions of Perl with mysterious syntax errors, put this sort of thing at
2189 the top of your file to signal that your code will work \fIonly\fR on Perls of
2190 a recent vintage:
2191 .Sp
2192 .Vb 2
2193 \& use v5.12; # so keys/values/each work on arrays
2194 \& use v5.18; # so each assigns to $_ in a lone while test
2195 .Ve
2196 .Sp
2197 See also \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR, and
2198 \&\f(CW\*(C`sort\*(C'\fR.
2199 .IP "eof FILEHANDLE" 4
2200 .IX Xref "eof end of file end-of-file"
2201 .IX Item "eof FILEHANDLE"
2202 .PD 0
2203 .IP "eof ()" 4
2204 .IX Item "eof ()"
2205 .IP eof 4
2206 .IX Item "eof"
2207 .PD
2208 Returns 1 if the next read on FILEHANDLE will return end of file \fIor\fR if
2209 FILEHANDLE is not open. FILEHANDLE may be an expression whose value
2210 gives the real filehandle. (Note that this function actually
2211 reads a character and then \f(CW\*(C`ungetc\*(C'\fRs it, so isn't useful in an
2212 interactive context.) Do not read from a terminal file (or call
2213 \&\f(CWeof(FILEHANDLE)\fR on it) after end-of-file is reached. File types such
2214 as terminals may lose the end-of-file condition if you do.
2215 .Sp
2216 An \f(CW\*(C`eof\*(C'\fR without an argument uses the last file
2217 read. Using \f(CWeof()\fR with empty parentheses is
2218 different. It refers to the pseudo file formed from the files listed on
2219 the command line and accessed via the \f(CW\*(C`<>\*(C'\fR operator. Since
2220 \&\f(CW\*(C`<>\*(C'\fR isn't explicitly opened, as a normal filehandle is, an
2221 \&\f(CWeof()\fR before \f(CW\*(C`<>\*(C'\fR has been used will cause
2222 \&\f(CW@ARGV\fR to be examined to determine if input is
2223 available. Similarly, an \f(CWeof()\fR after \f(CW\*(C`<>\*(C'\fR
2224 has returned end-of-file will assume you are processing another
2225 \&\f(CW@ARGV\fR list, and if you haven't set
2226 \&\f(CW@ARGV\fR, will read input from \f(CW\*(C`STDIN\*(C'\fR; see
2227 "I/O Operators" in perlop.
2228 .Sp
2229 In a \f(CW\*(C`while (<>)\*(C'\fR loop, \f(CW\*(C`eof\*(C'\fR or \f(CWeof(ARGV)\fR
2230 can be used to detect the end of each file, whereas
2231 \&\f(CWeof()\fR will detect the end of the very last file
2232 only. Examples:
2233 .Sp
2234 .Vb 7
2235 \& # reset line numbering on each input file
2236 \& while (<>) {
2237 \& next if /^\es*#/; # skip comments
2238 \& print "$.\et$_";
2239 \& } continue {
2240 \& close ARGV if eof; # Not eof()!
2241 \& }
2242 \&
2243 \& # insert dashes just before last line of last file
2244 \& while (<>) {
2245 \& if (eof()) { # check for end of last file
2246 \& print "\-\-\-\-\-\-\-\-\-\-\-\-\-\-\en";
2247 \& }
2248 \& print;
2249 \& last if eof(); # needed if we\*(Aqre reading from a terminal
2250 \& }
2251 .Ve
2252 .Sp
2253 Practical hint: you almost never need to use \f(CW\*(C`eof\*(C'\fR
2254 in Perl, because the input operators typically return \f(CW\*(C`undef\*(C'\fR when they run out of data or encounter an error.
2255 .IP "eval EXPR" 4
2256 .IX Xref "eval try catch evaluate parse execute error, handling exception, handling"
2257 .IX Item "eval EXPR"
2258 .PD 0
2259 .IP "eval BLOCK" 4
2260 .IX Item "eval BLOCK"
2261 .IP eval 4
2262 .IX Item "eval"
2263 .PD
2264 \&\f(CW\*(C`eval\*(C'\fR in all its forms is used to execute a little Perl program,
2265 trapping any errors encountered so they don't crash the calling program.
2266 .Sp
2267 Plain \f(CW\*(C`eval\*(C'\fR with no argument is just \f(CW\*(C`eval EXPR\*(C'\fR, where the
2268 expression is understood to be contained in \f(CW$_\fR. Thus
2269 there are only two real \f(CW\*(C`eval\*(C'\fR forms; the one with an EXPR is often
2270 called "string eval". In a string eval, the value of the expression
2271 (which is itself determined within scalar context) is first parsed, and
2272 if there were no errors, executed as a block within the lexical context
2273 of the current Perl program. This form is typically used to delay
2274 parsing and subsequent execution of the text of EXPR until run time.
2275 Note that the value is parsed every time the \f(CW\*(C`eval\*(C'\fR executes.
2276 .Sp
2277 The other form is called "block eval". It is less general than string
2278 eval, but the code within the BLOCK is parsed only once (at the same
2279 time the code surrounding the \f(CW\*(C`eval\*(C'\fR itself was parsed) and executed
2280 within the context of the current Perl program. This form is typically
2281 used to trap exceptions more efficiently than the first, while also
2282 providing the benefit of checking the code within BLOCK at compile time.
2283 BLOCK is parsed and compiled just once. Since errors are trapped, it
2284 often is used to check if a given feature is available.
2285 .Sp
2286 In both forms, the value returned is the value of the last expression
2287 evaluated inside the mini-program; a return statement may also be used, just
2288 as with subroutines. The expression providing the return value is evaluated
2289 in void, scalar, or list context, depending on the context of the
2290 \&\f(CW\*(C`eval\*(C'\fR itself. See \f(CW\*(C`wantarray\*(C'\fR for more
2291 on how the evaluation context can be determined.
2292 .Sp
2293 If there is a syntax error or runtime error, or a \f(CW\*(C`die\*(C'\fR
2294 statement is executed, \f(CW\*(C`eval\*(C'\fR returns
2295 \&\f(CW\*(C`undef\*(C'\fR in scalar context, or an empty list in list
2296 context, and \f(CW$@\fR is set to the error message. (Prior to
2297 5.16, a bug caused \f(CW\*(C`undef\*(C'\fR to be returned in list
2298 context for syntax errors, but not for runtime errors.) If there was no
2299 error, \f(CW$@\fR is set to the empty string. A control flow
2300 operator like \f(CW\*(C`last\*(C'\fR or \f(CW\*(C`goto\*(C'\fR can
2301 bypass the setting of \f(CW$@\fR. Beware that using
2302 \&\f(CW\*(C`eval\*(C'\fR neither silences Perl from printing warnings to
2303 STDERR, nor does it stuff the text of warning messages into
2304 \&\f(CW$@\fR. To do either of those, you have to use the
2305 \&\f(CW$SIG{_\|_WARN_\|_}\fR facility, or turn off warnings inside
2306 the BLOCK or EXPR using \f(CW\*(C`no\ warnings\ \*(Aqall\*(Aq\*(C'\fR. See
2307 \&\f(CW\*(C`warn\*(C'\fR, perlvar, and warnings.
2308 .Sp
2309 Note that, because \f(CW\*(C`eval\*(C'\fR traps otherwise-fatal errors,
2310 it is useful for determining whether a particular feature (such as
2311 \&\f(CW\*(C`socket\*(C'\fR or
2312 \&\f(CW\*(C`symlink\*(C'\fR) is implemented. It is also
2313 Perl's exception-trapping mechanism, where the \f(CW\*(C`die\*(C'\fR
2314 operator is used to raise exceptions.
2315 .Sp
2316 Before Perl 5.14, the assignment to \f(CW$@\fR occurred before
2317 restoration
2318 of localized variables, which means that for your code to run on older
2319 versions, a temporary is required if you want to mask some, but not all
2320 errors:
2321 .Sp
2322 .Vb 11
2323 \& # alter $@ on nefarious repugnancy only
2324 \& {
2325 \& my $e;
2326 \& {
2327 \& local $@; # protect existing $@
2328 \& eval { test_repugnancy() };
2329 \& # $@ =~ /nefarious/ and die $@; # Perl 5.14 and higher only
2330 \& $@ =~ /nefarious/ and $e = $@;
2331 \& }
2332 \& die $e if defined $e
2333 \& }
2334 .Ve
2335 .Sp
2336 There are some different considerations for each form:
2337 .RS 4
2338 .IP "String eval" 4
2339 .IX Item "String eval"
2340 Since the return value of EXPR is executed as a block within the lexical
2341 context of the current Perl program, any outer lexical variables are
2342 visible to it, and any package variable settings or subroutine and
2343 format definitions remain afterwards.
2344 .Sp
2345 Note that when \f(CW\*(C`BEGIN {}\*(C'\fR blocks are embedded inside of an eval block
2346 the contents of the block will be executed immediately and before the rest
2347 of the eval code is executed. You can disable this entirely by
2348 .Sp
2349 .Vb 2
2350 \& local ${^MAX_NESTED_EVAL_BEGIN_BLOCKS} = 0;
2351 \& eval $string;
2352 .Ve
2353 .Sp
2354 which will cause any embedded \f(CW\*(C`BEGIN\*(C'\fR blocks in \f(CW$string\fR to throw an
2355 exception.
2356 .RS 4
2357 .ie n .IP "Under the ""unicode_eval"" feature" 4
2358 .el .IP "Under the \f(CW""unicode_eval""\fR feature" 4
2359 .IX Item "Under the ""unicode_eval"" feature"
2360 If this feature is enabled (which is the default under a \f(CW\*(C`use 5.16\*(C'\fR or
2361 higher declaration), Perl assumes that EXPR is a character string.
2362 Any \f(CW\*(C`use\ utf8\*(C'\fR or \f(CW\*(C`no\ utf8\*(C'\fR declarations within
2363 the string thus have no effect. Source filters are forbidden as well.
2364 (\f(CW\*(C`unicode_strings\*(C'\fR, however, can appear within the string.)
2365 .Sp
2366 See also the \f(CW\*(C`evalbytes\*(C'\fR operator, which works properly
2367 with source filters.
2368 .ie n .IP "Outside the ""unicode_eval"" feature" 4
2369 .el .IP "Outside the \f(CW""unicode_eval""\fR feature" 4
2370 .IX Item "Outside the ""unicode_eval"" feature"
2371 In this case, the behavior is problematic and is not so easily
2372 described. Here are two bugs that cannot easily be fixed without
2373 breaking existing programs:
2374 .RS 4
2375 .IP \(bu 4
2376 Perl's internal storage of EXPR affects the behavior of the executed code.
2377 For example:
2378 .Sp
2379 .Vb 1
2380 \& my $v = eval "use utf8; \*(Aq$expr\*(Aq";
2381 .Ve
2382 .Sp
2383 If \f(CW$expr\fR is \f(CW"\exc4\ex80"\fR (U+0100 in UTF\-8), then the value stored in \f(CW$v\fR
2384 will depend on whether Perl stores \f(CW$expr\fR "upgraded" (cf. utf8) or
2385 not:
2386 .RS 4
2387 .IP \(bu 4
2388 If upgraded, \f(CW$v\fR will be \f(CW"\exc4\ex80"\fR (i.e., the
2389 \&\f(CW\*(C`use utf8\*(C'\fR has no effect.)
2390 .IP \(bu 4
2391 If non-upgraded, \f(CW$v\fR will be \f(CW"\ex{100}"\fR.
2392 .RE
2393 .RS 4
2394 .Sp
2395 This is undesirable since being
2396 upgraded or not should not affect a string's behavior.
2397 .RE
2398 .IP \(bu 4
2399 Source filters activated within \f(CW\*(C`eval\*(C'\fR leak out into whichever file
2400 scope is currently being compiled. To give an example with the CPAN module
2401 Semi::Semicolons:
2402 .Sp
2403 .Vb 2
2404 \& BEGIN { eval "use Semi::Semicolons; # not filtered" }
2405 \& # filtered here!
2406 .Ve
2407 .Sp
2408 \&\f(CW\*(C`evalbytes\*(C'\fR fixes that to work the way one would
2409 expect:
2410 .Sp
2411 .Vb 3
2412 \& use feature "evalbytes";
2413 \& BEGIN { evalbytes "use Semi::Semicolons; # filtered" }
2414 \& # not filtered
2415 .Ve
2416 .RE
2417 .RS 4
2418 .RE
2419 .RE
2420 .RS 4
2421 .Sp
2422 Problems can arise if the string expands a scalar containing a floating
2423 point number. That scalar can expand to letters, such as \f(CW"NaN"\fR or
2424 \&\f(CW"Infinity"\fR; or, within the scope of a \f(CW\*(C`use locale\*(C'\fR, the
2425 decimal point character may be something other than a dot (such as a
2426 comma). None of these are likely to parse as you are likely expecting.
2427 .Sp
2428 You should be especially careful to remember what's being looked at
2429 when:
2430 .Sp
2431 .Vb 2
2432 \& eval $x; # CASE 1
2433 \& eval "$x"; # CASE 2
2434 \&
2435 \& eval \*(Aq$x\*(Aq; # CASE 3
2436 \& eval { $x }; # CASE 4
2437 \&
2438 \& eval "\e$$x++"; # CASE 5
2439 \& $$x++; # CASE 6
2440 .Ve
2441 .Sp
2442 Cases 1 and 2 above behave identically: they run the code contained in
2443 the variable \f(CW$x\fR. (Although case 2 has misleading double quotes making
2444 the reader wonder what else might be happening (nothing is).) Cases 3
2445 and 4 likewise behave in the same way: they run the code \f(CW\*(Aq$x\*(Aq\fR, which
2446 does nothing but return the value of \f(CW$x\fR. (Case 4 is preferred for
2447 purely visual reasons, but it also has the advantage of compiling at
2448 compile-time instead of at run-time.) Case 5 is a place where
2449 normally you \fIwould\fR like to use double quotes, except that in this
2450 particular situation, you can just use symbolic references instead, as
2451 in case 6.
2452 .Sp
2453 An \f(CW\*(C`eval \*(Aq\*(Aq\*(C'\fR executed within a subroutine defined
2454 in the \f(CW\*(C`DB\*(C'\fR package doesn't see the usual
2455 surrounding lexical scope, but rather the scope of the first non-DB piece
2456 of code that called it. You don't normally need to worry about this unless
2457 you are writing a Perl debugger.
2458 .Sp
2459 The final semicolon, if any, may be omitted from the value of EXPR.
2460 .RE
2461 .IP "Block eval" 4
2462 .IX Item "Block eval"
2463 If the code to be executed doesn't vary, you may use the eval-BLOCK
2464 form to trap run-time errors without incurring the penalty of
2465 recompiling each time. The error, if any, is still returned in
2466 \&\f(CW$@\fR.
2467 Examples:
2468 .Sp
2469 .Vb 2
2470 \& # make divide\-by\-zero nonfatal
2471 \& eval { $answer = $x / $y; }; warn $@ if $@;
2472 \&
2473 \& # same thing, but less efficient
2474 \& eval \*(Aq$answer = $x / $y\*(Aq; warn $@ if $@;
2475 \&
2476 \& # a compile\-time error
2477 \& eval { $answer = }; # WRONG
2478 \&
2479 \& # a run\-time error
2480 \& eval \*(Aq$answer =\*(Aq; # sets $@
2481 .Ve
2482 .Sp
2483 If you want to trap errors when loading an XS module, some problems with
2484 the binary interface (such as Perl version skew) may be fatal even with
2485 \&\f(CW\*(C`eval\*(C'\fR unless \f(CW$ENV{PERL_DL_NONLAZY}\fR is set. See
2486 perlrun.
2487 .Sp
2488 Using the \f(CW\*(C`eval {}\*(C'\fR form as an exception trap in libraries does have some
2489 issues. Due to the current arguably broken state of \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks, you
2490 may wish not to trigger any \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks that user code may have installed.
2491 You can use the \f(CW\*(C`local $SIG{_\|_DIE_\|_}\*(C'\fR construct for this purpose,
2492 as this example shows:
2493 .Sp
2494 .Vb 3
2495 \& # a private exception trap for divide\-by\-zero
2496 \& eval { local $SIG{\*(Aq_\|_DIE_\|_\*(Aq}; $answer = $x / $y; };
2497 \& warn $@ if $@;
2498 .Ve
2499 .Sp
2500 This is especially significant, given that \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks can call
2501 \&\f(CW\*(C`die\*(C'\fR again, which has the effect of changing their error
2502 messages:
2503 .Sp
2504 .Vb 7
2505 \& # _\|_DIE_\|_ hooks may modify error messages
2506 \& {
2507 \& local $SIG{\*(Aq_\|_DIE_\|_\*(Aq} =
2508 \& sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x };
2509 \& eval { die "foo lives here" };
2510 \& print $@ if $@; # prints "bar lives here"
2511 \& }
2512 .Ve
2513 .Sp
2514 Because this promotes action at a distance, this counterintuitive behavior
2515 may be fixed in a future release.
2516 .Sp
2517 \&\f(CW\*(C`eval BLOCK\*(C'\fR does \fInot\fR count as a loop, so the loop control statements
2518 \&\f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, or
2519 \&\f(CW\*(C`redo\*(C'\fR cannot be used to leave or restart the block.
2520 .Sp
2521 The final semicolon, if any, may be omitted from within the BLOCK.
2522 .RE
2523 .RS 4
2524 .RE
2525 .IP "evalbytes EXPR" 4
2526 .IX Xref "evalbytes"
2527 .IX Item "evalbytes EXPR"
2528 .PD 0
2529 .IP evalbytes 4
2530 .IX Item "evalbytes"
2531 .PD
2532 This function is similar to a string eval, except it
2533 always parses its argument (or \f(CW$_\fR if EXPR is omitted)
2534 as a byte string. If the string contains any code points above 255, then
2535 it cannot be a byte string, and the \f(CW\*(C`evalbytes\*(C'\fR will fail with the error
2536 stored in \f(CW$@\fR.
2537 .Sp
2538 \&\f(CW\*(C`use utf8\*(C'\fR and \f(CW\*(C`no utf8\*(C'\fR within the string have their usual effect.
2539 .Sp
2540 Source filters activated within the evaluated code apply to the code
2541 itself.
2542 .Sp
2543 \&\f(CW\*(C`evalbytes\*(C'\fR is available starting in Perl v5.16. To
2544 access it, you must say \f(CW\*(C`CORE::evalbytes\*(C'\fR, but you can omit the
2545 \&\f(CW\*(C`CORE::\*(C'\fR if the
2546 \&\f(CW"evalbytes"\fR feature
2547 is enabled. This is enabled automatically with a \f(CW\*(C`use v5.16\*(C'\fR (or
2548 higher) declaration in the current scope.
2549 .IP "exec LIST" 4
2550 .IX Xref "exec execute"
2551 .IX Item "exec LIST"
2552 .PD 0
2553 .IP "exec PROGRAM LIST" 4
2554 .IX Item "exec PROGRAM LIST"
2555 .PD
2556 The \f(CW\*(C`exec\*(C'\fR function executes a system command \fIand never
2557 returns\fR; use \f(CW\*(C`system\*(C'\fR instead of \f(CW\*(C`exec\*(C'\fR
2558 if you want it to return. It fails and
2559 returns false only if the command does not exist \fIand\fR it is executed
2560 directly instead of via your system's command shell (see below).
2561 .Sp
2562 Since it's a common mistake to use \f(CW\*(C`exec\*(C'\fR instead of
2563 \&\f(CW\*(C`system\*(C'\fR, Perl warns you if \f(CW\*(C`exec\*(C'\fR is
2564 called in void context and if there is a following statement that isn't
2565 \&\f(CW\*(C`die\*(C'\fR, \f(CW\*(C`warn\*(C'\fR, or \f(CW\*(C`exit\*(C'\fR (if
2566 warnings are enabled\-\-but you always do that, right?). If you
2567 \&\fIreally\fR want to follow an \f(CW\*(C`exec\*(C'\fR with some other
2568 statement, you can use one of these styles to avoid the warning:
2569 .Sp
2570 .Vb 2
2571 \& exec (\*(Aqfoo\*(Aq) or print STDERR "couldn\*(Aqt exec foo: $!";
2572 \& { exec (\*(Aqfoo\*(Aq) }; print STDERR "couldn\*(Aqt exec foo: $!";
2573 .Ve
2574 .Sp
2575 If there is more than one argument in LIST, this calls \fBexecvp\fR\|(3) with the
2576 arguments in LIST. If there is only one element in LIST, the argument is
2577 checked for shell metacharacters, and if there are any, the entire
2578 argument is passed to the system's command shell for parsing (this is
2579 \&\f(CW\*(C`/bin/sh \-c\*(C'\fR on Unix platforms, but varies on other platforms). If
2580 there are no shell metacharacters in the argument, it is split into words
2581 and passed directly to \f(CW\*(C`execvp\*(C'\fR, which is more efficient. Examples:
2582 .Sp
2583 .Vb 2
2584 \& exec \*(Aq/bin/echo\*(Aq, \*(AqYour arguments are: \*(Aq, @ARGV;
2585 \& exec "sort $outfile | uniq";
2586 .Ve
2587 .Sp
2588 If you don't really want to execute the first argument, but want to lie
2589 to the program you are executing about its own name, you can specify
2590 the program you actually want to run as an "indirect object" (without a
2591 comma) in front of the LIST, as in \f(CW\*(C`exec PROGRAM LIST\*(C'\fR. (This always
2592 forces interpretation of the LIST as a multivalued list, even if there
2593 is only a single scalar in the list.) Example:
2594 .Sp
2595 .Vb 2
2596 \& my $shell = \*(Aq/bin/csh\*(Aq;
2597 \& exec $shell \*(Aq\-sh\*(Aq; # pretend it\*(Aqs a login shell
2598 .Ve
2599 .Sp
2600 or, more directly,
2601 .Sp
2602 .Vb 1
2603 \& exec {\*(Aq/bin/csh\*(Aq} \*(Aq\-sh\*(Aq; # pretend it\*(Aqs a login shell
2604 .Ve
2605 .Sp
2606 When the arguments get executed via the system shell, results are
2607 subject to its quirks and capabilities. See "`STRING`" in perlop
2608 for details.
2609 .Sp
2610 Using an indirect object with \f(CW\*(C`exec\*(C'\fR or
2611 \&\f(CW\*(C`system\*(C'\fR is also more secure. This usage (which also
2612 works fine with \f(CW\*(C`system\*(C'\fR) forces
2613 interpretation of the arguments as a multivalued list, even if the
2614 list had just one argument. That way you're safe from the shell
2615 expanding wildcards or splitting up words with whitespace in them.
2616 .Sp
2617 .Vb 1
2618 \& my @args = ( "echo surprise" );
2619 \&
2620 \& exec @args; # subject to shell escapes
2621 \& # if @args == 1
2622 \& exec { $args[0] } @args; # safe even with one\-arg list
2623 .Ve
2624 .Sp
2625 The first version, the one without the indirect object, ran the \fIecho\fR
2626 program, passing it \f(CW"surprise"\fR an argument. The second version didn't;
2627 it tried to run a program named \fI"echo surprise"\fR, didn't find it, and set
2628 \&\f(CW$?\fR to a non-zero value indicating failure.
2629 .Sp
2630 On Windows, only the \f(CW\*(C`exec PROGRAM LIST\*(C'\fR indirect object syntax will
2631 reliably avoid using the shell; \f(CW\*(C`exec LIST\*(C'\fR, even with more than one
2632 element, will fall back to the shell if the first spawn fails.
2633 .Sp
2634 Perl attempts to flush all files opened for output before the exec,
2635 but this may not be supported on some platforms (see perlport).
2636 To be safe, you may need to set \f(CW$|\fR
2637 (\f(CW$AUTOFLUSH\fR in English) or call the \f(CW\*(C`autoflush\*(C'\fR method of
2638 \&\f(CW\*(C`IO::Handle\*(C'\fR on any open handles to avoid lost
2639 output.
2640 .Sp
2641 Note that \f(CW\*(C`exec\*(C'\fR will not call your \f(CW\*(C`END\*(C'\fR blocks, nor
2642 will it invoke \f(CW\*(C`DESTROY\*(C'\fR methods on your objects.
2643 .Sp
2644 Portability issues: "exec" in perlport.
2645 .IP "exists EXPR" 4
2646 .IX Xref "exists autovivification"
2647 .IX Item "exists EXPR"
2648 Given an expression that specifies an element of a hash, returns true if the
2649 specified element in the hash has ever been initialized, even if the
2650 corresponding value is undefined.
2651 .Sp
2652 .Vb 3
2653 \& print "Exists\en" if exists $hash{$key};
2654 \& print "Defined\en" if defined $hash{$key};
2655 \& print "True\en" if $hash{$key};
2656 .Ve
2657 .Sp
2658 exists may also be called on array elements, but its behavior is much less
2659 obvious and is strongly tied to the use of \f(CW\*(C`delete\*(C'\fR on
2660 arrays.
2661 .Sp
2662 \&\fBWARNING:\fR Calling \f(CW\*(C`exists\*(C'\fR on array values is
2663 strongly discouraged. The
2664 notion of deleting or checking the existence of Perl array elements is not
2665 conceptually coherent, and can lead to surprising behavior.
2666 .Sp
2667 .Vb 3
2668 \& print "Exists\en" if exists $array[$index];
2669 \& print "Defined\en" if defined $array[$index];
2670 \& print "True\en" if $array[$index];
2671 .Ve
2672 .Sp
2673 A hash or array element can be true only if it's defined and defined only if
2674 it exists, but the reverse doesn't necessarily hold true.
2675 .Sp
2676 Given an expression that specifies the name of a subroutine,
2677 returns true if the specified subroutine has ever been declared, even
2678 if it is undefined. Mentioning a subroutine name for exists or defined
2679 does not count as declaring it. Note that a subroutine that does not
2680 exist may still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR
2681 method that makes it spring into existence the first time that it is
2682 called; see perlsub.
2683 .Sp
2684 .Vb 2
2685 \& print "Exists\en" if exists &subroutine;
2686 \& print "Defined\en" if defined &subroutine;
2687 .Ve
2688 .Sp
2689 Note that the EXPR can be arbitrarily complicated as long as the final
2690 operation is a hash or array key lookup or subroutine name:
2691 .Sp
2692 .Vb 2
2693 \& if (exists $ref\->{A}\->{B}\->{$key}) { }
2694 \& if (exists $hash{A}{B}{$key}) { }
2695 \&
2696 \& if (exists $ref\->{A}\->{B}\->[$ix]) { }
2697 \& if (exists $hash{A}{B}[$ix]) { }
2698 \&
2699 \& if (exists &{$ref\->{A}{B}{$key}}) { }
2700 .Ve
2701 .Sp
2702 Although the most deeply nested array or hash element will not spring into
2703 existence just because its existence was tested, any intervening ones will.
2704 Thus \f(CW\*(C`$ref\->{"A"}\*(C'\fR and \f(CW\*(C`$ref\->{"A"}\->{"B"}\*(C'\fR will spring
2705 into existence due to the existence test for the \f(CW$key\fR element above.
2706 This happens anywhere the arrow operator is used, including even here:
2707 .Sp
2708 .Vb 3
2709 \& undef $ref;
2710 \& if (exists $ref\->{"Some key"}) { }
2711 \& print $ref; # prints HASH(0x80d3d5c)
2712 .Ve
2713 .Sp
2714 Use of a subroutine call, rather than a subroutine name, as an argument
2715 to \f(CW\*(C`exists\*(C'\fR is an error.
2716 .Sp
2717 .Vb 2
2718 \& exists &sub; # OK
2719 \& exists &sub(); # Error
2720 .Ve
2721 .IP "exit EXPR" 4
2722 .IX Xref "exit terminate abort"
2723 .IX Item "exit EXPR"
2724 .PD 0
2725 .IP exit 4
2726 .IX Item "exit"
2727 .PD
2728 Evaluates EXPR and exits immediately with that value. Example:
2729 .Sp
2730 .Vb 2
2731 \& my $ans = <STDIN>;
2732 \& exit 0 if $ans =~ /^[Xx]/;
2733 .Ve
2734 .Sp
2735 See also \f(CW\*(C`die\*(C'\fR. If EXPR is omitted, exits with \f(CW0\fR
2736 status. The only
2737 universally recognized values for EXPR are \f(CW0\fR for success and \f(CW1\fR
2738 for error; other values are subject to interpretation depending on the
2739 environment in which the Perl program is running. For example, exiting
2740 69 (EX_UNAVAILABLE) from a \fIsendmail\fR incoming-mail filter will cause
2741 the mailer to return the item undelivered, but that's not true everywhere.
2742 .Sp
2743 Don't use \f(CW\*(C`exit\*(C'\fR to abort a subroutine if there's any
2744 chance that someone might want to trap whatever error happened. Use
2745 \&\f(CW\*(C`die\*(C'\fR instead, which can be trapped by an
2746 \&\f(CW\*(C`eval\*(C'\fR.
2747 .Sp
2748 The \f(CW\*(C`exit\*(C'\fR function does not always exit immediately. It
2749 calls any defined \f(CW\*(C`END\*(C'\fR routines first, but these \f(CW\*(C`END\*(C'\fR routines may
2750 not themselves abort the exit. Likewise any object destructors that
2751 need to be called are called before the real exit. \f(CW\*(C`END\*(C'\fR routines and
2752 destructors can change the exit status by modifying \f(CW$?\fR.
2753 If this is a problem, you can call
2754 \&\f(CWPOSIX::_exit($status)\fR to avoid \f(CW\*(C`END\*(C'\fR and destructor
2755 processing. See perlmod for details.
2756 .Sp
2757 Portability issues: "exit" in perlport.
2758 .IP "exp EXPR" 4
2759 .IX Xref "exp exponential antilog antilogarithm e"
2760 .IX Item "exp EXPR"
2761 .PD 0
2762 .IP exp 4
2763 .IX Item "exp"
2764 .PD
2765 Returns \fIe\fR (the natural logarithm base) to the power of EXPR.
2766 If EXPR is omitted, gives \f(CWexp($_)\fR.
2767 .IP "fc EXPR" 4
2768 .IX Xref "fc foldcase casefold fold-case case-fold"
2769 .IX Item "fc EXPR"
2770 .PD 0
2771 .IP fc 4
2772 .IX Item "fc"
2773 .PD
2774 Returns the casefolded version of EXPR. This is the internal function
2775 implementing the \f(CW\*(C`\eF\*(C'\fR escape in double-quoted strings.
2776 .Sp
2777 Casefolding is the process of mapping strings to a form where case
2778 differences are erased; comparing two strings in their casefolded
2779 form is effectively a way of asking if two strings are equal,
2780 regardless of case.
2781 .Sp
2782 Roughly, if you ever found yourself writing this
2783 .Sp
2784 .Vb 5
2785 \& lc($this) eq lc($that) # Wrong!
2786 \& # or
2787 \& uc($this) eq uc($that) # Also wrong!
2788 \& # or
2789 \& $this =~ /^\eQ$that\eE\ez/i # Right!
2790 .Ve
2791 .Sp
2792 Now you can write
2793 .Sp
2794 .Vb 1
2795 \& fc($this) eq fc($that)
2796 .Ve
2797 .Sp
2798 And get the correct results.
2799 .Sp
2800 Perl only implements the full form of casefolding, but you can access
2801 the simple folds using "\fBcasefold()\fR" in Unicode::UCD and
2802 "\fBprop_invmap()\fR" in Unicode::UCD.
2803 For further information on casefolding, refer to
2804 the Unicode Standard, specifically sections 3.13 \f(CW\*(C`Default Case Operations\*(C'\fR,
2805 4.2 \f(CW\*(C`Case\-Normative\*(C'\fR, and 5.18 \f(CW\*(C`Case Mappings\*(C'\fR,
2806 available at <https://www.unicode.org/versions/latest/>, as well as the
2807 Case Charts available at <https://www.unicode.org/charts/case/>.
2808 .Sp
2809 If EXPR is omitted, uses \f(CW$_\fR.
2810 .Sp
2811 This function behaves the same way under various pragmas, such as within
2812 \&\f(CW"use\ feature\ \*(Aqunicode_strings"\fR,
2813 as \f(CW\*(C`lc\*(C'\fR does, with the single exception of
2814 \&\f(CW\*(C`fc\*(C'\fR of \fILATIN CAPITAL LETTER SHARP S\fR (U+1E9E) within the
2815 scope of \f(CW\*(C`use\ locale\*(C'\fR. The foldcase of this character
2816 would normally be \f(CW"ss"\fR, but as explained in the \f(CW\*(C`lc\*(C'\fR
2817 section, case
2818 changes that cross the 255/256 boundary are problematic under locales,
2819 and are hence prohibited. Therefore, this function under locale returns
2820 instead the string \f(CW"\ex{17F}\ex{17F}"\fR, which is the \fILATIN SMALL LETTER
2821 LONG S\fR. Since that character itself folds to \f(CW"s"\fR, the string of two
2822 of them together should be equivalent to a single U+1E9E when foldcased.
2823 .Sp
2824 While the Unicode Standard defines two additional forms of casefolding,
2825 one for Turkic languages and one that never maps one character into multiple
2826 characters, these are not provided by the Perl core. However, the CPAN module
2827 \&\f(CW\*(C`Unicode::Casing\*(C'\fR may be used to provide an implementation.
2828 .Sp
2829 \&\f(CW\*(C`fc\*(C'\fR is available only if the
2830 \&\f(CW"fc"\fR feature is enabled or if it is
2831 prefixed with \f(CW\*(C`CORE::\*(C'\fR. The
2832 \&\f(CW"fc"\fR feature is enabled automatically
2833 with a \f(CW\*(C`use v5.16\*(C'\fR (or higher) declaration in the current scope.
2834 .IP "fcntl FILEHANDLE,FUNCTION,SCALAR" 4
2835 .IX Xref "fcntl"
2836 .IX Item "fcntl FILEHANDLE,FUNCTION,SCALAR"
2837 Implements the \fBfcntl\fR\|(2) function. You'll probably have to say
2838 .Sp
2839 .Vb 1
2840 \& use Fcntl;
2841 .Ve
2842 .Sp
2843 first to get the correct constant definitions. Argument processing and
2844 value returned work just like \f(CW\*(C`ioctl\*(C'\fR below. For example:
2845 .Sp
2846 .Vb 3
2847 \& use Fcntl;
2848 \& my $flags = fcntl($filehandle, F_GETFL, 0)
2849 \& or die "Can\*(Aqt fcntl F_GETFL: $!";
2850 .Ve
2851 .Sp
2852 You don't have to check for \f(CW\*(C`defined\*(C'\fR on the return
2853 from \f(CW\*(C`fcntl\*(C'\fR. Like
2854 \&\f(CW\*(C`ioctl\*(C'\fR, it maps a \f(CW0\fR return
2855 from the system call into \f(CW"0 but true"\fR in Perl. This string is true
2856 in boolean context and \f(CW0\fR in numeric context. It is also exempt from
2857 the normal
2858 \&\f(CW\*(C`Argument "..." isn\*(Aqt numeric\*(C'\fR
2859 warnings on improper numeric conversions.
2860 .Sp
2861 Note that \f(CW\*(C`fcntl\*(C'\fR raises an
2862 exception if used on a machine that doesn't implement \fBfcntl\fR\|(2). See
2863 the Fcntl module or your \fBfcntl\fR\|(2) manpage to learn what functions
2864 are available on your system.
2865 .Sp
2866 Here's an example of setting a filehandle named \f(CW$REMOTE\fR to be
2867 non-blocking at the system level. You'll have to negotiate
2868 \&\f(CW$|\fR on your own, though.
2869 .Sp
2870 .Vb 1
2871 \& use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
2872 \&
2873 \& my $flags = fcntl($REMOTE, F_GETFL, 0)
2874 \& or die "Can\*(Aqt get flags for the socket: $!\en";
2875 \&
2876 \& fcntl($REMOTE, F_SETFL, $flags | O_NONBLOCK)
2877 \& or die "Can\*(Aqt set flags for the socket: $!\en";
2878 .Ve
2879 .Sp
2880 Portability issues: "fcntl" in perlport.
2881 .IP _\|_FILE_\|_ 4
2882 .IX Xref "__FILE__"
2883 .IX Item "__FILE__"
2884 A special token that returns the name of the file in which it occurs.
2885 It can be altered by the mechanism described at
2886 "Plain Old Comments (Not!)" in perlsyn.
2887 .IP "field VARNAME" 4
2888 .IX Xref "field"
2889 .IX Item "field VARNAME"
2890 Declares a new field variable within the current class. Methods and
2891 \&\f(CW\*(C`ADJUST\*(C'\fR blocks of the class will have access to this variable as if it
2892 was a lexical in scope at that point.
2893 .IP "fileno FILEHANDLE" 4
2894 .IX Xref "fileno"
2895 .IX Item "fileno FILEHANDLE"
2896 .PD 0
2897 .IP "fileno DIRHANDLE" 4
2898 .IX Item "fileno DIRHANDLE"
2899 .PD
2900 Returns the file descriptor for a filehandle or directory handle,
2901 or undefined if the
2902 filehandle is not open. If there is no real file descriptor at the OS
2903 level, as can happen with filehandles connected to memory objects via
2904 \&\f(CW\*(C`open\*(C'\fR with a reference for the third
2905 argument, \-1 is returned.
2906 .Sp
2907 This is mainly useful for constructing bitmaps for
2908 \&\f(CW\*(C`select\*(C'\fR and low-level POSIX
2909 tty-handling operations.
2910 If FILEHANDLE is an expression, the value is taken as an indirect
2911 filehandle, generally its name.
2912 .Sp
2913 You can use this to find out whether two handles refer to the
2914 same underlying descriptor:
2915 .Sp
2916 .Vb 9
2917 \& if (fileno($this) != \-1 && fileno($this) == fileno($that)) {
2918 \& print "\e$this and \e$that are dups\en";
2919 \& } elsif (fileno($this) != \-1 && fileno($that) != \-1) {
2920 \& print "\e$this and \e$that have different " .
2921 \& "underlying file descriptors\en";
2922 \& } else {
2923 \& print "At least one of \e$this and \e$that does " .
2924 \& "not have a real file descriptor\en";
2925 \& }
2926 .Ve
2927 .Sp
2928 The behavior of \f(CW\*(C`fileno\*(C'\fR on a directory handle
2929 depends on the operating system. On a system with \fBdirfd\fR\|(3) or
2930 similar, \f(CW\*(C`fileno\*(C'\fR on a directory
2931 handle returns the underlying file descriptor associated with the
2932 handle; on systems with no such support, it returns the undefined value,
2933 and sets \f(CW$!\fR (errno).
2934 .IP "flock FILEHANDLE,OPERATION" 4
2935 .IX Xref "flock lock locking"
2936 .IX Item "flock FILEHANDLE,OPERATION"
2937 Calls \fBflock\fR\|(2), or an emulation of it, on FILEHANDLE. Returns true
2938 for success, false on failure. Produces a fatal error if used on a
2939 machine that doesn't implement \fBflock\fR\|(2), \fBfcntl\fR\|(2) locking, or
2940 \&\fBlockf\fR\|(3). \f(CW\*(C`flock\*(C'\fR is Perl's portable
2941 file-locking interface, although it locks entire files only, not
2942 records.
2943 .Sp
2944 Two potentially non-obvious but traditional \f(CW\*(C`flock\*(C'\fR semantics are
2945 that it waits indefinitely until the lock is granted, and that its locks
2946 are \fBmerely advisory\fR. Such discretionary locks are more flexible, but
2947 offer fewer guarantees. This means that programs that do not also use
2948 \&\f(CW\*(C`flock\*(C'\fR may modify files locked with
2949 \&\f(CW\*(C`flock\*(C'\fR. See perlport,
2950 your port's specific documentation, and your system-specific local manpages
2951 for details. It's best to assume traditional behavior if you're writing
2952 portable programs. (But if you're not, you should as always feel perfectly
2953 free to write for your own system's idiosyncrasies (sometimes called
2954 "features"). Slavish adherence to portability concerns shouldn't get
2955 in the way of your getting your job done.)
2956 .Sp
2957 OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly combined with
2958 LOCK_NB. These constants are traditionally valued 1, 2, 8 and 4, but
2959 you can use the symbolic names if you import them from the Fcntl module,
2960 either individually, or as a group using the \f(CW\*(C`:flock\*(C'\fR tag. LOCK_SH
2961 requests a shared lock, LOCK_EX requests an exclusive lock, and LOCK_UN
2962 releases a previously requested lock. If LOCK_NB is bitwise-or'ed with
2963 LOCK_SH or LOCK_EX, then \f(CW\*(C`flock\*(C'\fR returns
2964 immediately rather than blocking waiting for the lock; check the return
2965 status to see if you got it.
2966 .Sp
2967 To avoid the possibility of miscoordination, Perl now flushes FILEHANDLE
2968 before locking or unlocking it.
2969 .Sp
2970 Note that the emulation built with \fBlockf\fR\|(3) doesn't provide shared
2971 locks, and it requires that FILEHANDLE be open with write intent. These
2972 are the semantics that \fBlockf\fR\|(3) implements. Most if not all systems
2973 implement \fBlockf\fR\|(3) in terms of \fBfcntl\fR\|(2) locking, though, so the
2974 differing semantics shouldn't bite too many people.
2975 .Sp
2976 Note that the \fBfcntl\fR\|(2) emulation of \fBflock\fR\|(3) requires that FILEHANDLE
2977 be open with read intent to use LOCK_SH and requires that it be open
2978 with write intent to use LOCK_EX.
2979 .Sp
2980 Note also that some versions of \f(CW\*(C`flock\*(C'\fR
2981 cannot lock things over the network; you would need to use the more
2982 system-specific \f(CW\*(C`fcntl\*(C'\fR for
2983 that. If you like you can force Perl to ignore your system's \fBflock\fR\|(2)
2984 function, and so provide its own \fBfcntl\fR\|(2)\-based emulation, by passing
2985 the switch \f(CW\*(C`\-Ud_flock\*(C'\fR to the \fIConfigure\fR program when you configure
2986 and build a new Perl.
2987 .Sp
2988 Here's a mailbox appender for BSD systems.
2989 .Sp
2990 .Vb 2
2991 \& # import LOCK_* and SEEK_END constants
2992 \& use Fcntl qw(:flock SEEK_END);
2993 \&
2994 \& sub lock {
2995 \& my ($fh) = @_;
2996 \& flock($fh, LOCK_EX) or die "Cannot lock mailbox \- $!\en";
2997 \& # and, in case we\*(Aqre running on a very old UNIX
2998 \& # variant without the modern O_APPEND semantics...
2999 \& seek($fh, 0, SEEK_END) or die "Cannot seek \- $!\en";
3000 \& }
3001 \&
3002 \& sub unlock {
3003 \& my ($fh) = @_;
3004 \& flock($fh, LOCK_UN) or die "Cannot unlock mailbox \- $!\en";
3005 \& }
3006 \&
3007 \& open(my $mbox, ">>", "/usr/spool/mail/$ENV{\*(AqUSER\*(Aq}")
3008 \& or die "Can\*(Aqt open mailbox: $!";
3009 \&
3010 \& lock($mbox);
3011 \& print $mbox $msg,"\en\en";
3012 \& unlock($mbox);
3013 .Ve
3014 .Sp
3015 On systems that support a real \fBflock\fR\|(2), locks are inherited across
3016 \&\f(CW\*(C`fork\*(C'\fR calls, whereas those that must resort to the more
3017 capricious \fBfcntl\fR\|(2) function lose their locks, making it seriously
3018 harder to write servers.
3019 .Sp
3020 See also DB_File for other \f(CW\*(C`flock\*(C'\fR
3021 examples.
3022 .Sp
3023 Portability issues: "flock" in perlport.
3024 .IP fork 4
3025 .IX Xref "fork child parent"
3026 .IX Item "fork"
3027 Does a \fBfork\fR\|(2) system call to create a new process running the
3028 same program at the same point. It returns the child pid to the
3029 parent process, \f(CW0\fR to the child process, or \f(CW\*(C`undef\*(C'\fR if
3030 the fork is
3031 unsuccessful. File descriptors (and sometimes locks on those descriptors)
3032 are shared, while everything else is copied. On most systems supporting
3033 \&\fBfork\fR\|(2), great care has gone into making it extremely efficient (for
3034 example, using copy-on-write technology on data pages), making it the
3035 dominant paradigm for multitasking over the last few decades.
3036 .Sp
3037 Perl attempts to flush all files opened for output before forking the
3038 child process, but this may not be supported on some platforms (see
3039 perlport). To be safe, you may need to set
3040 \&\f(CW$|\fR (\f(CW$AUTOFLUSH\fR in English) or
3041 call the \f(CW\*(C`autoflush\*(C'\fR method of \f(CW\*(C`IO::Handle\*(C'\fR on
3042 any open handles to avoid duplicate output.
3043 .Sp
3044 If you \f(CW\*(C`fork\*(C'\fR without ever waiting on your children, you will
3045 accumulate zombies. On some systems, you can avoid this by setting
3046 \&\f(CW$SIG{CHLD}\fR to \f(CW"IGNORE"\fR. See also perlipc for
3047 more examples of forking and reaping moribund children.
3048 .Sp
3049 Note that if your forked child inherits system file descriptors like
3050 STDIN and STDOUT that are actually connected by a pipe or socket, even
3051 if you exit, then the remote server (such as, say, a CGI script or a
3052 backgrounded job launched from a remote shell) won't think you're done.
3053 You should reopen those to \fI/dev/null\fR if it's any issue.
3054 .Sp
3055 On some platforms such as Windows, where the \fBfork\fR\|(2) system call is
3056 not available, Perl can be built to emulate \f(CW\*(C`fork\*(C'\fR in the Perl
3057 interpreter. The emulation is designed, at the level of the Perl
3058 program, to be as compatible as possible with the "Unix" \fBfork\fR\|(2).
3059 However it has limitations that have to be considered in code intended
3060 to be portable. See perlfork for more details.
3061 .Sp
3062 Portability issues: "fork" in perlport.
3063 .IP format 4
3064 .IX Xref "format"
3065 .IX Item "format"
3066 Declare a picture format for use by the \f(CW\*(C`write\*(C'\fR
3067 function. For example:
3068 .Sp
3069 .Vb 4
3070 \& format Something =
3071 \& Test: @<<<<<<<< @||||| @>>>>>
3072 \& $str, $%, \*(Aq$\*(Aq . int($num)
3073 \& .
3074 \&
3075 \& $str = "widget";
3076 \& $num = $cost/$quantity;
3077 \& $~ = \*(AqSomething\*(Aq;
3078 \& write;
3079 .Ve
3080 .Sp
3081 See perlform for many details and examples.
3082 .IP "formline PICTURE,LIST" 4
3083 .IX Xref "formline"
3084 .IX Item "formline PICTURE,LIST"
3085 This is an internal function used by \f(CW\*(C`format\*(C'\fRs, though you
3086 may call it, too. It formats (see perlform) a list of values
3087 according to the contents of PICTURE, placing the output into the format
3088 output accumulator, \f(CW$^A\fR (or \f(CW$ACCUMULATOR\fR in
3089 English). Eventually, when a \f(CW\*(C`write\*(C'\fR is done,
3090 the contents of \f(CW$^A\fR are written to some filehandle.
3091 You could also read \f(CW$^A\fR and then set
3092 \&\f(CW$^A\fR back to \f(CW""\fR. Note that a format typically does
3093 one \f(CW\*(C`formline\*(C'\fR per line of form, but the
3094 \&\f(CW\*(C`formline\*(C'\fR function itself doesn't care how
3095 many newlines are embedded in the PICTURE. This means that the \f(CW\*(C`~\*(C'\fR and
3096 \&\f(CW\*(C`~~\*(C'\fR tokens treat the entire PICTURE as a single line. You may
3097 therefore need to use multiple formlines to implement a single record
3098 format, just like the \f(CW\*(C`format\*(C'\fR compiler.
3099 .Sp
3100 Be careful if you put double quotes around the picture, because an \f(CW\*(C`@\*(C'\fR
3101 character may be taken to mean the beginning of an array name.
3102 \&\f(CW\*(C`formline\*(C'\fR always returns true. See
3103 perlform for other examples.
3104 .Sp
3105 If you are trying to use this instead of \f(CW\*(C`write\*(C'\fR
3106 to capture the output, you may find it easier to open a filehandle to a
3107 scalar (\f(CW\*(C`open my $fh, ">", \e$output\*(C'\fR) and write to that instead.
3108 .IP "getc FILEHANDLE" 4
3109 .IX Xref "getc getchar character file, read"
3110 .IX Item "getc FILEHANDLE"
3111 .PD 0
3112 .IP getc 4
3113 .IX Item "getc"
3114 .PD
3115 Returns the next character from the input file attached to FILEHANDLE,
3116 or the undefined value at end of file or if there was an error (in
3117 the latter case \f(CW$!\fR is set). If FILEHANDLE is omitted,
3118 reads from
3119 STDIN. This is not particularly efficient. However, it cannot be
3120 used by itself to fetch single characters without waiting for the user
3121 to hit enter. For that, try something more like:
3122 .Sp
3123 .Vb 6
3124 \& if ($BSD_STYLE) {
3125 \& system "stty cbreak </dev/tty >/dev/tty 2>&1";
3126 \& }
3127 \& else {
3128 \& system "stty", \*(Aq\-icanon\*(Aq, \*(Aqeol\*(Aq, "\e001";
3129 \& }
3130 \&
3131 \& my $key = getc(STDIN);
3132 \&
3133 \& if ($BSD_STYLE) {
3134 \& system "stty \-cbreak </dev/tty >/dev/tty 2>&1";
3135 \& }
3136 \& else {
3137 \& system \*(Aqstty\*(Aq, \*(Aqicanon\*(Aq, \*(Aqeol\*(Aq, \*(Aq^@\*(Aq; # ASCII NUL
3138 \& }
3139 \& print "\en";
3140 .Ve
3141 .Sp
3142 Determination of whether \f(CW$BSD_STYLE\fR should be set is left as an
3143 exercise to the reader.
3144 .Sp
3145 The \f(CW\*(C`POSIX::getattr\*(C'\fR function can do this more
3146 portably on systems purporting POSIX compliance. See also the
3147 \&\f(CW\*(C`Term::ReadKey\*(C'\fR module on CPAN.
3148 .IP getlogin 4
3149 .IX Xref "getlogin login"
3150 .IX Item "getlogin"
3151 This implements the C library function of the same name, which on most
3152 systems returns the current login from \fI/etc/utmp\fR, if any. If it
3153 returns the empty string, use \f(CW\*(C`getpwuid\*(C'\fR.
3154 .Sp
3155 .Vb 1
3156 \& my $login = getlogin || getpwuid($<) || "Kilroy";
3157 .Ve
3158 .Sp
3159 Do not consider \f(CW\*(C`getlogin\*(C'\fR for authentication: it is not
3160 as secure as \f(CW\*(C`getpwuid\*(C'\fR.
3161 .Sp
3162 Portability issues: "getlogin" in perlport.
3163 .IP "getpeername SOCKET" 4
3164 .IX Xref "getpeername peer"
3165 .IX Item "getpeername SOCKET"
3166 Returns the packed sockaddr address of the other end of the SOCKET
3167 connection.
3168 .Sp
3169 .Vb 5
3170 \& use Socket;
3171 \& my $hersockaddr = getpeername($sock);
3172 \& my ($port, $iaddr) = sockaddr_in($hersockaddr);
3173 \& my $herhostname = gethostbyaddr($iaddr, AF_INET);
3174 \& my $herstraddr = inet_ntoa($iaddr);
3175 .Ve
3176 .IP "getpgrp PID" 4
3177 .IX Xref "getpgrp group"
3178 .IX Item "getpgrp PID"
3179 Returns the current process group for the specified PID. Use
3180 a PID of \f(CW0\fR to get the current process group for the
3181 current process. Will raise an exception if used on a machine that
3182 doesn't implement \fBgetpgrp\fR\|(2). If PID is omitted, returns the process
3183 group of the current process.
3184 .Sp
3185 Some very old machines may not support \f(CW\*(C`PID != 0\*(C'\fR and will throw an
3186 exception if \f(CW\*(C`PID != 0\*(C'\fR.
3187 .Sp
3188 Portability issues: "getpgrp" in perlport.
3189 .IP getppid 4
3190 .IX Xref "getppid parent pid"
3191 .IX Item "getppid"
3192 Returns the process id of the parent process.
3193 .Sp
3194 Note for Linux users: Between v5.8.1 and v5.16.0 Perl would work
3195 around non-POSIX thread semantics the minority of Linux systems (and
3196 Debian GNU/kFreeBSD systems) that used LinuxThreads, this emulation
3197 has since been removed. See the documentation for $$ for
3198 details.
3199 .Sp
3200 Portability issues: "getppid" in perlport.
3201 .IP "getpriority WHICH,WHO" 4
3202 .IX Xref "getpriority priority nice"
3203 .IX Item "getpriority WHICH,WHO"
3204 Returns the current priority for a process, a process group, or a user.
3205 (See \fBgetpriority\fR\|(2).) Will raise a fatal exception if used on a
3206 machine that doesn't implement \fBgetpriority\fR\|(2).
3207 .Sp
3208 \&\f(CW\*(C`WHICH\*(C'\fR can be any of \f(CW\*(C`PRIO_PROCESS\*(C'\fR, \f(CW\*(C`PRIO_PGRP\*(C'\fR or \f(CW\*(C`PRIO_USER\*(C'\fR
3209 imported from "RESOURCE CONSTANTS" in POSIX.
3210 .Sp
3211 Portability issues: "getpriority" in perlport.
3212 .IP "getpwnam NAME" 4
3213 .IX Xref "getpwnam getgrnam gethostbyname getnetbyname getprotobyname getpwuid getgrgid getservbyname gethostbyaddr getnetbyaddr getprotobynumber getservbyport getpwent getgrent gethostent getnetent getprotoent getservent setpwent setgrent sethostent setnetent setprotoent setservent endpwent endgrent endhostent endnetent endprotoent endservent"
3214 .IX Item "getpwnam NAME"
3215 .PD 0
3216 .IP "getgrnam NAME" 4
3217 .IX Item "getgrnam NAME"
3218 .IP "gethostbyname NAME" 4
3219 .IX Item "gethostbyname NAME"
3220 .IP "getnetbyname NAME" 4
3221 .IX Item "getnetbyname NAME"
3222 .IP "getprotobyname NAME" 4
3223 .IX Item "getprotobyname NAME"
3224 .IP "getpwuid UID" 4
3225 .IX Item "getpwuid UID"
3226 .IP "getgrgid GID" 4
3227 .IX Item "getgrgid GID"
3228 .IP "getservbyname NAME,PROTO" 4
3229 .IX Item "getservbyname NAME,PROTO"
3230 .IP "gethostbyaddr ADDR,ADDRTYPE" 4
3231 .IX Item "gethostbyaddr ADDR,ADDRTYPE"
3232 .IP "getnetbyaddr ADDR,ADDRTYPE" 4
3233 .IX Item "getnetbyaddr ADDR,ADDRTYPE"
3234 .IP "getprotobynumber NUMBER" 4
3235 .IX Item "getprotobynumber NUMBER"
3236 .IP "getservbyport PORT,PROTO" 4
3237 .IX Item "getservbyport PORT,PROTO"
3238 .IP getpwent 4
3239 .IX Item "getpwent"
3240 .IP getgrent 4
3241 .IX Item "getgrent"
3242 .IP gethostent 4
3243 .IX Item "gethostent"
3244 .IP getnetent 4
3245 .IX Item "getnetent"
3246 .IP getprotoent 4
3247 .IX Item "getprotoent"
3248 .IP getservent 4
3249 .IX Item "getservent"
3250 .IP setpwent 4
3251 .IX Item "setpwent"
3252 .IP setgrent 4
3253 .IX Item "setgrent"
3254 .IP "sethostent STAYOPEN" 4
3255 .IX Item "sethostent STAYOPEN"
3256 .IP "setnetent STAYOPEN" 4
3257 .IX Item "setnetent STAYOPEN"
3258 .IP "setprotoent STAYOPEN" 4
3259 .IX Item "setprotoent STAYOPEN"
3260 .IP "setservent STAYOPEN" 4
3261 .IX Item "setservent STAYOPEN"
3262 .IP endpwent 4
3263 .IX Item "endpwent"
3264 .IP endgrent 4
3265 .IX Item "endgrent"
3266 .IP endhostent 4
3267 .IX Item "endhostent"
3268 .IP endnetent 4
3269 .IX Item "endnetent"
3270 .IP endprotoent 4
3271 .IX Item "endprotoent"
3272 .IP endservent 4
3273 .IX Item "endservent"
3274 .PD
3275 These routines are the same as their counterparts in the
3276 system C library. In list context, the return values from the
3277 various get routines are as follows:
3278 .Sp
3279 .Vb 9
3280 \& # 0 1 2 3 4
3281 \& my ( $name, $passwd, $gid, $members ) = getgr*
3282 \& my ( $name, $aliases, $addrtype, $net ) = getnet*
3283 \& my ( $name, $aliases, $port, $proto ) = getserv*
3284 \& my ( $name, $aliases, $proto ) = getproto*
3285 \& my ( $name, $aliases, $addrtype, $length, @addrs ) = gethost*
3286 \& my ( $name, $passwd, $uid, $gid, $quota,
3287 \& $comment, $gcos, $dir, $shell, $expire ) = getpw*
3288 \& # 5 6 7 8 9
3289 .Ve
3290 .Sp
3291 (If the entry doesn't exist, the return value is a single meaningless true
3292 value.)
3293 .Sp
3294 The exact meaning of the \f(CW$gcos\fR field varies but it usually contains
3295 the real name of the user (as opposed to the login name) and other
3296 information pertaining to the user. Beware, however, that in many
3297 system users are able to change this information and therefore it
3298 cannot be trusted and therefore the \f(CW$gcos\fR is tainted (see
3299 perlsec). The \f(CW$passwd\fR and \f(CW$shell\fR, user's encrypted password and
3300 login shell, are also tainted, for the same reason.
3301 .Sp
3302 In scalar context, you get the name, unless the function was a
3303 lookup by name, in which case you get the other thing, whatever it is.
3304 (If the entry doesn't exist you get the undefined value.) For example:
3305 .Sp
3306 .Vb 7
3307 \& my $uid = getpwnam($name);
3308 \& my $name = getpwuid($num);
3309 \& my $name = getpwent();
3310 \& my $gid = getgrnam($name);
3311 \& my $name = getgrgid($num);
3312 \& my $name = getgrent();
3313 \& # etc.
3314 .Ve
3315 .Sp
3316 In \fIgetpw*()\fR the fields \f(CW$quota\fR, \f(CW$comment\fR, and \f(CW$expire\fR are special
3317 in that they are unsupported on many systems. If the
3318 \&\f(CW$quota\fR is unsupported, it is an empty scalar. If it is supported, it
3319 usually encodes the disk quota. If the \f(CW$comment\fR field is unsupported,
3320 it is an empty scalar. If it is supported it usually encodes some
3321 administrative comment about the user. In some systems the \f(CW$quota\fR
3322 field may be \f(CW$change\fR or \f(CW$age\fR, fields that have to do with password
3323 aging. In some systems the \f(CW$comment\fR field may be \f(CW$class\fR. The \f(CW$expire\fR
3324 field, if present, encodes the expiration period of the account or the
3325 password. For the availability and the exact meaning of these fields
3326 in your system, please consult \fBgetpwnam\fR\|(3) and your system's
3327 \&\fIpwd.h\fR file. You can also find out from within Perl what your
3328 \&\f(CW$quota\fR and \f(CW$comment\fR fields mean and whether you have the \f(CW$expire\fR field
3329 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,
3330 \&\f(CW\*(C`d_pwchange\*(C'\fR, \f(CW\*(C`d_pwcomment\*(C'\fR, and \f(CW\*(C`d_pwexpire\*(C'\fR. Shadow password
3331 files are supported only if your vendor has implemented them in the
3332 intuitive fashion that calling the regular C library routines gets the
3333 shadow versions if you're running under privilege or if there exists
3334 the \fBshadow\fR\|(3) functions as found in System V (this includes Solaris
3335 and Linux). Those systems that implement a proprietary shadow password
3336 facility are unlikely to be supported.
3337 .Sp
3338 The \f(CW$members\fR value returned by \fIgetgr*()\fR is a space-separated list of
3339 the login names of the members of the group.
3340 .Sp
3341 For the \fIgethost*()\fR functions, if the \f(CW\*(C`h_errno\*(C'\fR variable is supported in
3342 C, it will be returned to you via \f(CW$?\fR if the function
3343 call fails. The
3344 \&\f(CW@addrs\fR value returned by a successful call is a list of raw
3345 addresses returned by the corresponding library call. In the
3346 Internet domain, each address is four bytes long; you can unpack it
3347 by saying something like:
3348 .Sp
3349 .Vb 1
3350 \& my ($w,$x,$y,$z) = unpack(\*(AqW4\*(Aq,$addr[0]);
3351 .Ve
3352 .Sp
3353 The Socket library makes this slightly easier:
3354 .Sp
3355 .Vb 3
3356 \& use Socket;
3357 \& my $iaddr = inet_aton("127.1"); # or whatever address
3358 \& my $name = gethostbyaddr($iaddr, AF_INET);
3359 \&
3360 \& # or going the other way
3361 \& my $straddr = inet_ntoa($iaddr);
3362 .Ve
3363 .Sp
3364 In the opposite way, to resolve a hostname to the IP address
3365 you can write this:
3366 .Sp
3367 .Vb 6
3368 \& use Socket;
3369 \& my $packed_ip = gethostbyname("www.perl.org");
3370 \& my $ip_address;
3371 \& if (defined $packed_ip) {
3372 \& $ip_address = inet_ntoa($packed_ip);
3373 \& }
3374 .Ve
3375 .Sp
3376 Make sure \f(CW\*(C`gethostbyname\*(C'\fR is called in SCALAR
3377 context and that its return value is checked for definedness.
3378 .Sp
3379 The \f(CW\*(C`getprotobynumber\*(C'\fR function, even
3380 though it only takes one argument, has the precedence of a list
3381 operator, so beware:
3382 .Sp
3383 .Vb 3
3384 \& getprotobynumber $number eq \*(Aqicmp\*(Aq # WRONG
3385 \& getprotobynumber($number eq \*(Aqicmp\*(Aq) # actually means this
3386 \& getprotobynumber($number) eq \*(Aqicmp\*(Aq # better this way
3387 .Ve
3388 .Sp
3389 If you get tired of remembering which element of the return list
3390 contains which return value, by-name interfaces are provided in standard
3391 modules: \f(CW\*(C`File::stat\*(C'\fR, \f(CW\*(C`Net::hostent\*(C'\fR,
3392 \&\f(CW\*(C`Net::netent\*(C'\fR, \f(CW\*(C`Net::protoent\*(C'\fR,
3393 \&\f(CW\*(C`Net::servent\*(C'\fR, \f(CW\*(C`Time::gmtime\*(C'\fR,
3394 \&\f(CW\*(C`Time::localtime\*(C'\fR, and
3395 \&\f(CW\*(C`User::grent\*(C'\fR. These override the normal built-ins,
3396 supplying versions that return objects with the appropriate names for
3397 each field. For example:
3398 .Sp
3399 .Vb 3
3400 \& use File::stat;
3401 \& use User::pwent;
3402 \& my $is_theirs = (stat($filename)\->uid == getpwnam($whoever)\->uid);
3403 .Ve
3404 .Sp
3405 Even though it looks as though they're the same method calls (uid),
3406 they aren't, because a \f(CW\*(C`File::stat\*(C'\fR object is different from
3407 a \f(CW\*(C`User::pwent\*(C'\fR object.
3408 .Sp
3409 Many of these functions are not safe in a multi-threaded environment
3410 where more than one thread can be using them. In particular, functions
3411 like \f(CWgetpwent()\fR iterate per-process and not per-thread, so if two
3412 threads are simultaneously iterating, neither will get all the records.
3413 .Sp
3414 Some systems have thread-safe versions of some of the functions, such as
3415 \&\f(CWgetpwnam_r()\fR instead of \f(CWgetpwnam()\fR. There, Perl automatically and
3416 invisibly substitutes the thread-safe version, without notice. This
3417 means that code that safely runs on some systems can fail on others that
3418 lack the thread-safe versions.
3419 .Sp
3420 Portability issues: "getpwnam" in perlport to "endservent" in perlport.
3421 .IP "getsockname SOCKET" 4
3422 .IX Xref "getsockname"
3423 .IX Item "getsockname SOCKET"
3424 Returns the packed sockaddr address of this end of the SOCKET connection,
3425 in case you don't know the address because you have several different
3426 IPs that the connection might have come in on.
3427 .Sp
3428 .Vb 6
3429 \& use Socket;
3430 \& my $mysockaddr = getsockname($sock);
3431 \& my ($port, $myaddr) = sockaddr_in($mysockaddr);
3432 \& printf "Connect to %s [%s]\en",
3433 \& scalar gethostbyaddr($myaddr, AF_INET),
3434 \& inet_ntoa($myaddr);
3435 .Ve
3436 .IP "getsockopt SOCKET,LEVEL,OPTNAME" 4
3437 .IX Xref "getsockopt"
3438 .IX Item "getsockopt SOCKET,LEVEL,OPTNAME"
3439 Queries the option named OPTNAME associated with SOCKET at a given LEVEL.
3440 Options may exist at multiple protocol levels depending on the socket
3441 type, but at least the uppermost socket level SOL_SOCKET (defined in the
3442 \&\f(CW\*(C`Socket\*(C'\fR module) will exist. To query options at another
3443 level the protocol number of the appropriate protocol controlling the
3444 option should be supplied. For example, to indicate that an option is
3445 to be interpreted by the TCP protocol, LEVEL should be set to the
3446 protocol number of TCP, which you can get using
3447 \&\f(CW\*(C`getprotobyname\*(C'\fR.
3448 .Sp
3449 The function returns a packed string representing the requested socket
3450 option, or \f(CW\*(C`undef\*(C'\fR on error, with the reason for the
3451 error placed in \f(CW$!\fR. Just what is in the packed string
3452 depends on LEVEL and OPTNAME; consult \fBgetsockopt\fR\|(2) for details. A
3453 common case is that the option is an integer, in which case the result
3454 is a packed integer, which you can decode using
3455 \&\f(CW\*(C`unpack\*(C'\fR with the \f(CW\*(C`i\*(C'\fR (or \f(CW\*(C`I\*(C'\fR) format.
3456 .Sp
3457 Here's an example to test whether Nagle's algorithm is enabled on a socket:
3458 .Sp
3459 .Vb 1
3460 \& use Socket qw(:all);
3461 \&
3462 \& defined(my $tcp = getprotobyname("tcp"))
3463 \& or die "Could not determine the protocol number for tcp";
3464 \& # my $tcp = IPPROTO_TCP; # Alternative
3465 \& my $packed = getsockopt($socket, $tcp, TCP_NODELAY)
3466 \& or die "getsockopt TCP_NODELAY: $!";
3467 \& my $nodelay = unpack("I", $packed);
3468 \& print "Nagle\*(Aqs algorithm is turned ",
3469 \& $nodelay ? "off\en" : "on\en";
3470 .Ve
3471 .Sp
3472 Portability issues: "getsockopt" in perlport.
3473 .IP "glob EXPR" 4
3474 .IX Xref "glob wildcard filename, expansion expand"
3475 .IX Item "glob EXPR"
3476 .PD 0
3477 .IP glob 4
3478 .IX Item "glob"
3479 .PD
3480 In list context, returns a (possibly empty) list of filename expansions on
3481 the value of EXPR such as the Unix shell Bash would do. In
3482 scalar context, glob iterates through such filename expansions, returning
3483 \&\f(CW\*(C`undef\*(C'\fR when the list is exhausted. If EXPR is omitted,
3484 \&\f(CW$_\fR is used.
3485 .Sp
3486 .Vb 3
3487 \& # List context
3488 \& my @txt_files = glob("*.txt");
3489 \& my @perl_files = glob("*.pl *.pm");
3490 \&
3491 \& # Scalar context
3492 \& while (my $file = glob("*.mp3")) {
3493 \& # Do stuff
3494 \& }
3495 .Ve
3496 .Sp
3497 Glob also supports an alternate syntax using \f(CW\*(C`<\*(C'\fR \f(CW\*(C`>\*(C'\fR as
3498 delimiters. While this syntax is supported, it is recommended that you
3499 use \f(CW\*(C`glob\*(C'\fR instead as it is more readable and searchable.
3500 .Sp
3501 .Vb 1
3502 \& my @txt_files = <"*.txt">;
3503 .Ve
3504 .Sp
3505 If you need case insensitive file globbing that can be achieved using the
3506 \&\f(CW\*(C`:nocase\*(C'\fR parameter of the \f(CW\*(C`bsd_glob\*(C'\fR module.
3507 .Sp
3508 .Vb 1
3509 \& use File::Glob qw(:globally :nocase);
3510 \&
3511 \& my @txt = glob("readme*"); # README readme.txt Readme.md
3512 .Ve
3513 .Sp
3514 Note that \f(CW\*(C`glob\*(C'\fR splits its arguments on whitespace and
3515 treats
3516 each segment as separate pattern. As such, \f(CW\*(C`glob("*.c *.h")\*(C'\fR
3517 matches all files with a \fI.c\fR or \fI.h\fR extension. The expression
3518 \&\f(CW\*(C`glob(".* *")\*(C'\fR matches all files in the current working directory.
3519 If you want to glob filenames that might contain whitespace, you'll
3520 have to use extra quotes around the spacey filename to protect it.
3521 For example, to glob filenames that have an \f(CW\*(C`e\*(C'\fR followed by a space
3522 followed by an \f(CW\*(C`f\*(C'\fR, use one of:
3523 .Sp
3524 .Vb 3
3525 \& my @spacies = <"*e f*">;
3526 \& my @spacies = glob(\*(Aq"*e f*"\*(Aq);
3527 \& my @spacies = glob(q("*e f*"));
3528 .Ve
3529 .Sp
3530 If you had to get a variable through, you could do this:
3531 .Sp
3532 .Vb 2
3533 \& my @spacies = glob("\*(Aq*${var}e f*\*(Aq");
3534 \& my @spacies = glob(qq("*${var}e f*"));
3535 .Ve
3536 .Sp
3537 If non-empty braces are the only wildcard characters used in the
3538 \&\f(CW\*(C`glob\*(C'\fR, no filenames are matched, but potentially many
3539 strings are returned. For example, this produces nine strings, one for
3540 each pairing of fruits and colors:
3541 .Sp
3542 .Vb 1
3543 \& my @many = glob("{apple,tomato,cherry}={green,yellow,red}");
3544 .Ve
3545 .Sp
3546 This operator is implemented using the standard \f(CW\*(C`File::Glob\*(C'\fR extension.
3547 See \f(CW\*(C`bsd_glob\*(C'\fR for details, including
3548 \&\f(CW\*(C`bsd_glob\*(C'\fR, which does not treat whitespace
3549 as a pattern separator.
3550 .Sp
3551 If a \f(CW\*(C`glob\*(C'\fR expression is used as the condition of a \f(CW\*(C`while\*(C'\fR or \f(CW\*(C`for\*(C'\fR
3552 loop, then it will be implicitly assigned to \f(CW$_\fR. If either a \f(CW\*(C`glob\*(C'\fR
3553 expression or an explicit assignment of a \f(CW\*(C`glob\*(C'\fR expression to a scalar
3554 is used as a \f(CW\*(C`while\*(C'\fR/\f(CW\*(C`for\*(C'\fR condition, then the condition actually
3555 tests for definedness of the expression's value, not for its regular
3556 truth value.
3557 .Sp
3558 Internal implemenation details:
3559 .Sp
3560 This is the internal function implementing the \f(CW\*(C`<*.c>\*(C'\fR operator,
3561 but you can use it directly. The \f(CW\*(C`<*.c>\*(C'\fR operator is discussed in
3562 more detail in "I/O Operators" in perlop.
3563 .Sp
3564 Portability issues: "glob" in perlport.
3565 .IP "gmtime EXPR" 4
3566 .IX Xref "gmtime UTC Greenwich"
3567 .IX Item "gmtime EXPR"
3568 .PD 0
3569 .IP gmtime 4
3570 .IX Item "gmtime"
3571 .PD
3572 Works just like \f(CW\*(C`localtime\*(C'\fR, but the returned values
3573 are localized for the standard Greenwich time zone.
3574 .Sp
3575 Note: When called in list context, \f(CW$isdst\fR, the last value
3576 returned by gmtime, is always \f(CW0\fR. There is no
3577 Daylight Saving Time in GMT.
3578 .Sp
3579 Portability issues: "gmtime" in perlport.
3580 .IP "goto LABEL" 4
3581 .IX Xref "goto jump jmp"
3582 .IX Item "goto LABEL"
3583 .PD 0
3584 .IP "goto EXPR" 4
3585 .IX Item "goto EXPR"
3586 .IP "goto &NAME" 4
3587 .IX Item "goto &NAME"
3588 .PD
3589 The \f(CW\*(C`goto LABEL\*(C'\fR form finds the statement labeled with LABEL and
3590 resumes execution there. It can't be used to get out of a block or
3591 subroutine given to \f(CW\*(C`sort\*(C'\fR. It can be used to go
3592 almost anywhere else within the dynamic scope, including out of
3593 subroutines, but it's usually better to use some other construct such as
3594 \&\f(CW\*(C`last\*(C'\fR or \f(CW\*(C`die\*(C'\fR. The author of Perl has
3595 never felt the need to use this form of \f(CW\*(C`goto\*(C'\fR (in Perl,
3596 that is; C is another matter). (The difference is that C does not offer
3597 named loops combined with loop control. Perl does, and this replaces
3598 most structured uses of \f(CW\*(C`goto\*(C'\fR in other languages.)
3599 .Sp
3600 The \f(CW\*(C`goto EXPR\*(C'\fR form expects to evaluate \f(CW\*(C`EXPR\*(C'\fR to a code reference or
3601 a label name. If it evaluates to a code reference, it will be handled
3602 like \f(CW\*(C`goto &NAME\*(C'\fR, below. This is especially useful for implementing
3603 tail recursion via \f(CW\*(C`goto _\|_SUB_\|_\*(C'\fR.
3604 .Sp
3605 If the expression evaluates to a label name, its scope will be resolved
3606 dynamically. This allows for computed \f(CW\*(C`goto\*(C'\fRs per
3607 FORTRAN, but isn't necessarily recommended if you're optimizing for
3608 maintainability:
3609 .Sp
3610 .Vb 1
3611 \& goto ("FOO", "BAR", "GLARCH")[$i];
3612 .Ve
3613 .Sp
3614 As shown in this example, \f(CW\*(C`goto EXPR\*(C'\fR is exempt from the "looks like a
3615 function" rule. A pair of parentheses following it does not (necessarily)
3616 delimit its argument. \f(CW\*(C`goto("NE")."XT"\*(C'\fR is equivalent to \f(CW\*(C`goto NEXT\*(C'\fR.
3617 Also, unlike most named operators, this has the same precedence as
3618 assignment.
3619 .Sp
3620 Use of \f(CW\*(C`goto LABEL\*(C'\fR or \f(CW\*(C`goto EXPR\*(C'\fR to jump into a construct is
3621 deprecated and will issue a warning; it will become a fatal error in
3622 Perl 5.42. While still available, it may not be used to
3623 go into any construct that requires initialization, such as a
3624 subroutine, a \f(CW\*(C`foreach\*(C'\fR loop, or a \f(CW\*(C`given\*(C'\fR
3625 block. In general, it may not be used to jump into the parameter
3626 of a binary or list operator, but it may be used to jump into the
3627 \&\fIfirst\fR parameter of a binary operator. (The \f(CW\*(C`=\*(C'\fR
3628 assignment operator's "first" operand is its right-hand
3629 operand.) It also can't be used to go into a
3630 construct that is optimized away.
3631 .Sp
3632 The \f(CW\*(C`goto &NAME\*(C'\fR form is quite different from the other forms of
3633 \&\f(CW\*(C`goto\*(C'\fR. In fact, it isn't a goto in the normal sense at
3634 all, and doesn't have the stigma associated with other gotos. Instead,
3635 it exits the current subroutine (losing any changes set by
3636 \&\f(CW\*(C`local\*(C'\fR) and immediately calls in its place the named
3637 subroutine using the current value of \f(CW@_\fR. This is used
3638 by \f(CW\*(C`AUTOLOAD\*(C'\fR subroutines that wish to load another subroutine and then
3639 pretend that the other subroutine had been called in the first place
3640 (except that any modifications to \f(CW@_\fR in the current
3641 subroutine are propagated to the other subroutine.) After the
3642 \&\f(CW\*(C`goto\*(C'\fR, not even \f(CW\*(C`caller\*(C'\fR will be able
3643 to tell that this routine was called first.
3644 .Sp
3645 NAME needn't be the name of a subroutine; it can be a scalar variable
3646 containing a code reference or a block that evaluates to a code
3647 reference.
3648 .IP "grep BLOCK LIST" 4
3649 .IX Xref "grep"
3650 .IX Item "grep BLOCK LIST"
3651 .PD 0
3652 .IP "grep EXPR,LIST" 4
3653 .IX Item "grep EXPR,LIST"
3654 .PD
3655 This is similar in spirit to, but not the same as, \fBgrep\fR\|(1) and its
3656 relatives. In particular, it is not limited to using regular expressions.
3657 .Sp
3658 Evaluates the BLOCK or EXPR for each element of LIST (locally setting
3659 \&\f(CW$_\fR to each element) and returns the list value
3660 consisting of those
3661 elements for which the expression evaluated to true. In scalar
3662 context, returns the number of times the expression was true.
3663 .Sp
3664 .Vb 1
3665 \& my @foo = grep(!/^#/, @bar); # weed out comments
3666 .Ve
3667 .Sp
3668 or equivalently,
3669 .Sp
3670 .Vb 1
3671 \& my @foo = grep {!/^#/} @bar; # weed out comments
3672 .Ve
3673 .Sp
3674 Note that \f(CW$_\fR is an alias to the list value, so it can
3675 be used to
3676 modify the elements of the LIST. While this is useful and supported,
3677 it can cause bizarre results if the elements of LIST are not variables.
3678 Similarly, grep returns aliases into the original list, much as a for
3679 loop's index variable aliases the list elements. That is, modifying an
3680 element of a list returned by grep (for example, in a \f(CW\*(C`foreach\*(C'\fR,
3681 \&\f(CW\*(C`map\*(C'\fR or another \f(CW\*(C`grep\*(C'\fR)
3682 actually modifies the element in the original list.
3683 This is usually something to be avoided when writing clear code.
3684 .Sp
3685 See also \f(CW\*(C`map\*(C'\fR for a list composed of the results of
3686 the BLOCK or EXPR.
3687 .IP "hex EXPR" 4
3688 .IX Xref "hex hexadecimal"
3689 .IX Item "hex EXPR"
3690 .PD 0
3691 .IP hex 4
3692 .IX Item "hex"
3693 .PD
3694 Interprets EXPR as a hex string and returns the corresponding numeric value.
3695 If EXPR is omitted, uses \f(CW$_\fR.
3696 .Sp
3697 .Vb 3
3698 \& print hex \*(Aq0xAf\*(Aq; # prints \*(Aq175\*(Aq
3699 \& print hex \*(AqaF\*(Aq; # same
3700 \& $valid_input =~ /\eA(?:0?[xX])?(?:_?[0\-9a\-fA\-F])*\ez/
3701 .Ve
3702 .Sp
3703 A hex string consists of hex digits and an optional \f(CW\*(C`0x\*(C'\fR or \f(CW\*(C`x\*(C'\fR prefix.
3704 Each hex digit may be preceded by a single underscore, which will be ignored.
3705 Any other character triggers a warning and causes the rest of the string
3706 to be ignored (even leading whitespace, unlike \f(CW\*(C`oct\*(C'\fR).
3707 Only integers can be represented, and integer overflow triggers a warning.
3708 .Sp
3709 To convert strings that might start with any of \f(CW0\fR, \f(CW\*(C`0x\*(C'\fR, or \f(CW\*(C`0b\*(C'\fR,
3710 see \f(CW\*(C`oct\*(C'\fR. To present something as hex, look into
3711 \&\f(CW\*(C`printf\*(C'\fR,
3712 \&\f(CW\*(C`sprintf\*(C'\fR, and
3713 \&\f(CW\*(C`unpack\*(C'\fR.
3714 .IP "import LIST" 4
3715 .IX Xref "import"
3716 .IX Item "import LIST"
3717 There is no builtin \f(CW\*(C`import\*(C'\fR function. It is just an
3718 ordinary method (subroutine) defined (or inherited) by modules that wish
3719 to export names to another module. The
3720 \&\f(CW\*(C`use\*(C'\fR function calls the
3721 \&\f(CW\*(C`import\*(C'\fR method for the package used. See also
3722 \&\f(CW\*(C`use\*(C'\fR, perlmod, and Exporter.
3723 .IP "index STR,SUBSTR,POSITION" 4
3724 .IX Xref "index indexOf InStr"
3725 .IX Item "index STR,SUBSTR,POSITION"
3726 .PD 0
3727 .IP "index STR,SUBSTR" 4
3728 .IX Item "index STR,SUBSTR"
3729 .PD
3730 The index function searches for one string within another, but without
3731 the wildcard-like behavior of a full regular-expression pattern match.
3732 It returns the position of the first occurrence of SUBSTR in STR at
3733 or after POSITION. If POSITION is omitted, starts searching from the
3734 beginning of the string. POSITION before the beginning of the string
3735 or after its end is treated as if it were the beginning or the end,
3736 respectively. POSITION and the return value are based at zero.
3737 If the substring is not found, \f(CW\*(C`index\*(C'\fR
3738 returns \-1.
3739 .Sp
3740 Find characters or strings:
3741 .Sp
3742 .Vb 3
3743 \& index("Perl is great", "P"); # Returns 0
3744 \& index("Perl is great", "g"); # Returns 8
3745 \& index("Perl is great", "great"); # Also returns 8
3746 .Ve
3747 .Sp
3748 Attempting to find something not there:
3749 .Sp
3750 .Vb 1
3751 \& index("Perl is great", "Z"); # Returns \-1 (not found)
3752 .Ve
3753 .Sp
3754 Using an offset to find the \fIsecond\fR occurrence:
3755 .Sp
3756 .Vb 1
3757 \& index("Perl is great", "e", 5); # Returns 10
3758 .Ve
3759 .IP "int EXPR" 4
3760 .IX Xref "int integer truncate trunc floor"
3761 .IX Item "int EXPR"
3762 .PD 0
3763 .IP int 4
3764 .IX Item "int"
3765 .PD
3766 Returns the integer portion of EXPR. If EXPR is omitted, uses
3767 \&\f(CW$_\fR.
3768 You should not use this function for rounding: one because it truncates
3769 towards \f(CW0\fR, and two because machine representations of floating-point
3770 numbers can sometimes produce counterintuitive results. For example,
3771 \&\f(CWint(\-6.725/0.025)\fR produces \-268 rather than the correct \-269; that's
3772 because it's really more like \-268.99999999999994315658 instead. Usually,
3773 the \f(CW\*(C`sprintf\*(C'\fR,
3774 \&\f(CW\*(C`printf\*(C'\fR, or the
3775 \&\f(CW\*(C`POSIX::floor\*(C'\fR and \f(CW\*(C`POSIX::ceil\*(C'\fR
3776 functions will serve you better than will \f(CW\*(C`int\*(C'\fR.
3777 .IP "ioctl FILEHANDLE,FUNCTION,SCALAR" 4
3778 .IX Xref "ioctl"
3779 .IX Item "ioctl FILEHANDLE,FUNCTION,SCALAR"
3780 Implements the \fBioctl\fR\|(2) function. You'll probably first have to say
3781 .Sp
3782 .Vb 2
3783 \& require "sys/ioctl.ph"; # probably in
3784 \& # $Config{archlib}/sys/ioctl.ph
3785 .Ve
3786 .Sp
3787 to get the correct function definitions. If \fIsys/ioctl.ph\fR doesn't
3788 exist or doesn't have the correct definitions you'll have to roll your
3789 own, based on your C header files such as \fI<sys/ioctl.h>\fR.
3790 (There is a Perl script called \fBh2ph\fR that comes with the Perl kit that
3791 may help you in this, but it's nontrivial.) SCALAR will be read and/or
3792 written depending on the FUNCTION; a C pointer to the string value of SCALAR
3793 will be passed as the third argument of the actual
3794 \&\f(CW\*(C`ioctl\*(C'\fR call. (If SCALAR
3795 has no string value but does have a numeric value, that value will be
3796 passed rather than a pointer to the string value. To guarantee this to be
3797 true, add a \f(CW0\fR to the scalar before using it.) The
3798 \&\f(CW\*(C`pack\*(C'\fR and \f(CW\*(C`unpack\*(C'\fR
3799 functions may be needed to manipulate the values of structures used by
3800 \&\f(CW\*(C`ioctl\*(C'\fR.
3801 .Sp
3802 The return value of \f(CW\*(C`ioctl\*(C'\fR (and
3803 \&\f(CW\*(C`fcntl\*(C'\fR) is as follows:
3804 .Sp
3805 .Vb 4
3806 \& if OS returns: then Perl returns:
3807 \& \-1 undefined value
3808 \& 0 string "0 but true"
3809 \& anything else that number
3810 .Ve
3811 .Sp
3812 Thus Perl returns true on success and false on failure, yet you can
3813 still easily determine the actual value returned by the operating
3814 system:
3815 .Sp
3816 .Vb 2
3817 \& my $retval = ioctl(...) || \-1;
3818 \& printf "System returned %d\en", $retval;
3819 .Ve
3820 .Sp
3821 The special string \f(CW"0 but true"\fR is exempt from
3822 \&\f(CW\*(C`Argument "..." isn\*(Aqt numeric\*(C'\fR
3823 warnings on improper numeric conversions.
3824 .Sp
3825 Portability issues: "ioctl" in perlport.
3826 .IP "join EXPR,LIST" 4
3827 .IX Xref "join"
3828 .IX Item "join EXPR,LIST"
3829 Joins the separate strings of LIST into a single string with fields
3830 separated by the value of EXPR, and returns that new string. Example:
3831 .Sp
3832 .Vb 1
3833 \& my $rec = join(\*(Aq:\*(Aq, $login,$passwd,$uid,$gid,$gcos,$home,$shell);
3834 .Ve
3835 .Sp
3836 Beware that unlike \f(CW\*(C`split\*(C'\fR,
3837 \&\f(CW\*(C`join\*(C'\fR doesn't take a pattern as its first argument.
3838 Compare \f(CW\*(C`split\*(C'\fR.
3839 .IP "keys HASH" 4
3840 .IX Xref "keys key"
3841 .IX Item "keys HASH"
3842 .PD 0
3843 .IP "keys ARRAY" 4
3844 .IX Item "keys ARRAY"
3845 .PD
3846 Called in list context, returns a list consisting of all the keys of the
3847 named hash, or in Perl 5.12 or later only, the indices of an array. Perl
3848 releases prior to 5.12 will produce a syntax error if you try to use an
3849 array argument. In scalar context, returns the number of keys or indices.
3850 .Sp
3851 Hash entries are returned in an apparently random order. The actual random
3852 order is specific to a given hash; the exact same series of operations
3853 on two hashes may result in a different order for each hash. Any insertion
3854 into the hash may change the order, as will any deletion, with the exception
3855 that the most recent key returned by \f(CW\*(C`each\*(C'\fR or
3856 \&\f(CW\*(C`keys\*(C'\fR may be deleted without changing the order. So
3857 long as a given hash is unmodified you may rely on
3858 \&\f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR and \f(CW\*(C`each\*(C'\fR to repeatedly return the same order
3859 as each other. See "Algorithmic Complexity Attacks" in perlsec for
3860 details on why hash order is randomized. Aside from the guarantees
3861 provided here the exact details of Perl's hash algorithm and the hash
3862 traversal order are subject to change in any release of Perl. Tied hashes
3863 may behave differently to Perl's hashes with respect to changes in order on
3864 insertion and deletion of items.
3865 .Sp
3866 As a side effect, calling \f(CW\*(C`keys\*(C'\fR resets the internal
3867 iterator of the HASH or ARRAY (see \f(CW\*(C`each\*(C'\fR) before
3868 yielding the keys. In
3869 particular, calling \f(CW\*(C`keys\*(C'\fR in void context resets the
3870 iterator with no other overhead.
3871 .Sp
3872 Here is yet another way to print your environment:
3873 .Sp
3874 .Vb 5
3875 \& my @keys = keys %ENV;
3876 \& my @values = values %ENV;
3877 \& while (@keys) {
3878 \& print pop(@keys), \*(Aq=\*(Aq, pop(@values), "\en";
3879 \& }
3880 .Ve
3881 .Sp
3882 or how about sorted by key:
3883 .Sp
3884 .Vb 3
3885 \& foreach my $key (sort(keys %ENV)) {
3886 \& print $key, \*(Aq=\*(Aq, $ENV{$key}, "\en";
3887 \& }
3888 .Ve
3889 .Sp
3890 The returned values are copies of the original keys in the hash, so
3891 modifying them will not affect the original hash. Compare
3892 \&\f(CW\*(C`values\*(C'\fR.
3893 .Sp
3894 To sort a hash by value, you'll need to use a
3895 \&\f(CW\*(C`sort\*(C'\fR function. Here's a descending numeric
3896 sort of a hash by its values:
3897 .Sp
3898 .Vb 3
3899 \& foreach my $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
3900 \& printf "%4d %s\en", $hash{$key}, $key;
3901 \& }
3902 .Ve
3903 .Sp
3904 Used as an lvalue, \f(CW\*(C`keys\*(C'\fR allows you to increase the
3905 number of hash buckets
3906 allocated for the given hash. This can gain you a measure of efficiency if
3907 you know the hash is going to get big. (This is similar to pre-extending
3908 an array by assigning a larger number to $#array.) If you say
3909 .Sp
3910 .Vb 1
3911 \& keys %hash = 200;
3912 .Ve
3913 .Sp
3914 then \f(CW%hash\fR will have at least 200 buckets allocated for it\-\-256 of them,
3915 in fact, since it rounds up to the next power of two. These
3916 buckets will be retained even if you do \f(CW\*(C`%hash = ()\*(C'\fR, use \f(CWundef
3917 %hash\fR if you want to free the storage while \f(CW%hash\fR is still in scope.
3918 You can't shrink the number of buckets allocated for the hash using
3919 \&\f(CW\*(C`keys\*(C'\fR in this way (but you needn't worry about doing
3920 this by accident, as trying has no effect). \f(CW\*(C`keys @array\*(C'\fR in an lvalue
3921 context is a syntax error.
3922 .Sp
3923 Starting with Perl 5.14, an experimental feature allowed
3924 \&\f(CW\*(C`keys\*(C'\fR to take a scalar expression. This experiment has
3925 been deemed unsuccessful, and was removed as of Perl 5.24.
3926 .Sp
3927 To avoid confusing would-be users of your code who are running earlier
3928 versions of Perl with mysterious syntax errors, put this sort of thing at
3929 the top of your file to signal that your code will work \fIonly\fR on Perls of
3930 a recent vintage:
3931 .Sp
3932 .Vb 1
3933 \& use v5.12; # so keys/values/each work on arrays
3934 .Ve
3935 .Sp
3936 See also \f(CW\*(C`each\*(C'\fR, \f(CW\*(C`values\*(C'\fR, and
3937 \&\f(CW\*(C`sort\*(C'\fR.
3938 .IP "kill SIGNAL, LIST" 4
3939 .IX Item "kill SIGNAL, LIST"
3940 .PD 0
3941 .IP "kill SIGNAL" 4
3942 .IX Xref "kill signal"
3943 .IX Item "kill SIGNAL"
3944 .PD
3945 Sends a signal to a list of processes. Returns the number of arguments
3946 that were successfully used to signal (which is not necessarily the same
3947 as the number of processes actually killed, e.g. where a process group is
3948 killed).
3949 .Sp
3950 .Vb 2
3951 \& my $cnt = kill \*(AqHUP\*(Aq, $child1, $child2;
3952 \& kill \*(AqKILL\*(Aq, @goners;
3953 .Ve
3954 .Sp
3955 SIGNAL may be either a signal name (a string) or a signal number. A signal
3956 name may start with a \f(CW\*(C`SIG\*(C'\fR prefix, thus \f(CW\*(C`FOO\*(C'\fR and \f(CW\*(C`SIGFOO\*(C'\fR refer to the
3957 same signal. The string form of SIGNAL is recommended for portability because
3958 the same signal may have different numbers in different operating systems.
3959 .Sp
3960 A list of signal names supported by the current platform can be found in
3961 \&\f(CW$Config{sig_name}\fR, which is provided by the \f(CW\*(C`Config\*(C'\fR
3962 module. See Config for more details.
3963 .Sp
3964 A negative signal name is the same as a negative signal number, killing process
3965 groups instead of processes. For example, \f(CW\*(C`kill \*(Aq\-KILL\*(Aq, $pgrp\*(C'\fR and
3966 \&\f(CW\*(C`kill \-9, $pgrp\*(C'\fR will send \f(CW\*(C`SIGKILL\*(C'\fR to
3967 the entire process group specified. That
3968 means you usually want to use positive not negative signals.
3969 .Sp
3970 If SIGNAL is either the number 0 or the string \f(CW\*(C`ZERO\*(C'\fR (or \f(CW\*(C`SIGZERO\*(C'\fR),
3971 no signal is sent to the process, but \f(CW\*(C`kill\*(C'\fR
3972 checks whether it's \fIpossible\fR to send a signal to it
3973 (that means, to be brief, that the process is owned by the same user, or we are
3974 the super-user). This is useful to check that a child process is still
3975 alive (even if only as a zombie) and hasn't changed its UID. See
3976 perlport for notes on the portability of this construct.
3977 .Sp
3978 The behavior of kill when a \fIPROCESS\fR number is zero or negative depends on
3979 the operating system. For example, on POSIX-conforming systems, zero will
3980 signal the current process group, \-1 will signal all processes, and any
3981 other negative PROCESS number will act as a negative signal number and
3982 kill the entire process group specified.
3983 .Sp
3984 If both the SIGNAL and the PROCESS are negative, the results are undefined.
3985 A warning may be produced in a future version.
3986 .Sp
3987 See "Signals" in perlipc for more details.
3988 .Sp
3989 On some platforms such as Windows where the \fBfork\fR\|(2) system call is not
3990 available, Perl can be built to emulate \f(CW\*(C`fork\*(C'\fR at the
3991 interpreter level.
3992 This emulation has limitations related to kill that have to be considered,
3993 for code running on Windows and in code intended to be portable.
3994 .Sp
3995 See perlfork for more details.
3996 .Sp
3997 If there is no \fILIST\fR of processes, no signal is sent, and the return
3998 value is 0. This form is sometimes used, however, because it causes
3999 tainting checks to be run, if your perl support taint checks. But see
4000 "Laundering and Detecting Tainted Data" in perlsec.
4001 .Sp
4002 Portability issues: "kill" in perlport.
4003 .IP "last LABEL" 4
4004 .IX Xref "last break"
4005 .IX Item "last LABEL"
4006 .PD 0
4007 .IP "last EXPR" 4
4008 .IX Item "last EXPR"
4009 .IP last 4
4010 .IX Item "last"
4011 .PD
4012 The \f(CW\*(C`last\*(C'\fR command is like the \f(CW\*(C`break\*(C'\fR statement in C
4013 (as used in
4014 loops); it immediately exits the loop in question. If the LABEL is
4015 omitted, the command refers to the innermost enclosing
4016 loop. The \f(CW\*(C`last EXPR\*(C'\fR form, available starting in Perl
4017 5.18.0, allows a label name to be computed at run time,
4018 and is otherwise identical to \f(CW\*(C`last LABEL\*(C'\fR. The
4019 \&\f(CW\*(C`continue\*(C'\fR block, if any, is not executed:
4020 .Sp
4021 .Vb 4
4022 \& LINE: while (<STDIN>) {
4023 \& last LINE if /^$/; # exit when done with header
4024 \& #...
4025 \& }
4026 .Ve
4027 .Sp
4028 \&\f(CW\*(C`last\*(C'\fR cannot return a value from a block that typically
4029 returns a value, such as \f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR, or \f(CW\*(C`do {}\*(C'\fR. It will perform
4030 its flow control behavior, which precludes any return value. It should not be
4031 used to exit a \f(CW\*(C`grep\*(C'\fR or \f(CW\*(C`map\*(C'\fR
4032 operation.
4033 .Sp
4034 Note that a block by itself is semantically identical to a loop
4035 that executes once. Thus \f(CW\*(C`last\*(C'\fR can be used to effect
4036 an early exit out of such a block.
4037 .Sp
4038 See also \f(CW\*(C`continue\*(C'\fR for an illustration of how
4039 \&\f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
4040 \&\f(CW\*(C`redo\*(C'\fR work.
4041 .Sp
4042 Unlike most named operators, this has the same precedence as assignment.
4043 It is also exempt from the looks-like-a-function rule, so
4044 \&\f(CW\*(C`last ("foo")."bar"\*(C'\fR will cause "bar" to be part of the argument to
4045 \&\f(CW\*(C`last\*(C'\fR.
4046 .IP "lc EXPR" 4
4047 .IX Xref "lc lowercase"
4048 .IX Item "lc EXPR"
4049 .PD 0
4050 .IP lc 4
4051 .IX Item "lc"
4052 .PD
4053 Returns a lowercased version of EXPR. If EXPR is omitted, uses
4054 \&\f(CW$_\fR.
4055 .Sp
4056 .Vb 1
4057 \& my $str = lc("Perl is GREAT"); # "perl is great"
4058 .Ve
4059 .Sp
4060 What gets returned depends on several factors:
4061 .RS 4
4062 .ie n .IP "If ""use bytes"" is in effect:" 4
4063 .el .IP "If \f(CWuse bytes\fR is in effect:" 4
4064 .IX Item "If use bytes is in effect:"
4065 The results follow ASCII rules. Only the characters \f(CW\*(C`A\-Z\*(C'\fR change,
4066 to \f(CW\*(C`a\-z\*(C'\fR respectively.
4067 .ie n .IP "Otherwise, if ""use locale"" for ""LC_CTYPE"" is in effect:" 4
4068 .el .IP "Otherwise, if \f(CWuse locale\fR for \f(CWLC_CTYPE\fR is in effect:" 4
4069 .IX Item "Otherwise, if use locale for LC_CTYPE is in effect:"
4070 Respects current \f(CW\*(C`LC_CTYPE\*(C'\fR locale for code points < 256; and uses Unicode
4071 rules for the remaining code points (this last can only happen if
4072 the UTF8 flag is also set). See perllocale.
4073 .Sp
4074 Starting in v5.20, Perl uses full Unicode rules if the locale is
4075 UTF\-8. Otherwise, there is a deficiency in this scheme, which is that
4076 case changes that cross the 255/256
4077 boundary are not well-defined. For example, the lower case of LATIN CAPITAL
4078 LETTER SHARP S (U+1E9E) in Unicode rules is U+00DF (on ASCII
4079 platforms). But under \f(CW\*(C`use locale\*(C'\fR (prior to v5.20 or not a UTF\-8
4080 locale), the lower case of U+1E9E is
4081 itself, because 0xDF may not be LATIN SMALL LETTER SHARP S in the
4082 current locale, and Perl has no way of knowing if that character even
4083 exists in the locale, much less what code point it is. Perl returns
4084 a result that is above 255 (almost always the input character unchanged),
4085 for all instances (and there aren't many) where the 255/256 boundary
4086 would otherwise be crossed; and starting in v5.22, it raises a
4087 locale warning.
4088 .IP "Otherwise, If EXPR has the UTF8 flag set:" 4
4089 .IX Item "Otherwise, If EXPR has the UTF8 flag set:"
4090 Unicode rules are used for the case change.
4091 .ie n .IP "Otherwise, if ""use feature \*(Aqunicode_strings\*(Aq"" or ""use locale \*(Aq:not_characters\*(Aq"" is in effect:" 4
4092 .el .IP "Otherwise, if \f(CWuse feature \*(Aqunicode_strings\*(Aq\fR or \f(CWuse locale \*(Aq:not_characters\*(Aq\fR is in effect:" 4
4093 .IX Item "Otherwise, if use feature unicode_strings or use locale :not_characters is in effect:"
4094 Unicode rules are used for the case change.
4095 .IP Otherwise: 4
4096 .IX Item "Otherwise:"
4097 ASCII rules are used for the case change. The lowercase of any character
4098 outside the ASCII range is the character itself.
4099 .RE
4100 .RS 4
4101 .Sp
4102 \&\fBNote:\fR This is the internal function implementing the
4103 \&\f(CW\*(C`\eL\*(C'\fR escape in double-quoted
4104 strings.
4105 .Sp
4106 .Vb 1
4107 \& my $str = "Perl is \eLGREAT\eE"; # "Perl is great"
4108 .Ve
4109 .RE
4110 .IP "lcfirst EXPR" 4
4111 .IX Xref "lcfirst lowercase"
4112 .IX Item "lcfirst EXPR"
4113 .PD 0
4114 .IP lcfirst 4
4115 .IX Item "lcfirst"
4116 .PD
4117 Returns the value of EXPR with the first character lowercased. This
4118 is the internal function implementing the \f(CW\*(C`\el\*(C'\fR escape in
4119 double-quoted strings.
4120 .Sp
4121 If EXPR is omitted, uses \f(CW$_\fR.
4122 .Sp
4123 This function behaves the same way under various pragmas, such as in a locale,
4124 as \f(CW\*(C`lc\*(C'\fR does.
4125 .IP "length EXPR" 4
4126 .IX Xref "length size"
4127 .IX Item "length EXPR"
4128 .PD 0
4129 .IP length 4
4130 .IX Item "length"
4131 .PD
4132 Returns the length in \fIcharacters\fR of the value of EXPR. If EXPR is
4133 omitted, returns the length of \f(CW$_\fR. If EXPR is
4134 undefined, returns \f(CW\*(C`undef\*(C'\fR.
4135 .Sp
4136 This function cannot be used on an entire array or hash to find out how
4137 many elements these have. For that, use \f(CW\*(C`scalar @array\*(C'\fR and \f(CWscalar keys
4138 %hash\fR, respectively.
4139 .Sp
4140 Like all Perl character operations, \f(CW\*(C`length\*(C'\fR normally
4141 deals in logical
4142 characters, not physical bytes. For how many bytes a string encoded as
4143 UTF\-8 would take up, use \f(CW\*(C`length(Encode::encode(\*(AqUTF\-8\*(Aq, EXPR))\*(C'\fR
4144 (you'll have to \f(CW\*(C`use Encode\*(C'\fR first). See Encode and perlunicode.
4145 .IP _\|_LINE_\|_ 4
4146 .IX Xref "__LINE__"
4147 .IX Item "__LINE__"
4148 A special token that compiles to the current line number.
4149 It can be altered by the mechanism described at
4150 "Plain Old Comments (Not!)" in perlsyn.
4151 .IP "link OLDFILE,NEWFILE" 4
4152 .IX Xref "link"
4153 .IX Item "link OLDFILE,NEWFILE"
4154 Creates a new filename linked to the old filename. Returns true for
4155 success, false otherwise.
4156 .Sp
4157 Portability issues: "link" in perlport.
4158 .IP "listen SOCKET,QUEUESIZE" 4
4159 .IX Xref "listen"
4160 .IX Item "listen SOCKET,QUEUESIZE"
4161 Does the same thing that the \fBlisten\fR\|(2) system call does. Returns true if
4162 it succeeded, false otherwise. See the example in
4163 "Sockets: Client/Server Communication" in perlipc.
4164 .IP "local EXPR" 4
4165 .IX Xref "local"
4166 .IX Item "local EXPR"
4167 You really probably want to be using \f(CW\*(C`my\*(C'\fR instead,
4168 because \f(CW\*(C`local\*(C'\fR isn't what most people think of as
4169 "local". See "Private Variables via \fBmy()\fR" in perlsub for details.
4170 .Sp
4171 A local modifies the listed variables to be local to the enclosing
4172 block, file, or eval. If more than one value is listed, the list must
4173 be placed in parentheses. See "Temporary Values via \fBlocal()\fR" in perlsub
4174 for details, including issues with tied arrays and hashes.
4175 .Sp
4176 Like \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`state\*(C'\fR, and
4177 \&\f(CW\*(C`our\*(C'\fR, \f(CW\*(C`local\*(C'\fR can operate on a variable
4178 anywhere it appears in an expression (aside from interpolation in strings).
4179 Unlike the other declarations, the effect of \f(CW\*(C`local\*(C'\fR happens
4180 at runtime, and so it will apply to additional uses of the same variable
4181 executed after the declaration, even within the same statement. Note that
4182 this does not include uses within an expression assigned to the variable
4183 when it is localized, because the assigned expression is evaluated before
4184 the localization.
4185 .Sp
4186 .Vb 7
4187 \& package main;
4188 \& our $x = 2;
4189 \& {
4190 \& foo($x, local $x = $x + 1, $x); # foo() receives (2, 3, 3)
4191 \& # $main::x is 3 within the call to foo()
4192 \& }
4193 \& foo($x); # foo() receives (2) and $main::x is 2
4194 .Ve
4195 .Sp
4196 The \f(CW\*(C`delete local EXPR\*(C'\fR construct can also be used to localize the deletion
4197 of array/hash elements to the current block.
4198 See "Localized deletion of elements of composite types" in perlsub.
4199 .IP "localtime EXPR" 4
4200 .IX Xref "localtime ctime"
4201 .IX Item "localtime EXPR"
4202 .PD 0
4203 .IP localtime 4
4204 .IX Item "localtime"
4205 .PD
4206 Converts a time as returned by the time function to a 9\-element list
4207 with the time analyzed for the local time zone. Typically used as
4208 follows:
4209 .Sp
4210 .Vb 3
4211 \& # 0 1 2 3 4 5 6 7 8
4212 \& my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
4213 \& localtime(time);
4214 .Ve
4215 .Sp
4216 All list elements are numeric and come straight out of the C 'struct
4217 tm'. \f(CW$sec\fR, \f(CW$min\fR, and \f(CW$hour\fR are the seconds, minutes, and hours
4218 of the specified time.
4219 .Sp
4220 \&\f(CW$mday\fR is the day of the month and \f(CW$mon\fR the month in
4221 the range \f(CW0..11\fR, with 0 indicating January and 11 indicating December.
4222 This makes it easy to get a month name from a list:
4223 .Sp
4224 .Vb 3
4225 \& my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
4226 \& print "$abbr[$mon] $mday";
4227 \& # $mon=9, $mday=18 gives "Oct 18"
4228 .Ve
4229 .Sp
4230 \&\f(CW$year\fR contains the number of years since 1900. To get the full
4231 year write:
4232 .Sp
4233 .Vb 1
4234 \& $year += 1900;
4235 .Ve
4236 .Sp
4237 To get the last two digits of the year (e.g., "01" in 2001) do:
4238 .Sp
4239 .Vb 1
4240 \& $year = sprintf("%02d", $year % 100);
4241 .Ve
4242 .Sp
4243 \&\f(CW$wday\fR is the day of the week, with 0 indicating Sunday and 3 indicating
4244 Wednesday. \f(CW$yday\fR is the day of the year, in the range \f(CW0..364\fR
4245 (or \f(CW0..365\fR in leap years.)
4246 .Sp
4247 \&\f(CW$isdst\fR is true if the specified time occurs when Daylight Saving
4248 Time is in effect, false otherwise.
4249 .Sp
4250 If EXPR is omitted, \f(CW\*(C`localtime\*(C'\fR uses the current
4251 time (as returned by \f(CW\*(C`time\*(C'\fR).
4252 .Sp
4253 In scalar context, \f(CW\*(C`localtime\*(C'\fR returns the
4254 \&\fBctime\fR\|(3) value:
4255 .Sp
4256 .Vb 1
4257 \& my $now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994"
4258 .Ve
4259 .Sp
4260 This scalar value is always in English, and is \fBnot\fR locale-dependent.
4261 To get similar but locale-dependent date strings, try for example:
4262 .Sp
4263 .Vb 4
4264 \& use POSIX qw(strftime);
4265 \& my $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
4266 \& # or for GMT formatted appropriately for your locale:
4267 \& my $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;
4268 .Ve
4269 .Sp
4270 \&\f(CW$now_string\fR will be formatted according to the current LC_TIME locale
4271 the program or thread is running in. See perllocale for how to set
4272 up and change that locale. Note that \f(CW%a\fR and \f(CW%b\fR, the short forms
4273 of the day of the week and the month of the year, may not necessarily be
4274 three characters wide.
4275 .Sp
4276 The Time::gmtime and Time::localtime modules provide a convenient,
4277 by-name access mechanism to the \f(CW\*(C`gmtime\*(C'\fR and
4278 \&\f(CW\*(C`localtime\*(C'\fR functions, respectively.
4279 .Sp
4280 For a comprehensive date and time representation look at the
4281 DateTime module on CPAN.
4282 .Sp
4283 For GMT instead of local time use the \f(CW\*(C`gmtime\*(C'\fR builtin.
4284 .Sp
4285 See also the \f(CW\*(C`Time::Local\*(C'\fR module (for converting
4286 seconds, minutes, hours, and such back to the integer value returned by
4287 \&\f(CW\*(C`time\*(C'\fR), and the POSIX module's
4288 \&\f(CW\*(C`mktime\*(C'\fR function.
4289 .Sp
4290 Portability issues: "localtime" in perlport.
4291 .IP "lock THING" 4
4292 .IX Xref "lock"
4293 .IX Item "lock THING"
4294 This function places an advisory lock on a shared variable or referenced
4295 object contained in \fITHING\fR until the lock goes out of scope.
4296 .Sp
4297 The value returned is the scalar itself, if the argument is a scalar, or a
4298 reference, if the argument is a hash, array or subroutine.
4299 .Sp
4300 \&\f(CW\*(C`lock\*(C'\fR is a "weak keyword"; this means that if you've
4301 defined a function
4302 by this name (before any calls to it), that function will be called
4303 instead. If you are not under \f(CW\*(C`use threads::shared\*(C'\fR this does nothing.
4304 See threads::shared.
4305 .IP "log EXPR" 4
4306 .IX Xref "log logarithm e ln base"
4307 .IX Item "log EXPR"
4308 .PD 0
4309 .IP log 4
4310 .IX Item "log"
4311 .PD
4312 Returns the natural logarithm (base \fIe\fR) of EXPR. If EXPR is omitted,
4313 returns the log of \f(CW$_\fR. To get the
4314 log of another base, use basic algebra:
4315 The base-N log of a number is equal to the natural log of that number
4316 divided by the natural log of N. For example:
4317 .Sp
4318 .Vb 4
4319 \& sub log10 {
4320 \& my $n = shift;
4321 \& return log($n)/log(10);
4322 \& }
4323 .Ve
4324 .Sp
4325 See also \f(CW\*(C`exp\*(C'\fR for the inverse operation.
4326 .IP "lstat FILEHANDLE" 4
4327 .IX Xref "lstat"
4328 .IX Item "lstat FILEHANDLE"
4329 .PD 0
4330 .IP "lstat EXPR" 4
4331 .IX Item "lstat EXPR"
4332 .IP "lstat DIRHANDLE" 4
4333 .IX Item "lstat DIRHANDLE"
4334 .IP lstat 4
4335 .IX Item "lstat"
4336 .PD
4337 Does the same thing as the \f(CW\*(C`stat\*(C'\fR function
4338 (including setting the special \f(CW\*(C`_\*(C'\fR filehandle) but stats a symbolic
4339 link instead of the file the symbolic link points to. If symbolic links
4340 are unimplemented on your system, a normal \f(CW\*(C`stat\*(C'\fR
4341 is done. For much more detailed information, please see the
4342 documentation for \f(CW\*(C`stat\*(C'\fR.
4343 .Sp
4344 If EXPR is omitted, stats \f(CW$_\fR.
4345 .Sp
4346 Portability issues: "lstat" in perlport.
4347 .IP m// 4
4348 .IX Item "m//"
4349 The match operator. See "Regexp Quote-Like Operators" in perlop.
4350 .IP "map BLOCK LIST" 4
4351 .IX Xref "map"
4352 .IX Item "map BLOCK LIST"
4353 .PD 0
4354 .IP "map EXPR,LIST" 4
4355 .IX Item "map EXPR,LIST"
4356 .PD
4357 Evaluates the BLOCK or EXPR for each element of LIST (locally setting
4358 \&\f(CW$_\fR to each element) and composes a list of the results of
4359 each such evaluation. Each element of LIST may produce zero, one, or more
4360 elements in the generated list, so the number of elements in the generated
4361 list may differ from that in LIST. In scalar context, returns the total
4362 number of elements so generated. In list context, returns the generated list.
4363 .Sp
4364 .Vb 1
4365 \& my @chars = map(chr, @numbers);
4366 .Ve
4367 .Sp
4368 translates a list of numbers to the corresponding characters.
4369 .Sp
4370 .Vb 1
4371 \& my @squares = map { $_ * $_ } @numbers;
4372 .Ve
4373 .Sp
4374 translates a list of numbers to their squared values.
4375 .Sp
4376 .Vb 1
4377 \& my @squares = map { $_ > 5 ? ($_ * $_) : () } @numbers;
4378 .Ve
4379 .Sp
4380 shows that number of returned elements can differ from the number of
4381 input elements. To omit an element, return an empty list ().
4382 This could also be achieved by writing
4383 .Sp
4384 .Vb 1
4385 \& my @squares = map { $_ * $_ } grep { $_ > 5 } @numbers;
4386 .Ve
4387 .Sp
4388 which makes the intention more clear.
4389 .Sp
4390 Map always returns a list, which can be
4391 assigned to a hash such that the elements
4392 become key/value pairs. See perldata for more details.
4393 .Sp
4394 .Vb 1
4395 \& my %hash = map { get_a_key_for($_) => $_ } @array;
4396 .Ve
4397 .Sp
4398 is just a funny way to write
4399 .Sp
4400 .Vb 4
4401 \& my %hash;
4402 \& foreach (@array) {
4403 \& $hash{get_a_key_for($_)} = $_;
4404 \& }
4405 .Ve
4406 .Sp
4407 Note that \f(CW$_\fR is an alias to the list value, so it can
4408 be used to modify the elements of the LIST. While this is useful and
4409 supported, it can cause bizarre results if the elements of LIST are not
4410 variables. Using a regular \f(CW\*(C`foreach\*(C'\fR loop for this purpose would be
4411 clearer in most cases. See also \f(CW\*(C`grep\*(C'\fR for a
4412 list composed of those items of the original list for which the BLOCK
4413 or EXPR evaluates to true.
4414 .Sp
4415 \&\f(CW\*(C`{\*(C'\fR starts both hash references and blocks, so \f(CW\*(C`map { ...\*(C'\fR could be either
4416 the start of map BLOCK LIST or map EXPR, LIST. Because Perl doesn't look
4417 ahead for the closing \f(CW\*(C`}\*(C'\fR it has to take a guess at which it's dealing with
4418 based on what it finds just after the
4419 \&\f(CW\*(C`{\*(C'\fR. Usually it gets it right, but if it
4420 doesn't it won't realize something is wrong until it gets to the \f(CW\*(C`}\*(C'\fR and
4421 encounters the missing (or unexpected) comma. The syntax error will be
4422 reported close to the \f(CW\*(C`}\*(C'\fR, but you'll need to change something near the \f(CW\*(C`{\*(C'\fR
4423 such as using a unary \f(CW\*(C`+\*(C'\fR or semicolon to give Perl some help:
4424 .Sp
4425 .Vb 6
4426 \& my %hash = map { "\eL$_" => 1 } @array # perl guesses EXPR. wrong
4427 \& my %hash = map { +"\eL$_" => 1 } @array # perl guesses BLOCK. right
4428 \& my %hash = map {; "\eL$_" => 1 } @array # this also works
4429 \& my %hash = map { ("\eL$_" => 1) } @array # as does this
4430 \& my %hash = map { lc($_) => 1 } @array # and this.
4431 \& my %hash = map +( lc($_) => 1 ), @array # this is EXPR and works!
4432 \&
4433 \& my %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array)
4434 .Ve
4435 .Sp
4436 or to force an anon hash constructor use \f(CW\*(C`+{\*(C'\fR:
4437 .Sp
4438 .Vb 2
4439 \& my @hashes = map +{ lc($_) => 1 }, @array # EXPR, so needs
4440 \& # comma at end
4441 .Ve
4442 .Sp
4443 to get a list of anonymous hashes each with only one entry apiece.
4444 .IP "method NAME BLOCK" 4
4445 .IX Xref "method"
4446 .IX Item "method NAME BLOCK"
4447 .PD 0
4448 .IP "method NAME : ATTRS BLOCK" 4
4449 .IX Item "method NAME : ATTRS BLOCK"
4450 .PD
4451 Creates a new named method in the scope of the class that it appears within.
4452 This is only valid inside a \f(CW\*(C`class\*(C'\fR declaration.
4453 .IP "mkdir FILENAME,MODE" 4
4454 .IX Xref "mkdir md directory, create"
4455 .IX Item "mkdir FILENAME,MODE"
4456 .PD 0
4457 .IP "mkdir FILENAME" 4
4458 .IX Item "mkdir FILENAME"
4459 .IP mkdir 4
4460 .IX Item "mkdir"
4461 .PD
4462 Creates the directory specified by FILENAME, with permissions
4463 specified by MODE (as modified by \f(CW\*(C`umask\*(C'\fR). If it
4464 succeeds it returns true; otherwise it returns false and sets
4465 \&\f(CW$!\fR (errno).
4466 MODE defaults to 0777 if omitted, and FILENAME defaults
4467 to \f(CW$_\fR if omitted.
4468 .Sp
4469 In general, it is better to create directories with a permissive MODE
4470 and let the user modify that with their \f(CW\*(C`umask\*(C'\fR than it
4471 is to supply
4472 a restrictive MODE and give the user no way to be more permissive.
4473 The exceptions to this rule are when the file or directory should be
4474 kept private (mail files, for instance). The documentation for
4475 \&\f(CW\*(C`umask\*(C'\fR discusses the choice of MODE in more detail.
4476 If bits in MODE other than the permission bits are set, the result may
4477 be implementation defined, per POSIX 1003.1\-2008.
4478 .Sp
4479 Note that according to the POSIX 1003.1\-1996 the FILENAME may have any
4480 number of trailing slashes. Some operating and filesystems do not get
4481 this right, so Perl automatically removes all trailing slashes to keep
4482 everyone happy.
4483 .Sp
4484 To recursively create a directory structure, look at
4485 the \f(CW\*(C`make_path\*(C'\fR function
4486 of the File::Path module.
4487 .IP "msgctl ID,CMD,ARG" 4
4488 .IX Xref "msgctl"
4489 .IX Item "msgctl ID,CMD,ARG"
4490 Calls the System V IPC function \fBmsgctl\fR\|(2). You'll probably have to say
4491 .Sp
4492 .Vb 1
4493 \& use IPC::SysV;
4494 .Ve
4495 .Sp
4496 first to get the correct constant definitions. If CMD is \f(CW\*(C`IPC_STAT\*(C'\fR,
4497 then ARG must be a variable that will hold the returned \f(CW\*(C`msqid_ds\*(C'\fR
4498 structure. Returns like \f(CW\*(C`ioctl\*(C'\fR:
4499 the undefined value for error, \f(CW"0 but true"\fR for zero, or the actual
4500 return value otherwise. See also "SysV IPC" in perlipc and the
4501 documentation for \f(CW\*(C`IPC::SysV\*(C'\fR and
4502 \&\f(CW\*(C`IPC::Semaphore\*(C'\fR.
4503 .Sp
4504 Portability issues: "msgctl" in perlport.
4505 .IP "msgget KEY,FLAGS" 4
4506 .IX Xref "msgget"
4507 .IX Item "msgget KEY,FLAGS"
4508 Calls the System V IPC function \fBmsgget\fR\|(2). Returns the message queue
4509 id, or \f(CW\*(C`undef\*(C'\fR on error. See also "SysV IPC" in perlipc
4510 and the documentation for \f(CW\*(C`IPC::SysV\*(C'\fR and
4511 \&\f(CW\*(C`IPC::Msg\*(C'\fR.
4512 .Sp
4513 Portability issues: "msgget" in perlport.
4514 .IP "msgrcv ID,VAR,SIZE,TYPE,FLAGS" 4
4515 .IX Xref "msgrcv"
4516 .IX Item "msgrcv ID,VAR,SIZE,TYPE,FLAGS"
4517 Calls the System V IPC function msgrcv to receive a message from
4518 message queue ID into variable VAR with a maximum message size of
4519 SIZE. Note that when a message is received, the message type as a
4520 native long integer will be the first thing in VAR, followed by the
4521 actual message. This packing may be opened with \f(CW\*(C`unpack("l! a*")\*(C'\fR.
4522 Taints the variable. Returns true if successful, false
4523 on error. See also "SysV IPC" in perlipc and the documentation for
4524 \&\f(CW\*(C`IPC::SysV\*(C'\fR and \f(CW\*(C`IPC::Msg\*(C'\fR.
4525 .Sp
4526 Portability issues: "msgrcv" in perlport.
4527 .IP "msgsnd ID,MSG,FLAGS" 4
4528 .IX Xref "msgsnd"
4529 .IX Item "msgsnd ID,MSG,FLAGS"
4530 Calls the System V IPC function msgsnd to send the message MSG to the
4531 message queue ID. MSG must begin with the native long integer message
4532 type, followed by the message itself. This kind of packing can be achieved
4533 with \f(CW\*(C`pack("l! a*", $type, $message)\*(C'\fR. Returns true if successful,
4534 false on error. See also "SysV IPC" in perlipc and the documentation
4535 for \f(CW\*(C`IPC::SysV\*(C'\fR and \f(CW\*(C`IPC::Msg\*(C'\fR.
4536 .Sp
4537 Portability issues: "msgsnd" in perlport.
4538 .IP "my VARLIST" 4
4539 .IX Xref "my"
4540 .IX Item "my VARLIST"
4541 .PD 0
4542 .IP "my TYPE VARLIST" 4
4543 .IX Item "my TYPE VARLIST"
4544 .IP "my VARLIST : ATTRS" 4
4545 .IX Item "my VARLIST : ATTRS"
4546 .IP "my TYPE VARLIST : ATTRS" 4
4547 .IX Item "my TYPE VARLIST : ATTRS"
4548 .PD
4549 A \f(CW\*(C`my\*(C'\fR declares the listed variables to be local
4550 (lexically) to the enclosing block, file, or \f(CW\*(C`eval\*(C'\fR. If
4551 more than one variable is listed, the list must be placed in
4552 parentheses.
4553 .Sp
4554 Note that with a parenthesised list, \f(CW\*(C`undef\*(C'\fR can be used
4555 as a dummy placeholder, for example to skip assignment of initial
4556 values:
4557 .Sp
4558 .Vb 1
4559 \& my ( undef, $min, $hour ) = localtime;
4560 .Ve
4561 .Sp
4562 Like \f(CW\*(C`state\*(C'\fR, \f(CW\*(C`local\*(C'\fR, and
4563 \&\f(CW\*(C`our\*(C'\fR, \f(CW\*(C`my\*(C'\fR can operate on a variable
4564 anywhere it appears in an expression (aside from interpolation in strings).
4565 The declaration will not apply to additional uses of the same variable until
4566 the next statement. This means additional uses of that variable within the
4567 same statement will act as they would have before that declaration occurred,
4568 or result in a strict 'vars' error, as appropriate.
4569 .Sp
4570 .Vb 4
4571 \& package main;
4572 \& our $x = 2;
4573 \& foo($x, my $x = $x + 1, $x); # foo() receives (2, 3, 2)
4574 \& foo($x, $main::x); # foo() receives (3, 2)
4575 .Ve
4576 .Sp
4577 Redeclaring a variable in the same scope or statement will "shadow" the
4578 previous declaration, creating a new instance and preventing access to
4579 the previous one. This is usually undesired and, if warnings are enabled,
4580 will result in a warning in the \f(CW\*(C`shadow\*(C'\fR category.
4581 .Sp
4582 The exact semantics and interface of TYPE and ATTRS are still
4583 evolving. TYPE may be a bareword, a constant declared
4584 with \f(CW\*(C`use constant\*(C'\fR, or \f(CW\*(C`_\|_PACKAGE_\|_\*(C'\fR. It
4585 is
4586 currently bound to the use of the fields pragma,
4587 and attributes are handled using the attributes pragma, or starting
4588 from Perl 5.8.0 also via the Attribute::Handlers module. See
4589 "Private Variables via \fBmy()\fR" in perlsub for details.
4590 .IP "next LABEL" 4
4591 .IX Xref "next continue"
4592 .IX Item "next LABEL"
4593 .PD 0
4594 .IP "next EXPR" 4
4595 .IX Item "next EXPR"
4596 .IP next 4
4597 .IX Item "next"
4598 .PD
4599 The \f(CW\*(C`next\*(C'\fR command is like the \f(CW\*(C`continue\*(C'\fR statement in
4600 C; it starts the next iteration of the loop:
4601 .Sp
4602 .Vb 4
4603 \& LINE: while (<STDIN>) {
4604 \& next LINE if /^#/; # discard comments
4605 \& #...
4606 \& }
4607 .Ve
4608 .Sp
4609 Note that if there were a \f(CW\*(C`continue\*(C'\fR block on the
4610 above, it would get
4611 executed even on discarded lines. If LABEL is omitted, the command
4612 refers to the innermost enclosing loop. The \f(CW\*(C`next EXPR\*(C'\fR form, available
4613 as of Perl 5.18.0, allows a label name to be computed at run time, being
4614 otherwise identical to \f(CW\*(C`next LABEL\*(C'\fR.
4615 .Sp
4616 \&\f(CW\*(C`next\*(C'\fR cannot return a value from a block that typically
4617 returns a value, such as \f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR, or \f(CW\*(C`do {}\*(C'\fR. It will perform
4618 its flow control behavior, which precludes any return value. It should not be
4619 used to exit a \f(CW\*(C`grep\*(C'\fR or \f(CW\*(C`map\*(C'\fR
4620 operation.
4621 .Sp
4622 Note that a block by itself is semantically identical to a loop
4623 that executes once. Thus \f(CW\*(C`next\*(C'\fR will exit such a block
4624 early.
4625 .Sp
4626 See also \f(CW\*(C`continue\*(C'\fR for an illustration of how
4627 \&\f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
4628 \&\f(CW\*(C`redo\*(C'\fR work.
4629 .Sp
4630 Unlike most named operators, this has the same precedence as assignment.
4631 It is also exempt from the looks-like-a-function rule, so
4632 \&\f(CW\*(C`next ("foo")."bar"\*(C'\fR will cause "bar" to be part of the argument to
4633 \&\f(CW\*(C`next\*(C'\fR.
4634 .IP "no MODULE VERSION LIST" 4
4635 .IX Xref "no declarations unimporting"
4636 .IX Item "no MODULE VERSION LIST"
4637 .PD 0
4638 .IP "no MODULE VERSION" 4
4639 .IX Item "no MODULE VERSION"
4640 .IP "no MODULE LIST" 4
4641 .IX Item "no MODULE LIST"
4642 .IP "no MODULE" 4
4643 .IX Item "no MODULE"
4644 .IP "no VERSION" 4
4645 .IX Item "no VERSION"
4646 .PD
4647 See the \f(CW\*(C`use\*(C'\fR function, of which
4648 \&\f(CW\*(C`no\*(C'\fR is the opposite.
4649 .IP "oct EXPR" 4
4650 .IX Xref "oct octal hex hexadecimal binary bin"
4651 .IX Item "oct EXPR"
4652 .PD 0
4653 .IP oct 4
4654 .IX Item "oct"
4655 .PD
4656 Interprets EXPR as an octal string and returns the corresponding
4657 value. An octal string consists of octal digits and, as of Perl 5.33.5,
4658 an optional \f(CW\*(C`0o\*(C'\fR or \f(CW\*(C`o\*(C'\fR prefix. Each octal digit may be preceded by
4659 a single underscore, which will be ignored.
4660 (If EXPR happens to start off with \f(CW\*(C`0x\*(C'\fR or \f(CW\*(C`x\*(C'\fR, interprets it as a
4661 hex string. If EXPR starts off with \f(CW\*(C`0b\*(C'\fR or \f(CW\*(C`b\*(C'\fR, it is interpreted as a
4662 binary string. Leading whitespace is ignored in all three cases.)
4663 The following will handle decimal, binary, octal, and hex in standard
4664 Perl notation:
4665 .Sp
4666 .Vb 1
4667 \& $val = oct($val) if $val =~ /^0/;
4668 .Ve
4669 .Sp
4670 If EXPR is omitted, uses \f(CW$_\fR. To go the other way
4671 (produce a number in octal), use \f(CW\*(C`sprintf\*(C'\fR or
4672 \&\f(CW\*(C`printf\*(C'\fR:
4673 .Sp
4674 .Vb 2
4675 \& my $dec_perms = (stat("filename"))[2] & 07777;
4676 \& my $oct_perm_str = sprintf "%o", $perms;
4677 .Ve
4678 .Sp
4679 The \f(CW\*(C`oct\*(C'\fR function is commonly used when a string such as
4680 \&\f(CW644\fR needs
4681 to be converted into a file mode, for example. Although Perl
4682 automatically converts strings into numbers as needed, this automatic
4683 conversion assumes base 10.
4684 .Sp
4685 Leading white space is ignored without warning, as too are any trailing
4686 non-digits, such as a decimal point (\f(CW\*(C`oct\*(C'\fR only handles
4687 non-negative integers, not negative integers or floating point).
4688 .IP "open FILEHANDLE,MODE,EXPR" 4
4689 .IX Xref "open pipe file, open fopen"
4690 .IX Item "open FILEHANDLE,MODE,EXPR"
4691 .PD 0
4692 .IP "open FILEHANDLE,MODE,EXPR,LIST" 4
4693 .IX Item "open FILEHANDLE,MODE,EXPR,LIST"
4694 .IP "open FILEHANDLE,MODE,REFERENCE" 4
4695 .IX Item "open FILEHANDLE,MODE,REFERENCE"
4696 .IP "open FILEHANDLE,EXPR" 4
4697 .IX Item "open FILEHANDLE,EXPR"
4698 .IP "open FILEHANDLE" 4
4699 .IX Item "open FILEHANDLE"
4700 .PD
4701 Associates an internal FILEHANDLE with the external file specified by
4702 EXPR. That filehandle will subsequently allow you to perform
4703 I/O operations on that file, such as reading from it or writing to it.
4704 .Sp
4705 Instead of a filename, you may specify an external command
4706 (plus an optional argument list) or a scalar reference, in order to open
4707 filehandles on commands or in-memory scalars, respectively.
4708 .Sp
4709 A thorough reference to \f(CW\*(C`open\*(C'\fR follows. For a gentler introduction to
4710 the basics of \f(CW\*(C`open\*(C'\fR, see also the perlopentut manual page.
4711 .RS 4
4712 .IP "Working with files" 4
4713 .IX Item "Working with files"
4714 Most often, \f(CW\*(C`open\*(C'\fR gets invoked with three arguments: the required
4715 FILEHANDLE (usually an empty scalar variable), followed by MODE (usually
4716 a literal describing the I/O mode the filehandle will use), and then the
4717 filename that the new filehandle will refer to.
4718 .RS 4
4719 .IP "Simple examples" 4
4720 .IX Item "Simple examples"
4721 Reading from a file:
4722 .Sp
4723 .Vb 2
4724 \& open(my $fh, "<", "input.txt")
4725 \& or die "Can\*(Aqt open < input.txt: $!";
4726 \&
4727 \& # Process every line in input.txt
4728 \& while (my $line = readline($fh)) {
4729 \& #
4730 \& # ... do something interesting with $line here ...
4731 \& #
4732 \& }
4733 .Ve
4734 .Sp
4735 or writing to one:
4736 .Sp
4737 .Vb 2
4738 \& open(my $fh, ">", "output.txt")
4739 \& or die "Can\*(Aqt open > output.txt: $!";
4740 \&
4741 \& print $fh "This line gets printed into output.txt.\en";
4742 .Ve
4743 .Sp
4744 For a summary of common filehandle operations such as these, see
4745 "Files and I/O" in perlintro.
4746 .IP "About filehandles" 4
4747 .IX Item "About filehandles"
4748 The first argument to \f(CW\*(C`open\*(C'\fR, labeled FILEHANDLE in this reference, is
4749 usually a scalar variable. (Exceptions exist, described in "Other
4750 considerations", below.) If the call to \f(CW\*(C`open\*(C'\fR succeeds, then the
4751 expression provided as FILEHANDLE will get assigned an open
4752 \&\fIfilehandle\fR. That filehandle provides an internal reference to the
4753 specified external file, conveniently stored in a Perl variable, and
4754 ready for I/O operations such as reading and writing.
4755 .IP "About modes" 4
4756 .IX Item "About modes"
4757 When calling \f(CW\*(C`open\*(C'\fR with three or more arguments, the second argument
4758 \&\-\- labeled MODE here \-\- defines the \fIopen mode\fR. MODE is usually a
4759 literal string comprising special characters that define the intended
4760 I/O role of the filehandle being created: whether it's read-only, or
4761 read-and-write, and so on.
4762 .Sp
4763 If MODE is \f(CW\*(C`<\*(C'\fR, the file is opened for input (read-only).
4764 If MODE is \f(CW\*(C`>\*(C'\fR, the file is opened for output, with existing files
4765 first being truncated ("clobbered") and nonexisting files newly created.
4766 If MODE is \f(CW\*(C`>>\*(C'\fR, the file is opened for appending, again being
4767 created if necessary.
4768 .Sp
4769 You can put a \f(CW\*(C`+\*(C'\fR in front of the \f(CW\*(C`>\*(C'\fR or \f(CW\*(C`<\*(C'\fR to
4770 indicate that you want both read and write access to the file; thus
4771 \&\f(CW\*(C`+<\*(C'\fR is almost always preferred for read/write updates\-\-the
4772 \&\f(CW\*(C`+>\*(C'\fR mode would clobber the file first. You can't usually use
4773 either read-write mode for updating textfiles, since they have
4774 variable-length records. See the \fB\-i\fR switch in
4775 perlrun for a better approach. The file is
4776 created with permissions of \f(CW0666\fR modified by the process's
4777 \&\f(CW\*(C`umask\*(C'\fR value.
4778 .Sp
4779 These various prefixes correspond to the \fBfopen\fR\|(3) modes of \f(CW\*(C`r\*(C'\fR,
4780 \&\f(CW\*(C`r+\*(C'\fR, \f(CW\*(C`w\*(C'\fR, \f(CW\*(C`w+\*(C'\fR, \f(CW\*(C`a\*(C'\fR, and \f(CW\*(C`a+\*(C'\fR.
4781 .Sp
4782 More examples of different modes in action:
4783 .Sp
4784 .Vb 3
4785 \& # Open a file for concatenation
4786 \& open(my $log, ">>", "/usr/spool/news/twitlog")
4787 \& or warn "Couldn\*(Aqt open log file; discarding input";
4788 \&
4789 \& # Open a file for reading and writing
4790 \& open(my $dbase, "+<", "dbase.mine")
4791 \& or die "Can\*(Aqt open \*(Aqdbase.mine\*(Aq for update: $!";
4792 .Ve
4793 .IP "Checking the return value" 4
4794 .IX Item "Checking the return value"
4795 Open returns nonzero on success, the undefined value otherwise. If the
4796 \&\f(CW\*(C`open\*(C'\fR involved a pipe, the return value happens to be the pid of the
4797 subprocess.
4798 .Sp
4799 When opening a file, it's seldom a good idea to continue if the request
4800 failed, so \f(CW\*(C`open\*(C'\fR is frequently used with \f(CW\*(C`die\*(C'\fR. Even if
4801 you want your code to do something other than \f(CW\*(C`die\*(C'\fR on a failed open,
4802 you should still always check the return value from opening a file.
4803 .RE
4804 .RS 4
4805 .RE
4806 .IP "Specifying I/O layers in MODE" 4
4807 .IX Item "Specifying I/O layers in MODE"
4808 You can use the three-argument form of open to specify
4809 I/O layers (sometimes referred to as "disciplines") to apply to the new
4810 filehandle. These affect how the input and output are processed (see
4811 open and
4812 PerlIO for more details). For example:
4813 .Sp
4814 .Vb 3
4815 \& # loads PerlIO::encoding automatically
4816 \& open(my $fh, "<:encoding(UTF\-8)", $filename)
4817 \& || die "Can\*(Aqt open UTF\-8 encoded $filename: $!";
4818 .Ve
4819 .Sp
4820 This opens the UTF8\-encoded file containing Unicode characters;
4821 see perluniintro. Note that if layers are specified in the
4822 three-argument form, then default layers stored in
4823 \&\f(CW\*(C`${^OPEN}\*(C'\fR
4824 (usually set by the open pragma or the switch \f(CW\*(C`\-CioD\*(C'\fR) are ignored.
4825 Those layers will also be ignored if you specify a colon with no name
4826 following it. In that case the default layer for the operating system
4827 (:raw on Unix, :crlf on Windows) is used.
4828 .Sp
4829 On some systems (in general, DOS\- and Windows-based systems)
4830 \&\f(CW\*(C`binmode\*(C'\fR is necessary when you're not
4831 working with a text file. For the sake of portability it is a good idea
4832 always to use it when appropriate, and never to use it when it isn't
4833 appropriate. Also, people can set their I/O to be by default
4834 UTF8\-encoded Unicode, not bytes.
4835 .ie n .IP "Using ""undef"" for temporary files" 4
4836 .el .IP "Using \f(CWundef\fR for temporary files" 4
4837 .IX Item "Using undef for temporary files"
4838 As a special case the three-argument form with a read/write mode and the third
4839 argument being \f(CW\*(C`undef\*(C'\fR:
4840 .Sp
4841 .Vb 1
4842 \& open(my $tmp, "+>", undef) or die ...
4843 .Ve
4844 .Sp
4845 opens a filehandle to a newly created empty anonymous temporary file.
4846 (This happens under any mode, which makes \f(CW\*(C`+>\*(C'\fR the only useful and
4847 sensible mode to use.) You will need to
4848 \&\f(CW\*(C`seek\*(C'\fR to do the reading.
4849 .IP "Opening a filehandle into an in-memory scalar" 4
4850 .IX Item "Opening a filehandle into an in-memory scalar"
4851 You can open filehandles directly to Perl scalars instead of a file or
4852 other resource external to the program. To do so, provide a reference to
4853 that scalar as the third argument to \f(CW\*(C`open\*(C'\fR, like so:
4854 .Sp
4855 .Vb 3
4856 \& open(my $memory, ">", \e$var)
4857 \& or die "Can\*(Aqt open memory file: $!";
4858 \& print $memory "foo!\en"; # output will appear in $var
4859 .Ve
4860 .Sp
4861 To (re)open \f(CW\*(C`STDOUT\*(C'\fR or \f(CW\*(C`STDERR\*(C'\fR as an in-memory file, close it first:
4862 .Sp
4863 .Vb 3
4864 \& close STDOUT;
4865 \& open(STDOUT, ">", \e$variable)
4866 \& or die "Can\*(Aqt open STDOUT: $!";
4867 .Ve
4868 .Sp
4869 The scalars for in-memory files are treated as octet strings: unless
4870 the file is being opened with truncation the scalar may not contain
4871 any code points over 0xFF.
4872 .Sp
4873 Opening in-memory files \fIcan\fR fail for a variety of reasons. As with
4874 any other \f(CW\*(C`open\*(C'\fR, check the return value for success.
4875 .Sp
4876 \&\fITechnical note\fR: This feature works only when Perl is built with
4877 PerlIO \-\- the default, except with older (pre\-5.16) Perl installations
4878 that were configured to not include it (e.g. via \f(CW\*(C`Configure
4879 \&\-Uuseperlio\*(C'\fR). You can see whether your Perl was built with PerlIO by
4880 running \f(CW\*(C`perl \-V:useperlio\*(C'\fR. If it says \f(CW\*(Aqdefine\*(Aq\fR, you have PerlIO;
4881 otherwise you don't.
4882 .Sp
4883 See perliol for detailed info on PerlIO.
4884 .IP "Opening a filehandle into a command" 4
4885 .IX Item "Opening a filehandle into a command"
4886 If MODE is \f(CW\*(C`|\-\*(C'\fR, then the filename is
4887 interpreted as a command to which output is to be piped, and if MODE
4888 is \f(CW\*(C`\-|\*(C'\fR, the filename is interpreted as a command that pipes
4889 output to us. In the two-argument (and one-argument) form, one should
4890 replace dash (\f(CW\*(C`\-\*(C'\fR) with the command.
4891 See "Using \fBopen()\fR for IPC" in perlipc for more examples of this.
4892 (You are not allowed to \f(CW\*(C`open\*(C'\fR to a command
4893 that pipes both in \fIand\fR out, but see IPC::Open2, IPC::Open3, and
4894 "Bidirectional Communication with Another Process" in perlipc for
4895 alternatives.)
4896 .Sp
4897 .Vb 3
4898 \& open(my $article_fh, "\-|", "caesar <$article") # decrypt
4899 \& # article
4900 \& or die "Can\*(Aqt start caesar: $!";
4901 \&
4902 \& open(my $article_fh, "caesar <$article |") # ditto
4903 \& or die "Can\*(Aqt start caesar: $!";
4904 \&
4905 \& open(my $out_fh, "|\-", "sort >Tmp$$") # $$ is our process id
4906 \& or die "Can\*(Aqt start sort: $!";
4907 .Ve
4908 .Sp
4909 In the form of pipe opens taking three or more arguments, if LIST is specified
4910 (extra arguments after the command name) then LIST becomes arguments
4911 to the command invoked if the platform supports it. The meaning of
4912 \&\f(CW\*(C`open\*(C'\fR with more than three arguments for
4913 non-pipe modes is not yet defined, but experimental "layers" may give
4914 extra LIST arguments meaning.
4915 .Sp
4916 If you open a pipe on the command \f(CW\*(C`\-\*(C'\fR (that is, specify either \f(CW\*(C`|\-\*(C'\fR or \f(CW\*(C`\-|\*(C'\fR
4917 with the one\- or two-argument forms of
4918 \&\f(CW\*(C`open\*(C'\fR), an implicit \f(CW\*(C`fork\*(C'\fR is done,
4919 so \f(CW\*(C`open\*(C'\fR returns twice: in the parent process
4920 it returns the pid
4921 of the child process, and in the child process it returns (a defined) \f(CW0\fR.
4922 Use \f(CWdefined($pid)\fR or \f(CW\*(C`//\*(C'\fR to determine whether the open was successful.
4923 .Sp
4924 For example, use either
4925 .Sp
4926 .Vb 2
4927 \& my $child_pid = open(my $from_kid, "\-|")
4928 \& // die "Can\*(Aqt fork: $!";
4929 .Ve
4930 .Sp
4931 or
4932 .Sp
4933 .Vb 2
4934 \& my $child_pid = open(my $to_kid, "|\-")
4935 \& // die "Can\*(Aqt fork: $!";
4936 .Ve
4937 .Sp
4938 followed by
4939 .Sp
4940 .Vb 10
4941 \& if ($child_pid) {
4942 \& # am the parent:
4943 \& # either write $to_kid or else read $from_kid
4944 \& ...
4945 \& waitpid $child_pid, 0;
4946 \& } else {
4947 \& # am the child; use STDIN/STDOUT normally
4948 \& ...
4949 \& exit;
4950 \& }
4951 .Ve
4952 .Sp
4953 The filehandle behaves normally for the parent, but I/O to that
4954 filehandle is piped from/to the STDOUT/STDIN of the child process.
4955 In the child process, the filehandle isn't opened\-\-I/O happens from/to
4956 the new STDOUT/STDIN. Typically this is used like the normal
4957 piped open when you want to exercise more control over just how the
4958 pipe command gets executed, such as when running setuid and
4959 you don't want to have to scan shell commands for metacharacters.
4960 .Sp
4961 The following blocks are more or less equivalent:
4962 .Sp
4963 .Vb 4
4964 \& open(my $fh, "|tr \*(Aq[a\-z]\*(Aq \*(Aq[A\-Z]\*(Aq");
4965 \& open(my $fh, "|\-", "tr \*(Aq[a\-z]\*(Aq \*(Aq[A\-Z]\*(Aq");
4966 \& open(my $fh, "|\-") || exec \*(Aqtr\*(Aq, \*(Aq[a\-z]\*(Aq, \*(Aq[A\-Z]\*(Aq;
4967 \& open(my $fh, "|\-", "tr", \*(Aq[a\-z]\*(Aq, \*(Aq[A\-Z]\*(Aq);
4968 \&
4969 \& open(my $fh, "cat \-n \*(Aq$file\*(Aq|");
4970 \& open(my $fh, "\-|", "cat \-n \*(Aq$file\*(Aq");
4971 \& open(my $fh, "\-|") || exec "cat", "\-n", $file;
4972 \& open(my $fh, "\-|", "cat", "\-n", $file);
4973 .Ve
4974 .Sp
4975 The last two examples in each block show the pipe as "list form", which
4976 is not yet supported on all platforms. (If your platform has a real
4977 \&\f(CW\*(C`fork\*(C'\fR, such as Linux and macOS, you can use the list form; it
4978 also works on Windows with Perl 5.22 or later.) You would want to use
4979 the list form of the pipe so you can pass literal arguments to the
4980 command without risk of the shell interpreting any shell metacharacters
4981 in them. However, this also bars you from opening pipes to commands that
4982 intentionally contain shell metacharacters, such as:
4983 .Sp
4984 .Vb 2
4985 \& open(my $fh, "|cat \-n | expand \-4 | lpr")
4986 \& || die "Can\*(Aqt open pipeline to lpr: $!";
4987 .Ve
4988 .Sp
4989 See "Safe Pipe Opens" in perlipc for more examples of this.
4990 .IP "Duping filehandles" 4
4991 .IX Item "Duping filehandles"
4992 You may also, in the Bourne shell tradition, specify an EXPR beginning
4993 with \f(CW\*(C`>&\*(C'\fR, in which case the rest of the string is interpreted
4994 as the name of a filehandle (or file descriptor, if numeric) to be
4995 duped (as in \fBdup\fR\|(2)) and opened. You may use \f(CW\*(C`&\*(C'\fR after \f(CW\*(C`>\*(C'\fR,
4996 \&\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.
4997 The mode you specify should match the mode of the original filehandle.
4998 (Duping a filehandle does not take into account any existing contents
4999 of IO buffers.) If you use the three-argument
5000 form, then you can pass either a
5001 number, the name of a filehandle, or the normal "reference to a glob".
5002 .Sp
5003 Here is a script that saves, redirects, and restores \f(CW\*(C`STDOUT\*(C'\fR and
5004 \&\f(CW\*(C`STDERR\*(C'\fR using various methods:
5005 .Sp
5006 .Vb 5
5007 \& #!/usr/bin/perl
5008 \& open(my $oldout, ">&STDOUT")
5009 \& or die "Can\*(Aqt dup STDOUT: $!";
5010 \& open(OLDERR, ">&", \e*STDERR)
5011 \& or die "Can\*(Aqt dup STDERR: $!";
5012 \&
5013 \& open(STDOUT, \*(Aq>\*(Aq, "foo.out")
5014 \& or die "Can\*(Aqt redirect STDOUT: $!";
5015 \& open(STDERR, ">&STDOUT")
5016 \& or die "Can\*(Aqt dup STDOUT: $!";
5017 \&
5018 \& select STDERR; $| = 1; # make unbuffered
5019 \& select STDOUT; $| = 1; # make unbuffered
5020 \&
5021 \& print STDOUT "stdout 1\en"; # this works for
5022 \& print STDERR "stderr 1\en"; # subprocesses too
5023 \&
5024 \& open(STDOUT, ">&", $oldout)
5025 \& or die "Can\*(Aqt dup \e$oldout: $!";
5026 \& open(STDERR, ">&OLDERR")
5027 \& or die "Can\*(Aqt dup OLDERR: $!";
5028 \&
5029 \& print STDOUT "stdout 2\en";
5030 \& print STDERR "stderr 2\en";
5031 .Ve
5032 .Sp
5033 If you specify \f(CW\*(Aq<&=X\*(Aq\fR, where \f(CW\*(C`X\*(C'\fR is a file descriptor number
5034 or a filehandle, then Perl will do an equivalent of C's \fBfdopen\fR\|(3) of
5035 that file descriptor (and not call \fBdup\fR\|(2)); this is more
5036 parsimonious of file descriptors. For example:
5037 .Sp
5038 .Vb 2
5039 \& # open for input, reusing the fileno of $fd
5040 \& open(my $fh, "<&=", $fd)
5041 .Ve
5042 .Sp
5043 or
5044 .Sp
5045 .Vb 1
5046 \& open(my $fh, "<&=$fd")
5047 .Ve
5048 .Sp
5049 or
5050 .Sp
5051 .Vb 2
5052 \& # open for append, using the fileno of $oldfh
5053 \& open(my $fh, ">>&=", $oldfh)
5054 .Ve
5055 .Sp
5056 Being parsimonious on filehandles is also useful (besides being
5057 parsimonious) for example when something is dependent on file
5058 descriptors, like for example locking using
5059 \&\f(CW\*(C`flock\*(C'\fR. If you do just
5060 \&\f(CW\*(C`open(my $A, ">>&", $B)\*(C'\fR, the filehandle \f(CW$A\fR will not have the
5061 same file descriptor as \f(CW$B\fR, and therefore \f(CWflock($A)\fR will not
5062 \&\f(CWflock($B)\fR nor vice versa. But with \f(CW\*(C`open(my $A, ">>&=", $B)\*(C'\fR,
5063 the filehandles will share the same underlying system file descriptor.
5064 .Sp
5065 Note that under Perls older than 5.8.0, Perl uses the standard C library's'
5066 \&\fBfdopen\fR\|(3) to implement the \f(CW\*(C`=\*(C'\fR functionality. On many Unix systems,
5067 \&\fBfdopen\fR\|(3) fails when file descriptors exceed a certain value, typically 255.
5068 For Perls 5.8.0 and later, PerlIO is (most often) the default.
5069 .IP "Legacy usage" 4
5070 .IX Item "Legacy usage"
5071 This section describes ways to call \f(CW\*(C`open\*(C'\fR outside of best practices;
5072 you may encounter these uses in older code. Perl does not consider their
5073 use deprecated, exactly, but neither is it recommended in new code, for
5074 the sake of clarity and readability.
5075 .RS 4
5076 .IP "Specifying mode and filename as a single argument" 4
5077 .IX Item "Specifying mode and filename as a single argument"
5078 In the one\- and two-argument forms of the call, the mode and filename
5079 should be concatenated (in that order), preferably separated by white
5080 space. You can\-\-but shouldn't\-\-omit the mode in these forms when that mode
5081 is \f(CW\*(C`<\*(C'\fR. It is safe to use the two-argument form of
5082 \&\f(CW\*(C`open\*(C'\fR if the filename argument is a known literal.
5083 .Sp
5084 .Vb 2
5085 \& open(my $dbase, "+<dbase.mine") # ditto
5086 \& or die "Can\*(Aqt open \*(Aqdbase.mine\*(Aq for update: $!";
5087 .Ve
5088 .Sp
5089 In the two-argument (and one-argument) form, opening \f(CW\*(C`<\-\*(C'\fR
5090 or \f(CW\*(C`\-\*(C'\fR opens STDIN and opening \f(CW\*(C`>\-\*(C'\fR opens STDOUT.
5091 .Sp
5092 New code should favor the three-argument form of \f(CW\*(C`open\*(C'\fR over this older
5093 form. Declaring the mode and the filename as two distinct arguments
5094 avoids any confusion between the two.
5095 .IP "Assigning a filehandle to a bareword" 4
5096 .IX Item "Assigning a filehandle to a bareword"
5097 An older style is to use a bareword as the filehandle, as
5098 .Sp
5099 .Vb 2
5100 \& open(FH, "<", "input.txt")
5101 \& or die "Can\*(Aqt open < input.txt: $!";
5102 .Ve
5103 .Sp
5104 Then you can use \f(CW\*(C`FH\*(C'\fR as the filehandle, in \f(CW\*(C`close FH\*(C'\fR and \f(CW\*(C`<FH>\*(C'\fR and so on. Note that it's a global variable, so this form is
5105 not recommended when dealing with filehandles other than Perl's built-in ones
5106 (e.g. STDOUT and STDIN). In fact, using a bareword for the filehandle is
5107 an error when the
5108 \&\f(CW"bareword_filehandles"\fR feature
5109 has been disabled. This feature is disabled automatically when in the
5110 scope of \f(CW\*(C`use v5.36.0\*(C'\fR or later.
5111 .ie n .IP "Calling ""open"" with one argument via global variables" 4
5112 .el .IP "Calling \f(CWopen\fR with one argument via global variables" 4
5113 .IX Item "Calling open with one argument via global variables"
5114 As a shortcut, a one-argument call takes the filename from the global
5115 scalar variable of the same name as the bareword filehandle:
5116 .Sp
5117 .Vb 3
5118 \& $ARTICLE = 100;
5119 \& open(ARTICLE)
5120 \& or die "Can\*(Aqt find article $ARTICLE: $!\en";
5121 .Ve
5122 .Sp
5123 Here \f(CW$ARTICLE\fR must be a global scalar variable in the same package
5124 as the filehandle \- not one declared with \f(CW\*(C`my\*(C'\fR or
5125 \&\f(CW\*(C`state\*(C'\fR.
5126 .RE
5127 .RS 4
5128 .RE
5129 .IP "Other considerations" 4
5130 .IX Item "Other considerations"
5131 .RS 4
5132 .PD 0
5133 .IP "Automatic filehandle closure" 4
5134 .IX Item "Automatic filehandle closure"
5135 .PD
5136 The filehandle will be closed when its reference count reaches zero. If
5137 it is a lexically scoped variable declared with \f(CW\*(C`my\*(C'\fR,
5138 that usually means the end of the enclosing scope. However, this
5139 automatic close does not check for errors, so it is better to explicitly
5140 close filehandles, especially those used for writing:
5141 .Sp
5142 .Vb 2
5143 \& close($handle)
5144 \& || warn "close failed: $!";
5145 .Ve
5146 .IP "Automatic pipe flushing" 4
5147 .IX Item "Automatic pipe flushing"
5148 Perl will attempt to flush all files opened for
5149 output before any operation that may do a fork, but this may not be
5150 supported on some platforms (see perlport). To be safe, you may need
5151 to set \f(CW$|\fR (\f(CW$AUTOFLUSH\fR in English)
5152 or call the \f(CW\*(C`autoflush\*(C'\fR method of \f(CW\*(C`IO::Handle\*(C'\fR
5153 on any open handles.
5154 .Sp
5155 On systems that support a close-on-exec flag on files, the flag will
5156 be set for the newly opened file descriptor as determined by the value
5157 of \f(CW$^F\fR. See "$^F" in perlvar.
5158 .Sp
5159 Closing any piped filehandle causes the parent process to wait for the
5160 child to finish, then returns the status value in \f(CW$?\fR and
5161 \&\f(CW\*(C`${^CHILD_ERROR_NATIVE}\*(C'\fR.
5162 .IP "Direct versus by-reference assignment of filehandles" 4
5163 .IX Item "Direct versus by-reference assignment of filehandles"
5164 If FILEHANDLE \-\- the first argument in a call to \f(CW\*(C`open\*(C'\fR \-\- is an
5165 undefined scalar variable (or array or hash element), a new filehandle
5166 is autovivified, meaning that the variable is assigned a reference to a
5167 newly allocated anonymous filehandle. Otherwise if FILEHANDLE is an
5168 expression, its value is the real filehandle. (This is considered a
5169 symbolic reference, so \f(CW\*(C`use strict "refs"\*(C'\fR should \fInot\fR be in effect.)
5170 .IP "Whitespace and special characters in the filename argument" 4
5171 .IX Item "Whitespace and special characters in the filename argument"
5172 The filename passed to the one\- and two-argument forms of
5173 \&\f(CW\*(C`open\*(C'\fR will
5174 have leading and trailing whitespace deleted and normal
5175 redirection characters honored. This property, known as "magic open",
5176 can often be used to good effect. A user could specify a filename of
5177 \&\fI"rsh cat file |"\fR, or you could change certain filenames as needed:
5178 .Sp
5179 .Vb 3
5180 \& $filename =~ s/(.*\e.gz)\es*$/gzip \-dc < $1|/;
5181 \& open(my $fh, $filename)
5182 \& or die "Can\*(Aqt open $filename: $!";
5183 .Ve
5184 .Sp
5185 Use the three-argument form to open a file with arbitrary weird characters in it,
5186 .Sp
5187 .Vb 2
5188 \& open(my $fh, "<", $file)
5189 \& || die "Can\*(Aqt open $file: $!";
5190 .Ve
5191 .Sp
5192 otherwise it's necessary to protect any leading and trailing whitespace:
5193 .Sp
5194 .Vb 3
5195 \& $file =~ s#^(\es)#./$1#;
5196 \& open(my $fh, "< $file\e0")
5197 \& || die "Can\*(Aqt open $file: $!";
5198 .Ve
5199 .Sp
5200 (this may not work on some bizarre filesystems). One should
5201 conscientiously choose between the \fImagic\fR and \fIthree-argument\fR form
5202 of \f(CW\*(C`open\*(C'\fR:
5203 .Sp
5204 .Vb 1
5205 \& open(my $in, $ARGV[0]) || die "Can\*(Aqt open $ARGV[0]: $!";
5206 .Ve
5207 .Sp
5208 will allow the user to specify an argument of the form \f(CW"rsh cat file |"\fR,
5209 but will not work on a filename that happens to have a trailing space, while
5210 .Sp
5211 .Vb 2
5212 \& open(my $in, "<", $ARGV[0])
5213 \& || die "Can\*(Aqt open $ARGV[0]: $!";
5214 .Ve
5215 .Sp
5216 will have exactly the opposite restrictions. (However, some shells
5217 support the syntax \f(CW\*(C`perl your_program.pl <( rsh cat file )\*(C'\fR, which
5218 produces a filename that can be opened normally.)
5219 .ie n .IP "Invoking C\-style ""open""" 4
5220 .el .IP "Invoking C\-style \f(CWopen\fR" 4
5221 .IX Item "Invoking C-style open"
5222 If you want a "real" C \fBopen\fR\|(2), then you should use the
5223 \&\f(CW\*(C`sysopen\*(C'\fR function, which involves
5224 no such magic (but uses different filemodes than Perl
5225 \&\f(CW\*(C`open\*(C'\fR, which corresponds to C \fBfopen\fR\|(3)).
5226 This is another way to protect your filenames from interpretation. For
5227 example:
5228 .Sp
5229 .Vb 7
5230 \& use IO::Handle;
5231 \& sysopen(my $fh, $path, O_RDWR|O_CREAT|O_EXCL)
5232 \& or die "Can\*(Aqt open $path: $!";
5233 \& $fh\->autoflush(1);
5234 \& print $fh "stuff $$\en";
5235 \& seek($fh, 0, 0);
5236 \& print "File contains: ", readline($fh);
5237 .Ve
5238 .Sp
5239 See \f(CW\*(C`seek\*(C'\fR for some details about
5240 mixing reading and writing.
5241 .IP "Portability issues" 4
5242 .IX Item "Portability issues"
5243 See "open" in perlport.
5244 .RE
5245 .RS 4
5246 .RE
5247 .RE
5248 .RS 4
5249 .RE
5250 .IP "opendir DIRHANDLE,EXPR" 4
5251 .IX Xref "opendir"
5252 .IX Item "opendir DIRHANDLE,EXPR"
5253 Opens a directory named EXPR for processing by
5254 \&\f(CW\*(C`readdir\*(C'\fR, \f(CW\*(C`telldir\*(C'\fR,
5255 \&\f(CW\*(C`seekdir\*(C'\fR,
5256 \&\f(CW\*(C`rewinddir\*(C'\fR, and
5257 \&\f(CW\*(C`closedir\*(C'\fR. Returns true if successful.
5258 DIRHANDLE may be an expression whose value can be used as an indirect
5259 dirhandle, usually the real dirhandle name. If DIRHANDLE is an undefined
5260 scalar variable (or array or hash element), the variable is assigned a
5261 reference to a new anonymous dirhandle; that is, it's autovivified.
5262 Dirhandles are the same objects as filehandles; an I/O object can only
5263 be open as one of these handle types at once.
5264 .Sp
5265 See the example at \f(CW\*(C`readdir\*(C'\fR.
5266 .IP "ord EXPR" 4
5267 .IX Xref "ord encoding"
5268 .IX Item "ord EXPR"
5269 .PD 0
5270 .IP ord 4
5271 .IX Item "ord"
5272 .PD
5273 Returns the code point of the first character of EXPR.
5274 If EXPR is an empty string, returns 0. If EXPR is omitted, uses
5275 \&\f(CW$_\fR.
5276 (Note \fIcharacter\fR, not byte.)
5277 .Sp
5278 For the reverse, see \f(CW\*(C`chr\*(C'\fR.
5279 See perlunicode for more about Unicode.
5280 .IP "our VARLIST" 4
5281 .IX Xref "our global"
5282 .IX Item "our VARLIST"
5283 .PD 0
5284 .IP "our TYPE VARLIST" 4
5285 .IX Item "our TYPE VARLIST"
5286 .IP "our VARLIST : ATTRS" 4
5287 .IX Item "our VARLIST : ATTRS"
5288 .IP "our TYPE VARLIST : ATTRS" 4
5289 .IX Item "our TYPE VARLIST : ATTRS"
5290 .PD
5291 \&\f(CW\*(C`our\*(C'\fR makes a lexical alias to a package (i.e. global)
5292 variable of the same name in the current package for use within the
5293 current lexical scope.
5294 .Sp
5295 \&\f(CW\*(C`our\*(C'\fR has the same scoping rules as
5296 \&\f(CW\*(C`my\*(C'\fR or \f(CW\*(C`state\*(C'\fR, meaning that it is
5297 only valid within a lexical scope. Unlike \f(CW\*(C`my\*(C'\fR and
5298 \&\f(CW\*(C`state\*(C'\fR, which both declare new (lexical) variables,
5299 \&\f(CW\*(C`our\*(C'\fR only creates an alias to an existing variable: a
5300 package variable of the same name.
5301 .Sp
5302 This means that when \f(CW\*(C`use strict \*(Aqvars\*(Aq\*(C'\fR is in effect, \f(CW\*(C`our\*(C'\fR lets you use a package variable without qualifying it with the
5303 package name, but only within the lexical scope of the
5304 \&\f(CW\*(C`our\*(C'\fR declaration. This applies immediately\-\-even
5305 within the same statement.
5306 .Sp
5307 .Vb 2
5308 \& package Foo;
5309 \& use v5.36; # which implies "use strict;"
5310 \&
5311 \& $Foo::foo = 23;
5312 \&
5313 \& {
5314 \& our $foo; # alias to $Foo::foo
5315 \& print $foo; # prints 23
5316 \& }
5317 \&
5318 \& print $Foo::foo; # prints 23
5319 \&
5320 \& print $foo; # ERROR: requires explicit package name
5321 .Ve
5322 .Sp
5323 This works even if the package variable has not been used before, as
5324 package variables spring into existence when first used.
5325 .Sp
5326 .Vb 2
5327 \& package Foo;
5328 \& use v5.36;
5329 \&
5330 \& our $foo = 23; # just like $Foo::foo = 23
5331 \&
5332 \& print $Foo::foo; # prints 23
5333 .Ve
5334 .Sp
5335 Because the variable becomes legal immediately under \f(CW\*(C`use strict \*(Aqvars\*(Aq\*(C'\fR, so
5336 long as there is no variable with that name is already in scope, you can then
5337 reference the package variable again even within the same statement.
5338 .Sp
5339 .Vb 2
5340 \& package Foo;
5341 \& use v5.36;
5342 \&
5343 \& my $foo = $foo; # error, undeclared $foo on right\-hand side
5344 \& our $foo = $foo; # no errors
5345 .Ve
5346 .Sp
5347 If more than one variable is listed, the list must be placed
5348 in parentheses.
5349 .Sp
5350 .Vb 1
5351 \& our($bar, $baz);
5352 .Ve
5353 .Sp
5354 Like \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`state\*(C'\fR, and
5355 \&\f(CW\*(C`local\*(C'\fR, \f(CW\*(C`our\*(C'\fR can operate on a variable
5356 anywhere it appears in an expression (aside from interpolation in strings).
5357 The declaration will not apply to additional uses of the same variable until
5358 the next statement. This means additional uses of that variable within the
5359 same statement will act as they would have before that declaration occurred,
5360 with the exception that it will still satisfy strict 'vars' and interpret that
5361 variable as the newly aliased package variable if it was not yet declared in
5362 that scope.
5363 .Sp
5364 .Vb 4
5365 \& package main;
5366 \& my $x = 2;
5367 \& foo($x, our $x = $x + 1, $x); # foo() receives (2, 3, 2)
5368 \& foo($x, our $z = 5, $z); # foo() receives (3, 5, 5)
5369 .Ve
5370 .Sp
5371 An \f(CW\*(C`our\*(C'\fR declaration declares an alias for a package
5372 variable that will be visible
5373 across its entire lexical scope, even across package boundaries. The
5374 package in which the variable is entered is determined at the point
5375 of the declaration, not at the point of use. This means the following
5376 behavior holds:
5377 .Sp
5378 .Vb 3
5379 \& package Foo;
5380 \& our $bar; # declares $Foo::bar for rest of lexical scope
5381 \& $bar = 20;
5382 \&
5383 \& package Bar;
5384 \& print $bar; # prints 20, as it refers to $Foo::bar
5385 .Ve
5386 .Sp
5387 Multiple \f(CW\*(C`our\*(C'\fR declarations with the same name in the
5388 same lexical
5389 scope are allowed if they are in different packages. If they happen
5390 to be in the same package, Perl will emit warnings if you have asked
5391 for them, just like multiple \f(CW\*(C`my\*(C'\fR declarations. Unlike
5392 a second \f(CW\*(C`my\*(C'\fR declaration, which will bind the name to a
5393 fresh variable, a second \f(CW\*(C`our\*(C'\fR declaration in the same
5394 package, in the same scope, is merely redundant.
5395 .Sp
5396 .Vb 4
5397 \& use warnings;
5398 \& package Foo;
5399 \& our $bar; # declares $Foo::bar for rest of lexical scope
5400 \& $bar = 20;
5401 \&
5402 \& package Bar;
5403 \& our $bar = 30; # declares $Bar::bar for rest of lexical scope
5404 \& print $bar; # prints 30
5405 \&
5406 \& our $bar; # emits warning but has no other effect
5407 \& print $bar; # still prints 30
5408 .Ve
5409 .Sp
5410 An \f(CW\*(C`our\*(C'\fR declaration may also have a list of attributes
5411 associated with it.
5412 .Sp
5413 The exact semantics and interface of TYPE and ATTRS are still
5414 evolving. TYPE is currently bound to the use of the fields pragma,
5415 and attributes are handled using the attributes pragma, or, starting
5416 from Perl 5.8.0, also via the Attribute::Handlers module. See
5417 "Private Variables via \fBmy()\fR" in perlsub for details.
5418 .Sp
5419 Note that with a parenthesised list, \f(CW\*(C`undef\*(C'\fR can be used
5420 as a dummy placeholder, for example to skip assignment of initial
5421 values:
5422 .Sp
5423 .Vb 1
5424 \& our ( undef, $min, $hour ) = localtime;
5425 .Ve
5426 .Sp
5427 \&\f(CW\*(C`our\*(C'\fR differs from \f(CW\*(C`use vars\*(C'\fR, which allows
5428 use of an unqualified name \fIonly\fR within the affected package, but
5429 across scopes.
5430 .IP "pack TEMPLATE,LIST" 4
5431 .IX Xref "pack"
5432 .IX Item "pack TEMPLATE,LIST"
5433 Takes a LIST of values and converts it into a string using the rules
5434 given by the TEMPLATE. The resulting string is the concatenation of
5435 the converted values. Typically, each converted value looks
5436 like its machine-level representation. For example, on 32\-bit machines
5437 an integer may be represented by a sequence of 4 bytes, which will in
5438 Perl be presented as a string that's 4 characters long.
5439 .Sp
5440 See perlpacktut for an introduction to this function.
5441 .Sp
5442 The TEMPLATE is a sequence of characters that give the order and type
5443 of values, as follows:
5444 .Sp
5445 .Vb 3
5446 \& a A string with arbitrary binary data, will be null padded.
5447 \& A A text (ASCII) string, will be space padded.
5448 \& Z A null\-terminated (ASCIZ) string, will be null padded.
5449 \&
5450 \& b A bit string (ascending bit order inside each byte,
5451 \& like vec()).
5452 \& B A bit string (descending bit order inside each byte).
5453 \& h A hex string (low nybble first).
5454 \& H A hex string (high nybble first).
5455 \&
5456 \& c A signed char (8\-bit) value.
5457 \& C An unsigned char (octet) value.
5458 \& W An unsigned char value (can be greater than 255).
5459 \&
5460 \& s A signed short (16\-bit) value.
5461 \& S An unsigned short value.
5462 \&
5463 \& l A signed long (32\-bit) value.
5464 \& L An unsigned long value.
5465 \&
5466 \& q A signed quad (64\-bit) value.
5467 \& Q An unsigned quad value.
5468 \& (Quads are available only if your system supports 64\-bit
5469 \& integer values _and_ if Perl has been compiled to support
5470 \& those. Raises an exception otherwise.)
5471 \&
5472 \& i A signed integer value.
5473 \& I An unsigned integer value.
5474 \& (This \*(Aqinteger\*(Aq is _at_least_ 32 bits wide. Its exact
5475 \& size depends on what a local C compiler calls \*(Aqint\*(Aq.)
5476 \&
5477 \& n An unsigned short (16\-bit) in "network" (big\-endian) order.
5478 \& N An unsigned long (32\-bit) in "network" (big\-endian) order.
5479 \& v An unsigned short (16\-bit) in "VAX" (little\-endian) order.
5480 \& V An unsigned long (32\-bit) in "VAX" (little\-endian) order.
5481 \&
5482 \& j A Perl internal signed integer value (IV).
5483 \& J A Perl internal unsigned integer value (UV).
5484 \&
5485 \& f A single\-precision float in native format.
5486 \& d A double\-precision float in native format.
5487 \&
5488 \& F A Perl internal floating\-point value (NV) in native format
5489 \& D A float of long\-double precision in native format.
5490 \& (Long doubles are available only if your system supports
5491 \& long double values. Raises an exception otherwise.
5492 \& Note that there are different long double formats.)
5493 \&
5494 \& p A pointer to a null\-terminated string.
5495 \& P A pointer to a structure (fixed\-length string).
5496 \&
5497 \& u A uuencoded string.
5498 \& U A Unicode character number. Encodes to a character in char\-
5499 \& acter mode and UTF\-8 (or UTF\-EBCDIC in EBCDIC platforms) in
5500 \& byte mode. Also on EBCDIC platforms, the character number will
5501 \& be the native EBCDIC value for character numbers below 256.
5502 \& This allows most programs using this feature to not have to
5503 \& care which type of platform they are running on.
5504 \&
5505 \& w A BER compressed integer (not an ASN.1 BER, see perlpacktut
5506 \& for details). Its bytes represent an unsigned integer in
5507 \& base 128, most significant digit first, with as few digits
5508 \& as possible. Bit eight (the high bit) is set on each byte
5509 \& except the last.
5510 \&
5511 \& x A null byte (a.k.a ASCII NUL, "\e000", chr(0))
5512 \& X Back up a byte.
5513 \& @ Null\-fill or truncate to absolute position, counted from the
5514 \& start of the innermost ()\-group.
5515 \& . Null\-fill or truncate to absolute position specified by
5516 \& the value.
5517 \& ( Start of a ()\-group.
5518 .Ve
5519 .Sp
5520 One or more modifiers below may optionally follow certain letters in the
5521 TEMPLATE (the second column lists letters for which the modifier is valid):
5522 .Sp
5523 .Vb 2
5524 \& ! sSlLiI Forces native (short, long, int) sizes instead
5525 \& of fixed (16\-/32\-bit) sizes.
5526 \&
5527 \& ! xX Make x and X act as alignment commands.
5528 \&
5529 \& ! nNvV Treat integers as signed instead of unsigned.
5530 \&
5531 \& ! @. Specify position as byte offset in the internal
5532 \& representation of the packed string. Efficient
5533 \& but dangerous.
5534 \&
5535 \& > sSiIlLqQ Force big\-endian byte\-order on the type.
5536 \& jJfFdDpP (The "big end" touches the construct.)
5537 \&
5538 \& < sSiIlLqQ Force little\-endian byte\-order on the type.
5539 \& jJfFdDpP (The "little end" touches the construct.)
5540 .Ve
5541 .Sp
5542 The \f(CW\*(C`>\*(C'\fR and \f(CW\*(C`<\*(C'\fR modifiers can also be used on \f(CW\*(C`()\*(C'\fR groups
5543 to force a particular byte-order on all components in that group,
5544 including all its subgroups.
5545 .Sp
5546 The following rules apply:
5547 .RS 4
5548 .IP \(bu 4
5549 Each letter may optionally be followed by a number indicating the repeat
5550 count. A numeric repeat count may optionally be enclosed in brackets, as
5551 in \f(CW\*(C`pack("C[80]", @arr)\*(C'\fR. The repeat count gobbles that many values from
5552 the LIST when used with all format types other than \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`A\*(C'\fR, \f(CW\*(C`Z\*(C'\fR, \f(CW\*(C`b\*(C'\fR,
5553 \&\f(CW\*(C`B\*(C'\fR, \f(CW\*(C`h\*(C'\fR, \f(CW\*(C`H\*(C'\fR, \f(CW\*(C`@\*(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, where it means
5554 something else, described below. Supplying a \f(CW\*(C`*\*(C'\fR for the repeat count
5555 instead of a number means to use however many items are left, except for:
5556 .RS 4
5557 .IP \(bu 4
5558 \&\f(CW\*(C`@\*(C'\fR, \f(CW\*(C`x\*(C'\fR, and \f(CW\*(C`X\*(C'\fR, where it is equivalent to \f(CW0\fR.
5559 .IP \(bu 4
5560 <.>, where it means relative to the start of the string.
5561 .IP \(bu 4
5562 \&\f(CW\*(C`u\*(C'\fR, where it is equivalent to 1 (or 45, which here is equivalent).
5563 .RE
5564 .RS 4
5565 .Sp
5566 One can replace a numeric repeat count with a template letter enclosed in
5567 brackets to use the packed byte length of the bracketed template for the
5568 repeat count.
5569 .Sp
5570 For example, the template \f(CW\*(C`x[L]\*(C'\fR skips as many bytes as in a packed long,
5571 and the template \f(CW"$t X[$t] $t"\fR unpacks twice whatever \f(CW$t\fR (when
5572 variable-expanded) unpacks. If the template in brackets contains alignment
5573 commands (such as \f(CW\*(C`x![d]\*(C'\fR), its packed length is calculated as if the
5574 start of the template had the maximal possible alignment.
5575 .Sp
5576 When used with \f(CW\*(C`Z\*(C'\fR, a \f(CW\*(C`*\*(C'\fR as the repeat count is guaranteed to add a
5577 trailing null byte, so the resulting string is always one byte longer than
5578 the byte length of the item itself.
5579 .Sp
5580 When used with \f(CW\*(C`@\*(C'\fR, the repeat count represents an offset from the start
5581 of the innermost \f(CW\*(C`()\*(C'\fR group.
5582 .Sp
5583 When used with \f(CW\*(C`.\*(C'\fR, the repeat count determines the starting position to
5584 calculate the value offset as follows:
5585 .IP \(bu 4
5586 If the repeat count is \f(CW0\fR, it's relative to the current position.
5587 .IP \(bu 4
5588 If the repeat count is \f(CW\*(C`*\*(C'\fR, the offset is relative to the start of the
5589 packed string.
5590 .IP \(bu 4
5591 And if it's an integer \fIn\fR, the offset is relative to the start of the
5592 \&\fIn\fRth innermost \f(CW\*(C`( )\*(C'\fR group, or to the start of the string if \fIn\fR is
5593 bigger then the group level.
5594 .RE
5595 .RS 4
5596 .Sp
5597 The repeat count for \f(CW\*(C`u\*(C'\fR is interpreted as the maximal number of bytes
5598 to encode per line of output, with 0, 1 and 2 replaced by 45. The repeat
5599 count should not be more than 65.
5600 .RE
5601 .IP \(bu 4
5602 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
5603 string of length count, padding with nulls or spaces as needed. When
5604 unpacking, \f(CW\*(C`A\*(C'\fR strips trailing whitespace and nulls, \f(CW\*(C`Z\*(C'\fR strips everything
5605 after the first null, and \f(CW\*(C`a\*(C'\fR returns data with no stripping at all.
5606 .Sp
5607 If the value to pack is too long, the result is truncated. If it's too
5608 long and an explicit count is provided, \f(CW\*(C`Z\*(C'\fR packs only \f(CW\*(C`$count\-1\*(C'\fR bytes,
5609 followed by a null byte. Thus \f(CW\*(C`Z\*(C'\fR always packs a trailing null, except
5610 when the count is 0.
5611 .IP \(bu 4
5612 Likewise, the \f(CW\*(C`b\*(C'\fR and \f(CW\*(C`B\*(C'\fR formats pack a string that's that many bits long.
5613 Each such format generates 1 bit of the result. These are typically followed
5614 by a repeat count like \f(CW\*(C`B8\*(C'\fR or \f(CW\*(C`B64\*(C'\fR.
5615 .Sp
5616 Each result bit is based on the least-significant bit of the corresponding
5617 input character, i.e., on \f(CW\*(C`ord($char)%2\*(C'\fR. In particular, characters \f(CW"0"\fR
5618 and \f(CW"1"\fR generate bits 0 and 1, as do characters \f(CW"\e000"\fR and \f(CW"\e001"\fR.
5619 .Sp
5620 Starting from the beginning of the input string, each 8\-tuple
5621 of characters is converted to 1 character of output. With format \f(CW\*(C`b\*(C'\fR,
5622 the first character of the 8\-tuple determines the least-significant bit of a
5623 character; with format \f(CW\*(C`B\*(C'\fR, it determines the most-significant bit of
5624 a character.
5625 .Sp
5626 If the length of the input string is not evenly divisible by 8, the
5627 remainder is packed as if the input string were padded by null characters
5628 at the end. Similarly during unpacking, "extra" bits are ignored.
5629 .Sp
5630 If the input string is longer than needed, remaining characters are ignored.
5631 .Sp
5632 A \f(CW\*(C`*\*(C'\fR for the repeat count uses all characters of the input field.
5633 On unpacking, bits are converted to a string of \f(CW0\fRs and \f(CW1\fRs.
5634 .IP \(bu 4
5635 The \f(CW\*(C`h\*(C'\fR and \f(CW\*(C`H\*(C'\fR formats pack a string that many nybbles (4\-bit groups,
5636 representable as hexadecimal digits, \f(CW"0".."9"\fR \f(CW"a".."f"\fR) long.
5637 .Sp
5638 For each such format, \f(CW\*(C`pack\*(C'\fR generates 4 bits of result.
5639 With non-alphabetical characters, the result is based on the 4 least-significant
5640 bits of the input character, i.e., on \f(CW\*(C`ord($char)%16\*(C'\fR. In particular,
5641 characters \f(CW"0"\fR and \f(CW"1"\fR generate nybbles 0 and 1, as do bytes
5642 \&\f(CW"\e000"\fR and \f(CW"\e001"\fR. For characters \f(CW"a".."f"\fR and \f(CW"A".."F"\fR, the result
5643 is compatible with the usual hexadecimal digits, so that \f(CW"a"\fR and
5644 \&\f(CW"A"\fR both generate the nybble \f(CW\*(C`0xA==10\*(C'\fR. Use only these specific hex
5645 characters with this format.
5646 .Sp
5647 Starting from the beginning of the template to
5648 \&\f(CW\*(C`pack\*(C'\fR, each pair
5649 of characters is converted to 1 character of output. With format \f(CW\*(C`h\*(C'\fR, the
5650 first character of the pair determines the least-significant nybble of the
5651 output character; with format \f(CW\*(C`H\*(C'\fR, it determines the most-significant
5652 nybble.
5653 .Sp
5654 If the length of the input string is not even, it behaves as if padded by
5655 a null character at the end. Similarly, "extra" nybbles are ignored during
5656 unpacking.
5657 .Sp
5658 If the input string is longer than needed, extra characters are ignored.
5659 .Sp
5660 A \f(CW\*(C`*\*(C'\fR for the repeat count uses all characters of the input field. For
5661 \&\f(CW\*(C`unpack\*(C'\fR, nybbles are converted to a string of
5662 hexadecimal digits.
5663 .IP \(bu 4
5664 The \f(CW\*(C`p\*(C'\fR format packs a pointer to a null-terminated string. You are
5665 responsible for ensuring that the string is not a temporary value, as that
5666 could potentially get deallocated before you got around to using the packed
5667 result. The \f(CW\*(C`P\*(C'\fR format packs a pointer to a structure of the size indicated
5668 by the length. A null pointer is created if the corresponding value for
5669 \&\f(CW\*(C`p\*(C'\fR or \f(CW\*(C`P\*(C'\fR is \f(CW\*(C`undef\*(C'\fR; similarly with
5670 \&\f(CW\*(C`unpack\*(C'\fR, where a null pointer unpacks into
5671 \&\f(CW\*(C`undef\*(C'\fR.
5672 .Sp
5673 If your system has a strange pointer size\-\-meaning a pointer is neither as
5674 big as an int nor as big as a long\-\-it may not be possible to pack or
5675 unpack pointers in big\- or little-endian byte order. Attempting to do
5676 so raises an exception.
5677 .IP \(bu 4
5678 The \f(CW\*(C`/\*(C'\fR template character allows packing and unpacking of a sequence of
5679 items where the packed structure contains a packed item count followed by
5680 the packed items themselves. This is useful when the structure you're
5681 unpacking has encoded the sizes or repeat counts for some of its fields
5682 within the structure itself as separate fields.
5683 .Sp
5684 For \f(CW\*(C`pack\*(C'\fR, you write
5685 \&\fIlength-item\fR\f(CW\*(C`/\*(C'\fR\fIsequence-item\fR, and the
5686 \&\fIlength-item\fR describes how the length value is packed. Formats likely
5687 to be of most use are integer-packing ones like \f(CW\*(C`n\*(C'\fR for Java strings,
5688 \&\f(CW\*(C`w\*(C'\fR for ASN.1 or SNMP, and \f(CW\*(C`N\*(C'\fR for Sun XDR.
5689 .Sp
5690 For \f(CW\*(C`pack\*(C'\fR, \fIsequence-item\fR may have a repeat
5691 count, in which case
5692 the minimum of that and the number of available items is used as the argument
5693 for \fIlength-item\fR. If it has no repeat count or uses a '*', the number
5694 of available items is used.
5695 .Sp
5696 For \f(CW\*(C`unpack\*(C'\fR, an internal stack of integer
5697 arguments unpacked so far is
5698 used. You write \f(CW\*(C`/\*(C'\fR\fIsequence-item\fR and the repeat count is obtained by
5699 popping off the last element from the stack. The \fIsequence-item\fR must not
5700 have a repeat count.
5701 .Sp
5702 If \fIsequence-item\fR refers to a string type (\f(CW"A"\fR, \f(CW"a"\fR, or \f(CW"Z"\fR),
5703 the \fIlength-item\fR is the string length, not the number of strings. With
5704 an explicit repeat count for pack, the packed string is adjusted to that
5705 length. For example:
5706 .Sp
5707 .Vb 1
5708 \& This code: gives this result:
5709 \&
5710 \& unpack("W/a", "\e004Gurusamy") ("Guru")
5711 \& unpack("a3/A A*", "007 Bond J ") (" Bond", "J")
5712 \& unpack("a3 x2 /A A*", "007: Bond, J.") ("Bond, J", ".")
5713 \&
5714 \& pack("n/a* w/a","hello,","world") "\e000\e006hello,\e005world"
5715 \& pack("a/W2", ord("a") .. ord("z")) "2ab"
5716 .Ve
5717 .Sp
5718 The \fIlength-item\fR is not returned explicitly from
5719 \&\f(CW\*(C`unpack\*(C'\fR.
5720 .Sp
5721 Supplying a count to the \fIlength-item\fR format letter is only useful with
5722 \&\f(CW\*(C`A\*(C'\fR, \f(CW\*(C`a\*(C'\fR, or \f(CW\*(C`Z\*(C'\fR. Packing with a \fIlength-item\fR of \f(CW\*(C`a\*(C'\fR or \f(CW\*(C`Z\*(C'\fR may
5723 introduce \f(CW"\e000"\fR characters, which Perl does not regard as legal in
5724 numeric strings.
5725 .IP \(bu 4
5726 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
5727 followed by a \f(CW\*(C`!\*(C'\fR modifier to specify native shorts or
5728 longs. As shown in the example above, a bare \f(CW\*(C`l\*(C'\fR means
5729 exactly 32 bits, although the native \f(CW\*(C`long\*(C'\fR as seen by the local C compiler
5730 may be larger. This is mainly an issue on 64\-bit platforms. You can
5731 see whether using \f(CW\*(C`!\*(C'\fR makes any difference this way:
5732 .Sp
5733 .Vb 2
5734 \& printf "format s is %d, s! is %d\en",
5735 \& length pack("s"), length pack("s!");
5736 \&
5737 \& printf "format l is %d, l! is %d\en",
5738 \& length pack("l"), length pack("l!");
5739 .Ve
5740 .Sp
5741 \&\f(CW\*(C`i!\*(C'\fR and \f(CW\*(C`I!\*(C'\fR are also allowed, but only for completeness' sake:
5742 they are identical to \f(CW\*(C`i\*(C'\fR and \f(CW\*(C`I\*(C'\fR.
5743 .Sp
5744 The actual sizes (in bytes) of native shorts, ints, longs, and long
5745 longs on the platform where Perl was built are also available from
5746 the command line:
5747 .Sp
5748 .Vb 5
5749 \& $ perl \-V:{short,int,long{,long}}size
5750 \& shortsize=\*(Aq2\*(Aq;
5751 \& intsize=\*(Aq4\*(Aq;
5752 \& longsize=\*(Aq4\*(Aq;
5753 \& longlongsize=\*(Aq8\*(Aq;
5754 .Ve
5755 .Sp
5756 or programmatically via the \f(CW\*(C`Config\*(C'\fR module:
5757 .Sp
5758 .Vb 5
5759 \& use Config;
5760 \& print $Config{shortsize}, "\en";
5761 \& print $Config{intsize}, "\en";
5762 \& print $Config{longsize}, "\en";
5763 \& print $Config{longlongsize}, "\en";
5764 .Ve
5765 .Sp
5766 \&\f(CW$Config{longlongsize}\fR is undefined on systems without
5767 long long support.
5768 .IP \(bu 4
5769 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 are
5770 inherently non-portable between processors and operating systems because
5771 they obey native byteorder and endianness. For example, a 4\-byte integer
5772 0x12345678 (305419896 decimal) would be ordered natively (arranged in and
5773 handled by the CPU registers) into bytes as
5774 .Sp
5775 .Vb 2
5776 \& 0x12 0x34 0x56 0x78 # big\-endian
5777 \& 0x78 0x56 0x34 0x12 # little\-endian
5778 .Ve
5779 .Sp
5780 Basically, Intel and VAX CPUs are little-endian, while everybody else,
5781 including Motorola m68k/88k, PPC, Sparc, HP PA, Power, and Cray, are
5782 big-endian. Alpha and MIPS can be either: Digital/Compaq uses (well, used)
5783 them in little-endian mode, but SGI/Cray uses them in big-endian mode.
5784 .Sp
5785 The names \fIbig-endian\fR and \fIlittle-endian\fR are comic references to the
5786 egg-eating habits of the little-endian Lilliputians and the big-endian
5787 Blefuscudians from the classic Jonathan Swift satire, \fIGulliver's Travels\fR.
5788 This entered computer lingo via the paper "On Holy Wars and a Plea for
5789 Peace" by Danny Cohen, USC/ISI IEN 137, April 1, 1980.
5790 .Sp
5791 Some systems may have even weirder byte orders such as
5792 .Sp
5793 .Vb 2
5794 \& 0x56 0x78 0x12 0x34
5795 \& 0x34 0x12 0x78 0x56
5796 .Ve
5797 .Sp
5798 These are called mid-endian, middle-endian, mixed-endian, or just weird.
5799 .Sp
5800 You can determine your system endianness with this incantation:
5801 .Sp
5802 .Vb 1
5803 \& printf("%#02x ", $_) for unpack("W*", pack L=>0x12345678);
5804 .Ve
5805 .Sp
5806 The byteorder on the platform where Perl was built is also available
5807 via Config:
5808 .Sp
5809 .Vb 2
5810 \& use Config;
5811 \& print "$Config{byteorder}\en";
5812 .Ve
5813 .Sp
5814 or from the command line:
5815 .Sp
5816 .Vb 1
5817 \& $ perl \-V:byteorder
5818 .Ve
5819 .Sp
5820 Byteorders \f(CW"1234"\fR and \f(CW"12345678"\fR are little-endian; \f(CW"4321"\fR
5821 and \f(CW"87654321"\fR are big-endian. Systems with multiarchitecture binaries
5822 will have \f(CW"ffff"\fR, signifying that static information doesn't work,
5823 one must use runtime probing.
5824 .Sp
5825 For portably packed integers, either use the formats \f(CW\*(C`n\*(C'\fR, \f(CW\*(C`N\*(C'\fR, \f(CW\*(C`v\*(C'\fR,
5826 and \f(CW\*(C`V\*(C'\fR or else use the \f(CW\*(C`>\*(C'\fR and \f(CW\*(C`<\*(C'\fR modifiers described
5827 immediately below. See also perlport.
5828 .IP \(bu 4
5829 Also floating point numbers have endianness. Usually (but not always)
5830 this agrees with the integer endianness. Even though most platforms
5831 these days use the IEEE 754 binary format, there are differences,
5832 especially if the long doubles are involved. You can see the
5833 \&\f(CW\*(C`Config\*(C'\fR variables \f(CW\*(C`doublekind\*(C'\fR and \f(CW\*(C`longdblkind\*(C'\fR (also \f(CW\*(C`doublesize\*(C'\fR,
5834 \&\f(CW\*(C`longdblsize\*(C'\fR): the "kind" values are enums, unlike \f(CW\*(C`byteorder\*(C'\fR.
5835 .Sp
5836 Portability-wise the best option is probably to keep to the IEEE 754
5837 64\-bit doubles, and of agreed-upon endianness. Another possibility
5838 is the \f(CW"%a"\fR) format of \f(CW\*(C`printf\*(C'\fR.
5839 .IP \(bu 4
5840 Starting with Perl 5.10.0, integer and floating-point formats, along with
5841 the \f(CW\*(C`p\*(C'\fR and \f(CW\*(C`P\*(C'\fR formats and \f(CW\*(C`()\*(C'\fR groups, may all be followed by the
5842 \&\f(CW\*(C`>\*(C'\fR or \f(CW\*(C`<\*(C'\fR endianness modifiers to respectively enforce big\-
5843 or little-endian byte-order. These modifiers are especially useful
5844 given how \f(CW\*(C`n\*(C'\fR, \f(CW\*(C`N\*(C'\fR, \f(CW\*(C`v\*(C'\fR, and \f(CW\*(C`V\*(C'\fR don't cover signed integers,
5845 64\-bit integers, or floating-point values.
5846 .Sp
5847 Here are some concerns to keep in mind when using an endianness modifier:
5848 .RS 4
5849 .IP \(bu 4
5850 Exchanging signed integers between different platforms works only
5851 when all platforms store them in the same format. Most platforms store
5852 signed integers in two's-complement notation, so usually this is not an issue.
5853 .IP \(bu 4
5854 The \f(CW\*(C`>\*(C'\fR or \f(CW\*(C`<\*(C'\fR modifiers can only be used on floating-point
5855 formats on big\- or little-endian machines. Otherwise, attempting to
5856 use them raises an exception.
5857 .IP \(bu 4
5858 Forcing big\- or little-endian byte-order on floating-point values for
5859 data exchange can work only if all platforms use the same
5860 binary representation such as IEEE floating-point. Even if all
5861 platforms are using IEEE, there may still be subtle differences. Being able
5862 to use \f(CW\*(C`>\*(C'\fR or \f(CW\*(C`<\*(C'\fR on floating-point values can be useful,
5863 but also dangerous if you don't know exactly what you're doing.
5864 It is not a general way to portably store floating-point values.
5865 .IP \(bu 4
5866 When using \f(CW\*(C`>\*(C'\fR or \f(CW\*(C`<\*(C'\fR on a \f(CW\*(C`()\*(C'\fR group, this affects
5867 all types inside the group that accept byte-order modifiers,
5868 including all subgroups. It is silently ignored for all other
5869 types. You are not allowed to override the byte-order within a group
5870 that already has a byte-order modifier suffix.
5871 .RE
5872 .RS 4
5873 .RE
5874 .IP \(bu 4
5875 Real numbers (floats and doubles) are in native machine format only.
5876 Due to the multiplicity of floating-point formats and the lack of a
5877 standard "network" representation for them, no facility for interchange has been
5878 made. This means that packed floating-point data written on one machine
5879 may not be readable on another, even if both use IEEE floating-point
5880 arithmetic (because the endianness of the memory representation is not part
5881 of the IEEE spec). See also perlport.
5882 .Sp
5883 If you know \fIexactly\fR what you're doing, you can use the \f(CW\*(C`>\*(C'\fR or \f(CW\*(C`<\*(C'\fR
5884 modifiers to force big\- or little-endian byte-order on floating-point values.
5885 .Sp
5886 Because Perl uses doubles (or long doubles, if configured) internally for
5887 all numeric calculation, converting from double into float and thence
5888 to double again loses precision, so \f(CW\*(C`unpack("f", pack("f", $foo)\*(C'\fR)
5889 will not in general equal \f(CW$foo\fR.
5890 .IP \(bu 4
5891 Pack and unpack can operate in two modes: character mode (\f(CW\*(C`C0\*(C'\fR mode) where
5892 the packed string is processed per character, and UTF\-8 byte mode (\f(CW\*(C`U0\*(C'\fR mode)
5893 where the packed string is processed in its UTF\-8\-encoded Unicode form on
5894 a byte-by-byte basis. Character mode is the default
5895 unless the format string starts with \f(CW\*(C`U\*(C'\fR. You
5896 can always switch mode mid-format with an explicit
5897 \&\f(CW\*(C`C0\*(C'\fR or \f(CW\*(C`U0\*(C'\fR in the format. This mode remains in effect until the next
5898 mode change, or until the end of the \f(CW\*(C`()\*(C'\fR group it (directly) applies to.
5899 .Sp
5900 Using \f(CW\*(C`C0\*(C'\fR to get Unicode characters while using \f(CW\*(C`U0\*(C'\fR to get \fInon\fR\-Unicode
5901 bytes is not necessarily obvious. Probably only the first of these
5902 is what you want:
5903 .Sp
5904 .Vb 12
5905 \& $ perl \-CS \-E \*(Aqsay "\ex{3B1}\ex{3C9}"\*(Aq |
5906 \& perl \-CS \-ne \*(Aqprintf "%v04X\en", $_ for unpack("C0A*", $_)\*(Aq
5907 \& 03B1.03C9
5908 \& $ perl \-CS \-E \*(Aqsay "\ex{3B1}\ex{3C9}"\*(Aq |
5909 \& perl \-CS \-ne \*(Aqprintf "%v02X\en", $_ for unpack("U0A*", $_)\*(Aq
5910 \& CE.B1.CF.89
5911 \& $ perl \-CS \-E \*(Aqsay "\ex{3B1}\ex{3C9}"\*(Aq |
5912 \& perl \-C0 \-ne \*(Aqprintf "%v02X\en", $_ for unpack("C0A*", $_)\*(Aq
5913 \& CE.B1.CF.89
5914 \& $ perl \-CS \-E \*(Aqsay "\ex{3B1}\ex{3C9}"\*(Aq |
5915 \& perl \-C0 \-ne \*(Aqprintf "%v02X\en", $_ for unpack("U0A*", $_)\*(Aq
5916 \& C3.8E.C2.B1.C3.8F.C2.89
5917 .Ve
5918 .Sp
5919 Those examples also illustrate that you should not try to use
5920 \&\f(CW\*(C`pack\*(C'\fR/\f(CW\*(C`unpack\*(C'\fR as a
5921 substitute for the Encode module.
5922 .IP \(bu 4
5923 You must yourself do any alignment or padding by inserting, for example,
5924 enough \f(CW"x"\fRes while packing. There is no way for
5925 \&\f(CW\*(C`pack\*(C'\fR and \f(CW\*(C`unpack\*(C'\fR
5926 to know where characters are going to or coming from, so they
5927 handle their output and input as flat sequences of characters.
5928 .IP \(bu 4
5929 A \f(CW\*(C`()\*(C'\fR group is a sub-TEMPLATE enclosed in parentheses. A group may
5930 take a repeat count either as postfix, or for
5931 \&\f(CW\*(C`unpack\*(C'\fR, also via the \f(CW\*(C`/\*(C'\fR
5932 template character. Within each repetition of a group, positioning with
5933 \&\f(CW\*(C`@\*(C'\fR starts over at 0. Therefore, the result of
5934 .Sp
5935 .Vb 1
5936 \& pack("@1A((@2A)@3A)", qw[X Y Z])
5937 .Ve
5938 .Sp
5939 is the string \f(CW"\e0X\e0\e0YZ"\fR.
5940 .IP \(bu 4
5941 \&\f(CW\*(C`x\*(C'\fR and \f(CW\*(C`X\*(C'\fR accept the \f(CW\*(C`!\*(C'\fR modifier to act as alignment commands: they
5942 jump forward or back to the closest position aligned at a multiple of \f(CW\*(C`count\*(C'\fR
5943 characters. For example, to \f(CW\*(C`pack\*(C'\fR or
5944 \&\f(CW\*(C`unpack\*(C'\fR a C structure like
5945 .Sp
5946 .Vb 5
5947 \& struct {
5948 \& char c; /* one signed, 8\-bit character */
5949 \& double d;
5950 \& char cc[2];
5951 \& }
5952 .Ve
5953 .Sp
5954 one may need to use the template \f(CW\*(C`c x![d] d c[2]\*(C'\fR. This assumes that
5955 doubles must be aligned to the size of double.
5956 .Sp
5957 For alignment commands, a \f(CW\*(C`count\*(C'\fR of 0 is equivalent to a \f(CW\*(C`count\*(C'\fR of 1;
5958 both are no-ops.
5959 .IP \(bu 4
5960 \&\f(CW\*(C`n\*(C'\fR, \f(CW\*(C`N\*(C'\fR, \f(CW\*(C`v\*(C'\fR and \f(CW\*(C`V\*(C'\fR accept the \f(CW\*(C`!\*(C'\fR modifier to
5961 represent signed 16\-/32\-bit integers in big\-/little\-endian order.
5962 This is portable only when all platforms sharing packed data use the
5963 same binary representation for signed integers; for example, when all
5964 platforms use two's-complement representation.
5965 .IP \(bu 4
5966 Comments can be embedded in a TEMPLATE using \f(CW\*(C`#\*(C'\fR through the end of line.
5967 White space can separate pack codes from each other, but modifiers and
5968 repeat counts must follow immediately. Breaking complex templates into
5969 individual line-by-line components, suitably annotated, can do as much to
5970 improve legibility and maintainability of pack/unpack formats as \f(CW\*(C`/x\*(C'\fR can
5971 for complicated pattern matches.
5972 .IP \(bu 4
5973 If TEMPLATE requires more arguments than \f(CW\*(C`pack\*(C'\fR
5974 is given, \f(CW\*(C`pack\*(C'\fR
5975 assumes additional \f(CW""\fR arguments. If TEMPLATE requires fewer arguments
5976 than given, extra arguments are ignored.
5977 .IP \(bu 4
5978 Attempting to pack the special floating point values \f(CW\*(C`Inf\*(C'\fR and \f(CW\*(C`NaN\*(C'\fR
5979 (infinity, also in negative, and not-a-number) into packed integer values
5980 (like \f(CW"L"\fR) is a fatal error. The reason for this is that there simply
5981 isn't any sensible mapping for these special values into integers.
5982 .RE
5983 .RS 4
5984 .Sp
5985 Examples:
5986 .Sp
5987 .Vb 10
5988 \& $foo = pack("WWWW",65,66,67,68);
5989 \& # foo eq "ABCD"
5990 \& $foo = pack("W4",65,66,67,68);
5991 \& # same thing
5992 \& $foo = pack("W4",0x24b6,0x24b7,0x24b8,0x24b9);
5993 \& # same thing with Unicode circled letters.
5994 \& $foo = pack("U4",0x24b6,0x24b7,0x24b8,0x24b9);
5995 \& # same thing with Unicode circled letters. You don\*(Aqt get the
5996 \& # UTF\-8 bytes because the U at the start of the format caused
5997 \& # a switch to U0\-mode, so the UTF\-8 bytes get joined into
5998 \& # characters
5999 \& $foo = pack("C0U4",0x24b6,0x24b7,0x24b8,0x24b9);
6000 \& # foo eq "\exe2\ex92\exb6\exe2\ex92\exb7\exe2\ex92\exb8\exe2\ex92\exb9"
6001 \& # This is the UTF\-8 encoding of the string in the
6002 \& # previous example
6003 \&
6004 \& $foo = pack("ccxxcc",65,66,67,68);
6005 \& # foo eq "AB\e0\e0CD"
6006 \&
6007 \& # NOTE: The examples above featuring "W" and "c" are true
6008 \& # only on ASCII and ASCII\-derived systems such as ISO Latin 1
6009 \& # and UTF\-8. On EBCDIC systems, the first example would be
6010 \& # $foo = pack("WWWW",193,194,195,196);
6011 \&
6012 \& $foo = pack("s2",1,2);
6013 \& # "\e001\e000\e002\e000" on little\-endian
6014 \& # "\e000\e001\e000\e002" on big\-endian
6015 \&
6016 \& $foo = pack("a4","abcd","x","y","z");
6017 \& # "abcd"
6018 \&
6019 \& $foo = pack("aaaa","abcd","x","y","z");
6020 \& # "axyz"
6021 \&
6022 \& $foo = pack("a14","abcdefg");
6023 \& # "abcdefg\e0\e0\e0\e0\e0\e0\e0"
6024 \&
6025 \& $foo = pack("i9pl", gmtime);
6026 \& # a real struct tm (on my system anyway)
6027 \&
6028 \& $utmp_template = "Z8 Z8 Z16 L";
6029 \& $utmp = pack($utmp_template, @utmp1);
6030 \& # a struct utmp (BSDish)
6031 \&
6032 \& @utmp2 = unpack($utmp_template, $utmp);
6033 \& # "@utmp1" eq "@utmp2"
6034 \&
6035 \& sub bintodec {
6036 \& unpack("N", pack("B32", substr("0" x 32 . shift, \-32)));
6037 \& }
6038 \&
6039 \& $foo = pack(\*(Aqsx2l\*(Aq, 12, 34);
6040 \& # short 12, two zero bytes padding, long 34
6041 \& $bar = pack(\*(Aqs@4l\*(Aq, 12, 34);
6042 \& # short 12, zero fill to position 4, long 34
6043 \& # $foo eq $bar
6044 \& $baz = pack(\*(Aqs.l\*(Aq, 12, 4, 34);
6045 \& # short 12, zero fill to position 4, long 34
6046 \&
6047 \& $foo = pack(\*(AqnN\*(Aq, 42, 4711);
6048 \& # pack big\-endian 16\- and 32\-bit unsigned integers
6049 \& $foo = pack(\*(AqS>L>\*(Aq, 42, 4711);
6050 \& # exactly the same
6051 \& $foo = pack(\*(Aqs<l<\*(Aq, \-42, 4711);
6052 \& # pack little\-endian 16\- and 32\-bit signed integers
6053 \& $foo = pack(\*(Aq(sl)<\*(Aq, \-42, 4711);
6054 \& # exactly the same
6055 .Ve
6056 .Sp
6057 The same template may generally also be used in
6058 \&\f(CW\*(C`unpack\*(C'\fR.
6059 .RE
6060 .IP "package NAMESPACE" 4
6061 .IX Item "package NAMESPACE"
6062 .PD 0
6063 .IP "package NAMESPACE VERSION" 4
6064 .IX Xref "package module namespace version"
6065 .IX Item "package NAMESPACE VERSION"
6066 .IP "package NAMESPACE BLOCK" 4
6067 .IX Item "package NAMESPACE BLOCK"
6068 .IP "package NAMESPACE VERSION BLOCK" 4
6069 .IX Xref "package module namespace version"
6070 .IX Item "package NAMESPACE VERSION BLOCK"
6071 .PD
6072 Declares the BLOCK or the rest of the compilation unit as being in the
6073 given namespace. The scope of the package declaration is either the
6074 supplied code BLOCK or, in the absence of a BLOCK, from the declaration
6075 itself through the end of current scope (the enclosing block, file, or
6076 \&\f(CW\*(C`eval\*(C'\fR). That is, the forms without a BLOCK are
6077 operative through the end of the current scope, just like the
6078 \&\f(CW\*(C`my\*(C'\fR, \f(CW\*(C`state\*(C'\fR, and
6079 \&\f(CW\*(C`our\*(C'\fR operators. All unqualified dynamic identifiers
6080 in this scope will be in the given namespace, except where overridden by
6081 another \f(CW\*(C`package\*(C'\fR declaration or
6082 when they're one of the special identifiers that qualify into \f(CW\*(C`main::\*(C'\fR,
6083 like \f(CW\*(C`STDOUT\*(C'\fR, \f(CW\*(C`ARGV\*(C'\fR, \f(CW\*(C`ENV\*(C'\fR, and the punctuation variables.
6084 .Sp
6085 A package statement affects dynamic variables only, including those
6086 you've used \f(CW\*(C`local\*(C'\fR on, but \fInot\fR lexically-scoped
6087 variables, which are created with \f(CW\*(C`my\*(C'\fR,
6088 \&\f(CW\*(C`state\*(C'\fR, or \f(CW\*(C`our\*(C'\fR. Typically it
6089 would be the first declaration in a file included by
6090 \&\f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR.
6091 You can switch into a
6092 package in more than one place, since this only determines which default
6093 symbol table the compiler uses for the rest of that block. You can refer to
6094 identifiers in other packages than the current one by prefixing the identifier
6095 with the package name and a double colon, as in \f(CW$SomePack::var\fR
6096 or \f(CW\*(C`ThatPack::INPUT_HANDLE\*(C'\fR. If package name is omitted, the \f(CW\*(C`main\*(C'\fR
6097 package is assumed. That is, \f(CW$::sail\fR is equivalent to
6098 \&\f(CW$main::sail\fR (as well as to \f(CW\*(C`$main\*(Aqsail\*(C'\fR, still seen in ancient
6099 code, mostly from Perl 4).
6100 .Sp
6101 If VERSION is provided, \f(CW\*(C`package\*(C'\fR sets the
6102 \&\f(CW$VERSION\fR variable in the given
6103 namespace to a version object with the VERSION provided. VERSION must be a
6104 "strict" style version number as defined by the version module: a positive
6105 decimal number (integer or decimal-fraction) without exponentiation or else a
6106 dotted-decimal v\-string with a leading 'v' character and at least three
6107 components. You should set \f(CW$VERSION\fR only once per package.
6108 .Sp
6109 See "Packages" in perlmod for more information about packages, modules,
6110 and classes. See perlsub for other scoping issues.
6111 .IP _\|_PACKAGE_\|_ 4
6112 .IX Xref "__PACKAGE__"
6113 .IX Item "__PACKAGE__"
6114 A special token that returns the name of the package in which it occurs.
6115 .IP _\|_CLASS_\|_ 4
6116 .IX Xref "__CLASS__"
6117 .IX Item "__CLASS__"
6118 Invoked within a \f(CW\*(C`method\*(C'\fR, or similar location, such as
6119 a field initializer expression, this token returns the name of the class of
6120 the invoking instance. Essentially it is equivalent to \f(CWref($self)\fR except
6121 that it can additionally be used in a field initializer to gain access to
6122 class methods, before the instance is fully constructed.
6123 .Sp
6124 .Vb 1
6125 \& use feature \*(Aqclass\*(Aq;
6126 \&
6127 \& class Example1 {
6128 \& field $f = _\|_CLASS_\|_\->default_f;
6129 \&
6130 \& sub default_f { 10 }
6131 \& }
6132 .Ve
6133 .Sp
6134 In a basic class, this value will be the same as
6135 \&\f(CW\*(C`_\|_PACKAGE_\|_\*(C'\fR. The distinction can be seen when a subclass
6136 is constructed; it will give the class name of the instance being constructed,
6137 rather than just the package name that the actual code belongs to.
6138 .Sp
6139 .Vb 3
6140 \& class Example2 :isa(Example1) {
6141 \& sub default_f { 20 }
6142 \& }
6143 \&
6144 \& my $obj = Example2\->new;
6145 \& # The $f field now has the value 20
6146 .Ve
6147 .IP "pipe READHANDLE,WRITEHANDLE" 4
6148 .IX Xref "pipe"
6149 .IX Item "pipe READHANDLE,WRITEHANDLE"
6150 Opens a pair of connected pipes like the corresponding system call.
6151 Note that if you set up a loop of piped processes, deadlock can occur
6152 unless you are very careful. In addition, note that Perl's pipes use
6153 IO buffering, so you may need to set \f(CW$|\fR
6154 to flush your WRITEHANDLE after each command, depending on the
6155 application.
6156 .Sp
6157 Returns true on success.
6158 .Sp
6159 See IPC::Open2, IPC::Open3, and
6160 "Bidirectional Communication with Another Process" in perlipc
6161 for examples of such things.
6162 .Sp
6163 On systems that support a close-on-exec flag on files, that flag is set
6164 on all newly opened file descriptors whose
6165 \&\f(CW\*(C`fileno\*(C'\fRs are \fIhigher\fR than the current value of
6166 \&\f(CW$^F\fR (by default 2 for \f(CW\*(C`STDERR\*(C'\fR). See "$^F" in perlvar.
6167 .IP "pop ARRAY" 4
6168 .IX Xref "pop stack"
6169 .IX Item "pop ARRAY"
6170 .PD 0
6171 .IP pop 4
6172 .IX Item "pop"
6173 .PD
6174 Removes and returns the \fBlast\fR element of the array, shortening the array by
6175 one element.
6176 .Sp
6177 .Vb 2
6178 \& my @arr = (\*(Aqcat\*(Aq, \*(Aqdog\*(Aq, \*(Aqmouse\*(Aq);
6179 \& my $item = pop(@arr); # \*(Aqmouse\*(Aq
6180 \&
6181 \& # @arr is now (\*(Aqcat\*(Aq, \*(Aqdog\*(Aq)
6182 .Ve
6183 .Sp
6184 Returns \f(CW\*(C`undef\*(C'\fR if the array is empty.
6185 .Sp
6186 \&\fBNote:\fR \f(CW\*(C`pop\*(C'\fR may also return \f(CW\*(C`undef\*(C'\fR if the last element in the array
6187 is \f(CW\*(C`undef\*(C'\fR.
6188 .Sp
6189 .Vb 2
6190 \& my @arr = (\*(Aqone\*(Aq, \*(Aqtwo\*(Aq, undef);
6191 \& my $item = pop(@arr); # undef
6192 .Ve
6193 .Sp
6194 If ARRAY is omitted, \f(CW\*(C`pop\*(C'\fR operates on the \f(CW@ARGV\fR array
6195 in the main program, but the \f(CW@_\fR array in subroutines. \f(CW\*(C`pop\*(C'\fR
6196 will operate on the \f(CW@ARGV\fR array in \f(CW\*(C`eval STRING\*(C'\fR, \f(CW\*(C`BEGIN {}\*(C'\fR, \f(CW\*(C`INIT {}\*(C'\fR,
6197 \&\f(CW\*(C`CHECK {}\*(C'\fR blocks.
6198 .Sp
6199 Starting with Perl 5.14, an experimental feature allowed
6200 \&\f(CW\*(C`pop\*(C'\fR to take a
6201 scalar expression. This experiment has been deemed unsuccessful, and was
6202 removed as of Perl 5.24.
6203 .IP "pos SCALAR" 4
6204 .IX Xref "pos match, position"
6205 .IX Item "pos SCALAR"
6206 .PD 0
6207 .IP pos 4
6208 .IX Item "pos"
6209 .PD
6210 Returns the offset of where the last \f(CW\*(C`m//g\*(C'\fR search left off for the
6211 variable in question (\f(CW$_\fR is used when the variable is not
6212 specified). This offset is in characters unless the
6213 (no-longer-recommended) \f(CW\*(C`use bytes\*(C'\fR pragma is in effect, in
6214 which case the offset is in bytes. Note that 0 is a valid match offset.
6215 \&\f(CW\*(C`undef\*(C'\fR indicates
6216 that the search position is reset (usually due to match failure, but
6217 can also be because no match has yet been run on the scalar).
6218 .Sp
6219 \&\f(CW\*(C`pos\*(C'\fR directly accesses the location used by the regexp
6220 engine to store the offset, so assigning to \f(CW\*(C`pos\*(C'\fR will
6221 change that offset, and so will also influence the \f(CW\*(C`\eG\*(C'\fR zero-width
6222 assertion in regular expressions. Both of these effects take place for
6223 the next match, so you can't affect the position with
6224 \&\f(CW\*(C`pos\*(C'\fR during the current match, such as in
6225 \&\f(CW\*(C`(?{pos() = 5})\*(C'\fR or \f(CW\*(C`s//pos() = 5/e\*(C'\fR.
6226 .Sp
6227 Setting \f(CW\*(C`pos\*(C'\fR also resets the \fImatched with
6228 zero-length\fR flag, described
6229 under "Repeated Patterns Matching a Zero-length Substring" in perlre.
6230 .Sp
6231 Because a failed \f(CW\*(C`m//gc\*(C'\fR match doesn't reset the offset, the return
6232 from \f(CW\*(C`pos\*(C'\fR won't change either in this case. See
6233 perlre and perlop.
6234 .IP "print FILEHANDLE LIST" 4
6235 .IX Xref "print"
6236 .IX Item "print FILEHANDLE LIST"
6237 .PD 0
6238 .IP "print FILEHANDLE" 4
6239 .IX Item "print FILEHANDLE"
6240 .IP "print LIST" 4
6241 .IX Item "print LIST"
6242 .IP print 4
6243 .IX Item "print"
6244 .PD
6245 Prints a string or a list of strings. Returns true if successful.
6246 FILEHANDLE may be a scalar variable containing the name of or a reference
6247 to the filehandle, thus introducing one level of indirection. (NOTE: If
6248 FILEHANDLE is a variable and the next token is a term, it may be
6249 misinterpreted as an operator unless you interpose a \f(CW\*(C`+\*(C'\fR or put
6250 parentheses around the arguments.) If FILEHANDLE is omitted, prints to the
6251 last selected (see \f(CW\*(C`select\*(C'\fR) output handle. If
6252 LIST is omitted, prints \f(CW$_\fR to the currently selected
6253 output handle. To use FILEHANDLE alone to print the content of
6254 \&\f(CW$_\fR to it, you must use a bareword filehandle like
6255 \&\f(CW\*(C`FH\*(C'\fR, not an indirect one like \f(CW$fh\fR. To set the default output handle
6256 to something other than STDOUT, use the select operation.
6257 .Sp
6258 The current value of \f(CW$,\fR (if any) is printed between
6259 each LIST item. The current value of \f(CW\*(C`$\e\*(C'\fR (if any) is
6260 printed after the entire LIST has been printed. Because print takes a
6261 LIST, anything in the LIST is evaluated in list context, including any
6262 subroutines whose return lists you pass to
6263 \&\f(CW\*(C`print\*(C'\fR. Be careful not to follow the print
6264 keyword with a left
6265 parenthesis unless you want the corresponding right parenthesis to
6266 terminate the arguments to the print; put parentheses around all arguments
6267 (or interpose a \f(CW\*(C`+\*(C'\fR, but that doesn't look as good).
6268 .Sp
6269 If you're storing handles in an array or hash, or in general whenever
6270 you're using any expression more complex than a bareword handle or a plain,
6271 unsubscripted scalar variable to retrieve it, you will have to use a block
6272 returning the filehandle value instead, in which case the LIST may not be
6273 omitted:
6274 .Sp
6275 .Vb 2
6276 \& print { $files[$i] } "stuff\en";
6277 \& print { $OK ? *STDOUT : *STDERR } "stuff\en";
6278 .Ve
6279 .Sp
6280 Printing to a closed pipe or socket will generate a SIGPIPE signal. See
6281 perlipc for more on signal handling.
6282 .IP "printf FILEHANDLE FORMAT, LIST" 4
6283 .IX Xref "printf"
6284 .IX Item "printf FILEHANDLE FORMAT, LIST"
6285 .PD 0
6286 .IP "printf FILEHANDLE" 4
6287 .IX Item "printf FILEHANDLE"
6288 .IP "printf FORMAT, LIST" 4
6289 .IX Item "printf FORMAT, LIST"
6290 .IP printf 4
6291 .IX Item "printf"
6292 .PD
6293 Equivalent to \f(CW\*(C`print FILEHANDLE sprintf(FORMAT, LIST)\*(C'\fR, except that
6294 \&\f(CW\*(C`$\e\*(C'\fR (the output record separator) is not appended. The
6295 FORMAT and the LIST are actually parsed as a single list. The first
6296 argument of the list will be interpreted as the
6297 \&\f(CW\*(C`printf\*(C'\fR format. This means that
6298 \&\f(CWprintf(@_)\fR will use \f(CW$_[0]\fR as the format. See
6299 sprintf for an explanation of the format
6300 argument. If \f(CW\*(C`use locale\*(C'\fR (including \f(CW\*(C`use locale \*(Aq:not_characters\*(Aq\*(C'\fR)
6301 is in effect and \f(CW\*(C`POSIX::setlocale\*(C'\fR has been
6302 called, the character used for the decimal separator in formatted
6303 floating-point numbers is affected by the \f(CW\*(C`LC_NUMERIC\*(C'\fR locale setting.
6304 See perllocale and POSIX.
6305 .Sp
6306 For historical reasons, if you omit the list, \f(CW$_\fR is
6307 used as the format;
6308 to use FILEHANDLE without a list, you must use a bareword filehandle like
6309 \&\f(CW\*(C`FH\*(C'\fR, not an indirect one like \f(CW$fh\fR. However, this will rarely do what
6310 you want; if \f(CW$_\fR contains formatting codes, they will be
6311 replaced with the empty string and a warning will be emitted if
6312 warnings are enabled. Just use \f(CW\*(C`print\*(C'\fR if
6313 you want to print the contents of \f(CW$_\fR.
6314 .Sp
6315 Don't fall into the trap of using a
6316 \&\f(CW\*(C`printf\*(C'\fR when a simple
6317 \&\f(CW\*(C`print\*(C'\fR would do. The
6318 \&\f(CW\*(C`print\*(C'\fR is more efficient and less error
6319 prone.
6320 .IP "prototype FUNCTION" 4
6321 .IX Xref "prototype"
6322 .IX Item "prototype FUNCTION"
6323 .PD 0
6324 .IP prototype 4
6325 .IX Item "prototype"
6326 .PD
6327 Returns the prototype of a function as a string (or
6328 \&\f(CW\*(C`undef\*(C'\fR if the
6329 function has no prototype). FUNCTION is a reference to, or the name of,
6330 the function whose prototype you want to retrieve. If FUNCTION is omitted,
6331 \&\f(CW$_\fR is used.
6332 .Sp
6333 If FUNCTION is a string starting with \f(CW\*(C`CORE::\*(C'\fR, the rest is taken as a
6334 name for a Perl builtin. If the builtin's arguments
6335 cannot be adequately expressed by a prototype
6336 (such as \f(CW\*(C`system\*(C'\fR), \f(CW\*(C`prototype\*(C'\fR
6337 returns \f(CW\*(C`undef\*(C'\fR, because the builtin
6338 does not really behave like a Perl function. Otherwise, the string
6339 describing the equivalent prototype is returned.
6340 .IP "push ARRAY,LIST" 4
6341 .IX Xref "push stack"
6342 .IX Item "push ARRAY,LIST"
6343 Adds one or more items to the \fBend\fR of an array.
6344 .Sp
6345 .Vb 2
6346 \& my @animals = ("cat");
6347 \& push(@animals, "mouse"); # ("cat", "mouse")
6348 \&
6349 \& my @colors = ("red");
6350 \& push(@colors, ("blue", "green")); # ("red", "blue", "green")
6351 .Ve
6352 .Sp
6353 Returns the number of elements in the array following the completed
6354 \&\f(CW\*(C`push\*(C'\fR.
6355 .Sp
6356 .Vb 1
6357 \& my $color_count = push(@colors, ("yellow", "purple"));
6358 \&
6359 \& say "There are $color_count colors in the updated array";
6360 .Ve
6361 .Sp
6362 Starting with Perl 5.14, an experimental feature allowed
6363 \&\f(CW\*(C`push\*(C'\fR to take a
6364 scalar expression. This experiment has been deemed unsuccessful, and was
6365 removed as of Perl 5.24.
6366 .IP q/STRING/ 4
6367 .IX Item "q/STRING/"
6368 .PD 0
6369 .IP qq/STRING/ 4
6370 .IX Item "qq/STRING/"
6371 .IP qw/STRING/ 4
6372 .IX Item "qw/STRING/"
6373 .IP qx/STRING/ 4
6374 .IX Item "qx/STRING/"
6375 .PD
6376 Generalized quotes. See "Quote-Like Operators" in perlop.
6377 .IP qr/STRING/ 4
6378 .IX Item "qr/STRING/"
6379 Regexp-like quote. See "Regexp Quote-Like Operators" in perlop.
6380 .IP "quotemeta EXPR" 4
6381 .IX Xref "quotemeta metacharacter"
6382 .IX Item "quotemeta EXPR"
6383 .PD 0
6384 .IP quotemeta 4
6385 .IX Item "quotemeta"
6386 .PD
6387 Returns the value of EXPR with all the ASCII non\-"word"
6388 characters backslashed. (That is, all ASCII characters not matching
6389 \&\f(CW\*(C`/[A\-Za\-z_0\-9]/\*(C'\fR will be preceded by a backslash in the
6390 returned string, regardless of any locale settings.)
6391 This is the internal function implementing
6392 the \f(CW\*(C`\eQ\*(C'\fR escape in double-quoted strings.
6393 (See below for the behavior on non-ASCII code points.)
6394 .Sp
6395 If EXPR is omitted, uses \f(CW$_\fR.
6396 .Sp
6397 quotemeta (and \f(CW\*(C`\eQ\*(C'\fR ... \f(CW\*(C`\eE\*(C'\fR) are useful when interpolating strings into
6398 regular expressions, because by default an interpolated variable will be
6399 considered a mini-regular expression. For example:
6400 .Sp
6401 .Vb 3
6402 \& my $sentence = \*(AqThe quick brown fox jumped over the lazy dog\*(Aq;
6403 \& my $substring = \*(Aqquick.*?fox\*(Aq;
6404 \& $sentence =~ s{$substring}{big bad wolf};
6405 .Ve
6406 .Sp
6407 Will cause \f(CW$sentence\fR to become \f(CW\*(AqThe big bad wolf jumped over...\*(Aq\fR.
6408 .Sp
6409 On the other hand:
6410 .Sp
6411 .Vb 3
6412 \& my $sentence = \*(AqThe quick brown fox jumped over the lazy dog\*(Aq;
6413 \& my $substring = \*(Aqquick.*?fox\*(Aq;
6414 \& $sentence =~ s{\eQ$substring\eE}{big bad wolf};
6415 .Ve
6416 .Sp
6417 Or:
6418 .Sp
6419 .Vb 4
6420 \& my $sentence = \*(AqThe quick brown fox jumped over the lazy dog\*(Aq;
6421 \& my $substring = \*(Aqquick.*?fox\*(Aq;
6422 \& my $quoted_substring = quotemeta($substring);
6423 \& $sentence =~ s{$quoted_substring}{big bad wolf};
6424 .Ve
6425 .Sp
6426 Will both leave the sentence as is.
6427 Normally, when accepting literal string input from the user,
6428 \&\f(CW\*(C`quotemeta\*(C'\fR or \f(CW\*(C`\eQ\*(C'\fR must be used.
6429 .Sp
6430 Beware that if you put literal backslashes (those not inside
6431 interpolated variables) between \f(CW\*(C`\eQ\*(C'\fR and \f(CW\*(C`\eE\*(C'\fR, double-quotish
6432 backslash interpolation may lead to confusing results. If you
6433 \&\fIneed\fR to use literal backslashes within \f(CW\*(C`\eQ...\eE\*(C'\fR,
6434 consult "Gory details of parsing quoted constructs" in perlop.
6435 .Sp
6436 Because the result of \f(CW"\eQ\ \fR\f(CISTRING\fR\f(CW\ \eE"\fR has all metacharacters
6437 quoted, there is no way to insert a literal \f(CW\*(C`$\*(C'\fR or \f(CW\*(C`@\*(C'\fR inside a
6438 \&\f(CW\*(C`\eQ\eE\*(C'\fR pair. If protected by \f(CW\*(C`\e\*(C'\fR, \f(CW\*(C`$\*(C'\fR will be quoted to become
6439 \&\f(CW"\e\e\e$"\fR; if not, it is interpreted as the start of an interpolated
6440 scalar.
6441 .Sp
6442 In Perl v5.14, all non-ASCII characters are quoted in non\-UTF\-8\-encoded
6443 strings, but not quoted in UTF\-8 strings.
6444 .Sp
6445 Starting in Perl v5.16, Perl adopted a Unicode-defined strategy for
6446 quoting non-ASCII characters; the quoting of ASCII characters is
6447 unchanged.
6448 .Sp
6449 Also unchanged is the quoting of non\-UTF\-8 strings when outside the
6450 scope of a
6451 \&\f(CW\*(C`use feature \*(Aqunicode_strings\*(Aq\*(C'\fR,
6452 which is to quote all
6453 characters in the upper Latin1 range. This provides complete backwards
6454 compatibility for old programs which do not use Unicode. (Note that
6455 \&\f(CW\*(C`unicode_strings\*(C'\fR is automatically enabled within the scope of a
6456 \&\f(CW\*(C`use\ v5.12\*(C'\fR or greater.)
6457 .Sp
6458 Within the scope of \f(CW\*(C`use locale\*(C'\fR, all non-ASCII Latin1 code
6459 points
6460 are quoted whether the string is encoded as UTF\-8 or not. As mentioned
6461 above, locale does not affect the quoting of ASCII-range characters.
6462 This protects against those locales where characters such as \f(CW"|"\fR are
6463 considered to be word characters.
6464 .Sp
6465 Otherwise, Perl quotes non-ASCII characters using an adaptation from
6466 Unicode (see <https://www.unicode.org/reports/tr31/>).
6467 The only code points that are quoted are those that have any of the
6468 Unicode properties: Pattern_Syntax, Pattern_White_Space, White_Space,
6469 Default_Ignorable_Code_Point, or General_Category=Control.
6470 .Sp
6471 Of these properties, the two important ones are Pattern_Syntax and
6472 Pattern_White_Space. They have been set up by Unicode for exactly this
6473 purpose of deciding which characters in a regular expression pattern
6474 should be quoted. No character that can be in an identifier has these
6475 properties.
6476 .Sp
6477 Perl promises, that if we ever add regular expression pattern
6478 metacharacters to the dozen already defined
6479 (\f(CW\*(C`\e | ( ) [ { ^ $ * + ? .\*(C'\fR), that we will only use ones that have the
6480 Pattern_Syntax property. Perl also promises, that if we ever add
6481 characters that are considered to be white space in regular expressions
6482 (currently mostly affected by \f(CW\*(C`/x\*(C'\fR), they will all have the
6483 Pattern_White_Space property.
6484 .Sp
6485 Unicode promises that the set of code points that have these two
6486 properties will never change, so something that is not quoted in v5.16
6487 will never need to be quoted in any future Perl release. (Not all the
6488 code points that match Pattern_Syntax have actually had characters
6489 assigned to them; so there is room to grow, but they are quoted
6490 whether assigned or not. Perl, of course, would never use an
6491 unassigned code point as an actual metacharacter.)
6492 .Sp
6493 Quoting characters that have the other 3 properties is done to enhance
6494 the readability of the regular expression and not because they actually
6495 need to be quoted for regular expression purposes (characters with the
6496 White_Space property are likely to be indistinguishable on the page or
6497 screen from those with the Pattern_White_Space property; and the other
6498 two properties contain non-printing characters).
6499 .IP "rand EXPR" 4
6500 .IX Xref "rand random"
6501 .IX Item "rand EXPR"
6502 .PD 0
6503 .IP rand 4
6504 .IX Item "rand"
6505 .PD
6506 Returns a random fractional number greater than or equal to \f(CW0\fR and less
6507 than the value of EXPR. (EXPR should be positive.) If EXPR is
6508 omitted, the value \f(CW1\fR is used. Currently EXPR with the value \f(CW0\fR is
6509 also special-cased as \f(CW1\fR (this was undocumented before Perl 5.8.0
6510 and is subject to change in future versions of Perl). Automatically calls
6511 \&\f(CW\*(C`srand\*(C'\fR unless \f(CW\*(C`srand\*(C'\fR has already been
6512 called. See also \f(CW\*(C`srand\*(C'\fR.
6513 .Sp
6514 Apply \f(CW\*(C`int\*(C'\fR to the value returned by \f(CW\*(C`rand\*(C'\fR
6515 if you want random integers instead of random fractional numbers. For
6516 example,
6517 .Sp
6518 .Vb 1
6519 \& int(rand(10))
6520 .Ve
6521 .Sp
6522 returns a random integer between \f(CW0\fR and \f(CW9\fR, inclusive.
6523 .Sp
6524 (Note: If your rand function consistently returns numbers that are too
6525 large or too small, then your version of Perl was probably compiled
6526 with the wrong number of RANDBITS.)
6527 .Sp
6528 \&\fR\f(CB\*(C`rand\*(C'\fR\fB is not cryptographically secure. You should not rely
6529 on it in security-sensitive situations.\fR As of this writing, a
6530 number of third-party CPAN modules offer random number generators
6531 intended by their authors to be cryptographically secure,
6532 including: Data::Entropy, Crypt::Random, Math::Random::Secure,
6533 and Math::TrulyRandom.
6534 .IP "read FILEHANDLE,SCALAR,LENGTH,OFFSET" 4
6535 .IX Xref "read file, read"
6536 .IX Item "read FILEHANDLE,SCALAR,LENGTH,OFFSET"
6537 .PD 0
6538 .IP "read FILEHANDLE,SCALAR,LENGTH" 4
6539 .IX Item "read FILEHANDLE,SCALAR,LENGTH"
6540 .PD
6541 Attempts to read LENGTH \fIcharacters\fR of data into variable SCALAR
6542 from the specified FILEHANDLE. Returns the number of characters
6543 actually read, \f(CW0\fR at end of file, or undef if there was an error (in
6544 the latter case \f(CW$!\fR is also set). SCALAR will be grown
6545 or shrunk
6546 so that the last character actually read is the last character of the
6547 scalar after the read.
6548 .Sp
6549 An OFFSET may be specified to place the read data at some place in the
6550 string other than the beginning. A negative OFFSET specifies
6551 placement at that many characters counting backwards from the end of
6552 the string. A positive OFFSET greater than the length of SCALAR
6553 results in the string being padded to the required size with \f(CW"\e0"\fR
6554 bytes before the result of the read is appended.
6555 .Sp
6556 The call is implemented in terms of either Perl's or your system's native
6557 \&\fBfread\fR\|(3) library function, via the PerlIO layers applied to the
6558 handle. To get a true \fBread\fR\|(2) system call, see
6559 sysread.
6560 .Sp
6561 Note the \fIcharacters\fR: depending on the status of the filehandle,
6562 either (8\-bit) bytes or characters are read. By default, all
6563 filehandles operate on bytes, but for example if the filehandle has
6564 been opened with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see
6565 \&\f(CW\*(C`open\*(C'\fR, and the open
6566 pragma), the I/O will operate on UTF8\-encoded Unicode
6567 characters, not bytes. Similarly for the \f(CW\*(C`:encoding\*(C'\fR layer:
6568 in that case pretty much any characters can be read.
6569 .IP "readdir DIRHANDLE" 4
6570 .IX Xref "readdir"
6571 .IX Item "readdir DIRHANDLE"
6572 Returns the next directory entry for a directory opened by
6573 \&\f(CW\*(C`opendir\*(C'\fR.
6574 If used in list context, returns all the rest of the entries in the
6575 directory. If there are no more entries, returns the undefined value in
6576 scalar context and the empty list in list context.
6577 .Sp
6578 If you're planning to filetest the return values out of a
6579 \&\f(CW\*(C`readdir\*(C'\fR, you'd better prepend the directory in
6580 question. Otherwise, because we didn't \f(CW\*(C`chdir\*(C'\fR there,
6581 it would have been testing the wrong file.
6582 .Sp
6583 .Vb 3
6584 \& opendir(my $dh, $some_dir) || die "Can\*(Aqt opendir $some_dir: $!";
6585 \& my @dots = grep { /^\e./ && \-f "$some_dir/$_" } readdir($dh);
6586 \& closedir $dh;
6587 .Ve
6588 .Sp
6589 As of Perl 5.12 you can use a bare \f(CW\*(C`readdir\*(C'\fR in a
6590 \&\f(CW\*(C`while\*(C'\fR loop, which will set \f(CW$_\fR on every iteration.
6591 If either a \f(CW\*(C`readdir\*(C'\fR expression or an explicit assignment of a
6592 \&\f(CW\*(C`readdir\*(C'\fR expression to a scalar is used as a \f(CW\*(C`while\*(C'\fR/\f(CW\*(C`for\*(C'\fR condition,
6593 then the condition actually tests for definedness of the expression's
6594 value, not for its regular truth value.
6595 .Sp
6596 .Vb 5
6597 \& opendir(my $dh, $some_dir) || die "Can\*(Aqt open $some_dir: $!";
6598 \& while (readdir $dh) {
6599 \& print "$some_dir/$_\en";
6600 \& }
6601 \& closedir $dh;
6602 .Ve
6603 .Sp
6604 To avoid confusing would-be users of your code who are running earlier
6605 versions of Perl with mysterious failures, put this sort of thing at the
6606 top of your file to signal that your code will work \fIonly\fR on Perls of a
6607 recent vintage:
6608 .Sp
6609 .Vb 1
6610 \& use v5.12; # so readdir assigns to $_ in a lone while test
6611 .Ve
6612 .IP "readline EXPR" 4
6613 .IX Item "readline EXPR"
6614 .PD 0
6615 .IP readline 4
6616 .IX Xref "readline gets fgets"
6617 .IX Item "readline"
6618 .PD
6619 Reads from the filehandle whose typeglob is contained in EXPR (or from
6620 \&\f(CW*ARGV\fR if EXPR is not provided). In scalar context, each call reads and
6621 returns the next line until end-of-file is reached, whereupon the
6622 subsequent call returns \f(CW\*(C`undef\*(C'\fR. In list context, reads
6623 until end-of-file is reached and returns a list of lines. Note that the
6624 notion of "line" used here is whatever you may have defined with
6625 \&\f(CW$/\fR (or \f(CW$INPUT_RECORD_SEPARATOR\fR in
6626 English). See "$/" in perlvar.
6627 .Sp
6628 When \f(CW$/\fR is set to \f(CW\*(C`undef\*(C'\fR,
6629 when \f(CW\*(C`readline\*(C'\fR is in scalar context (i.e., file
6630 slurp mode), and when an empty file is read, it returns \f(CW\*(Aq\*(Aq\fR the first
6631 time, followed by \f(CW\*(C`undef\*(C'\fR subsequently.
6632 .Sp
6633 This is the internal function implementing the \f(CW\*(C`<EXPR>\*(C'\fR
6634 operator, but you can use it directly. The \f(CW\*(C`<EXPR>\*(C'\fR
6635 operator is discussed in more detail in "I/O Operators" in perlop.
6636 .Sp
6637 .Vb 2
6638 \& my $line = <STDIN>;
6639 \& my $line = readline(STDIN); # same thing
6640 .Ve
6641 .Sp
6642 If \f(CW\*(C`readline\*(C'\fR encounters an operating system error,
6643 \&\f(CW$!\fR will be set with the corresponding error message.
6644 It can be helpful to check \f(CW$!\fR when you are reading from
6645 filehandles you don't trust, such as a tty or a socket. The following
6646 example uses the operator form of \f(CW\*(C`readline\*(C'\fR and dies
6647 if the result is not defined.
6648 .Sp
6649 .Vb 4
6650 \& while ( ! eof($fh) ) {
6651 \& defined( $_ = readline $fh ) or die "readline failed: $!";
6652 \& ...
6653 \& }
6654 .Ve
6655 .Sp
6656 Note that you can't handle \f(CW\*(C`readline\*(C'\fR errors
6657 that way with the \f(CW\*(C`ARGV\*(C'\fR filehandle. In that case, you have to open
6658 each element of \f(CW@ARGV\fR yourself since
6659 \&\f(CW\*(C`eof\*(C'\fR handles \f(CW\*(C`ARGV\*(C'\fR differently.
6660 .Sp
6661 .Vb 2
6662 \& foreach my $arg (@ARGV) {
6663 \& open(my $fh, $arg) or warn "Can\*(Aqt open $arg: $!";
6664 \&
6665 \& while ( ! eof($fh) ) {
6666 \& defined( $_ = readline $fh )
6667 \& or die "readline failed for $arg: $!";
6668 \& ...
6669 \& }
6670 \& }
6671 .Ve
6672 .Sp
6673 Like the \f(CW\*(C`<EXPR>\*(C'\fR operator, if a \f(CW\*(C`readline\*(C'\fR expression is
6674 used as the condition of a \f(CW\*(C`while\*(C'\fR or \f(CW\*(C`for\*(C'\fR loop, then it will be
6675 implicitly assigned to \f(CW$_\fR. If either a \f(CW\*(C`readline\*(C'\fR expression or
6676 an explicit assignment of a \f(CW\*(C`readline\*(C'\fR expression to a scalar is used
6677 as a \f(CW\*(C`while\*(C'\fR/\f(CW\*(C`for\*(C'\fR condition, then the condition actually tests for
6678 definedness of the expression's value, not for its regular truth value.
6679 .IP "readlink EXPR" 4
6680 .IX Xref "readlink"
6681 .IX Item "readlink EXPR"
6682 .PD 0
6683 .IP readlink 4
6684 .IX Item "readlink"
6685 .PD
6686 Returns the value of a symbolic link, if symbolic links are
6687 implemented. If not, raises an exception. If there is a system
6688 error, returns the undefined value and sets \f(CW$!\fR (errno).
6689 If EXPR is omitted, uses \f(CW$_\fR.
6690 .Sp
6691 Portability issues: "readlink" in perlport.
6692 .IP "readpipe EXPR" 4
6693 .IX Item "readpipe EXPR"
6694 .PD 0
6695 .IP readpipe 4
6696 .IX Xref "readpipe"
6697 .IX Item "readpipe"
6698 .PD
6699 EXPR is executed as a system command.
6700 The collected standard output of the command is returned.
6701 In scalar context, it comes back as a single (potentially
6702 multi-line) string. In list context, returns a list of lines
6703 (however you've defined lines with \f(CW$/\fR (or
6704 \&\f(CW$INPUT_RECORD_SEPARATOR\fR in English)).
6705 This is the internal function implementing the \f(CW\*(C`qx/EXPR/\*(C'\fR
6706 operator, but you can use it directly. The \f(CW\*(C`qx/EXPR/\*(C'\fR
6707 operator is discussed in more detail in "\f(CW\*(C`qx/\fR\f(CISTRING\fR\f(CW/\*(C'\fR" in perlop.
6708 If EXPR is omitted, uses \f(CW$_\fR.
6709 .IP "recv SOCKET,SCALAR,LENGTH,FLAGS" 4
6710 .IX Xref "recv"
6711 .IX Item "recv SOCKET,SCALAR,LENGTH,FLAGS"
6712 Receives a message on a socket. Attempts to receive LENGTH characters
6713 of data into variable SCALAR from the specified SOCKET filehandle.
6714 SCALAR will be grown or shrunk to the length actually read. Takes the
6715 same flags as the system call of the same name. Returns the address
6716 of the sender if SOCKET's protocol supports this; returns an empty
6717 string otherwise. If there's an error, returns the undefined value.
6718 This call is actually implemented in terms of the \fBrecvfrom\fR\|(2) system call.
6719 See "UDP: Message Passing" in perlipc for examples.
6720 .Sp
6721 Note that if the socket has been marked as \f(CW\*(C`:utf8\*(C'\fR, \f(CW\*(C`recv\*(C'\fR will
6722 throw an exception. The \f(CW:encoding(...)\fR layer implicitly introduces
6723 the \f(CW\*(C`:utf8\*(C'\fR layer. See \f(CW\*(C`binmode\*(C'\fR.
6724 .IP "redo LABEL" 4
6725 .IX Xref "redo"
6726 .IX Item "redo LABEL"
6727 .PD 0
6728 .IP "redo EXPR" 4
6729 .IX Item "redo EXPR"
6730 .IP redo 4
6731 .IX Item "redo"
6732 .PD
6733 The \f(CW\*(C`redo\*(C'\fR command restarts the loop block without
6734 evaluating the conditional again. The \f(CW\*(C`continue\*(C'\fR
6735 block, if any, is not executed. If
6736 the LABEL is omitted, the command refers to the innermost enclosing
6737 loop. The \f(CW\*(C`redo EXPR\*(C'\fR form, available starting in Perl 5.18.0, allows a
6738 label name to be computed at run time, and is otherwise identical to \f(CW\*(C`redo
6739 LABEL\*(C'\fR. Programs that want to lie to themselves about what was just input
6740 normally use this command:
6741 .Sp
6742 .Vb 10
6743 \& # a simpleminded Pascal comment stripper
6744 \& # (warning: assumes no { or } in strings)
6745 \& LINE: while (<STDIN>) {
6746 \& while (s|({.*}.*){.*}|$1 |) {}
6747 \& s|{.*}| |;
6748 \& if (s|{.*| |) {
6749 \& my $front = $_;
6750 \& while (<STDIN>) {
6751 \& if (/}/) { # end of comment?
6752 \& s|^|$front\e{|;
6753 \& redo LINE;
6754 \& }
6755 \& }
6756 \& }
6757 \& print;
6758 \& }
6759 .Ve
6760 .Sp
6761 \&\f(CW\*(C`redo\*(C'\fR cannot return a value from a block that typically
6762 returns a value, such as \f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR, or \f(CW\*(C`do {}\*(C'\fR. It will perform
6763 its flow control behavior, which precludes any return value. It should not be
6764 used to exit a \f(CW\*(C`grep\*(C'\fR or \f(CW\*(C`map\*(C'\fR
6765 operation.
6766 .Sp
6767 Note that a block by itself is semantically identical to a loop
6768 that executes once. Thus \f(CW\*(C`redo\*(C'\fR inside such a block
6769 will effectively turn it into a looping construct.
6770 .Sp
6771 See also \f(CW\*(C`continue\*(C'\fR for an illustration of how
6772 \&\f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
6773 \&\f(CW\*(C`redo\*(C'\fR work.
6774 .Sp
6775 Unlike most named operators, this has the same precedence as assignment.
6776 It is also exempt from the looks-like-a-function rule, so
6777 \&\f(CW\*(C`redo ("foo")."bar"\*(C'\fR will cause "bar" to be part of the argument to
6778 \&\f(CW\*(C`redo\*(C'\fR.
6779 .IP "ref EXPR" 4
6780 .IX Xref "ref reference"
6781 .IX Item "ref EXPR"
6782 .PD 0
6783 .IP ref 4
6784 .IX Item "ref"
6785 .PD
6786 Examines the value of EXPR, expecting it to be a reference, and returns
6787 a string giving information about the reference and the type of referent.
6788 If EXPR is not specified, \f(CW$_\fR will be used.
6789 .Sp
6790 If the operand is not a reference, then the empty string will be returned.
6791 An empty string will only be returned in this situation. \f(CW\*(C`ref\*(C'\fR is often
6792 useful to just test whether a value is a reference, which can be done
6793 by comparing the result to the empty string. It is a common mistake
6794 to use the result of \f(CW\*(C`ref\*(C'\fR directly as a truth value: this goes wrong
6795 because \f(CW0\fR (which is false) can be returned for a reference.
6796 .Sp
6797 If the operand is a reference to a blessed object, then the name of
6798 the class into which the referent is blessed will be returned. \f(CW\*(C`ref\*(C'\fR
6799 doesn't care what the physical type of the referent is; blessing takes
6800 precedence over such concerns. Beware that exact comparison of \f(CW\*(C`ref\*(C'\fR
6801 results against a class name doesn't perform a class membership test:
6802 a class's members also include objects blessed into subclasses, for
6803 which \f(CW\*(C`ref\*(C'\fR will return the name of the subclass. Also beware that
6804 class names can clash with the built-in type names (described below).
6805 .Sp
6806 If the operand is a reference to an unblessed object, then the return
6807 value indicates the type of object. If the unblessed referent is not
6808 a scalar, then the return value will be one of the strings \f(CW\*(C`ARRAY\*(C'\fR,
6809 \&\f(CW\*(C`HASH\*(C'\fR, \f(CW\*(C`CODE\*(C'\fR, \f(CW\*(C`FORMAT\*(C'\fR, or \f(CW\*(C`IO\*(C'\fR, indicating only which kind of
6810 object it is. If the unblessed referent is a scalar, then the return
6811 value will be one of the strings \f(CW\*(C`SCALAR\*(C'\fR, \f(CW\*(C`VSTRING\*(C'\fR, \f(CW\*(C`REF\*(C'\fR, \f(CW\*(C`GLOB\*(C'\fR,
6812 \&\f(CW\*(C`LVALUE\*(C'\fR, or \f(CW\*(C`REGEXP\*(C'\fR, depending on the kind of value the scalar
6813 currently has. But note that \f(CW\*(C`qr//\*(C'\fR scalars are created already
6814 blessed, so \f(CW\*(C`ref qr/.../\*(C'\fR will likely return \f(CW\*(C`Regexp\*(C'\fR. Beware that
6815 these built-in type names can also be used as
6816 class names, so \f(CW\*(C`ref\*(C'\fR returning one of these names doesn't unambiguously
6817 indicate that the referent is of the kind to which the name refers.
6818 .Sp
6819 The ambiguity between built-in type names and class names significantly
6820 limits the utility of \f(CW\*(C`ref\*(C'\fR. For unambiguous information, use
6821 \&\f(CWScalar::Util::blessed()\fR for information about
6822 blessing, and \f(CWScalar::Util::reftype()\fR for
6823 information about physical types. Use the \f(CW\*(C`isa\*(C'\fR method for class membership tests, though one must be
6824 sure of blessedness before attempting a method call. Alternatively, the
6825 \&\f(CW\*(C`isa\*(C'\fR operator can test class
6826 membership without checking blessedness first.
6827 .Sp
6828 See also perlref and perlobj.
6829 .IP "rename OLDNAME,NEWNAME" 4
6830 .IX Xref "rename move mv ren"
6831 .IX Item "rename OLDNAME,NEWNAME"
6832 Changes the name of a file; an existing file NEWNAME will be
6833 clobbered. Returns true for success; on failure returns false and sets
6834 \&\f(CW$!\fR.
6835 .Sp
6836 Behavior of this function varies wildly depending on your system
6837 implementation. For example, it will usually not work across file system
6838 boundaries, even though the system \fImv\fR command sometimes compensates
6839 for this. Other restrictions include whether it works on directories,
6840 open files, or pre-existing files. Check perlport and either the
6841 \&\fBrename\fR\|(2) manpage or equivalent system documentation for details.
6842 .Sp
6843 For a platform independent \f(CW\*(C`move\*(C'\fR function look at
6844 the File::Copy module.
6845 .Sp
6846 Portability issues: "rename" in perlport.
6847 .IP "require VERSION" 4
6848 .IX Xref "require"
6849 .IX Item "require VERSION"
6850 .PD 0
6851 .IP "require EXPR" 4
6852 .IX Item "require EXPR"
6853 .IP require 4
6854 .IX Item "require"
6855 .PD
6856 Demands a version of Perl specified by VERSION, or demands some semantics
6857 specified by EXPR or by \f(CW$_\fR if EXPR is not supplied.
6858 .Sp
6859 VERSION may be either a literal such as v5.24.1, which will be
6860 compared to \f(CW$^V\fR (or \f(CW$PERL_VERSION\fR in English),
6861 or a numeric argument of the form 5.024001, which will be compared to
6862 \&\f(CW$]\fR. An exception is raised if VERSION is greater than
6863 the version of the current Perl interpreter. Compare with
6864 \&\f(CW\*(C`use\*(C'\fR, which can do a similar check at
6865 compile time.
6866 .Sp
6867 Specifying VERSION as a numeric argument of the form 5.024001 should
6868 generally be avoided as older less readable syntax compared to
6869 v5.24.1. Before perl 5.8.0 (released in 2002), the more verbose numeric
6870 form was the only supported syntax, which is why you might see it in
6871 older code.
6872 .Sp
6873 .Vb 4
6874 \& require v5.24.1; # run time version check
6875 \& require 5.24.1; # ditto
6876 \& require 5.024_001; # ditto; older syntax compatible
6877 \& with perl 5.6
6878 .Ve
6879 .Sp
6880 Otherwise, \f(CW\*(C`require\*(C'\fR demands that a library file be
6881 included if it hasn't already been included. The file is included via
6882 the do-FILE mechanism, which is essentially just a variety of
6883 \&\f(CW\*(C`eval\*(C'\fR with the
6884 caveat that lexical variables in the invoking script will be invisible
6885 to the included code. If it were implemented in pure Perl, it
6886 would have semantics similar to the following:
6887 .Sp
6888 .Vb 2
6889 \& use Carp \*(Aqcroak\*(Aq;
6890 \& use version;
6891 \&
6892 \& sub require {
6893 \& my ($filename) = @_;
6894 \& if ( my $version = eval { version\->parse($filename) } ) {
6895 \& if ( $version > $^V ) {
6896 \& my $vn = $version\->normal;
6897 \& croak "Perl $vn required\-\-this is only $^V, stopped";
6898 \& }
6899 \& return 1;
6900 \& }
6901 \&
6902 \& if (exists $INC{$filename}) {
6903 \& return 1 if $INC{$filename};
6904 \& croak "Compilation failed in require";
6905 \& }
6906 \&
6907 \& local $INC;
6908 \& # this type of loop lets a hook overwrite $INC if they wish
6909 \& for($INC = 0; $INC < @INC; $INC++) {
6910 \& my $prefix = $INC[$INC];
6911 \& if (!defined $prefix) {
6912 \& next;
6913 \& }
6914 \& if (ref $prefix) {
6915 \& #... do other stuff \- see text below ....
6916 \& }
6917 \& # (see text below about possible appending of .pmc
6918 \& # suffix to $filename)
6919 \& my $realfilename = "$prefix/$filename";
6920 \& next if ! \-e $realfilename || \-d _ || \-b _;
6921 \& $INC{$filename} = $realfilename;
6922 \& my $result = do($realfilename);
6923 \& # but run in caller\*(Aqs namespace
6924 \&
6925 \& if (!defined $result) {
6926 \& $INC{$filename} = undef;
6927 \& croak $@ ? "$@Compilation failed in require"
6928 \& : "Can\*(Aqt locate $filename: $!\en";
6929 \& }
6930 \& if (!$result) {
6931 \& delete $INC{$filename};
6932 \& croak "$filename did not return true value";
6933 \& }
6934 \& $! = 0;
6935 \& return $result;
6936 \& }
6937 \& croak "Can\*(Aqt locate $filename in \e@INC ...";
6938 \& }
6939 .Ve
6940 .Sp
6941 Note that the file will not be included twice under the same specified
6942 name.
6943 .Sp
6944 Historically the file must return true as the last statement to indicate
6945 successful execution of any initialization code, so it's customary to
6946 end such a file with \f(CW\*(C`1;\*(C'\fR unless you're sure it'll return true
6947 otherwise. But it's better just to put the \f(CW\*(C`1;\*(C'\fR, in case you add more
6948 statements. As of 5.37.6 this requirement may be avoided by enabling
6949 the 'module_true' feature, which is enabled by default in modern
6950 version bundles. Thus code with \f(CW\*(C`use v5.37;\*(C'\fR no longer needs to concern
6951 itself with this issue. See feature for more details. Note that this
6952 affects the compilation unit within which the feature is used, and using
6953 it before requiring a module will not change the behavior of existing
6954 modules that do not themselves also use it.
6955 .Sp
6956 If EXPR is a bareword, \f(CW\*(C`require\*(C'\fR assumes a \fI.pm\fR
6957 extension and replaces \f(CW\*(C`::\*(C'\fR with \f(CW\*(C`/\*(C'\fR in the filename for you,
6958 to make it easy to load standard modules. This form of loading of
6959 modules does not risk altering your namespace, however it will autovivify
6960 the stash for the required module.
6961 .Sp
6962 In other words, if you try this:
6963 .Sp
6964 .Vb 1
6965 \& require Foo::Bar; # a splendid bareword
6966 .Ve
6967 .Sp
6968 The require function will actually look for the \fIFoo/Bar.pm\fR file in the
6969 directories specified in the \f(CW@INC\fR array, and it will
6970 autovivify the \f(CW\*(C`Foo::Bar::\*(C'\fR stash at compile time.
6971 .Sp
6972 But if you try this:
6973 .Sp
6974 .Vb 4
6975 \& my $class = \*(AqFoo::Bar\*(Aq;
6976 \& require $class; # $class is not a bareword
6977 \& #or
6978 \& require "Foo::Bar"; # not a bareword because of the ""
6979 .Ve
6980 .Sp
6981 The require function will look for the \fIFoo::Bar\fR file in the
6982 \&\f(CW@INC\fR array and
6983 will complain about not finding \fIFoo::Bar\fR there. In this case you can do:
6984 .Sp
6985 .Vb 1
6986 \& eval "require $class";
6987 .Ve
6988 .Sp
6989 or you could do
6990 .Sp
6991 .Vb 1
6992 \& require "Foo/Bar.pm";
6993 .Ve
6994 .Sp
6995 Neither of these forms will autovivify any stashes at compile time and
6996 only have run time effects.
6997 .Sp
6998 Now that you understand how \f(CW\*(C`require\*(C'\fR looks for
6999 files with a bareword argument, there is a little extra functionality
7000 going on behind the scenes. Before \f(CW\*(C`require\*(C'\fR looks
7001 for a \fI.pm\fR extension, it will first look for a similar filename with a
7002 \&\fI.pmc\fR extension. If this file is found, it will be loaded in place of
7003 any file ending in a \fI.pm\fR extension. This applies to both the explicit
7004 \&\f(CW\*(C`require "Foo/Bar.pm";\*(C'\fR form and the \f(CW\*(C`require Foo::Bar;\*(C'\fR form.
7005 .Sp
7006 You can also insert hooks into the import facility by putting Perl
7007 coderefs or objects directly into the \f(CW@INC\fR array.
7008 There are two types of hooks, INC filters, and INCDIR hooks, and there
7009 are three forms of representing a hook: subroutine references, array
7010 references, and blessed objects.
7011 .Sp
7012 Subroutine references are the simplest case. When the inclusion system
7013 walks through \f(CW@INC\fR and encounters a subroutine, unless
7014 this subroutine is blessed and supports an INCDIR hook this
7015 subroutine will be assumed to be an INC hook will be called with two
7016 parameters, the first a reference to itself, and the second the name of
7017 the file to be included (e.g., \fIFoo/Bar.pm\fR). The subroutine should
7018 return either nothing or else a list of up to four values in the
7019 following order:
7020 .RS 4
7021 .IP 1. 4
7022 A reference to a scalar, containing any initial source code to prepend to
7023 the file or generator output.
7024 .IP 2. 4
7025 A filehandle, from which the file will be read.
7026 .IP 3. 4
7027 A reference to a subroutine. If there is no filehandle (previous item),
7028 then this subroutine is expected to generate one line of source code per
7029 call, writing the line into \f(CW$_\fR and returning 1, then
7030 finally at end of file returning 0. If there is a filehandle, then the
7031 subroutine will be called to act as a simple source filter, with the
7032 line as read in \f(CW$_\fR.
7033 Again, return 1 for each valid line, and 0 after all lines have been
7034 returned.
7035 For historical reasons the subroutine will receive a meaningless argument
7036 (in fact always the numeric value zero) as \f(CW$_[0]\fR.
7037 .IP 4. 4
7038 Optional state for the subroutine. The state is passed in as \f(CW$_[1]\fR.
7039 .RE
7040 .RS 4
7041 .Sp
7042 \&\f(CW\*(C`AUTOLOAD\*(C'\fR cannot be used to resolve the \f(CW\*(C`INCDIR\*(C'\fR method, \f(CW\*(C`INC\*(C'\fR is
7043 checked first, and \f(CW\*(C`AUTOLOAD\*(C'\fR would resolve that.
7044 .Sp
7045 If an empty list, \f(CW\*(C`undef\*(C'\fR, or nothing that matches the
7046 first 3 values above is returned, then \f(CW\*(C`require\*(C'\fR
7047 looks at the remaining elements of \f(CW@INC\fR.
7048 Note that this filehandle must be a real filehandle (strictly a typeglob
7049 or reference to a typeglob, whether blessed or unblessed); tied filehandles
7050 will be ignored and processing will stop there.
7051 .Sp
7052 If the hook is an object, it should provide an \f(CW\*(C`INC\*(C'\fR or \f(CW\*(C`INCDIR\*(C'\fR
7053 method that will be called as above, the first parameter being the
7054 object itself. If it does not provide either method, and the object is
7055 not CODE ref then an exception will be thrown, otherwise it will simply
7056 be executed like an unblessed CODE ref would. Note that you must fully
7057 qualify the method name when you declare an \f(CW\*(C`INC\*(C'\fR sub (unlike the
7058 \&\f(CW\*(C`INCDIR\*(C'\fR sub), as the unqualified symbol \f(CW\*(C`INC\*(C'\fR is always forced into
7059 package \f(CW\*(C`main\*(C'\fR. Here is a typical code layout for an \f(CW\*(C`INC\*(C'\fR hook:
7060 .Sp
7061 .Vb 7
7062 \& # In Foo.pm
7063 \& package Foo;
7064 \& sub new { ... }
7065 \& sub Foo::INC {
7066 \& my ($self, $filename) = @_;
7067 \& ...
7068 \& }
7069 \&
7070 \& # In the main program
7071 \& push @INC, Foo\->new(...);
7072 .Ve
7073 .Sp
7074 If the hook is an array reference, its first element must be a
7075 subroutine reference or an object as described above. When the first
7076 element is an object that supports an \f(CW\*(C`INC\*(C'\fR or \f(CW\*(C`INCDIR\*(C'\fR method then
7077 the method will be called with the object as the first argument, the
7078 filename requested as the second, and the hook array reference as the
7079 the third. When the first element is a subroutine then it will be
7080 called with the array as the first argument, and the filename as the
7081 second, no third parameter will be passed in. In both forms you can
7082 modify the contents of the array to provide state between calls, or
7083 whatever you like.
7084 .Sp
7085 In other words, you can write:
7086 .Sp
7087 .Vb 5
7088 \& push @INC, \e&my_sub;
7089 \& sub my_sub {
7090 \& my ($coderef, $filename) = @_; # $coderef is \e&my_sub
7091 \& ...
7092 \& }
7093 .Ve
7094 .Sp
7095 or:
7096 .Sp
7097 .Vb 7
7098 \& push @INC, [ \e&my_sub, $x, $y, ... ];
7099 \& sub my_sub {
7100 \& my ($arrayref, $filename) = @_;
7101 \& # Retrieve $x, $y, ...
7102 \& my (undef, @parameters) = @$arrayref;
7103 \& ...
7104 \& }
7105 .Ve
7106 .Sp
7107 or:
7108 .Sp
7109 .Vb 6
7110 \& push @INC, [ HookObj\->new(), $x, $y, ... ];
7111 \& sub HookObj::INC {
7112 \& my ($self, $filename, $arrayref)= @_;
7113 \& my (undef, @parameters) = @$arrayref;
7114 \& ...
7115 \& }
7116 .Ve
7117 .Sp
7118 These hooks are also permitted to set the \f(CW%INC\fR entry
7119 corresponding to the files they have loaded. See "%INC" in perlvar.
7120 Should an \f(CW\*(C`INC\*(C'\fR hook not do this then perl will set the \f(CW%INC\fR entry
7121 to be the hook reference itself.
7122 .Sp
7123 A hook may also be used to rewrite the \f(CW@INC\fR array. While this might
7124 sound strange, there are situations where it can be very useful to do
7125 this. Such hooks usually just return undef and do not mix filtering and
7126 \&\f(CW@INC\fR modifications. While in older versions of perl having a hook
7127 modify \f(CW@INC\fR was fraught with issues and could even result in
7128 segfaults or assert failures, as of 5.37.7 the logic has been made much
7129 more robust and the hook now has control over the loop iteration if it
7130 wishes to do so.
7131 .Sp
7132 There is a now a facility to control the iterator for the \f(CW@INC\fR array
7133 traversal that is performed during require. The \f(CW$INC\fR variable will be
7134 initialized with the index of the currently executing hook. Once the
7135 hook returns the next slot in \f(CW@INC\fR that will be checked will be the
7136 integer successor of value in \f(CW$INC\fR (or \-1 if it is undef). For example
7137 the following code
7138 .Sp
7139 .Vb 7
7140 \& push @INC, sub {
7141 \& splice @INC, $INC, 1; # remove this hook from @INC
7142 \& unshift @INC, sub { warn "A" };
7143 \& undef $INC; # reset the $INC iterator so we
7144 \& # execute the newly installed sub
7145 \& # immediately.
7146 \& };
7147 .Ve
7148 .Sp
7149 would install a sub into \f(CW@INC\fR that when executed as a hook (by for
7150 instance a require of a file that does not exist), the hook will splice
7151 itself out of \f(CW@INC\fR, and add a new sub to the front that will warn
7152 whenever someone does a require operation that requires an \f(CW@INC\fR
7153 search, and then immediately execute that hook.
7154 .Sp
7155 Prior to 5.37.7, there was no way to cause perl to use the newly
7156 installed hook immediately, or to inspect any changed items in \f(CW@INC\fR to
7157 the left of the iterator, and so the warning would only be generated on
7158 the second call to require. In more recent perl the presence of the last
7159 statement which undefines \f(CW$INC\fR will cause perl to restart the
7160 traversal of the \f(CW@INC\fR array at the beginning and execute the newly
7161 installed sub immediately.
7162 .Sp
7163 Whatever value \f(CW$INC\fR held, if any, will be restored at the end of the
7164 require. Any changes made to \f(CW$INC\fR during the lifetime of the hook
7165 will be unrolled after the hook exits, and its value only has meaning
7166 immediately after execution of the hook, thus setting \f(CW$INC\fR to some
7167 value prior to executing a \f(CW\*(C`require\*(C'\fR will have no effect on how the
7168 require executes at all.
7169 .Sp
7170 As of 5.37.7 \f(CW@INC\fR values of undef will be silently ignored.
7171 .Sp
7172 The function \f(CWrequire()\fR is difficult to wrap properly. Many modules
7173 consult the stack to find information about their caller, and injecting
7174 a new stack frame by wrapping \f(CWrequire()\fR often breaks things.
7175 Nevertheless it can be very helpful to have the ability to perform
7176 actions before and after a \f(CW\*(C`require\*(C'\fR, for instance for trace utilities
7177 like \f(CW\*(C`Devel::TraceUse\*(C'\fR or to measure time to load and the memory
7178 consumption of the require graph. Because of the difficulties in safely
7179 creating a \f(CWrequire()\fR wrapper in 5.37.10 we introduced a new mechanism.
7180 .Sp
7181 As of 5.37.10, prior to any other actions it performs, \f(CW\*(C`require\*(C'\fR will
7182 check if \f(CW\*(C`${^HOOK}{require_\|_before}\*(C'\fR contains a coderef, and if it does
7183 it will be called with the filename form of the item being loaded. The hook
7184 may modify \f(CW$_[0]\fR to load a different filename, or it may throw a fatal
7185 exception to cause the require to fail, which will be treated as though the
7186 required code itself had thrown an exception.
7187 .Sp
7188 The \f(CW\*(C`${^HOOK}{require_\|_before}\*(C'\fR hook may return a code reference, in
7189 which case the code reference will be executed (in an eval with the
7190 filname as a parameter) after the require completes. It will be executed
7191 regardless of how the compilation completed, and even if the require
7192 throws a fatal exception. The function may consult \f(CW%INC\fR to determine
7193 if the require failed or not. For instance the following code will print
7194 some diagnostics before and after every \f(CW\*(C`require\*(C'\fR statement. The
7195 example also includes logic to chain the signal, so that multiple
7196 signals can cooperate. Well behaved \f(CW\*(C`${^HOOK}{require_\|_before}\*(C'\fR
7197 handlers should always take this into account.
7198 .Sp
7199 .Vb 10
7200 \& {
7201 \& use Scalar::Util qw(reftype);
7202 \& my $old_hook = ${^HOOK}{require_\|_before};
7203 \& local ${^HOOK}{require_\|_before} = sub {
7204 \& my ($name) = @_;
7205 \& my $old_hook_ret;
7206 \& $old_hook_ret = $old_hook\->($name) if $old_hook;
7207 \& warn "Requiring: $name\en";
7208 \& return sub {
7209 \& $old_hook_ret\->() if ref($old_hook_ret)
7210 \& && reftype($old_hook_ret) eq "CODE";
7211 \& warn sprintf "Finished requiring %s: %s\en",
7212 \& $name, $INC{$name} ? "loaded" :"failed";
7213 \& };
7214 \& };
7215 \& require Whatever;
7216 \& }
7217 .Ve
7218 .Sp
7219 This hook executes for ALL \f(CW\*(C`require\*(C'\fR statements, unlike \f(CW\*(C`INC\*(C'\fR and
7220 \&\f(CW\*(C`INCDIR\*(C'\fR hooks, which are only executed for relative file names, and it
7221 executes first before any other special behaviour inside of require.
7222 Note that the initial hook in \f(CW\*(C`${^HOOK}{require_\|_before}\*(C'\fR is *not*
7223 executed inside of an eval, and throwing an exception will stop further
7224 processing, but the after hook it may return is executed inside of an
7225 eval, and any exceptions it throws will be silently ignored. This is
7226 because it executes inside of the scope cleanup logic that is triggered
7227 after the require completes, and an exception at this time would not
7228 stop the module from being loaded, etc.
7229 .Sp
7230 There is a similar hook that fires after require completes,
7231 \&\f(CW\*(C`${^HOOK}{require_\|_after}\*(C'\fR, which will be called after each require statement
7232 completes, either via an exception or successfully. It will be called with
7233 the filename of the most recently executed require statement. It is executed
7234 in an eval, and will not in any way affect execution.
7235 .Sp
7236 For a yet-more-powerful import facility built around \f(CW\*(C`require\*(C'\fR, see
7237 \&\f(CW\*(C`use\*(C'\fR and perlmod.
7238 .RE
7239 .IP "reset EXPR" 4
7240 .IX Xref "reset"
7241 .IX Item "reset EXPR"
7242 .PD 0
7243 .IP reset 4
7244 .IX Item "reset"
7245 .PD
7246 Generally used in a \f(CW\*(C`continue\*(C'\fR block at the end of a
7247 loop to clear variables and reset \f(CW\*(C`m?pattern?\*(C'\fR searches so that they
7248 work again. The
7249 expression is interpreted as a list of single characters (hyphens
7250 allowed for ranges). All variables (scalars, arrays, and hashes)
7251 in the current package beginning with one of
7252 those letters are reset to their pristine state. If the expression is
7253 omitted, one-match searches (\f(CW\*(C`m?pattern?\*(C'\fR) are reset to match again.
7254 Only resets variables or searches in the current package. Always returns
7255 1. Examples:
7256 .Sp
7257 .Vb 3
7258 \& reset \*(AqX\*(Aq; # reset all X variables
7259 \& reset \*(Aqa\-z\*(Aq; # reset lower case variables
7260 \& reset; # just reset m?one\-time? searches
7261 .Ve
7262 .Sp
7263 Resetting \f(CW"A\-Z"\fR is not recommended because you'll wipe out your
7264 \&\f(CW@ARGV\fR and \f(CW@INC\fR arrays and your
7265 \&\f(CW%ENV\fR hash.
7266 .Sp
7267 Resets only package variables; lexical variables are unaffected, but
7268 they clean themselves up on scope exit anyway, so you'll probably want
7269 to use them instead. See \f(CW\*(C`my\*(C'\fR.
7270 .IP "return EXPR" 4
7271 .IX Xref "return"
7272 .IX Item "return EXPR"
7273 .PD 0
7274 .IP return 4
7275 .IX Item "return"
7276 .PD
7277 Returns from a subroutine, \f(CW\*(C`eval\*(C'\fR,
7278 \&\f(CW\*(C`do FILE\*(C'\fR, \f(CW\*(C`sort\*(C'\fR block or regex
7279 eval block (but not a \f(CW\*(C`grep\*(C'\fR,
7280 \&\f(CW\*(C`map\*(C'\fR, or \f(CW\*(C`do BLOCK\*(C'\fR block) with the value
7281 given in EXPR. Evaluation of EXPR may be in list, scalar, or void
7282 context, depending on how the return value will be used, and the context
7283 may vary from one execution to the next (see
7284 \&\f(CW\*(C`wantarray\*(C'\fR). If no EXPR
7285 is given, returns an empty list in list context, the undefined value in
7286 scalar context, and (of course) nothing at all in void context.
7287 .Sp
7288 (In the absence of an explicit \f(CW\*(C`return\*(C'\fR, a subroutine,
7289 \&\f(CW\*(C`eval\*(C'\fR,
7290 or \f(CW\*(C`do FILE\*(C'\fR automatically returns the value of the last expression
7291 evaluated.)
7292 .Sp
7293 Unlike most named operators, this is also exempt from the
7294 looks-like-a-function rule, so \f(CW\*(C`return ("foo")."bar"\*(C'\fR will
7295 cause \f(CW"bar"\fR to be part of the argument to \f(CW\*(C`return\*(C'\fR.
7296 .IP "reverse LIST" 4
7297 .IX Xref "reverse rev invert"
7298 .IX Item "reverse LIST"
7299 In list context, returns a list value consisting of the elements
7300 of LIST in the opposite order. In scalar context, concatenates the
7301 elements of LIST and returns a string value with all characters
7302 in the opposite order.
7303 .Sp
7304 .Vb 1
7305 \& print join(", ", reverse "world", "Hello"); # Hello, world
7306 \&
7307 \& print scalar reverse "dlrow ,", "olleH"; # Hello, world
7308 .Ve
7309 .Sp
7310 Used without arguments in scalar context, \f(CW\*(C`reverse\*(C'\fR
7311 reverses \f(CW$_\fR.
7312 .Sp
7313 .Vb 3
7314 \& $_ = "dlrow ,olleH";
7315 \& print reverse; # No output, list context
7316 \& print scalar reverse; # Hello, world
7317 .Ve
7318 .Sp
7319 Note that reversing an array to itself (as in \f(CW\*(C`@a = reverse @a\*(C'\fR) will
7320 preserve non-existent elements whenever possible; i.e., for non-magical
7321 arrays or for tied arrays with \f(CW\*(C`EXISTS\*(C'\fR and \f(CW\*(C`DELETE\*(C'\fR methods.
7322 .Sp
7323 This operator is also handy for inverting a hash, although there are some
7324 caveats. If a value is duplicated in the original hash, only one of those
7325 can be represented as a key in the inverted hash. Also, this has to
7326 unwind one hash and build a whole new one, which may take some time
7327 on a large hash, such as from a DBM file.
7328 .Sp
7329 .Vb 1
7330 \& my %by_name = reverse %by_address; # Invert the hash
7331 .Ve
7332 .IP "rewinddir DIRHANDLE" 4
7333 .IX Xref "rewinddir"
7334 .IX Item "rewinddir DIRHANDLE"
7335 Sets the current position to the beginning of the directory for the
7336 \&\f(CW\*(C`readdir\*(C'\fR routine on DIRHANDLE.
7337 .Sp
7338 Portability issues: "rewinddir" in perlport.
7339 .IP "rindex STR,SUBSTR,POSITION" 4
7340 .IX Xref "rindex"
7341 .IX Item "rindex STR,SUBSTR,POSITION"
7342 .PD 0
7343 .IP "rindex STR,SUBSTR" 4
7344 .IX Item "rindex STR,SUBSTR"
7345 .PD
7346 Works just like \f(CW\*(C`index\*(C'\fR except that it
7347 returns the position of the \fIlast\fR
7348 occurrence of SUBSTR in STR. If POSITION is specified, returns the
7349 last occurrence beginning at or before that position.
7350 .IP "rmdir FILENAME" 4
7351 .IX Xref "rmdir rd directory, remove"
7352 .IX Item "rmdir FILENAME"
7353 .PD 0
7354 .IP rmdir 4
7355 .IX Item "rmdir"
7356 .PD
7357 Deletes the directory specified by FILENAME if that directory is
7358 empty. If it succeeds it returns true; otherwise it returns false and
7359 sets \f(CW$!\fR (errno). If FILENAME is omitted, uses
7360 \&\f(CW$_\fR.
7361 .Sp
7362 To remove a directory tree recursively (\f(CW\*(C`rm \-rf\*(C'\fR on Unix) look at
7363 the \f(CW\*(C`rmtree\*(C'\fR function of the File::Path
7364 module.
7365 .IP s/// 4
7366 .IX Item "s///"
7367 The substitution operator. See "Regexp Quote-Like Operators" in perlop.
7368 .IP "say FILEHANDLE LIST" 4
7369 .IX Xref "say"
7370 .IX Item "say FILEHANDLE LIST"
7371 .PD 0
7372 .IP "say FILEHANDLE" 4
7373 .IX Item "say FILEHANDLE"
7374 .IP "say LIST" 4
7375 .IX Item "say LIST"
7376 .IP say 4
7377 .IX Item "say"
7378 .PD
7379 Just like \f(CW\*(C`print\*(C'\fR, but implicitly appends a
7380 newline at the end of the LIST instead of any value \f(CW\*(C`$\e\*(C'\fR
7381 might have. To use FILEHANDLE without a LIST to
7382 print the contents of \f(CW$_\fR to it, you must use a bareword
7383 filehandle like \f(CW\*(C`FH\*(C'\fR, not an indirect one like \f(CW$fh\fR.
7384 .Sp
7385 \&\f(CW\*(C`say\*(C'\fR is available only if the
7386 \&\f(CW"say"\fR feature is enabled or if it is
7387 prefixed with \f(CW\*(C`CORE::\*(C'\fR. The
7388 \&\f(CW"say"\fR feature is enabled automatically
7389 with a \f(CW\*(C`use v5.10\*(C'\fR (or higher) declaration in the current scope.
7390 .IP "scalar EXPR" 4
7391 .IX Xref "scalar context"
7392 .IX Item "scalar EXPR"
7393 Forces EXPR to be interpreted in scalar context and returns the value
7394 of EXPR.
7395 .Sp
7396 .Vb 1
7397 \& my @counts = ( scalar @a, scalar @b, scalar @c );
7398 .Ve
7399 .Sp
7400 There is no equivalent operator to force an expression to
7401 be interpolated in list context because in practice, this is never
7402 needed. If you really wanted to do so, however, you could use
7403 the construction \f(CW\*(C`@{[ (some expression) ]}\*(C'\fR, but usually a simple
7404 \&\f(CW\*(C`(some expression)\*(C'\fR suffices.
7405 .Sp
7406 Because \f(CW\*(C`scalar\*(C'\fR is a unary operator, if you
7407 accidentally use a
7408 parenthesized list for the EXPR, this behaves as a scalar comma expression,
7409 evaluating all but the last element in void context and returning the final
7410 element evaluated in scalar context. This is seldom what you want.
7411 .Sp
7412 The following single statement:
7413 .Sp
7414 .Vb 1
7415 \& print uc(scalar(foo(), $bar)), $baz;
7416 .Ve
7417 .Sp
7418 is the moral equivalent of these two:
7419 .Sp
7420 .Vb 2
7421 \& foo();
7422 \& print(uc($bar), $baz);
7423 .Ve
7424 .Sp
7425 See perlop for more details on unary operators and the comma operator,
7426 and perldata for details on evaluating a hash in scalar context.
7427 .IP "seek FILEHANDLE,POSITION,WHENCE" 4
7428 .IX Xref "seek fseek filehandle, position"
7429 .IX Item "seek FILEHANDLE,POSITION,WHENCE"
7430 Sets FILEHANDLE's position, just like the \fBfseek\fR\|(3) call of C \f(CW\*(C`stdio\*(C'\fR.
7431 FILEHANDLE may be an expression whose value gives the name of the
7432 filehandle. The values for WHENCE are \f(CW0\fR to set the new position
7433 \&\fIin bytes\fR to POSITION; \f(CW1\fR to set it to the current position plus
7434 POSITION; and \f(CW2\fR to set it to EOF plus POSITION, typically
7435 negative. For WHENCE you may use the constants \f(CW\*(C`SEEK_SET\*(C'\fR,
7436 \&\f(CW\*(C`SEEK_CUR\*(C'\fR, and \f(CW\*(C`SEEK_END\*(C'\fR (start of the file, current position, end
7437 of the file) from the Fcntl module. Returns \f(CW1\fR on success, false
7438 otherwise.
7439 .Sp
7440 Note the emphasis on bytes: even if the filehandle has been set to operate
7441 on characters (for example using the \f(CW:encoding(UTF\-8)\fR I/O layer), the
7442 \&\f(CW\*(C`seek\*(C'\fR,
7443 \&\f(CW\*(C`tell\*(C'\fR, and
7444 \&\f(CW\*(C`sysseek\*(C'\fR
7445 family of functions use byte offsets, not character offsets,
7446 because seeking to a character offset would be very slow in a UTF\-8 file.
7447 .Sp
7448 If you want to position the file for
7449 \&\f(CW\*(C`sysread\*(C'\fR or
7450 \&\f(CW\*(C`syswrite\*(C'\fR, don't use
7451 \&\f(CW\*(C`seek\*(C'\fR, because buffering makes its
7452 effect on the file's read-write position unpredictable and non-portable.
7453 Use \f(CW\*(C`sysseek\*(C'\fR instead.
7454 .Sp
7455 Due to the rules and rigors of ANSI C, on some systems you have to do a
7456 seek whenever you switch between reading and writing. Amongst other
7457 things, this may have the effect of calling stdio's \fBclearerr\fR\|(3).
7458 A WHENCE of \f(CW1\fR (\f(CW\*(C`SEEK_CUR\*(C'\fR) is useful for not moving the file position:
7459 .Sp
7460 .Vb 1
7461 \& seek($fh, 0, 1);
7462 .Ve
7463 .Sp
7464 This is also useful for applications emulating \f(CW\*(C`tail \-f\*(C'\fR. Once you hit
7465 EOF on your read and then sleep for a while, you (probably) have to stick in a
7466 dummy \f(CW\*(C`seek\*(C'\fR to reset things. The
7467 \&\f(CW\*(C`seek\*(C'\fR doesn't change the position,
7468 but it \fIdoes\fR clear the end-of-file condition on the handle, so that the
7469 next \f(CW\*(C`readline FILE\*(C'\fR makes Perl try again to read something. (We hope.)
7470 .Sp
7471 If that doesn't work (some I/O implementations are particularly
7472 cantankerous), you might need something like this:
7473 .Sp
7474 .Vb 8
7475 \& for (;;) {
7476 \& for ($curpos = tell($fh); $_ = readline($fh);
7477 \& $curpos = tell($fh)) {
7478 \& # search for some stuff and put it into files
7479 \& }
7480 \& sleep($for_a_while);
7481 \& seek($fh, $curpos, 0);
7482 \& }
7483 .Ve
7484 .IP "seekdir DIRHANDLE,POS" 4
7485 .IX Xref "seekdir"
7486 .IX Item "seekdir DIRHANDLE,POS"
7487 Sets the current position for the \f(CW\*(C`readdir\*(C'\fR
7488 routine on DIRHANDLE. POS must be a value returned by
7489 \&\f(CW\*(C`telldir\*(C'\fR. \f(CW\*(C`seekdir\*(C'\fR
7490 also has the same caveats about possible directory compaction as the
7491 corresponding system library routine.
7492 .IP "select FILEHANDLE" 4
7493 .IX Xref "select filehandle, default"
7494 .IX Item "select FILEHANDLE"
7495 .PD 0
7496 .IP select 4
7497 .IX Item "select"
7498 .PD
7499 Returns the currently selected filehandle. If FILEHANDLE is supplied,
7500 sets the new current default filehandle for output. This has two
7501 effects: first, a \f(CW\*(C`write\*(C'\fR, \f(CW\*(C`print\*(C'\fR, or \f(CW\*(C`say\*(C'\fR without a
7502 filehandle will default to this FILEHANDLE. Second, references to variables
7503 related to output will refer to this output channel.
7504 .Sp
7505 For example, to set the top-of-form format for more than one
7506 output channel, you might do the following:
7507 .Sp
7508 .Vb 4
7509 \& select(REPORT1);
7510 \& $^ = \*(Aqreport1_top\*(Aq;
7511 \& select(REPORT2);
7512 \& $^ = \*(Aqreport2_top\*(Aq;
7513 .Ve
7514 .Sp
7515 FILEHANDLE may be an expression whose value gives the name of the
7516 actual filehandle. Thus:
7517 .Sp
7518 .Vb 1
7519 \& my $oldfh = select(STDERR); $| = 1; select($oldfh);
7520 .Ve
7521 .Sp
7522 Some programmers may prefer to think of filehandles as objects with
7523 methods, preferring to write the last example as:
7524 .Sp
7525 .Vb 1
7526 \& STDERR\->autoflush(1);
7527 .Ve
7528 .Sp
7529 (Prior to Perl version 5.14, you have to \f(CW\*(C`use IO::Handle;\*(C'\fR explicitly
7530 first.)
7531 .Sp
7532 Whilst you can use \f(CW\*(C`select\*(C'\fR to temporarily "capture" the output of
7533 \&\f(CW\*(C`print\*(C'\fR like this:
7534 .Sp
7535 .Vb 2
7536 \& {
7537 \& my $old_handle = select $new_handle;
7538 \&
7539 \& # This goes to $new_handle:
7540 \& print "ok 1\en";
7541 \& ...
7542 \&
7543 \& select $old_handle;
7544 \& }
7545 .Ve
7546 .Sp
7547 you might find it easier to localize the typeglob instead:
7548 .Sp
7549 .Vb 2
7550 \& {
7551 \& local *STDOUT = $new_handle;
7552 \&
7553 \& print "ok 1\en";
7554 \& ...
7555 \& }
7556 .Ve
7557 .Sp
7558 The two are not exactly equivalent, but the latter might be clearer and will
7559 restore STDOUT if the wrapped code dies. The difference is that in the
7560 former, the original STDOUT can still be accessed by explicitly using it in a
7561 \&\f(CW\*(C`print\*(C'\fR statement (as \f(CW\*(C`print STDOUT ...\*(C'\fR), whereas in the latter the meaning
7562 of the STDOUT handle itself has temporarily been changed.
7563 .Sp
7564 Portability issues: "select" in perlport.
7565 .IP "select RBITS,WBITS,EBITS,TIMEOUT" 4
7566 .IX Xref "select"
7567 .IX Item "select RBITS,WBITS,EBITS,TIMEOUT"
7568 This calls the \fBselect\fR\|(2) syscall with the bit masks specified, which
7569 can be constructed using \f(CW\*(C`fileno\*(C'\fR and
7570 \&\f(CW\*(C`vec\*(C'\fR, along these lines:
7571 .Sp
7572 .Vb 4
7573 \& my $rin = my $win = my $ein = \*(Aq\*(Aq;
7574 \& vec($rin, fileno(STDIN), 1) = 1;
7575 \& vec($win, fileno(STDOUT), 1) = 1;
7576 \& $ein = $rin | $win;
7577 .Ve
7578 .Sp
7579 If you want to select on many filehandles, you may wish to write a
7580 subroutine like this:
7581 .Sp
7582 .Vb 9
7583 \& sub fhbits {
7584 \& my @fhlist = @_;
7585 \& my $bits = "";
7586 \& for my $fh (@fhlist) {
7587 \& vec($bits, fileno($fh), 1) = 1;
7588 \& }
7589 \& return $bits;
7590 \& }
7591 \& my $rin = fhbits(\e*STDIN, $tty, $mysock);
7592 .Ve
7593 .Sp
7594 The usual idiom is:
7595 .Sp
7596 .Vb 3
7597 \& my ($nfound, $timeleft) =
7598 \& select(my $rout = $rin, my $wout = $win, my $eout = $ein,
7599 \& $timeout);
7600 .Ve
7601 .Sp
7602 or to block until something becomes ready just do this
7603 .Sp
7604 .Vb 2
7605 \& my $nfound =
7606 \& select(my $rout = $rin, my $wout = $win, my $eout = $ein, undef);
7607 .Ve
7608 .Sp
7609 Most systems do not bother to return anything useful in \f(CW$timeleft\fR, so
7610 calling \f(CW\*(C`select\*(C'\fR in scalar context
7611 just returns \f(CW$nfound\fR.
7612 .Sp
7613 Any of the bit masks can also be \f(CW\*(C`undef\*(C'\fR. The timeout,
7614 if specified, is
7615 in seconds, which may be fractional. Note: not all implementations are
7616 capable of returning the \f(CW$timeleft\fR. If not, they always return
7617 \&\f(CW$timeleft\fR equal to the supplied \f(CW$timeout\fR.
7618 .Sp
7619 You can effect a sleep of 250 milliseconds this way:
7620 .Sp
7621 .Vb 1
7622 \& select(undef, undef, undef, 0.25);
7623 .Ve
7624 .Sp
7625 Note that whether \f(CW\*(C`select\*(C'\fR gets
7626 restarted after signals (say, SIGALRM) is implementation-dependent. See
7627 also perlport for notes on the portability of
7628 \&\f(CW\*(C`select\*(C'\fR.
7629 .Sp
7630 On error, \f(CW\*(C`select\*(C'\fR behaves just
7631 like \fBselect\fR\|(2): it returns \f(CW\-1\fR and sets \f(CW$!\fR.
7632 .Sp
7633 On some Unixes, \fBselect\fR\|(2) may report a socket file descriptor as
7634 "ready for reading" even when no data is available, and thus any
7635 subsequent \f(CW\*(C`read\*(C'\fR would block.
7636 This can be avoided if you always use \f(CW\*(C`O_NONBLOCK\*(C'\fR on the socket. See
7637 \&\fBselect\fR\|(2) and \fBfcntl\fR\|(2) for further details.
7638 .Sp
7639 The standard \f(CW\*(C`IO::Select\*(C'\fR module provides a
7640 user-friendlier interface to
7641 \&\f(CW\*(C`select\*(C'\fR, mostly because it does
7642 all the bit-mask work for you.
7643 .Sp
7644 \&\fBWARNING\fR: One should not attempt to mix buffered I/O (like
7645 \&\f(CW\*(C`read\*(C'\fR or
7646 \&\f(CW\*(C`readline\*(C'\fR) with
7647 \&\f(CW\*(C`select\*(C'\fR, except as permitted by
7648 POSIX, and even then only on POSIX systems. You have to use
7649 \&\f(CW\*(C`sysread\*(C'\fR instead.
7650 .Sp
7651 Portability issues: "select" in perlport.
7652 .IP "semctl ID,SEMNUM,CMD,ARG" 4
7653 .IX Xref "semctl"
7654 .IX Item "semctl ID,SEMNUM,CMD,ARG"
7655 Calls the System V IPC function \fBsemctl\fR\|(2). You'll probably have to say
7656 .Sp
7657 .Vb 1
7658 \& use IPC::SysV;
7659 .Ve
7660 .Sp
7661 first to get the correct constant definitions. If CMD is IPC_STAT or
7662 GETALL, then ARG must be a variable that will hold the returned
7663 semid_ds structure or semaphore value array. Returns like
7664 \&\f(CW\*(C`ioctl\*(C'\fR:
7665 the undefined value for error, "\f(CW\*(C`0 but true\*(C'\fR" for zero, or the actual
7666 return value otherwise. The ARG must consist of a vector of native
7667 short integers, which may be created with \f(CW\*(C`pack("s!",(0)x$nsem)\*(C'\fR.
7668 See also "SysV IPC" in perlipc and the documentation for
7669 \&\f(CW\*(C`IPC::SysV\*(C'\fR and \f(CW\*(C`IPC::Semaphore\*(C'\fR.
7670 .Sp
7671 Portability issues: "semctl" in perlport.
7672 .IP "semget KEY,NSEMS,FLAGS" 4
7673 .IX Xref "semget"
7674 .IX Item "semget KEY,NSEMS,FLAGS"
7675 Calls the System V IPC function \fBsemget\fR\|(2). Returns the semaphore id, or
7676 the undefined value on error. See also
7677 "SysV IPC" in perlipc and the documentation for
7678 \&\f(CW\*(C`IPC::SysV\*(C'\fR and \f(CW\*(C`IPC::Semaphore\*(C'\fR.
7679 .Sp
7680 Portability issues: "semget" in perlport.
7681 .IP "semop KEY,OPSTRING" 4
7682 .IX Xref "semop"
7683 .IX Item "semop KEY,OPSTRING"
7684 Calls the System V IPC function \fBsemop\fR\|(2) for semaphore operations
7685 such as signalling and waiting. OPSTRING must be a packed array of
7686 semop structures. Each semop structure can be generated with
7687 \&\f(CW\*(C`pack("s!3", $semnum, $semop, $semflag)\*(C'\fR. The length of OPSTRING
7688 implies the number of semaphore operations. Returns true if
7689 successful, false on error. As an example, the
7690 following code waits on semaphore \f(CW$semnum\fR of semaphore id \f(CW$semid:\fR
7691 .Sp
7692 .Vb 2
7693 \& my $semop = pack("s!3", $semnum, \-1, 0);
7694 \& die "Semaphore trouble: $!\en" unless semop($semid, $semop);
7695 .Ve
7696 .Sp
7697 To signal the semaphore, replace \f(CW\-1\fR with \f(CW1\fR. See also
7698 "SysV IPC" in perlipc and the documentation for
7699 \&\f(CW\*(C`IPC::SysV\*(C'\fR and \f(CW\*(C`IPC::Semaphore\*(C'\fR.
7700 .Sp
7701 Portability issues: "semop" in perlport.
7702 .IP "send SOCKET,MSG,FLAGS,TO" 4
7703 .IX Xref "send"
7704 .IX Item "send SOCKET,MSG,FLAGS,TO"
7705 .PD 0
7706 .IP "send SOCKET,MSG,FLAGS" 4
7707 .IX Item "send SOCKET,MSG,FLAGS"
7708 .PD
7709 Sends a message on a socket. Attempts to send the scalar MSG to the SOCKET
7710 filehandle. Takes the same flags as the system call of the same name. On
7711 unconnected sockets, you must specify a destination to \fIsend to\fR, in which
7712 case it does a \fBsendto\fR\|(2) syscall. Returns the number of characters sent,
7713 or the undefined value on error. The \fBsendmsg\fR\|(2) syscall is currently
7714 unimplemented. See "UDP: Message Passing" in perlipc for examples.
7715 .Sp
7716 Note that if the socket has been marked as \f(CW\*(C`:utf8\*(C'\fR, \f(CW\*(C`send\*(C'\fR will
7717 throw an exception. The \f(CW:encoding(...)\fR layer implicitly introduces
7718 the \f(CW\*(C`:utf8\*(C'\fR layer. See \f(CW\*(C`binmode\*(C'\fR.
7719 .IP "setpgrp PID,PGRP" 4
7720 .IX Xref "setpgrp group"
7721 .IX Item "setpgrp PID,PGRP"
7722 Sets the current process group for the specified PID, \f(CW0\fR for the current
7723 process. Raises an exception when used on a machine that doesn't
7724 implement POSIX \fBsetpgid\fR\|(2) or BSD \fBsetpgrp\fR\|(2). If the arguments
7725 are omitted, it defaults to \f(CW\*(C`0,0\*(C'\fR. Note that the BSD 4.2 version of
7726 \&\f(CW\*(C`setpgrp\*(C'\fR does not accept any arguments, so only
7727 \&\f(CW\*(C`setpgrp(0,0)\*(C'\fR is portable. See also
7728 \&\f(CWPOSIX::setsid()\fR.
7729 .Sp
7730 Portability issues: "setpgrp" in perlport.
7731 .IP "setpriority WHICH,WHO,PRIORITY" 4
7732 .IX Xref "setpriority priority nice renice"
7733 .IX Item "setpriority WHICH,WHO,PRIORITY"
7734 Sets the current priority for a process, a process group, or a user.
7735 (See \fBsetpriority\fR\|(2).) Raises an exception when used on a machine
7736 that doesn't implement \fBsetpriority\fR\|(2).
7737 .Sp
7738 \&\f(CW\*(C`WHICH\*(C'\fR can be any of \f(CW\*(C`PRIO_PROCESS\*(C'\fR, \f(CW\*(C`PRIO_PGRP\*(C'\fR or \f(CW\*(C`PRIO_USER\*(C'\fR
7739 imported from "RESOURCE CONSTANTS" in POSIX.
7740 .Sp
7741 Portability issues: "setpriority" in perlport.
7742 .IP "setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL" 4
7743 .IX Xref "setsockopt"
7744 .IX Item "setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL"
7745 Sets the socket option requested. Returns \f(CW\*(C`undef\*(C'\fR on
7746 error. Use integer constants provided by the \f(CW\*(C`Socket\*(C'\fR module
7747 for
7748 LEVEL and OPNAME. Values for LEVEL can also be obtained from
7749 getprotobyname. OPTVAL might either be a packed string or an integer.
7750 An integer OPTVAL is shorthand for pack("i", OPTVAL).
7751 .Sp
7752 An example disabling Nagle's algorithm on a socket:
7753 .Sp
7754 .Vb 2
7755 \& use Socket qw(IPPROTO_TCP TCP_NODELAY);
7756 \& setsockopt($socket, IPPROTO_TCP, TCP_NODELAY, 1);
7757 .Ve
7758 .Sp
7759 Portability issues: "setsockopt" in perlport.
7760 .IP "shift ARRAY" 4
7761 .IX Xref "shift"
7762 .IX Item "shift ARRAY"
7763 .PD 0
7764 .IP shift 4
7765 .IX Item "shift"
7766 .PD
7767 Removes and returns the \fBfirst\fR element of an array. This shortens the
7768 array by one and moves everything down.
7769 .Sp
7770 .Vb 2
7771 \& my @arr = (\*(Aqcat\*(Aq, \*(Aqdog\*(Aq);
7772 \& my $item = shift(@arr); # \*(Aqcat\*(Aq
7773 \&
7774 \& # @arr is now (\*(Aqdog\*(Aq);
7775 .Ve
7776 .Sp
7777 Returns \f(CW\*(C`undef\*(C'\fR if the array is empty.
7778 .Sp
7779 \&\fBNote:\fR \f(CW\*(C`shift\*(C'\fR may also return \f(CW\*(C`undef\*(C'\fR if the first element in the array
7780 is \f(CW\*(C`undef\*(C'\fR.
7781 .Sp
7782 .Vb 2
7783 \& my @arr = (undef, \*(Aqtwo\*(Aq, \*(Aqthree\*(Aq);
7784 \& my $item = shift(@arr); # undef
7785 .Ve
7786 .Sp
7787 If ARRAY is omitted, \f(CW\*(C`shift\*(C'\fR operates on the \f(CW@ARGV\fR array in the main
7788 program, and the \f(CW@_\fR array in subroutines. \f(CW\*(C`shift\*(C'\fR will operate on the
7789 \&\f(CW@ARGV\fR array in \f(CW\*(C`eval STRING\*(C'\fR, \f(CW\*(C`BEGIN {}\*(C'\fR, \f(CW\*(C`INIT {}\*(C'\fR, \f(CW\*(C`CHECK {}\*(C'\fR blocks.
7790 .Sp
7791 Starting with Perl 5.14, an experimental feature allowed
7792 \&\f(CW\*(C`shift\*(C'\fR to take a
7793 scalar expression. This experiment has been deemed unsuccessful, and was
7794 removed as of Perl 5.24.
7795 .Sp
7796 See also \f(CW\*(C`unshift\*(C'\fR, \f(CW\*(C`push\*(C'\fR,
7797 and \f(CW\*(C`pop\*(C'\fR. \f(CW\*(C`shift\*(C'\fR and
7798 \&\f(CW\*(C`unshift\*(C'\fR do the same thing to the left end of
7799 an array that \f(CW\*(C`pop\*(C'\fR and \f(CW\*(C`push\*(C'\fR do to
7800 the right end.
7801 .IP "shmctl ID,CMD,ARG" 4
7802 .IX Xref "shmctl"
7803 .IX Item "shmctl ID,CMD,ARG"
7804 Calls the System V IPC function shmctl. You'll probably have to say
7805 .Sp
7806 .Vb 1
7807 \& use IPC::SysV;
7808 .Ve
7809 .Sp
7810 first to get the correct constant definitions. If CMD is \f(CW\*(C`IPC_STAT\*(C'\fR,
7811 then ARG must be a variable that will hold the returned \f(CW\*(C`shmid_ds\*(C'\fR
7812 structure. Returns like ioctl: \f(CW\*(C`undef\*(C'\fR for error; "\f(CW0\fR
7813 but true" for zero; and the actual return value otherwise.
7814 See also "SysV IPC" in perlipc and the documentation for
7815 \&\f(CW\*(C`IPC::SysV\*(C'\fR.
7816 .Sp
7817 Portability issues: "shmctl" in perlport.
7818 .IP "shmget KEY,SIZE,FLAGS" 4
7819 .IX Xref "shmget"
7820 .IX Item "shmget KEY,SIZE,FLAGS"
7821 Calls the System V IPC function shmget. Returns the shared memory
7822 segment id, or \f(CW\*(C`undef\*(C'\fR on error.
7823 See also "SysV IPC" in perlipc and the documentation for
7824 \&\f(CW\*(C`IPC::SysV\*(C'\fR.
7825 .Sp
7826 Portability issues: "shmget" in perlport.
7827 .IP "shmread ID,VAR,POS,SIZE" 4
7828 .IX Xref "shmread shmwrite"
7829 .IX Item "shmread ID,VAR,POS,SIZE"
7830 .PD 0
7831 .IP "shmwrite ID,STRING,POS,SIZE" 4
7832 .IX Item "shmwrite ID,STRING,POS,SIZE"
7833 .PD
7834 Reads or writes the System V shared memory segment ID starting at
7835 position POS for size SIZE by attaching to it, copying in/out, and
7836 detaching from it. When reading, VAR must be a variable that will
7837 hold the data read. When writing, if STRING is too long, only SIZE
7838 bytes are used; if STRING is too short, nulls are written to fill out
7839 SIZE bytes. Return true if successful, false on error.
7840 \&\f(CW\*(C`shmread\*(C'\fR taints the variable. See also
7841 "SysV IPC" in perlipc and the documentation for
7842 \&\f(CW\*(C`IPC::SysV\*(C'\fR and the \f(CW\*(C`IPC::Shareable\*(C'\fR
7843 module from CPAN.
7844 .Sp
7845 Portability issues: "shmread" in perlport and "shmwrite" in perlport.
7846 .IP "shutdown SOCKET,HOW" 4
7847 .IX Xref "shutdown"
7848 .IX Item "shutdown SOCKET,HOW"
7849 Shuts down a socket connection in the manner indicated by HOW, which
7850 has the same interpretation as in the syscall of the same name.
7851 .Sp
7852 .Vb 3
7853 \& shutdown($socket, 0); # I/we have stopped reading data
7854 \& shutdown($socket, 1); # I/we have stopped writing data
7855 \& shutdown($socket, 2); # I/we have stopped using this socket
7856 .Ve
7857 .Sp
7858 This is useful with sockets when you want to tell the other
7859 side you're done writing but not done reading, or vice versa.
7860 It's also a more insistent form of close because it also
7861 disables the file descriptor in any forked copies in other
7862 processes.
7863 .Sp
7864 Returns \f(CW1\fR for success; on error, returns \f(CW\*(C`undef\*(C'\fR if
7865 the first argument is not a valid filehandle, or returns \f(CW0\fR and sets
7866 \&\f(CW$!\fR for any other failure.
7867 .IP "sin EXPR" 4
7868 .IX Xref "sin sine asin arcsine"
7869 .IX Item "sin EXPR"
7870 .PD 0
7871 .IP sin 4
7872 .IX Item "sin"
7873 .PD
7874 Returns the sine of EXPR (expressed in radians). If EXPR is omitted,
7875 returns sine of \f(CW$_\fR.
7876 .Sp
7877 For the inverse sine operation, you may use the \f(CW\*(C`Math::Trig::asin\*(C'\fR
7878 function, or use this relation:
7879 .Sp
7880 .Vb 1
7881 \& sub asin { atan2($_[0], sqrt(1 \- $_[0] * $_[0])) }
7882 .Ve
7883 .IP "sleep EXPR" 4
7884 .IX Xref "sleep pause"
7885 .IX Item "sleep EXPR"
7886 .PD 0
7887 .IP sleep 4
7888 .IX Item "sleep"
7889 .PD
7890 Causes the script to sleep for (integer) EXPR seconds, or forever if no
7891 argument is given. Returns the integer number of seconds actually slept.
7892 .Sp
7893 EXPR should be a positive integer. If called with a negative integer,
7894 \&\f(CW\*(C`sleep\*(C'\fR does not sleep but instead emits a warning, sets
7895 \&\f(CW$!\fR (\f(CW\*(C`errno\*(C'\fR), and returns zero.
7896 .Sp
7897 If called with a non-integer, the fractional part is ignored.
7898 .Sp
7899 \&\f(CW\*(C`sleep 0\*(C'\fR is permitted, but a function call to the underlying platform
7900 implementation still occurs, with any side effects that may have.
7901 \&\f(CW\*(C`sleep 0\*(C'\fR is therefore not exactly identical to not sleeping at all.
7902 .Sp
7903 May be interrupted if the process receives a signal such as \f(CW\*(C`SIGALRM\*(C'\fR.
7904 .Sp
7905 .Vb 5
7906 \& eval {
7907 \& local $SIG{ALRM} = sub { die "Alarm!\en" };
7908 \& sleep;
7909 \& };
7910 \& die $@ unless $@ eq "Alarm!\en";
7911 .Ve
7912 .Sp
7913 You probably cannot mix \f(CW\*(C`alarm\*(C'\fR and
7914 \&\f(CW\*(C`sleep\*(C'\fR calls, because \f(CW\*(C`sleep\*(C'\fR is often
7915 implemented using \f(CW\*(C`alarm\*(C'\fR.
7916 .Sp
7917 On some older systems, it may sleep up to a full second less than what
7918 you requested, depending on how it counts seconds. Most modern systems
7919 always sleep the full amount. They may appear to sleep longer than that,
7920 however, because your process might not be scheduled right away in a
7921 busy multitasking system.
7922 .Sp
7923 For delays of finer granularity than one second, the Time::HiRes
7924 module (from CPAN, and starting from Perl 5.8 part of the standard
7925 distribution) provides \f(CW\*(C`usleep\*(C'\fR.
7926 You may also use Perl's four-argument
7927 version of \f(CW\*(C`select\*(C'\fR leaving the
7928 first three arguments undefined, or you might be able to use the
7929 \&\f(CW\*(C`syscall\*(C'\fR interface to access \fBsetitimer\fR\|(2)
7930 if your system supports it. See perlfaq8 for details.
7931 .Sp
7932 See also the POSIX module's \f(CW\*(C`pause\*(C'\fR function.
7933 .IP "socket SOCKET,DOMAIN,TYPE,PROTOCOL" 4
7934 .IX Xref "socket"
7935 .IX Item "socket SOCKET,DOMAIN,TYPE,PROTOCOL"
7936 Opens a socket of the specified kind and attaches it to filehandle
7937 SOCKET. DOMAIN, TYPE, and PROTOCOL are specified the same as for
7938 the syscall of the same name. You should \f(CW\*(C`use Socket\*(C'\fR first
7939 to get the proper definitions imported. See the examples in
7940 "Sockets: Client/Server Communication" in perlipc.
7941 .Sp
7942 On systems that support a close-on-exec flag on files, the flag will
7943 be set for the newly opened file descriptor, as determined by the
7944 value of \f(CW$^F\fR. See "$^F" in perlvar.
7945 .IP "socketpair SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL" 4
7946 .IX Xref "socketpair"
7947 .IX Item "socketpair SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL"
7948 Creates an unnamed pair of sockets in the specified domain, of the
7949 specified type. DOMAIN, TYPE, and PROTOCOL are specified the same as
7950 for the syscall of the same name. If unimplemented, raises an exception.
7951 Returns true if successful.
7952 .Sp
7953 On systems that support a close-on-exec flag on files, the flag will
7954 be set for the newly opened file descriptors, as determined by the value
7955 of \f(CW$^F\fR. See "$^F" in perlvar.
7956 .Sp
7957 Some systems define \f(CW\*(C`pipe\*(C'\fR in terms of
7958 \&\f(CW\*(C`socketpair\*(C'\fR, in
7959 which a call to \f(CW\*(C`pipe($rdr, $wtr)\*(C'\fR is essentially:
7960 .Sp
7961 .Vb 4
7962 \& use Socket;
7963 \& socketpair(my $rdr, my $wtr, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
7964 \& shutdown($rdr, 1); # no more writing for reader
7965 \& shutdown($wtr, 0); # no more reading for writer
7966 .Ve
7967 .Sp
7968 See perlipc for an example of socketpair use. Perl 5.8 and later will
7969 emulate socketpair using IP sockets to localhost if your system implements
7970 sockets but not socketpair.
7971 .Sp
7972 Portability issues: "socketpair" in perlport.
7973 .IP "sort SUBNAME LIST" 4
7974 .IX Xref "sort"
7975 .IX Item "sort SUBNAME LIST"
7976 .PD 0
7977 .IP "sort BLOCK LIST" 4
7978 .IX Item "sort BLOCK LIST"
7979 .IP "sort LIST" 4
7980 .IX Item "sort LIST"
7981 .PD
7982 In list context, this sorts the LIST and returns the sorted list value.
7983 In scalar context, the behaviour of \f(CW\*(C`sort\*(C'\fR is
7984 undefined.
7985 .Sp
7986 If SUBNAME or BLOCK is omitted, \f(CW\*(C`sort\*(C'\fRs in
7987 standard string comparison
7988 order. If SUBNAME is specified, it gives the name of a subroutine
7989 that returns a numeric value less than, equal to, or greater than \f(CW0\fR,
7990 depending on how the elements of the list are to be ordered. (The
7991 \&\f(CW\*(C`<=>\*(C'\fR and \f(CW\*(C`cmp\*(C'\fR operators are extremely useful in such routines.)
7992 SUBNAME may be a scalar variable name (unsubscripted), in which case
7993 the value provides the name of (or a reference to) the actual
7994 subroutine to use. In place of a SUBNAME, you can provide a BLOCK as
7995 an anonymous, in-line sort subroutine.
7996 .Sp
7997 If the subroutine's prototype is \f(CW\*(C`($$)\*(C'\fR, the elements to be compared are
7998 passed by reference in \f(CW@_\fR, as for a normal subroutine.
7999 This is slower than unprototyped subroutines, where the elements to be
8000 compared are passed into the subroutine as the package global variables
8001 \&\f(CW$a\fR and \f(CW$b\fR (see example below).
8002 .Sp
8003 If the subroutine is an XSUB, the elements to be compared are pushed on
8004 to the stack, the way arguments are usually passed to XSUBs. \f(CW$a\fR and
8005 \&\f(CW$b\fR are not set.
8006 .Sp
8007 The values to be compared are always passed by reference and should not
8008 be modified.
8009 .Sp
8010 You also cannot exit out of the sort block or subroutine using any of the
8011 loop control operators described in perlsyn or with
8012 \&\f(CW\*(C`goto\*(C'\fR.
8013 .Sp
8014 When \f(CW\*(C`use locale\*(C'\fR (but not \f(CW\*(C`use locale \*(Aq:not_characters\*(Aq\*(C'\fR)
8015 is in effect, \f(CW\*(C`sort LIST\*(C'\fR sorts LIST according to the
8016 current collation locale. See perllocale.
8017 .Sp
8018 \&\f(CW\*(C`sort\*(C'\fR returns aliases into the original list,
8019 much as a for loop's index variable aliases the list elements. That is,
8020 modifying an element of a list returned by \f(CW\*(C`sort\*(C'\fR
8021 (for example, in a \f(CW\*(C`foreach\*(C'\fR, \f(CW\*(C`map\*(C'\fR or
8022 \&\f(CW\*(C`grep\*(C'\fR)
8023 actually modifies the element in the original list. This is usually
8024 something to be avoided when writing clear code.
8025 .Sp
8026 Historically Perl has varied in whether sorting is stable by default.
8027 If stability matters, it can be controlled explicitly by using the
8028 sort pragma.
8029 .Sp
8030 Examples:
8031 .Sp
8032 .Vb 2
8033 \& # sort lexically
8034 \& my @articles = sort @files;
8035 \&
8036 \& # same thing, but with explicit sort routine
8037 \& my @articles = sort {$a cmp $b} @files;
8038 \&
8039 \& # now case\-insensitively
8040 \& my @articles = sort {fc($a) cmp fc($b)} @files;
8041 \&
8042 \& # same thing in reversed order
8043 \& my @articles = sort {$b cmp $a} @files;
8044 \&
8045 \& # sort numerically ascending
8046 \& my @articles = sort {$a <=> $b} @files;
8047 \&
8048 \& # sort numerically descending
8049 \& my @articles = sort {$b <=> $a} @files;
8050 \&
8051 \& # this sorts the %age hash by value instead of key
8052 \& # using an in\-line function
8053 \& my @eldest = sort { $age{$b} <=> $age{$a} } keys %age;
8054 \&
8055 \& # sort using explicit subroutine name
8056 \& sub byage {
8057 \& $age{$a} <=> $age{$b}; # presuming numeric
8058 \& }
8059 \& my @sortedclass = sort byage @class;
8060 \&
8061 \& sub backwards { $b cmp $a }
8062 \& my @harry = qw(dog cat x Cain Abel);
8063 \& my @george = qw(gone chased yz Punished Axed);
8064 \& print sort @harry;
8065 \& # prints AbelCaincatdogx
8066 \& print sort backwards @harry;
8067 \& # prints xdogcatCainAbel
8068 \& print sort @george, \*(Aqto\*(Aq, @harry;
8069 \& # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
8070 \&
8071 \& # inefficiently sort by descending numeric compare using
8072 \& # the first integer after the first = sign, or the
8073 \& # whole record case\-insensitively otherwise
8074 \&
8075 \& my @new = sort {
8076 \& ($b =~ /=(\ed+)/)[0] <=> ($a =~ /=(\ed+)/)[0]
8077 \& ||
8078 \& fc($a) cmp fc($b)
8079 \& } @old;
8080 \&
8081 \& # same thing, but much more efficiently;
8082 \& # we\*(Aqll build auxiliary indices instead
8083 \& # for speed
8084 \& my (@nums, @caps);
8085 \& for (@old) {
8086 \& push @nums, ( /=(\ed+)/ ? $1 : undef );
8087 \& push @caps, fc($_);
8088 \& }
8089 \&
8090 \& my @new = @old[ sort {
8091 \& $nums[$b] <=> $nums[$a]
8092 \& ||
8093 \& $caps[$a] cmp $caps[$b]
8094 \& } 0..$#old
8095 \& ];
8096 \&
8097 \& # same thing, but without any temps
8098 \& my @new = map { $_\->[0] }
8099 \& sort { $b\->[1] <=> $a\->[1]
8100 \& ||
8101 \& $a\->[2] cmp $b\->[2]
8102 \& } map { [$_, /=(\ed+)/, fc($_)] } @old;
8103 \&
8104 \& # using a prototype allows you to use any comparison subroutine
8105 \& # as a sort subroutine (including other package\*(Aqs subroutines)
8106 \& package Other;
8107 \& sub backwards ($$) { $_[1] cmp $_[0]; } # $a and $b are
8108 \& # not set here
8109 \& package main;
8110 \& my @new = sort Other::backwards @old;
8111 \&
8112 \& ## using a prototype with function signature
8113 \& use feature \*(Aqsignatures\*(Aq;
8114 \& sub function_with_signature :prototype($$) ($one, $two) {
8115 \& return $one <=> $two
8116 \& }
8117 \&
8118 \& my @new = sort function_with_signature @old;
8119 \&
8120 \& # guarantee stability
8121 \& use sort \*(Aqstable\*(Aq;
8122 \& my @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
8123 .Ve
8124 .Sp
8125 Warning: syntactical care is required when sorting the list returned from
8126 a function. If you want to sort the list returned by the function call
8127 \&\f(CWfind_records(@key)\fR, you can use:
8128 .Sp
8129 .Vb 4
8130 \& my @contact = sort { $a cmp $b } find_records @key;
8131 \& my @contact = sort +find_records(@key);
8132 \& my @contact = sort &find_records(@key);
8133 \& my @contact = sort(find_records(@key));
8134 .Ve
8135 .Sp
8136 If instead you want to sort the array \f(CW@key\fR with the comparison routine
8137 \&\f(CWfind_records()\fR then you can use:
8138 .Sp
8139 .Vb 4
8140 \& my @contact = sort { find_records() } @key;
8141 \& my @contact = sort find_records(@key);
8142 \& my @contact = sort(find_records @key);
8143 \& my @contact = sort(find_records (@key));
8144 .Ve
8145 .Sp
8146 \&\f(CW$a\fR and \f(CW$b\fR are set as package globals in the package the \fBsort()\fR is
8147 called from. That means \f(CW$main::a\fR and \f(CW$main::b\fR (or \f(CW$::a\fR and
8148 \&\f(CW$::b\fR) in the \f(CW\*(C`main\*(C'\fR package, \f(CW$FooPack::a\fR and \f(CW$FooPack::b\fR in the
8149 \&\f(CW\*(C`FooPack\*(C'\fR package, etc. If the sort block is in scope of a \f(CW\*(C`my\*(C'\fR or
8150 \&\f(CW\*(C`state\*(C'\fR declaration of \f(CW$a\fR and/or \f(CW$b\fR, you \fImust\fR spell out the full
8151 name of the variables in the sort block :
8152 .Sp
8153 .Vb 2
8154 \& package main;
8155 \& my $a = "C"; # DANGER, Will Robinson, DANGER !!!
8156 \&
8157 \& print sort { $a cmp $b } qw(A C E G B D F H);
8158 \& # WRONG
8159 \& sub badlexi { $a cmp $b }
8160 \& print sort badlexi qw(A C E G B D F H);
8161 \& # WRONG
8162 \& # the above prints BACFEDGH or some other incorrect ordering
8163 \&
8164 \& print sort { $::a cmp $::b } qw(A C E G B D F H);
8165 \& # OK
8166 \& print sort { our $a cmp our $b } qw(A C E G B D F H);
8167 \& # also OK
8168 \& print sort { our ($a, $b); $a cmp $b } qw(A C E G B D F H);
8169 \& # also OK
8170 \& sub lexi { our $a cmp our $b }
8171 \& print sort lexi qw(A C E G B D F H);
8172 \& # also OK
8173 \& # the above print ABCDEFGH
8174 .Ve
8175 .Sp
8176 With proper care you may mix package and my (or state) \f(CW$a\fR and/or \f(CW$b\fR:
8177 .Sp
8178 .Vb 7
8179 \& my $a = {
8180 \& tiny => \-2,
8181 \& small => \-1,
8182 \& normal => 0,
8183 \& big => 1,
8184 \& huge => 2
8185 \& };
8186 \&
8187 \& say sort { $a\->{our $a} <=> $a\->{our $b} }
8188 \& qw{ huge normal tiny small big};
8189 \&
8190 \& # prints tinysmallnormalbighuge
8191 .Ve
8192 .Sp
8193 \&\f(CW$a\fR and \f(CW$b\fR are implicitly local to the \fBsort()\fR execution and regain their
8194 former values upon completing the sort.
8195 .Sp
8196 Sort subroutines written using \f(CW$a\fR and \f(CW$b\fR are bound to their calling
8197 package. It is possible, but of limited interest, to define them in a
8198 different package, since the subroutine must still refer to the calling
8199 package's \f(CW$a\fR and \f(CW$b\fR :
8200 .Sp
8201 .Vb 4
8202 \& package Foo;
8203 \& sub lexi { $Bar::a cmp $Bar::b }
8204 \& package Bar;
8205 \& ... sort Foo::lexi ...
8206 .Ve
8207 .Sp
8208 Use the prototyped versions (see above) for a more generic alternative.
8209 .Sp
8210 The comparison function is required to behave. If it returns
8211 inconsistent results (sometimes saying \f(CW$x[1]\fR is less than \f(CW$x[2]\fR and
8212 sometimes saying the opposite, for example) the results are not
8213 well-defined.
8214 .Sp
8215 Because \f(CW\*(C`<=>\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR when either operand
8216 is \f(CW\*(C`NaN\*(C'\fR (not-a-number), be careful when sorting with a
8217 comparison function like \f(CW\*(C`$a <=> $b\*(C'\fR any lists that might contain a
8218 \&\f(CW\*(C`NaN\*(C'\fR. The following example takes advantage that \f(CW\*(C`NaN != NaN\*(C'\fR to
8219 eliminate any \f(CW\*(C`NaN\*(C'\fRs from the input list.
8220 .Sp
8221 .Vb 1
8222 \& my @result = sort { $a <=> $b } grep { $_ == $_ } @input;
8223 .Ve
8224 .Sp
8225 In this version of \fIperl\fR, the \f(CW\*(C`sort\*(C'\fR function is implemented via the
8226 mergesort algorithm.
8227 .IP "splice ARRAY,OFFSET,LENGTH,LIST" 4
8228 .IX Xref "splice"
8229 .IX Item "splice ARRAY,OFFSET,LENGTH,LIST"
8230 .PD 0
8231 .IP "splice ARRAY,OFFSET,LENGTH" 4
8232 .IX Item "splice ARRAY,OFFSET,LENGTH"
8233 .IP "splice ARRAY,OFFSET" 4
8234 .IX Item "splice ARRAY,OFFSET"
8235 .IP "splice ARRAY" 4
8236 .IX Item "splice ARRAY"
8237 .PD
8238 Removes the elements designated by OFFSET and LENGTH from an array, and
8239 replaces them with the elements of LIST, if any. In list context,
8240 returns the elements removed from the array. In scalar context,
8241 returns the last element removed, or \f(CW\*(C`undef\*(C'\fR if no
8242 elements are
8243 removed. The array grows or shrinks as necessary.
8244 If OFFSET is negative then it starts that far from the end of the array.
8245 If LENGTH is omitted, removes everything from OFFSET onward.
8246 If LENGTH is negative, removes the elements from OFFSET onward
8247 except for \-LENGTH elements at the end of the array.
8248 If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is
8249 past the end of the array and a LENGTH was provided, Perl issues a warning,
8250 and splices at the end of the array.
8251 .Sp
8252 The following equivalences hold (assuming \f(CW\*(C`$#a >= $i\*(C'\fR )
8253 .Sp
8254 .Vb 5
8255 \& push(@a,$x,$y) splice(@a,@a,0,$x,$y)
8256 \& pop(@a) splice(@a,\-1)
8257 \& shift(@a) splice(@a,0,1)
8258 \& unshift(@a,$x,$y) splice(@a,0,0,$x,$y)
8259 \& $a[$i] = $y splice(@a,$i,1,$y)
8260 .Ve
8261 .Sp
8262 \&\f(CW\*(C`splice\*(C'\fR can be used, for example,
8263 to implement n\-ary queue processing:
8264 .Sp
8265 .Vb 6
8266 \& sub nary_print {
8267 \& my $n = shift;
8268 \& while (my @next_n = splice @_, 0, $n) {
8269 \& say join q{ \-\- }, @next_n;
8270 \& }
8271 \& }
8272 \&
8273 \& nary_print(3, qw(a b c d e f g h));
8274 \& # prints:
8275 \& # a \-\- b \-\- c
8276 \& # d \-\- e \-\- f
8277 \& # g \-\- h
8278 .Ve
8279 .Sp
8280 Starting with Perl 5.14, an experimental feature allowed
8281 \&\f(CW\*(C`splice\*(C'\fR to take a
8282 scalar expression. This experiment has been deemed unsuccessful, and was
8283 removed as of Perl 5.24.
8284 .IP "split /PATTERN/,EXPR,LIMIT" 4
8285 .IX Xref "split"
8286 .IX Item "split /PATTERN/,EXPR,LIMIT"
8287 .PD 0
8288 .IP "split /PATTERN/,EXPR" 4
8289 .IX Item "split /PATTERN/,EXPR"
8290 .IP "split /PATTERN/" 4
8291 .IX Item "split /PATTERN/"
8292 .IP split 4
8293 .IX Item "split"
8294 .PD
8295 Splits the string EXPR into a list of strings and returns the
8296 list in list context, or the size of the list in scalar context.
8297 (Prior to Perl 5.11, it also overwrote \f(CW@_\fR with the list in
8298 void and scalar context. If you target old perls, beware.)
8299 .Sp
8300 If only PATTERN is given, EXPR defaults to \f(CW$_\fR.
8301 .Sp
8302 Anything in EXPR that matches PATTERN is taken to be a separator
8303 that separates the EXPR into substrings (called "\fIfields\fR") that
8304 do \fBnot\fR include the separator. Note that a separator may be
8305 longer than one character or even have no characters at all (the
8306 empty string, which is a zero-width match).
8307 .Sp
8308 The PATTERN need not be constant; an expression may be used
8309 to specify a pattern that varies at runtime.
8310 .Sp
8311 If PATTERN matches the empty string, the EXPR is split at the match
8312 position (between characters). As an example, the following:
8313 .Sp
8314 .Vb 1
8315 \& my @x = split(/b/, "abc"); # ("a", "c")
8316 .Ve
8317 .Sp
8318 uses the \f(CW\*(C`b\*(C'\fR in \f(CW\*(Aqabc\*(Aq\fR as a separator to produce the list ("a", "c").
8319 However, this:
8320 .Sp
8321 .Vb 1
8322 \& my @x = split(//, "abc"); # ("a", "b", "c")
8323 .Ve
8324 .Sp
8325 uses empty string matches as separators; thus, the empty string
8326 may be used to split EXPR into a list of its component characters.
8327 .Sp
8328 As a special case for \f(CW\*(C`split\*(C'\fR,
8329 the empty pattern given in
8330 match operator syntax (\f(CW\*(C`//\*(C'\fR)
8331 specifically matches the empty string, which is contrary to its usual
8332 interpretation as the last successful match.
8333 .Sp
8334 If PATTERN is \f(CW\*(C`/^/\*(C'\fR, then it is treated as if it used the
8335 multiline modifier (\f(CW\*(C`/^/m\*(C'\fR), since it
8336 isn't much use otherwise.
8337 .Sp
8338 \&\f(CW\*(C`/m\*(C'\fR and any of the other pattern modifiers valid for \f(CW\*(C`qr\*(C'\fR
8339 (summarized in "qr/STRING/msixpodualn" in perlop) may be
8340 specified explicitly.
8341 .Sp
8342 As another special case,
8343 \&\f(CW\*(C`split\*(C'\fR emulates the default
8344 behavior of the
8345 command line tool \fBawk\fR when the PATTERN is either omitted or a
8346 string composed of a single space character (such as \f(CW\*(Aq\ \*(Aq\fR or
8347 \&\f(CW"\ex20"\fR, but not e.g. \f(CW\*(C`/\ /\*(C'\fR). In this case, any leading
8348 whitespace in EXPR is removed before splitting occurs, and the PATTERN is
8349 instead treated as if it were \f(CW\*(C`/\es+/\*(C'\fR; in particular, this means that
8350 \&\fIany\fR contiguous whitespace (not just a single space character) is used as
8351 a separator.
8352 .Sp
8353 .Vb 2
8354 \& my @x = split(" ", " Quick brown fox\en");
8355 \& # ("Quick", "brown", "fox")
8356 \&
8357 \& my @x = split(" ", "RED\etGREEN\etBLUE");
8358 \& # ("RED", "GREEN", "BLUE")
8359 .Ve
8360 .Sp
8361 Using split in this fashion is very similar to how
8362 \&\f(CW\*(C`qw//\*(C'\fR works.
8363 .Sp
8364 However, this special treatment can be avoided by specifying
8365 the pattern \f(CW\*(C`/\ /\*(C'\fR instead of the string \f(CW"\ "\fR, thereby allowing
8366 only a single space character to be a separator. In earlier Perls this
8367 special case was restricted to the use of a plain \f(CW"\ "\fR as the
8368 pattern argument to split; in Perl 5.18.0 and later this special case is
8369 triggered by any expression which evaluates to the simple string \f(CW"\ "\fR.
8370 .Sp
8371 As of Perl 5.28, this special-cased whitespace splitting works as expected in
8372 the scope of \f(CW"use\ feature\ \*(Aqunicode_strings\*(Aq"\fR. In previous versions, and outside the scope of
8373 that feature, it exhibits "The "Unicode Bug"" in perlunicode: characters that are
8374 whitespace according to Unicode rules but not according to ASCII rules can be
8375 treated as part of fields rather than as field separators, depending on the
8376 string's internal encoding.
8377 .Sp
8378 As of Perl 5.39.9 the \f(CW\*(C`/x\*(C'\fR default modifier does NOT affect
8379 \&\f(CW\*(C`split STRING\*(C'\fR but does affect \f(CW\*(C`split PATTERN\*(C'\fR, this means that
8380 \&\f(CW\*(C`split " "\*(C'\fR will produces the expected \fIawk\fR emulation regardless as
8381 to whether it is used in the scope of a \f(CW\*(C`use re "/x"\*(C'\fR statement. If you
8382 want to split by spaces under \f(CW\*(C`use re "/x"\*(C'\fR you must do something like
8383 \&\f(CW\*(C`split /(?\-x: )/\*(C'\fR or \f(CW\*(C`split /\ex{20}/\*(C'\fR instead of \f(CW\*(C`split / /\*(C'\fR.
8384 .Sp
8385 If omitted, PATTERN defaults to a single space, \f(CW"\ "\fR, triggering
8386 the previously described \fIawk\fR emulation.
8387 .Sp
8388 If LIMIT is specified and positive, it represents the maximum number
8389 of fields into which the EXPR may be split; in other words, LIMIT is
8390 one greater than the maximum number of times EXPR may be split. Thus,
8391 the LIMIT value \f(CW1\fR means that EXPR may be split a maximum of zero
8392 times, producing a maximum of one field (namely, the entire value of
8393 EXPR). For instance:
8394 .Sp
8395 .Vb 4
8396 \& my @x = split(/,/, "a,b,c", 1); # ("a,b,c")
8397 \& my @x = split(/,/, "a,b,c", 2); # ("a", "b,c")
8398 \& my @x = split(/,/, "a,b,c", 3); # ("a", "b", "c")
8399 \& my @x = split(/,/, "a,b,c", 4); # ("a", "b", "c")
8400 .Ve
8401 .Sp
8402 If LIMIT is negative, it is treated as if it were instead arbitrarily
8403 large; as many fields as possible are produced.
8404 .Sp
8405 If LIMIT is omitted (or, equivalently, zero), then it is usually
8406 treated as if it were instead negative but with the exception that
8407 trailing empty fields are stripped (empty leading fields are always
8408 preserved); if all fields are empty, then all fields are considered to
8409 be trailing (and are thus stripped in this case). Thus, the following:
8410 .Sp
8411 .Vb 1
8412 \& my @x = split(/,/, "a,b,c,,,"); # ("a", "b", "c")
8413 .Ve
8414 .Sp
8415 produces only a three element list.
8416 .Sp
8417 .Vb 1
8418 \& my @x = split(/,/, "a,b,c,,,", \-1); # ("a", "b", "c", "", "", "")
8419 .Ve
8420 .Sp
8421 produces a six element list.
8422 .Sp
8423 In time-critical applications, it is worthwhile to avoid splitting
8424 into more fields than necessary. Thus, when assigning to a list,
8425 if LIMIT is omitted (or zero), then LIMIT is treated as though it
8426 were one larger than the number of variables in the list; for the
8427 following, LIMIT is implicitly 3:
8428 .Sp
8429 .Vb 1
8430 \& my ($login, $passwd) = split(/:/);
8431 .Ve
8432 .Sp
8433 Note that splitting an EXPR that evaluates to the empty string always
8434 produces zero fields, regardless of the LIMIT specified.
8435 .Sp
8436 An empty leading field is produced when there is a positive-width
8437 match at the beginning of EXPR. For instance:
8438 .Sp
8439 .Vb 1
8440 \& my @x = split(/ /, " abc"); # ("", "abc")
8441 .Ve
8442 .Sp
8443 splits into two elements. However, a zero-width match at the
8444 beginning of EXPR never produces an empty field, so that:
8445 .Sp
8446 .Vb 1
8447 \& my @x = split(//, " abc"); # (" ", "a", "b", "c")
8448 .Ve
8449 .Sp
8450 splits into four elements instead of five.
8451 .Sp
8452 An empty trailing field, on the other hand, is produced when there is a
8453 match at the end of EXPR, regardless of the length of the match
8454 (of course, unless a non-zero LIMIT is given explicitly, such fields are
8455 removed, as in the last example). Thus:
8456 .Sp
8457 .Vb 1
8458 \& my @x = split(//, " abc", \-1); # (" ", "a", "b", "c", "")
8459 .Ve
8460 .Sp
8461 If the PATTERN contains
8462 capturing groups,
8463 then for each separator, an additional field is produced for each substring
8464 captured by a group (in the order in which the groups are specified,
8465 as per backreferences); if any group does not
8466 match, then it captures the \f(CW\*(C`undef\*(C'\fR value instead of a
8467 substring. Also,
8468 note that any such additional field is produced whenever there is a
8469 separator (that is, whenever a split occurs), and such an additional field
8470 does \fBnot\fR count towards the LIMIT. Consider the following expressions
8471 evaluated in list context (each returned list is provided in the associated
8472 comment):
8473 .Sp
8474 .Vb 2
8475 \& my @x = split(/\-|,/ , "1\-10,20", 3);
8476 \& # ("1", "10", "20")
8477 \&
8478 \& my @x = split(/(\-|,)/ , "1\-10,20", 3);
8479 \& # ("1", "\-", "10", ",", "20")
8480 \&
8481 \& my @x = split(/\-|(,)/ , "1\-10,20", 3);
8482 \& # ("1", undef, "10", ",", "20")
8483 \&
8484 \& my @x = split(/(\-)|,/ , "1\-10,20", 3);
8485 \& # ("1", "\-", "10", undef, "20")
8486 \&
8487 \& my @x = split(/(\-)|(,)/, "1\-10,20", 3);
8488 \& # ("1", "\-", undef, "10", undef, ",", "20")
8489 .Ve
8490 .IP "sprintf FORMAT, LIST" 4
8491 .IX Xref "sprintf"
8492 .IX Item "sprintf FORMAT, LIST"
8493 Returns a string formatted by the usual
8494 \&\f(CW\*(C`printf\*(C'\fR conventions of the C
8495 library function \f(CW\*(C`sprintf\*(C'\fR. See below for
8496 more details and see \fBsprintf\fR\|(3) or \fBprintf\fR\|(3) on your system for an
8497 explanation of the general principles.
8498 .Sp
8499 For example:
8500 .Sp
8501 .Vb 2
8502 \& # Format number with up to 8 leading zeroes
8503 \& my $result = sprintf("%08d", $number);
8504 \&
8505 \& # Round number to 3 digits after decimal point
8506 \& my $rounded = sprintf("%.3f", $number);
8507 .Ve
8508 .Sp
8509 Perl does its own \f(CW\*(C`sprintf\*(C'\fR formatting: it
8510 emulates the C
8511 function \fBsprintf\fR\|(3), but doesn't use it except for floating-point
8512 numbers, and even then only standard modifiers are allowed.
8513 Non-standard extensions in your local \fBsprintf\fR\|(3) are
8514 therefore unavailable from Perl.
8515 .Sp
8516 Unlike \f(CW\*(C`printf\*(C'\fR,
8517 \&\f(CW\*(C`sprintf\*(C'\fR does not do what you probably mean
8518 when you pass it an array as your first argument.
8519 The array is given scalar context,
8520 and instead of using the 0th element of the array as the format, Perl will
8521 use the count of elements in the array as the format, which is almost never
8522 useful.
8523 .Sp
8524 Perl's \f(CW\*(C`sprintf\*(C'\fR permits the following
8525 universally-known conversions:
8526 .Sp
8527 .Vb 10
8528 \& %% a percent sign
8529 \& %c a character with the given number
8530 \& %s a string
8531 \& %d a signed integer, in decimal
8532 \& %u an unsigned integer, in decimal
8533 \& %o an unsigned integer, in octal
8534 \& %x an unsigned integer, in hexadecimal
8535 \& %e a floating\-point number, in scientific notation
8536 \& %f a floating\-point number, in fixed decimal notation
8537 \& %g a floating\-point number, in %e or %f notation
8538 .Ve
8539 .Sp
8540 In addition, Perl permits the following widely-supported conversions:
8541 .Sp
8542 .Vb 10
8543 \& %X like %x, but using upper\-case letters
8544 \& %E like %e, but using an upper\-case "E"
8545 \& %G like %g, but with an upper\-case "E" (if applicable)
8546 \& %b an unsigned integer, in binary
8547 \& %B like %b, but using an upper\-case "B" with the # flag
8548 \& %p a pointer (outputs the Perl value\*(Aqs address in hexadecimal)
8549 \& %n special: *stores* the number of characters output so far
8550 \& into the next argument in the parameter list
8551 \& %a hexadecimal floating point
8552 \& %A like %a, but using upper\-case letters
8553 \& %i a synonym for %d
8554 \& %D a synonym for %ld
8555 \& %U a synonym for %lu
8556 \& %O a synonym for %lo
8557 \& %F a synonym for %f
8558 .Ve
8559 .Sp
8560 Note that the number of exponent digits in the scientific notation produced
8561 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
8562 exponent less than 100 is system-dependent: it may be three or less
8563 (zero-padded as necessary). In other words, 1.23 times ten to the
8564 99th may be either "1.23e99" or "1.23e099". Similarly for \f(CW%a\fR and \f(CW%A\fR:
8565 the exponent or the hexadecimal digits may float: especially the
8566 "long doubles" Perl configuration option may cause surprises.
8567 .Sp
8568 Between the \f(CW\*(C`%\*(C'\fR and the format letter, you may specify several
8569 additional attributes controlling the interpretation of the format.
8570 In order, these are:
8571 .RS 4
8572 .IP "format parameter index" 4
8573 .IX Item "format parameter index"
8574 An explicit format parameter index, such as \f(CW\*(C`2$\*(C'\fR. By default sprintf
8575 will format the next unused argument in the list, but this allows you
8576 to take the arguments out of order:
8577 .Sp
8578 .Vb 2
8579 \& printf \*(Aq%2$d %1$d\*(Aq, 12, 34; # prints "34 12"
8580 \& printf \*(Aq%3$d %d %1$d\*(Aq, 1, 2, 3; # prints "3 1 1"
8581 .Ve
8582 .IP flags 4
8583 .IX Item "flags"
8584 one or more of:
8585 .Sp
8586 .Vb 7
8587 \& space prefix non\-negative number with a space
8588 \& + prefix non\-negative number with a plus sign
8589 \& \- left\-justify within the field
8590 \& 0 use zeros, not spaces, to right\-justify
8591 \& # ensure the leading "0" for any octal,
8592 \& prefix non\-zero hexadecimal with "0x" or "0X",
8593 \& prefix non\-zero binary with "0b" or "0B"
8594 .Ve
8595 .Sp
8596 For example:
8597 .Sp
8598 .Vb 10
8599 \& printf \*(Aq<% d>\*(Aq, 12; # prints "< 12>"
8600 \& printf \*(Aq<% d>\*(Aq, 0; # prints "< 0>"
8601 \& printf \*(Aq<% d>\*(Aq, \-12; # prints "<\-12>"
8602 \& printf \*(Aq<%+d>\*(Aq, 12; # prints "<+12>"
8603 \& printf \*(Aq<%+d>\*(Aq, 0; # prints "<+0>"
8604 \& printf \*(Aq<%+d>\*(Aq, \-12; # prints "<\-12>"
8605 \& printf \*(Aq<%6s>\*(Aq, 12; # prints "< 12>"
8606 \& printf \*(Aq<%\-6s>\*(Aq, 12; # prints "<12 >"
8607 \& printf \*(Aq<%06s>\*(Aq, 12; # prints "<000012>"
8608 \& printf \*(Aq<%#o>\*(Aq, 12; # prints "<014>"
8609 \& printf \*(Aq<%#x>\*(Aq, 12; # prints "<0xc>"
8610 \& printf \*(Aq<%#X>\*(Aq, 12; # prints "<0XC>"
8611 \& printf \*(Aq<%#b>\*(Aq, 12; # prints "<0b1100>"
8612 \& printf \*(Aq<%#B>\*(Aq, 12; # prints "<0B1100>"
8613 .Ve
8614 .Sp
8615 When a space and a plus sign are given as the flags at once,
8616 the space is ignored.
8617 .Sp
8618 .Vb 2
8619 \& printf \*(Aq<%+ d>\*(Aq, 12; # prints "<+12>"
8620 \& printf \*(Aq<% +d>\*(Aq, 12; # prints "<+12>"
8621 .Ve
8622 .Sp
8623 When the # flag and a precision are given in the \f(CW%o\fR conversion,
8624 the precision is incremented if it's necessary for the leading "0".
8625 .Sp
8626 .Vb 3
8627 \& printf \*(Aq<%#.5o>\*(Aq, 012; # prints "<00012>"
8628 \& printf \*(Aq<%#.5o>\*(Aq, 012345; # prints "<012345>"
8629 \& printf \*(Aq<%#.0o>\*(Aq, 0; # prints "<0>"
8630 .Ve
8631 .IP "vector flag" 4
8632 .IX Item "vector flag"
8633 This flag tells Perl to interpret the supplied string as a vector of
8634 integers, one for each character in the string. Perl applies the format to
8635 each integer in turn, then joins the resulting strings with a separator (a
8636 dot \f(CW\*(C`.\*(C'\fR by default). This can be useful for displaying ordinal values of
8637 characters in arbitrary strings:
8638 .Sp
8639 .Vb 2
8640 \& printf "%vd", "AB\ex{100}"; # prints "65.66.256"
8641 \& printf "version is v%vd\en", $^V; # Perl\*(Aqs version
8642 .Ve
8643 .Sp
8644 Put an asterisk \f(CW\*(C`*\*(C'\fR before the \f(CW\*(C`v\*(C'\fR to override the string to
8645 use to separate the numbers:
8646 .Sp
8647 .Vb 2
8648 \& printf "address is %*vX\en", ":", $addr; # IPv6 address
8649 \& printf "bits are %0*v8b\en", " ", $bits; # random bitstring
8650 .Ve
8651 .Sp
8652 You can also explicitly specify the argument number to use for
8653 the join string using something like \f(CW\*(C`*2$v\*(C'\fR; for example:
8654 .Sp
8655 .Vb 2
8656 \& printf \*(Aq%*4$vX %*4$vX %*4$vX\*(Aq, # 3 IPv6 addresses
8657 \& @addr[1..3], ":";
8658 .Ve
8659 .IP "(minimum) width" 4
8660 .IX Item "(minimum) width"
8661 Arguments are usually formatted to be only as wide as required to
8662 display the given value. You can override the width by putting
8663 a number here, or get the width from the next argument (with \f(CW\*(C`*\*(C'\fR)
8664 or from a specified argument (e.g., with \f(CW\*(C`*2$\*(C'\fR):
8665 .Sp
8666 .Vb 5
8667 \& printf "<%s>", "a"; # prints "<a>"
8668 \& printf "<%6s>", "a"; # prints "< a>"
8669 \& printf "<%*s>", 6, "a"; # prints "< a>"
8670 \& printf \*(Aq<%*2$s>\*(Aq, "a", 6; # prints "< a>"
8671 \& printf "<%2s>", "long"; # prints "<long>" (does not truncate)
8672 .Ve
8673 .Sp
8674 If a field width obtained through \f(CW\*(C`*\*(C'\fR is negative, it has the same
8675 effect as the \f(CW\*(C`\-\*(C'\fR flag: left-justification.
8676 .IP "precision, or maximum width" 4
8677 .IX Xref "precision"
8678 .IX Item "precision, or maximum width"
8679 You can specify a precision (for numeric conversions) or a maximum
8680 width (for string conversions) by specifying a \f(CW\*(C`.\*(C'\fR followed by a number.
8681 For floating-point formats except \f(CW\*(C`g\*(C'\fR and \f(CW\*(C`G\*(C'\fR, this specifies
8682 how many places right of the decimal point to show (the default being 6).
8683 For example:
8684 .Sp
8685 .Vb 7
8686 \& # these examples are subject to system\-specific variation
8687 \& printf \*(Aq<%f>\*(Aq, 1; # prints "<1.000000>"
8688 \& printf \*(Aq<%.1f>\*(Aq, 1; # prints "<1.0>"
8689 \& printf \*(Aq<%.0f>\*(Aq, 1; # prints "<1>"
8690 \& printf \*(Aq<%07.2f>\*(Aq, 1.3; # prints "<0001.30>"
8691 \& printf \*(Aq<%e>\*(Aq, 10; # prints "<1.000000e+01>"
8692 \& printf \*(Aq<%.1e>\*(Aq, 10; # prints "<1.0e+01>"
8693 .Ve
8694 .Sp
8695 For "g" and "G", this specifies the maximum number of significant digits to
8696 show; for example:
8697 .Sp
8698 .Vb 11
8699 \& # These examples are subject to system\-specific variation.
8700 \& printf \*(Aq<%g>\*(Aq, 1; # prints "<1>"
8701 \& printf \*(Aq<%.10g>\*(Aq, 1; # prints "<1>"
8702 \& printf \*(Aq<%g>\*(Aq, 100; # prints "<100>"
8703 \& printf \*(Aq<%.1g>\*(Aq, 100; # prints "<1e+02>"
8704 \& printf \*(Aq<%.2g>\*(Aq, 100.01; # prints "<1e+02>"
8705 \& printf \*(Aq<%.5g>\*(Aq, 100.01; # prints "<100.01>"
8706 \& printf \*(Aq<%.4g>\*(Aq, 100.01; # prints "<100>"
8707 \& printf \*(Aq<%.1g>\*(Aq, 0.0111; # prints "<0.01>"
8708 \& printf \*(Aq<%.2g>\*(Aq, 0.0111; # prints "<0.011>"
8709 \& printf \*(Aq<%.3g>\*(Aq, 0.0111; # prints "<0.0111>"
8710 .Ve
8711 .Sp
8712 For integer conversions, specifying a precision implies that the
8713 output of the number itself should be zero-padded to this width,
8714 where the 0 flag is ignored:
8715 .Sp
8716 .Vb 6
8717 \& printf \*(Aq<%.6d>\*(Aq, 1; # prints "<000001>"
8718 \& printf \*(Aq<%+.6d>\*(Aq, 1; # prints "<+000001>"
8719 \& printf \*(Aq<%\-10.6d>\*(Aq, 1; # prints "<000001 >"
8720 \& printf \*(Aq<%10.6d>\*(Aq, 1; # prints "< 000001>"
8721 \& printf \*(Aq<%010.6d>\*(Aq, 1; # prints "< 000001>"
8722 \& printf \*(Aq<%+10.6d>\*(Aq, 1; # prints "< +000001>"
8723 \&
8724 \& printf \*(Aq<%.6x>\*(Aq, 1; # prints "<000001>"
8725 \& printf \*(Aq<%#.6x>\*(Aq, 1; # prints "<0x000001>"
8726 \& printf \*(Aq<%\-10.6x>\*(Aq, 1; # prints "<000001 >"
8727 \& printf \*(Aq<%10.6x>\*(Aq, 1; # prints "< 000001>"
8728 \& printf \*(Aq<%010.6x>\*(Aq, 1; # prints "< 000001>"
8729 \& printf \*(Aq<%#10.6x>\*(Aq, 1; # prints "< 0x000001>"
8730 .Ve
8731 .Sp
8732 For string conversions, specifying a precision truncates the string
8733 to fit the specified width:
8734 .Sp
8735 .Vb 2
8736 \& printf \*(Aq<%.5s>\*(Aq, "truncated"; # prints "<trunc>"
8737 \& printf \*(Aq<%10.5s>\*(Aq, "truncated"; # prints "< trunc>"
8738 .Ve
8739 .Sp
8740 You can also get the precision from the next argument using \f(CW\*(C`.*\*(C'\fR, or from a
8741 specified argument (e.g., with \f(CW\*(C`.*2$\*(C'\fR):
8742 .Sp
8743 .Vb 2
8744 \& printf \*(Aq<%.6x>\*(Aq, 1; # prints "<000001>"
8745 \& printf \*(Aq<%.*x>\*(Aq, 6, 1; # prints "<000001>"
8746 \&
8747 \& printf \*(Aq<%.*2$x>\*(Aq, 1, 6; # prints "<000001>"
8748 \&
8749 \& printf \*(Aq<%6.*2$x>\*(Aq, 1, 4; # prints "< 0001>"
8750 .Ve
8751 .Sp
8752 If a precision obtained through \f(CW\*(C`*\*(C'\fR is negative, it counts
8753 as having no precision at all.
8754 .Sp
8755 .Vb 4
8756 \& printf \*(Aq<%.*s>\*(Aq, 7, "string"; # prints "<string>"
8757 \& printf \*(Aq<%.*s>\*(Aq, 3, "string"; # prints "<str>"
8758 \& printf \*(Aq<%.*s>\*(Aq, 0, "string"; # prints "<>"
8759 \& printf \*(Aq<%.*s>\*(Aq, \-1, "string"; # prints "<string>"
8760 \&
8761 \& printf \*(Aq<%.*d>\*(Aq, 1, 0; # prints "<0>"
8762 \& printf \*(Aq<%.*d>\*(Aq, 0, 0; # prints "<>"
8763 \& printf \*(Aq<%.*d>\*(Aq, \-1, 0; # prints "<0>"
8764 .Ve
8765 .IP size 4
8766 .IX Item "size"
8767 For numeric conversions, you can specify the size to interpret the
8768 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
8769 conversions (\f(CW\*(C`d u o x X b i D U O\*(C'\fR), numbers are usually assumed to be
8770 whatever the default integer size is on your platform (usually 32 or 64
8771 bits), but you can override this to use instead one of the standard C types,
8772 as supported by the compiler used to build Perl:
8773 .Sp
8774 .Vb 10
8775 \& hh interpret integer as C type "char" or "unsigned
8776 \& char" on Perl 5.14 or later
8777 \& h interpret integer as C type "short" or
8778 \& "unsigned short"
8779 \& j interpret integer as C type "intmax_t" on Perl
8780 \& 5.14 or later. Prior to Perl 5.30 this worked only
8781 \& with a C99 compiler, hence was unportable before
8782 \& that release.
8783 \& l interpret integer as C type "long" or
8784 \& "unsigned long"
8785 \& q, L, or ll interpret integer as C type "long long",
8786 \& "unsigned long long", or "quad" (typically
8787 \& 64\-bit integers)
8788 \& t interpret integer as C type "ptrdiff_t" on Perl
8789 \& 5.14 or later
8790 \& z interpret integer as C types "size_t" or
8791 \& "ssize_t" on Perl 5.14 or later
8792 .Ve
8793 .Sp
8794 Note that, in general, using the \f(CW\*(C`l\*(C'\fR modifier (for example, when writing
8795 \&\f(CW"%ld"\fR or \f(CW"%lu"\fR instead of \f(CW"%d"\fR and \f(CW"%u"\fR) is unnecessary
8796 when used from Perl code. Moreover, it may be harmful, for example on
8797 Windows 64\-bit where a long is 32\-bits.
8798 .Sp
8799 As of 5.14, none of these raises an exception if they are not supported on
8800 your platform. However, if warnings are enabled, a warning of the
8801 \&\f(CW\*(C`printf\*(C'\fR warning class is issued on an unsupported
8802 conversion flag. Should you instead prefer an exception, do this:
8803 .Sp
8804 .Vb 1
8805 \& use warnings FATAL => "printf";
8806 .Ve
8807 .Sp
8808 If you would like to know about a version dependency before you
8809 start running the program, put something like this at its top:
8810 .Sp
8811 .Vb 1
8812 \& use v5.14; # for hh/j/t/z/ printf modifiers
8813 .Ve
8814 .Sp
8815 You can find out whether your Perl supports quads via Config:
8816 .Sp
8817 .Vb 5
8818 \& use Config;
8819 \& if ($Config{use64bitint} eq "define"
8820 \& || $Config{longsize} >= 8) {
8821 \& print "Nice quads!\en";
8822 \& }
8823 .Ve
8824 .Sp
8825 For floating-point conversions (\f(CW\*(C`e f g E F G\*(C'\fR), numbers are usually assumed
8826 to be the default floating-point size on your platform (double or long double),
8827 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
8828 platform supports them. You can find out whether your Perl supports long
8829 doubles via Config:
8830 .Sp
8831 .Vb 2
8832 \& use Config;
8833 \& print "long doubles\en" if $Config{d_longdbl} eq "define";
8834 .Ve
8835 .Sp
8836 You can find out whether Perl considers "long double" to be the default
8837 floating-point size to use on your platform via Config:
8838 .Sp
8839 .Vb 4
8840 \& use Config;
8841 \& if ($Config{uselongdouble} eq "define") {
8842 \& print "long doubles by default\en";
8843 \& }
8844 .Ve
8845 .Sp
8846 It can also be that long doubles and doubles are the same thing:
8847 .Sp
8848 .Vb 3
8849 \& use Config;
8850 \& ($Config{doublesize} == $Config{longdblsize}) &&
8851 \& print "doubles are long doubles\en";
8852 .Ve
8853 .Sp
8854 The size specifier \f(CW\*(C`V\*(C'\fR has no effect for Perl code, but is supported for
8855 compatibility with XS code. It means "use the standard size for a Perl
8856 integer or floating-point number", which is the default.
8857 .IP "order of arguments" 4
8858 .IX Item "order of arguments"
8859 Normally, \f(CW\*(C`sprintf\*(C'\fR takes the next unused
8860 argument as the value to
8861 format for each format specification. If the format specification
8862 uses \f(CW\*(C`*\*(C'\fR to require additional arguments, these are consumed from
8863 the argument list in the order they appear in the format
8864 specification \fIbefore\fR the value to format. Where an argument is
8865 specified by an explicit index, this does not affect the normal
8866 order for the arguments, even when the explicitly specified index
8867 would have been the next argument.
8868 .Sp
8869 So:
8870 .Sp
8871 .Vb 1
8872 \& printf "<%*.*s>", $x, $y, $z;
8873 .Ve
8874 .Sp
8875 uses \f(CW$x\fR for the width, \f(CW$y\fR for the precision, and \f(CW$z\fR
8876 as the value to format; while:
8877 .Sp
8878 .Vb 1
8879 \& printf \*(Aq<%*1$.*s>\*(Aq, $x, $y;
8880 .Ve
8881 .Sp
8882 would use \f(CW$x\fR for the width and precision, and \f(CW$y\fR as the
8883 value to format.
8884 .Sp
8885 Here are some more examples; be aware that when using an explicit
8886 index, the \f(CW\*(C`$\*(C'\fR may need escaping:
8887 .Sp
8888 .Vb 5
8889 \& printf "%2\e$d %d\en", 12, 34; # will print "34 12\en"
8890 \& printf "%2\e$d %d %d\en", 12, 34; # will print "34 12 34\en"
8891 \& printf "%3\e$d %d %d\en", 12, 34, 56; # will print "56 12 34\en"
8892 \& printf "%2\e$*3\e$d %d\en", 12, 34, 3; # will print " 34 12\en"
8893 \& printf "%*1\e$.*f\en", 4, 5, 10; # will print "5.0000\en"
8894 .Ve
8895 .RE
8896 .RS 4
8897 .Sp
8898 If \f(CW\*(C`use locale\*(C'\fR (including \f(CW\*(C`use locale \*(Aq:not_characters\*(Aq\*(C'\fR)
8899 is in effect and \f(CW\*(C`POSIX::setlocale\*(C'\fR has been
8900 called,
8901 the character used for the decimal separator in formatted floating-point
8902 numbers is affected by the \f(CW\*(C`LC_NUMERIC\*(C'\fR locale. See perllocale
8903 and POSIX.
8904 .RE
8905 .IP "sqrt EXPR" 4
8906 .IX Xref "sqrt root square root"
8907 .IX Item "sqrt EXPR"
8908 .PD 0
8909 .IP sqrt 4
8910 .IX Item "sqrt"
8911 .PD
8912 Return the positive square root of EXPR. If EXPR is omitted, uses
8913 \&\f(CW$_\fR. Works only for non-negative operands unless you've
8914 loaded the \f(CW\*(C`Math::Complex\*(C'\fR module.
8915 .Sp
8916 .Vb 2
8917 \& use Math::Complex;
8918 \& print sqrt(\-4); # prints 2i
8919 .Ve
8920 .IP "srand EXPR" 4
8921 .IX Xref "srand seed randseed"
8922 .IX Item "srand EXPR"
8923 .PD 0
8924 .IP srand 4
8925 .IX Item "srand"
8926 .PD
8927 Sets and returns the random number seed for the \f(CW\*(C`rand\*(C'\fR
8928 operator.
8929 .Sp
8930 The point of the function is to "seed" the \f(CW\*(C`rand\*(C'\fR
8931 function so that \f(CW\*(C`rand\*(C'\fR can produce a different sequence
8932 each time you run your program. When called with a parameter,
8933 \&\f(CW\*(C`srand\*(C'\fR uses that for the seed; otherwise it
8934 (semi\-)randomly chooses a seed (see below). In either case, starting with Perl 5.14,
8935 it returns the seed. To signal that your code will work \fIonly\fR on Perls
8936 of a recent vintage:
8937 .Sp
8938 .Vb 1
8939 \& use v5.14; # so srand returns the seed
8940 .Ve
8941 .Sp
8942 If \f(CW\*(C`srand\*(C'\fR is not called explicitly, it is called
8943 implicitly without a parameter at the first use of the
8944 \&\f(CW\*(C`rand\*(C'\fR operator. However, there are a few situations
8945 where programs are likely to want to call \f(CW\*(C`srand\*(C'\fR. One
8946 is for generating predictable results, generally for testing or
8947 debugging. There, you use \f(CWsrand($seed)\fR, with the same \f(CW$seed\fR each
8948 time. Another case is that you may want to call \f(CW\*(C`srand\*(C'\fR
8949 after a \f(CW\*(C`fork\*(C'\fR to avoid child processes sharing the same seed
8950 value as the parent (and consequently each other).
8951 .Sp
8952 Do \fBnot\fR call \f(CWsrand()\fR (i.e., without an argument) more than once per
8953 process. The internal state of the random number generator should
8954 contain more entropy than can be provided by any seed, so calling
8955 \&\f(CW\*(C`srand\*(C'\fR again actually \fIloses\fR randomness.
8956 .Sp
8957 Most implementations of \f(CW\*(C`srand\*(C'\fR take an integer and will
8958 silently
8959 truncate decimal numbers. This means \f(CWsrand(42)\fR will usually
8960 produce the same results as \f(CWsrand(42.1)\fR. To be safe, always pass
8961 \&\f(CW\*(C`srand\*(C'\fR an integer.
8962 .Sp
8963 A typical use of the returned seed is for a test program which has too many
8964 combinations to test comprehensively in the time available to it each run. It
8965 can test a random subset each time, and should there be a failure, log the seed
8966 used for that run so that it can later be used to reproduce the same results.
8967 .Sp
8968 If the \f(CW\*(C`PERL_RAND_SEED\*(C'\fR environment variable is set to a non-negative
8969 integer during process startup then calls to \f(CWsrand()\fR with no
8970 arguments will initialize the perl random number generator with a
8971 consistent seed each time it is called, whether called explicitly with
8972 no arguments or implicitly via use of \f(CWrand()\fR. The exact seeding that
8973 a given \f(CW\*(C`PERL_RAND_SEED\*(C'\fR will produce is deliberately unspecified, but
8974 using different values for \f(CW\*(C`PERL_RAND_SEED\*(C'\fR should produce different
8975 results. This is intended for debugging and performance analysis and is
8976 only guaranteed to produce consistent results between invocations of the
8977 same perl executable running the same code when all other factors are
8978 equal. The environment variable is read only once during process
8979 startup, and changing it during the program flow will not affect the
8980 currently running process. See perlrun for more details.
8981 .Sp
8982 \&\fR\f(CB\*(C`rand\*(C'\fR\fB is not cryptographically secure. You should not rely
8983 on it in security-sensitive situations.\fR As of this writing, a
8984 number of third-party CPAN modules offer random number generators
8985 intended by their authors to be cryptographically secure,
8986 including: Data::Entropy, Crypt::Random, Math::Random::Secure,
8987 and Math::TrulyRandom.
8988 .IP "stat FILEHANDLE" 4
8989 .IX Xref "stat file, status ctime"
8990 .IX Item "stat FILEHANDLE"
8991 .PD 0
8992 .IP "stat EXPR" 4
8993 .IX Item "stat EXPR"
8994 .IP "stat DIRHANDLE" 4
8995 .IX Item "stat DIRHANDLE"
8996 .IP stat 4
8997 .IX Item "stat"
8998 .PD
8999 Returns a 13\-element list giving the status info for a file, either
9000 the file opened via FILEHANDLE or DIRHANDLE, or named by EXPR. If EXPR is
9001 omitted, it stats \f(CW$_\fR (not \f(CW\*(C`_\*(C'\fR!). Returns the empty
9002 list if \f(CW\*(C`stat\*(C'\fR fails. Typically
9003 used as follows:
9004 .Sp
9005 .Vb 3
9006 \& my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
9007 \& $atime,$mtime,$ctime,$blksize,$blocks)
9008 \& = stat($filename);
9009 .Ve
9010 .Sp
9011 Not all fields are supported on all filesystem types. Here are the
9012 meanings of the fields:
9013 .Sp
9014 .Vb 10
9015 \& 0 dev device number of filesystem
9016 \& 1 ino inode number
9017 \& 2 mode file mode (type and permissions)
9018 \& 3 nlink number of (hard) links to the file
9019 \& 4 uid numeric user ID of file\*(Aqs owner
9020 \& 5 gid numeric group ID of file\*(Aqs owner
9021 \& 6 rdev the device identifier (special files only)
9022 \& 7 size total size of file, in bytes
9023 \& 8 atime last access time in seconds since the epoch
9024 \& 9 mtime last modify time in seconds since the epoch
9025 \& 10 ctime inode change time in seconds since the epoch (*)
9026 \& 11 blksize preferred I/O size in bytes for interacting with the
9027 \& file (may vary from file to file)
9028 \& 12 blocks actual number of system\-specific blocks allocated
9029 \& on disk (often, but not always, 512 bytes each)
9030 .Ve
9031 .Sp
9032 (The epoch was at 00:00 January 1, 1970 GMT.)
9033 .Sp
9034 (*) Not all fields are supported on all filesystem types. Notably, the
9035 ctime field is non-portable. In particular, you cannot expect it to be a
9036 "creation time"; see "Files and Filesystems" in perlport for details.
9037 .Sp
9038 If \f(CW\*(C`stat\*(C'\fR is passed the special filehandle
9039 consisting of an underline, no stat is done, but the current contents of
9040 the stat structure from the last \f(CW\*(C`stat\*(C'\fR,
9041 \&\f(CW\*(C`lstat\*(C'\fR, or filetest are returned. Example:
9042 .Sp
9043 .Vb 3
9044 \& if (\-x $file && (($d) = stat(_)) && $d < 0) {
9045 \& print "$file is executable NFS file\en";
9046 \& }
9047 .Ve
9048 .Sp
9049 (This works on machines only for which the device number is negative
9050 under NFS.)
9051 .Sp
9052 On some platforms inode numbers are of a type larger than perl knows how
9053 to handle as integer numerical values. If necessary, an inode number will
9054 be returned as a decimal string in order to preserve the entire value.
9055 If used in a numeric context, this will be converted to a floating-point
9056 numerical value, with rounding, a fate that is best avoided. Therefore,
9057 you should prefer to compare inode numbers using \f(CW\*(C`eq\*(C'\fR rather than \f(CW\*(C`==\*(C'\fR.
9058 \&\f(CW\*(C`eq\*(C'\fR will work fine on inode numbers that are represented numerically,
9059 as well as those represented as strings.
9060 .Sp
9061 Because the mode contains both the file type and its permissions, you
9062 should mask off the file type portion and (s)printf using a \f(CW"%o"\fR
9063 if you want to see the real permissions.
9064 .Sp
9065 .Vb 2
9066 \& my $mode = (stat($filename))[2];
9067 \& printf "Permissions are %04o\en", $mode & 07777;
9068 .Ve
9069 .Sp
9070 In scalar context, \f(CW\*(C`stat\*(C'\fR returns a boolean value
9071 indicating success
9072 or failure, and, if successful, sets the information associated with
9073 the special filehandle \f(CW\*(C`_\*(C'\fR.
9074 .Sp
9075 The File::stat module provides a convenient, by-name access mechanism:
9076 .Sp
9077 .Vb 5
9078 \& use File::stat;
9079 \& my $sb = stat($filename);
9080 \& printf "File is %s, size is %s, perm %04o, mtime %s\en",
9081 \& $filename, $sb\->size, $sb\->mode & 07777,
9082 \& scalar localtime $sb\->mtime;
9083 .Ve
9084 .Sp
9085 You can import symbolic mode constants and functions
9086 (\f(CW\*(C`S_I*\*(C'\fR) from the Fcntl module:
9087 .Sp
9088 .Vb 1
9089 \& use Fcntl \*(Aq:mode\*(Aq;
9090 \&
9091 \& my $mode = (stat($filename))[2];
9092 \&
9093 \& my $user_rwx = ($mode & S_IRWXU) >> 6;
9094 \& my $group_read = ($mode & S_IRGRP) >> 3;
9095 \& my $other_execute = $mode & S_IXOTH;
9096 \&
9097 \& printf "Permissions are %04o\en", S_IMODE($mode), "\en";
9098 \&
9099 \& my $is_setuid = $mode & S_ISUID;
9100 \& my $is_directory = S_ISDIR($mode);
9101 .Ve
9102 .Sp
9103 You could write the last two using the \f(CW\*(C`\-u\*(C'\fR and \f(CW\*(C`\-d\*(C'\fR operators.
9104 Commonly available \f(CW\*(C`S_I*\*(C'\fR constants are:
9105 .Sp
9106 .Vb 1
9107 \& # Permissions: read, write, execute, for user, group, others.
9108 \&
9109 \& S_IRWXU S_IRUSR S_IWUSR S_IXUSR
9110 \& S_IRWXG S_IRGRP S_IWGRP S_IXGRP
9111 \& S_IRWXO S_IROTH S_IWOTH S_IXOTH
9112 \&
9113 \& # Setuid/Setgid/Stickiness/SaveText/EnforcedLocks.
9114 \& # Note that the exact meaning of these is system\-dependent.
9115 \&
9116 \& S_ISUID S_ISGID S_ISVTX S_ISTXT S_ENFMT
9117 \&
9118 \& # File types. Not all are necessarily available on
9119 \& # your system.
9120 \&
9121 \& S_IFREG S_IFDIR S_IFLNK S_IFBLK S_IFCHR
9122 \& S_IFIFO S_IFSOCK S_IFWHT
9123 \&
9124 \& # The following are compatibility aliases for S_IRUSR,
9125 \& # S_IWUSR, and S_IXUSR.
9126 \&
9127 \& S_IREAD S_IWRITE S_IEXEC
9128 .Ve
9129 .Sp
9130 and the \f(CW\*(C`S_I*\*(C'\fR functions are
9131 .Sp
9132 .Vb 2
9133 \& S_IMODE($mode) the part of $mode containing the permission
9134 \& bits and the setuid/setgid/sticky bits
9135 \&
9136 \& S_IFMT($mode) the part of $mode containing the file type,
9137 \& which will match one of the S_IF* constants
9138 \& (e.g. S_IFMT($mode) == S_IFDIR for directories),
9139 \& but see the following helper functions
9140 \&
9141 \& # The operators \-f, \-d, \-l, \-b, \-c, \-p, and \-S.
9142 \&
9143 \& S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)
9144 \& S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)
9145 \&
9146 \& # No direct \-X operator counterpart, but for the first one
9147 \& # the \-g operator is often equivalent. The ENFMT stands for
9148 \& # record flocking enforcement, a platform\-dependent feature.
9149 \&
9150 \& S_ISENFMT($mode) S_ISWHT($mode)
9151 .Ve
9152 .Sp
9153 See your native \fBchmod\fR\|(2) and \fBstat\fR\|(2) documentation for more details
9154 about the \f(CW\*(C`S_I*\*(C'\fR constants. To get status info for a symbolic link
9155 instead of the target file behind the link, use the
9156 \&\f(CW\*(C`lstat\*(C'\fR function.
9157 .Sp
9158 Portability issues: "stat" in perlport.
9159 .IP "state VARLIST" 4
9160 .IX Xref "state"
9161 .IX Item "state VARLIST"
9162 .PD 0
9163 .IP "state TYPE VARLIST" 4
9164 .IX Item "state TYPE VARLIST"
9165 .IP "state VARLIST : ATTRS" 4
9166 .IX Item "state VARLIST : ATTRS"
9167 .IP "state TYPE VARLIST : ATTRS" 4
9168 .IX Item "state TYPE VARLIST : ATTRS"
9169 .PD
9170 \&\f(CW\*(C`state\*(C'\fR declares a lexically scoped variable, just
9171 like \f(CW\*(C`my\*(C'\fR.
9172 However, those variables will never be reinitialized, contrary to
9173 lexical variables that are reinitialized each time their enclosing block
9174 is entered.
9175 See "Persistent Private Variables" in perlsub for details.
9176 .Sp
9177 If more than one variable is listed, the list must be placed in
9178 parentheses. With a parenthesised list, \f(CW\*(C`undef\*(C'\fR can be
9179 used as a
9180 dummy placeholder. However, since initialization of state variables in
9181 such lists is currently not possible this would serve no purpose.
9182 .Sp
9183 Like \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`local\*(C'\fR, and
9184 \&\f(CW\*(C`our\*(C'\fR, \f(CW\*(C`state\*(C'\fR can operate on a variable
9185 anywhere it appears in an expression (aside from interpolation in strings).
9186 The declaration will not apply to additional uses of the same variable until
9187 the next statement. This means additional uses of that variable within the
9188 same statement will act as they would have before that declaration occurred,
9189 or result in a strict 'vars' error, as appropriate.
9190 .Sp
9191 .Vb 5
9192 \& package main;
9193 \& use feature \*(Aqstate\*(Aq;
9194 \& our $x = 2;
9195 \& foo($x, state $x = $x + 1, $x); # foo() receives (2, 3, 2)
9196 \& foo($x, $main::x); # foo() receives (3, 2)
9197 .Ve
9198 .Sp
9199 Redeclaring a variable in the same scope or statement will "shadow" the
9200 previous declaration, creating a new instance and preventing access to
9201 the previous one. This is usually undesired and, if warnings are enabled,
9202 will result in a warning in the \f(CW\*(C`shadow\*(C'\fR category.
9203 .Sp
9204 \&\f(CW\*(C`state\*(C'\fR is available only if the
9205 \&\f(CW"state"\fR feature is enabled or if it is
9206 prefixed with \f(CW\*(C`CORE::\*(C'\fR. The
9207 \&\f(CW"state"\fR feature is enabled
9208 automatically with a \f(CW\*(C`use v5.10\*(C'\fR (or higher) declaration in the current
9209 scope.
9210 .IP "study SCALAR" 4
9211 .IX Xref "study"
9212 .IX Item "study SCALAR"
9213 .PD 0
9214 .IP study 4
9215 .IX Item "study"
9216 .PD
9217 At this time, \f(CW\*(C`study\*(C'\fR does nothing. This may change in the future.
9218 .Sp
9219 Prior to Perl version 5.16, it would create an inverted index of all characters
9220 that occurred in the given SCALAR (or \f(CW$_\fR if unspecified). When
9221 matching a pattern, the rarest character from the pattern would be looked up in
9222 this index. Rarity was based on some static frequency tables constructed from
9223 some C programs and English text.
9224 .IP "sub NAME BLOCK" 4
9225 .IX Xref "sub"
9226 .IX Item "sub NAME BLOCK"
9227 .PD 0
9228 .IP "sub NAME (PROTO) BLOCK" 4
9229 .IX Item "sub NAME (PROTO) BLOCK"
9230 .IP "sub NAME : ATTRS BLOCK" 4
9231 .IX Item "sub NAME : ATTRS BLOCK"
9232 .IP "sub NAME (PROTO) : ATTRS BLOCK" 4
9233 .IX Item "sub NAME (PROTO) : ATTRS BLOCK"
9234 .PD
9235 This is subroutine definition, not a real function \fIper se\fR. Without a
9236 BLOCK it's just a forward declaration. Without a NAME, it's an anonymous
9237 function declaration, so does return a value: the CODE ref of the closure
9238 just created.
9239 .Sp
9240 See perlsub and perlref for details about subroutines and
9241 references; see attributes and Attribute::Handlers for more
9242 information about attributes.
9243 .IP _\|_SUB_\|_ 4
9244 .IX Xref "__SUB__"
9245 .IX Item "__SUB__"
9246 A special token that returns a reference to the current subroutine, or
9247 \&\f(CW\*(C`undef\*(C'\fR outside of a subroutine.
9248 .Sp
9249 The behaviour of \f(CW\*(C`_\|_SUB_\|_\*(C'\fR within a regex code block (such
9250 as \f(CW\*(C`/(?{...})/\*(C'\fR) is subject to change.
9251 .Sp
9252 This token is only available under \f(CW\*(C`use v5.16\*(C'\fR or the
9253 \&\f(CW"current_sub"\fR feature.
9254 See feature.
9255 .IP "substr EXPR,OFFSET,LENGTH,REPLACEMENT" 4
9256 .IX Xref "substr substring mid left right"
9257 .IX Item "substr EXPR,OFFSET,LENGTH,REPLACEMENT"
9258 .PD 0
9259 .IP "substr EXPR,OFFSET,LENGTH" 4
9260 .IX Item "substr EXPR,OFFSET,LENGTH"
9261 .IP "substr EXPR,OFFSET" 4
9262 .IX Item "substr EXPR,OFFSET"
9263 .PD
9264 Extracts a substring out of EXPR and returns it. First character is at
9265 offset zero. If OFFSET is negative, starts
9266 that far back from the end of the string. If LENGTH is omitted, returns
9267 everything through the end of the string. If LENGTH is negative, leaves that
9268 many characters off the end of the string.
9269 .Sp
9270 .Vb 6
9271 \& my $s = "The black cat climbed the green tree";
9272 \& my $color = substr $s, 4, 5; # black
9273 \& my $middle = substr $s, 4, \-11; # black cat climbed the
9274 \& my $end = substr $s, 14; # climbed the green tree
9275 \& my $tail = substr $s, \-4; # tree
9276 \& my $z = substr $s, \-4, 2; # tr
9277 .Ve
9278 .Sp
9279 You can use the \f(CW\*(C`substr\*(C'\fR
9280 function as an lvalue, in which case EXPR
9281 must itself be an lvalue. If you assign something shorter than LENGTH,
9282 the string will shrink, and if you assign something longer than LENGTH,
9283 the string will grow to accommodate it. To keep the string the same
9284 length, you may need to pad or chop your value using
9285 \&\f(CW\*(C`sprintf\*(C'\fR.
9286 .Sp
9287 If OFFSET and LENGTH specify a substring that is partly outside the
9288 string, only the part within the string is returned. If the substring
9289 is beyond either end of the string,
9290 \&\f(CW\*(C`substr\*(C'\fR returns the undefined
9291 value and produces a warning. When used as an lvalue, specifying a
9292 substring that is entirely outside the string raises an exception.
9293 Here's an example showing the behavior for boundary cases:
9294 .Sp
9295 .Vb 5
9296 \& my $name = \*(Aqfred\*(Aq;
9297 \& substr($name, 4) = \*(Aqdy\*(Aq; # $name is now \*(Aqfreddy\*(Aq
9298 \& my $null = substr $name, 6, 2; # returns "" (no warning)
9299 \& my $oops = substr $name, 7; # returns undef, with warning
9300 \& substr($name, 7) = \*(Aqgap\*(Aq; # raises an exception
9301 .Ve
9302 .Sp
9303 An alternative to using
9304 \&\f(CW\*(C`substr\*(C'\fR as an lvalue is to
9305 specify the
9306 REPLACEMENT string as the 4th argument. This allows you to replace
9307 parts of the EXPR and return what was there before in one operation,
9308 just as you can with
9309 \&\f(CW\*(C`splice\*(C'\fR.
9310 .Sp
9311 .Vb 3
9312 \& my $s = "The black cat climbed the green tree";
9313 \& my $z = substr $s, 14, 7, "jumped from"; # climbed
9314 \& # $s is now "The black cat jumped from the green tree"
9315 .Ve
9316 .Sp
9317 Note that the lvalue returned by the three-argument version of
9318 \&\f(CW\*(C`substr\*(C'\fR acts as
9319 a 'magic bullet'; each time it is assigned to, it remembers which part
9320 of the original string is being modified; for example:
9321 .Sp
9322 .Vb 7
9323 \& my $x = \*(Aq1234\*(Aq;
9324 \& for (substr($x,1,2)) {
9325 \& $_ = \*(Aqa\*(Aq; print $x,"\en"; # prints 1a4
9326 \& $_ = \*(Aqxyz\*(Aq; print $x,"\en"; # prints 1xyz4
9327 \& $x = \*(Aq56789\*(Aq;
9328 \& $_ = \*(Aqpq\*(Aq; print $x,"\en"; # prints 5pq9
9329 \& }
9330 .Ve
9331 .Sp
9332 With negative offsets, it remembers its position from the end of the string
9333 when the target string is modified:
9334 .Sp
9335 .Vb 6
9336 \& my $x = \*(Aq1234\*(Aq;
9337 \& for (substr($x, \-3, 2)) {
9338 \& $_ = \*(Aqa\*(Aq; print $x,"\en"; # prints 1a4, as above
9339 \& $x = \*(Aqabcdefg\*(Aq;
9340 \& print $_,"\en"; # prints f
9341 \& }
9342 .Ve
9343 .Sp
9344 Prior to Perl version 5.10, the result of using an lvalue multiple times was
9345 unspecified. Prior to 5.16, the result with negative offsets was
9346 unspecified.
9347 .IP "symlink OLDFILE,NEWFILE" 4
9348 .IX Xref "symlink link symbolic link link, symbolic"
9349 .IX Item "symlink OLDFILE,NEWFILE"
9350 Creates a new filename symbolically linked to the old filename.
9351 Returns \f(CW1\fR for success, \f(CW0\fR otherwise. On systems that don't support
9352 symbolic links, raises an exception. To check for that,
9353 use eval:
9354 .Sp
9355 .Vb 1
9356 \& my $symlink_exists = eval { symlink("",""); 1 };
9357 .Ve
9358 .Sp
9359 Portability issues: "symlink" in perlport.
9360 .IP "syscall NUMBER, LIST" 4
9361 .IX Xref "syscall system call"
9362 .IX Item "syscall NUMBER, LIST"
9363 Calls the system call specified as the first element of the list,
9364 passing the remaining elements as arguments to the system call. If
9365 unimplemented, raises an exception. The arguments are interpreted
9366 as follows: if a given argument is numeric, the argument is passed as
9367 an int. If not, the pointer to the string value is passed. You are
9368 responsible to make sure a string is pre-extended long enough to
9369 receive any result that might be written into a string. You can't use a
9370 string literal (or other read-only string) as an argument to
9371 \&\f(CW\*(C`syscall\*(C'\fR because Perl has to assume that any
9372 string pointer might be written through. If your
9373 integer arguments are not literals and have never been interpreted in a
9374 numeric context, you may need to add \f(CW0\fR to them to force them to look
9375 like numbers. This emulates the
9376 \&\f(CW\*(C`syswrite\*(C'\fR function (or
9377 vice versa):
9378 .Sp
9379 .Vb 3
9380 \& require \*(Aqsyscall.ph\*(Aq; # may need to run h2ph
9381 \& my $s = "hi there\en";
9382 \& syscall(SYS_write(), fileno(STDOUT), $s, length $s);
9383 .Ve
9384 .Sp
9385 Note that Perl supports passing of up to only 14 arguments to your syscall,
9386 which in practice should (usually) suffice.
9387 .Sp
9388 Syscall returns whatever value returned by the system call it calls.
9389 If the system call fails, \f(CW\*(C`syscall\*(C'\fR returns
9390 \&\f(CW\-1\fR and sets \f(CW$!\fR (errno).
9391 Note that some system calls \fIcan\fR legitimately return \f(CW\-1\fR. The proper
9392 way to handle such calls is to assign \f(CW\*(C`$! = 0\*(C'\fR before the call, then
9393 check the value of \f(CW$!\fR if
9394 \&\f(CW\*(C`syscall\*(C'\fR returns \f(CW\-1\fR.
9395 .Sp
9396 There's a problem with \f(CW\*(C`syscall(SYS_pipe())\*(C'\fR: it returns the file
9397 number of the read end of the pipe it creates, but there is no way
9398 to retrieve the file number of the other end. You can avoid this
9399 problem by using \f(CW\*(C`pipe\*(C'\fR instead.
9400 .Sp
9401 Portability issues: "syscall" in perlport.
9402 .IP "sysopen FILEHANDLE,FILENAME,MODE" 4
9403 .IX Xref "sysopen"
9404 .IX Item "sysopen FILEHANDLE,FILENAME,MODE"
9405 .PD 0
9406 .IP "sysopen FILEHANDLE,FILENAME,MODE,PERMS" 4
9407 .IX Item "sysopen FILEHANDLE,FILENAME,MODE,PERMS"
9408 .PD
9409 Opens the file whose filename is given by FILENAME, and associates it with
9410 FILEHANDLE. If FILEHANDLE is an expression, its value is used as the real
9411 filehandle wanted; an undefined scalar will be suitably autovivified. This
9412 function calls the underlying operating system's \fBopen\fR\|(2) function with the
9413 parameters FILENAME, MODE, and PERMS.
9414 .Sp
9415 Returns true on success and \f(CW\*(C`undef\*(C'\fR otherwise.
9416 .Sp
9417 PerlIO layers will be applied to the handle the same way they would in an
9418 \&\f(CW\*(C`open\*(C'\fR call that does not specify layers. That is,
9419 the current value of \f(CW\*(C`${^OPEN}\*(C'\fR as set by the open
9420 pragma in a lexical scope, or the \f(CW\*(C`\-C\*(C'\fR command-line option or \f(CW\*(C`PERL_UNICODE\*(C'\fR
9421 environment variable in the main program scope, falling back to the platform
9422 defaults as described in "Defaults and how to override them" in PerlIO. If you
9423 want to remove any layers that may transform the byte stream, use
9424 \&\f(CW\*(C`binmode\*(C'\fR after opening it.
9425 .Sp
9426 The possible values and flag bits of the MODE parameter are
9427 system-dependent; they are available via the standard module
9428 \&\f(CW\*(C`Fcntl\*(C'\fR. See the documentation of your operating system's
9429 \&\fBopen\fR\|(2) syscall to see
9430 which values and flag bits are available. You may combine several flags
9431 using the \f(CW\*(C`|\*(C'\fR\-operator.
9432 .Sp
9433 Some of the most common values are \f(CW\*(C`O_RDONLY\*(C'\fR for opening the file in
9434 read-only mode, \f(CW\*(C`O_WRONLY\*(C'\fR for opening the file in write-only mode,
9435 and \f(CW\*(C`O_RDWR\*(C'\fR for opening the file in read-write mode.
9436 .IX Xref "O_RDONLY O_RDWR O_WRONLY"
9437 .Sp
9438 For historical reasons, some values work on almost every system
9439 supported by Perl: 0 means read-only, 1 means write-only, and 2
9440 means read/write. We know that these values do \fInot\fR work under
9441 OS/390; you probably don't want to use them in new code.
9442 .Sp
9443 If the file named by FILENAME does not exist and the
9444 \&\f(CW\*(C`open\*(C'\fR call creates
9445 it (typically because MODE includes the \f(CW\*(C`O_CREAT\*(C'\fR flag), then the value of
9446 PERMS specifies the permissions of the newly created file. If you omit
9447 the PERMS argument to \f(CW\*(C`sysopen\*(C'\fR,
9448 Perl uses the octal value \f(CW0666\fR.
9449 These permission values need to be in octal, and are modified by your
9450 process's current \f(CW\*(C`umask\*(C'\fR.
9451 .IX Xref "O_CREAT"
9452 .Sp
9453 In many systems the \f(CW\*(C`O_EXCL\*(C'\fR flag is available for opening files in
9454 exclusive mode. This is \fBnot\fR locking: exclusiveness means here that
9455 if the file already exists,
9456 \&\f(CW\*(C`sysopen\*(C'\fR fails. \f(CW\*(C`O_EXCL\*(C'\fR may
9457 not work
9458 on network filesystems, and has no effect unless the \f(CW\*(C`O_CREAT\*(C'\fR flag
9459 is set as well. Setting \f(CW\*(C`O_CREAT|O_EXCL\*(C'\fR prevents the file from
9460 being opened if it is a symbolic link. It does not protect against
9461 symbolic links in the file's path.
9462 .IX Xref "O_EXCL"
9463 .Sp
9464 Sometimes you may want to truncate an already-existing file. This
9465 can be done using the \f(CW\*(C`O_TRUNC\*(C'\fR flag. The behavior of
9466 \&\f(CW\*(C`O_TRUNC\*(C'\fR with \f(CW\*(C`O_RDONLY\*(C'\fR is undefined.
9467 .IX Xref "O_TRUNC"
9468 .Sp
9469 You should seldom if ever use \f(CW0644\fR as argument to
9470 \&\f(CW\*(C`sysopen\*(C'\fR, because
9471 that takes away the user's option to have a more permissive umask.
9472 Better to omit it. See \f(CW\*(C`umask\*(C'\fR for more on this.
9473 .Sp
9474 This function has no direct relation to the usage of
9475 \&\f(CW\*(C`sysread\*(C'\fR,
9476 \&\f(CW\*(C`syswrite\*(C'\fR,
9477 or \f(CW\*(C`sysseek\*(C'\fR. A handle opened with
9478 this function can be used with buffered IO just as one opened with
9479 \&\f(CW\*(C`open\*(C'\fR can be used with unbuffered IO.
9480 .Sp
9481 Note that under Perls older than 5.8.0,
9482 \&\f(CW\*(C`sysopen\*(C'\fR depends on the
9483 \&\fBfdopen\fR\|(3) C library function. On many Unix systems, \fBfdopen\fR\|(3) is known
9484 to fail when file descriptors exceed a certain value, typically 255. If
9485 you need more file descriptors than that, consider using the
9486 \&\f(CW\*(C`POSIX::open\*(C'\fR function. For Perls 5.8.0 and later,
9487 PerlIO is (most often) the default.
9488 .Sp
9489 See perlopentut for a kinder, gentler explanation of opening files.
9490 .Sp
9491 Portability issues: "sysopen" in perlport.
9492 .IP "sysread FILEHANDLE,SCALAR,LENGTH,OFFSET" 4
9493 .IX Xref "sysread"
9494 .IX Item "sysread FILEHANDLE,SCALAR,LENGTH,OFFSET"
9495 .PD 0
9496 .IP "sysread FILEHANDLE,SCALAR,LENGTH" 4
9497 .IX Item "sysread FILEHANDLE,SCALAR,LENGTH"
9498 .PD
9499 Attempts to read LENGTH bytes of data into variable SCALAR from the
9500 specified FILEHANDLE, using \fBread\fR\|(2). It bypasses any PerlIO layers
9501 including buffered IO (but is affected by the presence of the \f(CW\*(C`:utf8\*(C'\fR
9502 layer as described later), so mixing this with other kinds of reads,
9503 \&\f(CW\*(C`print\*(C'\fR, \f(CW\*(C`write\*(C'\fR,
9504 \&\f(CW\*(C`seek\*(C'\fR,
9505 \&\f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR can cause
9506 confusion because the
9507 \&\f(CW\*(C`:perlio\*(C'\fR or \f(CW\*(C`:crlf\*(C'\fR layers usually buffer data. Returns the number of
9508 bytes actually read, \f(CW0\fR at end of file, or undef if there was an
9509 error (in the latter case \f(CW$!\fR is also set). SCALAR will
9510 be grown or
9511 shrunk so that the last byte actually read is the last byte of the
9512 scalar after the read.
9513 .Sp
9514 An OFFSET may be specified to place the read data at some place in the
9515 string other than the beginning. A negative OFFSET specifies
9516 placement at that many characters counting backwards from the end of
9517 the string. A positive OFFSET greater than the length of SCALAR
9518 results in the string being padded to the required size with \f(CW"\e0"\fR
9519 bytes before the result of the read is appended.
9520 .Sp
9521 There is no \fBsyseof()\fR function, which is ok, since
9522 \&\f(CW\*(C`eof\*(C'\fR doesn't work well on device files (like ttys)
9523 anyway. Use \f(CW\*(C`sysread\*(C'\fR and
9524 check for a return value of 0 to decide whether you're done.
9525 .Sp
9526 Note that if the filehandle has been marked as \f(CW\*(C`:utf8\*(C'\fR, \f(CW\*(C`sysread\*(C'\fR will
9527 throw an exception. The \f(CW:encoding(...)\fR layer implicitly
9528 introduces the \f(CW\*(C`:utf8\*(C'\fR layer. See
9529 \&\f(CW\*(C`binmode\*(C'\fR,
9530 \&\f(CW\*(C`open\*(C'\fR, and the open pragma.
9531 .IP "sysseek FILEHANDLE,POSITION,WHENCE" 4
9532 .IX Xref "sysseek lseek"
9533 .IX Item "sysseek FILEHANDLE,POSITION,WHENCE"
9534 Sets FILEHANDLE's system position \fIin bytes\fR using \fBlseek\fR\|(2). FILEHANDLE may
9535 be an expression whose value gives the name of the filehandle. The values
9536 for WHENCE are \f(CW0\fR to set the new position to POSITION; \f(CW1\fR to set it
9537 to the current position plus POSITION; and \f(CW2\fR to set it to EOF plus
9538 POSITION, typically negative.
9539 .Sp
9540 Note the emphasis on bytes: even if the filehandle has been set to operate
9541 on characters (for example using the \f(CW:encoding(UTF\-8)\fR I/O layer), the
9542 \&\f(CW\*(C`seek\*(C'\fR,
9543 \&\f(CW\*(C`tell\*(C'\fR, and
9544 \&\f(CW\*(C`sysseek\*(C'\fR
9545 family of functions use byte offsets, not character offsets,
9546 because seeking to a character offset would be very slow in a UTF\-8 file.
9547 .Sp
9548 \&\f(CW\*(C`sysseek\*(C'\fR bypasses normal
9549 buffered IO, so mixing it with reads other than
9550 \&\f(CW\*(C`sysread\*(C'\fR (for example
9551 \&\f(CW\*(C`readline\*(C'\fR or
9552 \&\f(CW\*(C`read\*(C'\fR),
9553 \&\f(CW\*(C`print\*(C'\fR, \f(CW\*(C`write\*(C'\fR,
9554 \&\f(CW\*(C`seek\*(C'\fR,
9555 \&\f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR may cause
9556 confusion.
9557 .Sp
9558 For WHENCE, you may also use the constants \f(CW\*(C`SEEK_SET\*(C'\fR, \f(CW\*(C`SEEK_CUR\*(C'\fR,
9559 and \f(CW\*(C`SEEK_END\*(C'\fR (start of the file, current position, end of the file)
9560 from the Fcntl module. Use of the constants is also more portable
9561 than relying on 0, 1, and 2. For example to define a "systell" function:
9562 .Sp
9563 .Vb 2
9564 \& use Fcntl \*(AqSEEK_CUR\*(Aq;
9565 \& sub systell { sysseek($_[0], 0, SEEK_CUR) }
9566 .Ve
9567 .Sp
9568 Returns the new position, or the undefined value on failure. A position
9569 of zero is returned as the string \f(CW"0 but true"\fR; thus
9570 \&\f(CW\*(C`sysseek\*(C'\fR returns
9571 true on success and false on failure, yet you can still easily determine
9572 the new position.
9573 .IP "system LIST" 4
9574 .IX Xref "system shell"
9575 .IX Item "system LIST"
9576 .PD 0
9577 .IP "system PROGRAM LIST" 4
9578 .IX Item "system PROGRAM LIST"
9579 .PD
9580 Does exactly the same thing as \f(CW\*(C`exec\*(C'\fR, except that a fork is
9581 done first and the parent process waits for the child process to
9582 exit. Note that argument processing varies depending on the
9583 number of arguments. If there is more than one argument in LIST,
9584 or if LIST is an array with more than one value, starts the program
9585 given by the first element of the list with arguments given by the
9586 rest of the list. If there is only one scalar argument, the argument
9587 is checked for shell metacharacters, and if there are any, the
9588 entire argument is passed to the system's command shell for parsing
9589 (this is \f(CW\*(C`/bin/sh \-c\*(C'\fR on Unix platforms, but varies on other
9590 platforms). If there are no shell metacharacters in the argument,
9591 it is split into words and passed directly to \f(CW\*(C`execvp\*(C'\fR, which is
9592 more efficient. On Windows, only the \f(CW\*(C`system PROGRAM LIST\*(C'\fR syntax will
9593 reliably avoid using the shell; \f(CW\*(C`system LIST\*(C'\fR, even with more than one
9594 element, will fall back to the shell if the first spawn fails.
9595 .Sp
9596 Perl will attempt to flush all files opened for
9597 output before any operation that may do a fork, but this may not be
9598 supported on some platforms (see perlport). To be safe, you may need
9599 to set \f(CW$|\fR (\f(CW$AUTOFLUSH\fR in English)
9600 or call the \f(CW\*(C`autoflush\*(C'\fR method of \f(CW\*(C`IO::Handle\*(C'\fR
9601 on any open handles.
9602 .Sp
9603 The return value is the exit status of the program as returned by the
9604 \&\f(CW\*(C`wait\*(C'\fR call. To get the actual exit value, shift right by
9605 eight (see below). See also \f(CW\*(C`exec\*(C'\fR. This is \fInot\fR what
9606 you want to use to capture the output from a command; for that you
9607 should use merely backticks or
9608 \&\f(CW\*(C`qx//\*(C'\fR, as described in
9609 "`STRING`" in perlop. Return value of \-1 indicates a failure to start
9610 the program or an error of the \fBwait\fR\|(2) system call (inspect
9611 \&\f(CW$!\fR for the reason).
9612 .Sp
9613 If you'd like to make \f(CW\*(C`system\*(C'\fR (and many other bits of
9614 Perl) die on error, have a look at the autodie pragma.
9615 .Sp
9616 Like \f(CW\*(C`exec\*(C'\fR, \f(CW\*(C`system\*(C'\fR allows you to lie
9617 to a program about its name if you use the \f(CW\*(C`system PROGRAM LIST\*(C'\fR
9618 syntax. Again, see \f(CW\*(C`exec\*(C'\fR.
9619 .Sp
9620 Since \f(CW\*(C`SIGINT\*(C'\fR and \f(CW\*(C`SIGQUIT\*(C'\fR are ignored during the execution of
9621 \&\f(CW\*(C`system\*(C'\fR, if you expect your program to terminate on
9622 receipt of these signals you will need to arrange to do so yourself
9623 based on the return value.
9624 .Sp
9625 .Vb 3
9626 \& my @args = ("command", "arg1", "arg2");
9627 \& system(@args) == 0
9628 \& or die "system @args failed: $?";
9629 .Ve
9630 .Sp
9631 If you'd like to manually inspect \f(CW\*(C`system\*(C'\fR's failure,
9632 you can check all possible failure modes by inspecting
9633 \&\f(CW$?\fR like this:
9634 .Sp
9635 .Vb 10
9636 \& if ($? == \-1) {
9637 \& print "failed to execute: $!\en";
9638 \& }
9639 \& elsif ($? & 127) {
9640 \& printf "child died with signal %d, %s coredump\en",
9641 \& ($? & 127), ($? & 128) ? \*(Aqwith\*(Aq : \*(Aqwithout\*(Aq;
9642 \& }
9643 \& else {
9644 \& printf "child exited with value %d\en", $? >> 8;
9645 \& }
9646 .Ve
9647 .Sp
9648 Alternatively, you may inspect the value of
9649 \&\f(CW\*(C`${^CHILD_ERROR_NATIVE}\*(C'\fR with the
9650 \&\f(CW\*(C`W*()\*(C'\fR calls from the POSIX module.
9651 .Sp
9652 When \f(CW\*(C`system\*(C'\fR's arguments are executed indirectly by
9653 the shell, results and return codes are subject to its quirks.
9654 See "`STRING`" in perlop and \f(CW\*(C`exec\*(C'\fR for details.
9655 .Sp
9656 Since \f(CW\*(C`system\*(C'\fR does a \f(CW\*(C`fork\*(C'\fR and
9657 \&\f(CW\*(C`wait\*(C'\fR it may affect a \f(CW\*(C`SIGCHLD\*(C'\fR handler. See perlipc for
9658 details.
9659 .Sp
9660 Portability issues: "system" in perlport.
9661 .IP "syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET" 4
9662 .IX Xref "syswrite"
9663 .IX Item "syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET"
9664 .PD 0
9665 .IP "syswrite FILEHANDLE,SCALAR,LENGTH" 4
9666 .IX Item "syswrite FILEHANDLE,SCALAR,LENGTH"
9667 .IP "syswrite FILEHANDLE,SCALAR" 4
9668 .IX Item "syswrite FILEHANDLE,SCALAR"
9669 .PD
9670 Attempts to write LENGTH bytes of data from variable SCALAR to the
9671 specified FILEHANDLE, using \fBwrite\fR\|(2). If LENGTH is
9672 not specified, writes whole SCALAR. It bypasses any PerlIO layers
9673 including buffered IO (but is affected by the presence of the \f(CW\*(C`:utf8\*(C'\fR
9674 layer as described later), so
9675 mixing this with reads (other than \f(CW\*(C`sysread)\*(C'\fR),
9676 \&\f(CW\*(C`print\*(C'\fR, \f(CW\*(C`write\*(C'\fR,
9677 \&\f(CW\*(C`seek\*(C'\fR,
9678 \&\f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR may cause
9679 confusion because the \f(CW\*(C`:perlio\*(C'\fR and \f(CW\*(C`:crlf\*(C'\fR layers usually buffer data.
9680 Returns the number of bytes actually written, or \f(CW\*(C`undef\*(C'\fR
9681 if there was an error (in this case the errno variable
9682 \&\f(CW$!\fR is also set). If the LENGTH is greater than the
9683 data available in the SCALAR after the OFFSET, only as much data as is
9684 available will be written.
9685 .Sp
9686 An OFFSET may be specified to write the data from some part of the
9687 string other than the beginning. A negative OFFSET specifies writing
9688 that many characters counting backwards from the end of the string.
9689 If SCALAR is of length zero, you can only use an OFFSET of 0.
9690 .Sp
9691 \&\fBWARNING\fR: If the filehandle is marked \f(CW\*(C`:utf8\*(C'\fR, \f(CW\*(C`syswrite\*(C'\fR will raise an exception.
9692 The \f(CW:encoding(...)\fR layer implicitly introduces the \f(CW\*(C`:utf8\*(C'\fR layer.
9693 Alternately, if the handle is not marked with an encoding but you
9694 attempt to write characters with code points over 255, raises an exception.
9695 See \f(CW\*(C`binmode\*(C'\fR,
9696 \&\f(CW\*(C`open\*(C'\fR, and the open pragma.
9697 .IP "tell FILEHANDLE" 4
9698 .IX Xref "tell"
9699 .IX Item "tell FILEHANDLE"
9700 .PD 0
9701 .IP tell 4
9702 .IX Item "tell"
9703 .PD
9704 Returns the current position \fIin bytes\fR for FILEHANDLE, or \-1 on
9705 error. FILEHANDLE may be an expression whose value gives the name of
9706 the actual filehandle. If FILEHANDLE is omitted, assumes the file
9707 last read.
9708 .Sp
9709 Note the emphasis on bytes: even if the filehandle has been set to operate
9710 on characters (for example using the \f(CW:encoding(UTF\-8)\fR I/O layer), the
9711 \&\f(CW\*(C`seek\*(C'\fR,
9712 \&\f(CW\*(C`tell\*(C'\fR, and
9713 \&\f(CW\*(C`sysseek\*(C'\fR
9714 family of functions use byte offsets, not character offsets,
9715 because seeking to a character offset would be very slow in a UTF\-8 file.
9716 .Sp
9717 The return value of \f(CW\*(C`tell\*(C'\fR for the standard streams
9718 like the STDIN depends on the operating system: it may return \-1 or
9719 something else. \f(CW\*(C`tell\*(C'\fR on pipes, fifos, and
9720 sockets usually returns \-1.
9721 .Sp
9722 There is no \f(CW\*(C`systell\*(C'\fR function. Use
9723 \&\f(CW\*(C`sysseek($fh, 0, 1)\*(C'\fR for that.
9724 .Sp
9725 Do not use \f(CW\*(C`tell\*(C'\fR (or other buffered I/O
9726 operations) on a filehandle that has been manipulated by
9727 \&\f(CW\*(C`sysread\*(C'\fR,
9728 \&\f(CW\*(C`syswrite\*(C'\fR, or
9729 \&\f(CW\*(C`sysseek\*(C'\fR. Those functions
9730 ignore the buffering, while \f(CW\*(C`tell\*(C'\fR does not.
9731 .IP "telldir DIRHANDLE" 4
9732 .IX Xref "telldir"
9733 .IX Item "telldir DIRHANDLE"
9734 Returns the current position of the \f(CW\*(C`readdir\*(C'\fR
9735 routines on DIRHANDLE. Value may be given to
9736 \&\f(CW\*(C`seekdir\*(C'\fR to access a particular location in
9737 a directory. \f(CW\*(C`telldir\*(C'\fR has the same caveats
9738 about possible directory compaction as the corresponding system library
9739 routine.
9740 .IP "tie VARIABLE,CLASSNAME,LIST" 4
9741 .IX Xref "tie"
9742 .IX Item "tie VARIABLE,CLASSNAME,LIST"
9743 This function binds a variable to a package class that will provide the
9744 implementation for the variable. VARIABLE is the name of the variable
9745 to be enchanted. CLASSNAME is the name of a class implementing objects
9746 of correct type. Any additional arguments are passed to the
9747 appropriate constructor
9748 method of the class (meaning \f(CW\*(C`TIESCALAR\*(C'\fR, \f(CW\*(C`TIEHANDLE\*(C'\fR, \f(CW\*(C`TIEARRAY\*(C'\fR,
9749 or \f(CW\*(C`TIEHASH\*(C'\fR). Typically these are arguments such as might be passed
9750 to the \fBdbm_open\fR\|(3) function of C. The object returned by the
9751 constructor is also returned by the
9752 \&\f(CW\*(C`tie\*(C'\fR function, which would be useful
9753 if you want to access other methods in CLASSNAME.
9754 .Sp
9755 Note that functions such as \f(CW\*(C`keys\*(C'\fR and
9756 \&\f(CW\*(C`values\*(C'\fR may return huge lists when used on large
9757 objects, like DBM files. You may prefer to use the \f(CW\*(C`each\*(C'\fR function to iterate over such. Example:
9758 .Sp
9759 .Vb 6
9760 \& # print out history file offsets
9761 \& use NDBM_File;
9762 \& tie(my %HIST, \*(AqNDBM_File\*(Aq, \*(Aq/usr/lib/news/history\*(Aq, 1, 0);
9763 \& while (my ($key,$val) = each %HIST) {
9764 \& print $key, \*(Aq = \*(Aq, unpack(\*(AqL\*(Aq, $val), "\en";
9765 \& }
9766 .Ve
9767 .Sp
9768 A class implementing a hash should have the following methods:
9769 .Sp
9770 .Vb 11
9771 \& TIEHASH classname, LIST
9772 \& FETCH this, key
9773 \& STORE this, key, value
9774 \& DELETE this, key
9775 \& CLEAR this
9776 \& EXISTS this, key
9777 \& FIRSTKEY this
9778 \& NEXTKEY this, lastkey
9779 \& SCALAR this
9780 \& DESTROY this
9781 \& UNTIE this
9782 .Ve
9783 .Sp
9784 A class implementing an ordinary array should have the following methods:
9785 .Sp
9786 .Vb 10
9787 \& TIEARRAY classname, LIST
9788 \& FETCH this, key
9789 \& STORE this, key, value
9790 \& FETCHSIZE this
9791 \& STORESIZE this, count
9792 \& CLEAR this
9793 \& PUSH this, LIST
9794 \& POP this
9795 \& SHIFT this
9796 \& UNSHIFT this, LIST
9797 \& SPLICE this, offset, length, LIST
9798 \& EXTEND this, count
9799 \& DELETE this, key
9800 \& EXISTS this, key
9801 \& DESTROY this
9802 \& UNTIE this
9803 .Ve
9804 .Sp
9805 A class implementing a filehandle should have the following methods:
9806 .Sp
9807 .Vb 10
9808 \& TIEHANDLE classname, LIST
9809 \& READ this, scalar, length, offset
9810 \& READLINE this
9811 \& GETC this
9812 \& WRITE this, scalar, length, offset
9813 \& PRINT this, LIST
9814 \& PRINTF this, format, LIST
9815 \& BINMODE this
9816 \& EOF this
9817 \& FILENO this
9818 \& SEEK this, position, whence
9819 \& TELL this
9820 \& OPEN this, mode, LIST
9821 \& CLOSE this
9822 \& DESTROY this
9823 \& UNTIE this
9824 .Ve
9825 .Sp
9826 A class implementing a scalar should have the following methods:
9827 .Sp
9828 .Vb 5
9829 \& TIESCALAR classname, LIST
9830 \& FETCH this,
9831 \& STORE this, value
9832 \& DESTROY this
9833 \& UNTIE this
9834 .Ve
9835 .Sp
9836 Not all methods indicated above need be implemented. See perltie,
9837 Tie::Hash, Tie::Array, Tie::Scalar, and Tie::Handle.
9838 .Sp
9839 Unlike \f(CW\*(C`dbmopen\*(C'\fR, the
9840 \&\f(CW\*(C`tie\*(C'\fR function will not
9841 \&\f(CW\*(C`use\*(C'\fR or \f(CW\*(C`require\*(C'\fR a
9842 module for you; you need to do that explicitly yourself. See DB_File
9843 or the Config module for interesting
9844 \&\f(CW\*(C`tie\*(C'\fR implementations.
9845 .Sp
9846 For further details see perltie, \f(CW\*(C`tied\*(C'\fR.
9847 .IP "tied VARIABLE" 4
9848 .IX Xref "tied"
9849 .IX Item "tied VARIABLE"
9850 Returns a reference to the object underlying VARIABLE (the same value
9851 that was originally returned by the
9852 \&\f(CW\*(C`tie\*(C'\fR call that bound the variable
9853 to a package.) Returns the undefined value if VARIABLE isn't tied to a
9854 package.
9855 .IP time 4
9856 .IX Xref "time epoch"
9857 .IX Item "time"
9858 Returns the number of non-leap seconds since whatever time the system
9859 considers to be the epoch, suitable for feeding to
9860 \&\f(CW\*(C`gmtime\*(C'\fR and \f(CW\*(C`localtime\*(C'\fR. On most
9861 systems the epoch is 00:00:00 UTC, January 1, 1970;
9862 a prominent exception being Mac OS Classic which uses 00:00:00, January 1,
9863 1904 in the current local time zone for its epoch.
9864 .Sp
9865 For measuring time in better granularity than one second, use the
9866 Time::HiRes module from Perl 5.8 onwards (or from CPAN before then), or,
9867 if you have \fBgettimeofday\fR\|(2), you may be able to use the
9868 \&\f(CW\*(C`syscall\*(C'\fR interface of Perl. See perlfaq8
9869 for details.
9870 .Sp
9871 For date and time processing look at the many related modules on CPAN.
9872 For a comprehensive date and time representation look at the
9873 DateTime module.
9874 .IP times 4
9875 .IX Xref "times"
9876 .IX Item "times"
9877 Returns a four-element list giving the user and system times in
9878 seconds for this process and any exited children of this process.
9879 .Sp
9880 .Vb 1
9881 \& my ($user,$system,$cuser,$csystem) = times;
9882 .Ve
9883 .Sp
9884 In scalar context, \f(CW\*(C`times\*(C'\fR returns \f(CW$user\fR.
9885 .Sp
9886 Children's times are only included for terminated children.
9887 .Sp
9888 Portability issues: "times" in perlport.
9889 .IP tr/// 4
9890 .IX Item "tr///"
9891 The transliteration operator. Same as
9892 \&\f(CW\*(C`y///\*(C'\fR. See
9893 "Quote-Like Operators" in perlop.
9894 .IP "truncate FILEHANDLE,LENGTH" 4
9895 .IX Xref "truncate"
9896 .IX Item "truncate FILEHANDLE,LENGTH"
9897 .PD 0
9898 .IP "truncate EXPR,LENGTH" 4
9899 .IX Item "truncate EXPR,LENGTH"
9900 .PD
9901 Truncates the file opened on FILEHANDLE, or named by EXPR, to the
9902 specified length. Raises an exception if truncate isn't implemented
9903 on your system. Returns true if successful, \f(CW\*(C`undef\*(C'\fR on
9904 error.
9905 .Sp
9906 The behavior is undefined if LENGTH is greater than the length of the
9907 file.
9908 .Sp
9909 The position in the file of FILEHANDLE is left unchanged. You may want to
9910 call seek before writing to the
9911 file.
9912 .Sp
9913 Portability issues: "truncate" in perlport.
9914 .IP "uc EXPR" 4
9915 .IX Xref "uc uppercase toupper"
9916 .IX Item "uc EXPR"
9917 .PD 0
9918 .IP uc 4
9919 .IX Item "uc"
9920 .PD
9921 Returns an uppercased version of EXPR. If EXPR is omitted, uses
9922 \&\f(CW$_\fR.
9923 .Sp
9924 .Vb 1
9925 \& my $str = uc("Perl is GREAT"); # "PERL IS GREAT"
9926 .Ve
9927 .Sp
9928 This function behaves the same way under various pragmas, such as in a locale,
9929 as \f(CW\*(C`lc\*(C'\fR does.
9930 .Sp
9931 If you want titlecase mapping on initial letters see
9932 \&\f(CW\*(C`ucfirst\*(C'\fR instead.
9933 .Sp
9934 \&\fBNote:\fR This is the internal function implementing the
9935 \&\f(CW\*(C`\eU\*(C'\fR escape in double-quoted
9936 strings.
9937 .Sp
9938 .Vb 1
9939 \& my $str = "Perl is \eUgreat\eE"; # "Perl is GREAT"
9940 .Ve
9941 .IP "ucfirst EXPR" 4
9942 .IX Xref "ucfirst uppercase"
9943 .IX Item "ucfirst EXPR"
9944 .PD 0
9945 .IP ucfirst 4
9946 .IX Item "ucfirst"
9947 .PD
9948 Returns the value of EXPR with the \fBfirst\fR character in uppercase
9949 (Unicode calls this titlecase). If EXPR is omitted, \f(CW\*(C`ucfirst\*(C'\fR uses \f(CW$_\fR.
9950 .Sp
9951 .Vb 1
9952 \& my $str = ucfirst("hello world!"); # "Hello world!"
9953 .Ve
9954 .Sp
9955 This function behaves the same way under various pragmas, such as in a locale,
9956 as \f(CW\*(C`lc\*(C'\fR does.
9957 .Sp
9958 \&\fBNote:\fR This is the internal function implementing the \f(CW\*(C`\eu\*(C'\fR escape in
9959 double-quoted strings.
9960 .Sp
9961 .Vb 1
9962 \& my $str = "\euperl\eE is great"; # "Perl is great"
9963 .Ve
9964 .IP "umask EXPR" 4
9965 .IX Xref "umask"
9966 .IX Item "umask EXPR"
9967 .PD 0
9968 .IP umask 4
9969 .IX Item "umask"
9970 .PD
9971 Sets the umask for the process to EXPR and returns the previous value.
9972 If EXPR is omitted, merely returns the current umask.
9973 .Sp
9974 The Unix permission \f(CW\*(C`rwxr\-x\-\-\-\*(C'\fR is represented as three sets of three
9975 bits, or three octal digits: \f(CW0750\fR (the leading 0 indicates octal
9976 and isn't one of the digits). The \f(CW\*(C`umask\*(C'\fR value is such
9977 a number representing disabled permissions bits. The permission (or
9978 "mode") values you pass \f(CW\*(C`mkdir\*(C'\fR or
9979 \&\f(CW\*(C`sysopen\*(C'\fR are modified by your
9980 umask, so even if you tell
9981 \&\f(CW\*(C`sysopen\*(C'\fR to create a file with
9982 permissions \f(CW0777\fR, if your umask is \f(CW0022\fR, then the file will
9983 actually be created with permissions \f(CW0755\fR. If your
9984 \&\f(CW\*(C`umask\*(C'\fR were \f(CW0027\fR (group can't write; others can't
9985 read, write, or execute), then passing
9986 \&\f(CW\*(C`sysopen\*(C'\fR \f(CW0666\fR would create a
9987 file with mode \f(CW0640\fR (because \f(CW\*(C`0666 &~ 027\*(C'\fR is \f(CW0640\fR).
9988 .Sp
9989 Here's some advice: supply a creation mode of \f(CW0666\fR for regular
9990 files (in \f(CW\*(C`sysopen\*(C'\fR) and one of
9991 \&\f(CW0777\fR for directories (in \f(CW\*(C`mkdir\*(C'\fR) and
9992 executable files. This gives users the freedom of
9993 choice: if they want protected files, they might choose process umasks
9994 of \f(CW022\fR, \f(CW027\fR, or even the particularly antisocial mask of \f(CW077\fR.
9995 Programs should rarely if ever make policy decisions better left to
9996 the user. The exception to this is when writing files that should be
9997 kept private: mail files, web browser cookies, \fI.rhosts\fR files, and
9998 so on.
9999 .Sp
10000 If \fBumask\fR\|(2) is not implemented on your system and you are trying to
10001 restrict access for \fIyourself\fR (i.e., \f(CW\*(C`(EXPR & 0700) > 0\*(C'\fR),
10002 raises an exception. If \fBumask\fR\|(2) is not implemented and you are
10003 not trying to restrict access for yourself, returns
10004 \&\f(CW\*(C`undef\*(C'\fR.
10005 .Sp
10006 Remember that a umask is a number, usually given in octal; it is \fInot\fR a
10007 string of octal digits. See also \f(CW\*(C`oct\*(C'\fR, if all you have
10008 is a string.
10009 .Sp
10010 Portability issues: "umask" in perlport.
10011 .IP "undef EXPR" 4
10012 .IX Xref "undef undefine"
10013 .IX Item "undef EXPR"
10014 .PD 0
10015 .IP undef 4
10016 .IX Item "undef"
10017 .PD
10018 Undefines the value of EXPR, which must be an lvalue. Use only on a
10019 scalar value, an array (using \f(CW\*(C`@\*(C'\fR), a hash (using \f(CW\*(C`%\*(C'\fR), a subroutine
10020 (using \f(CW\*(C`&\*(C'\fR), or a typeglob (using \f(CW\*(C`*\*(C'\fR). Saying \f(CW\*(C`undef $hash{$key}\*(C'\fR
10021 will probably not do what you expect on most predefined variables or
10022 DBM list values, so don't do that; see \f(CW\*(C`delete\*(C'\fR.
10023 Always returns the undefined value.
10024 You can omit the EXPR, in which case nothing is
10025 undefined, but you still get an undefined value that you could, for
10026 instance, return from a subroutine, assign to a variable, or pass as a
10027 parameter. Examples:
10028 .Sp
10029 .Vb 9
10030 \& undef $foo;
10031 \& undef $bar{\*(Aqblurfl\*(Aq}; # Compare to: delete $bar{\*(Aqblurfl\*(Aq};
10032 \& undef @ary;
10033 \& undef %hash;
10034 \& undef &mysub;
10035 \& undef *xyz; # destroys $xyz, @xyz, %xyz, &xyz, etc.
10036 \& return (wantarray ? (undef, $errmsg) : undef) if $they_blew_it;
10037 \& select undef, undef, undef, 0.25;
10038 \& my ($x, $y, undef, $z) = foo(); # Ignore third value returned
10039 .Ve
10040 .Sp
10041 Note that this is a unary operator, not a list operator.
10042 .IP "unlink LIST" 4
10043 .IX Xref "unlink delete remove rm del"
10044 .IX Item "unlink LIST"
10045 .PD 0
10046 .IP unlink 4
10047 .IX Item "unlink"
10048 .PD
10049 Deletes a list of files. On success, it returns the number of files
10050 it successfully deleted. On failure, it returns false and sets
10051 \&\f(CW$!\fR (errno):
10052 .Sp
10053 .Vb 3
10054 \& my $unlinked = unlink \*(Aqa\*(Aq, \*(Aqb\*(Aq, \*(Aqc\*(Aq;
10055 \& unlink @goners;
10056 \& unlink glob "*.bak";
10057 .Ve
10058 .Sp
10059 On error, \f(CW\*(C`unlink\*(C'\fR will not tell you which files it
10060 could not remove.
10061 If you want to know which files you could not remove, try them one
10062 at a time:
10063 .Sp
10064 .Vb 3
10065 \& foreach my $file ( @goners ) {
10066 \& unlink $file or warn "Could not unlink $file: $!";
10067 \& }
10068 .Ve
10069 .Sp
10070 Note: \f(CW\*(C`unlink\*(C'\fR will not attempt to delete directories
10071 unless you are
10072 superuser and the \fB\-U\fR flag is supplied to Perl. Even if these
10073 conditions are met, be warned that unlinking a directory can inflict
10074 damage on your filesystem. Finally, using \f(CW\*(C`unlink\*(C'\fR on
10075 directories is not supported on many operating systems. Use
10076 \&\f(CW\*(C`rmdir\*(C'\fR instead.
10077 .Sp
10078 If LIST is omitted, \f(CW\*(C`unlink\*(C'\fR uses \f(CW$_\fR.
10079 .IP "unpack TEMPLATE,EXPR" 4
10080 .IX Xref "unpack"
10081 .IX Item "unpack TEMPLATE,EXPR"
10082 .PD 0
10083 .IP "unpack TEMPLATE" 4
10084 .IX Item "unpack TEMPLATE"
10085 .PD
10086 \&\f(CW\*(C`unpack\*(C'\fR does the reverse of
10087 \&\f(CW\*(C`pack\*(C'\fR: it takes a string
10088 and expands it out into a list of values.
10089 (In scalar context, it returns merely the first value produced.)
10090 .Sp
10091 If EXPR is omitted, unpacks the \f(CW$_\fR string.
10092 See perlpacktut for an introduction to this function.
10093 .Sp
10094 The string is broken into chunks described by the TEMPLATE. Each chunk
10095 is converted separately to a value. Typically, either the string is a result
10096 of \f(CW\*(C`pack\*(C'\fR, or the characters of the string
10097 represent a C structure of some kind.
10098 .Sp
10099 The TEMPLATE has the same format as in the
10100 \&\f(CW\*(C`pack\*(C'\fR function.
10101 Here's a subroutine that does substring:
10102 .Sp
10103 .Vb 4
10104 \& sub substr {
10105 \& my ($what, $where, $howmuch) = @_;
10106 \& unpack("x$where a$howmuch", $what);
10107 \& }
10108 .Ve
10109 .Sp
10110 and then there's
10111 .Sp
10112 .Vb 1
10113 \& sub ordinal { unpack("W",$_[0]); } # same as ord()
10114 .Ve
10115 .Sp
10116 In addition to fields allowed in \f(CW\*(C`pack\*(C'\fR, you may
10117 prefix a field with a %<number> to indicate that
10118 you want a <number>\-bit checksum of the items instead of the items
10119 themselves. Default is a 16\-bit checksum. The checksum is calculated by
10120 summing numeric values of expanded values (for string fields the sum of
10121 \&\f(CWord($char)\fR is taken; for bit fields the sum of zeroes and ones).
10122 .Sp
10123 For example, the following
10124 computes the same number as the System V sum program:
10125 .Sp
10126 .Vb 4
10127 \& my $checksum = do {
10128 \& local $/; # slurp!
10129 \& unpack("%32W*", readline) % 65535;
10130 \& };
10131 .Ve
10132 .Sp
10133 The following efficiently counts the number of set bits in a bit vector:
10134 .Sp
10135 .Vb 1
10136 \& my $setbits = unpack("%32b*", $selectmask);
10137 .Ve
10138 .Sp
10139 The \f(CW\*(C`p\*(C'\fR and \f(CW\*(C`P\*(C'\fR formats should be used with care. Since Perl
10140 has no way of checking whether the value passed to
10141 \&\f(CW\*(C`unpack\*(C'\fR
10142 corresponds to a valid memory location, passing a pointer value that's
10143 not known to be valid is likely to have disastrous consequences.
10144 .Sp
10145 If there are more pack codes or if the repeat count of a field or a group
10146 is larger than what the remainder of the input string allows, the result
10147 is not well defined: the repeat count may be decreased, or
10148 \&\f(CW\*(C`unpack\*(C'\fR may produce empty strings or zeros,
10149 or it may raise an exception.
10150 If the input string is longer than one described by the TEMPLATE,
10151 the remainder of that input string is ignored.
10152 .Sp
10153 See \f(CW\*(C`pack\*(C'\fR for more examples and notes.
10154 .IP "unshift ARRAY,LIST" 4
10155 .IX Xref "unshift"
10156 .IX Item "unshift ARRAY,LIST"
10157 Add one or more elements to the \fBbeginning\fR of an array. This is the
10158 opposite of a \f(CW\*(C`shift\*(C'\fR.
10159 .Sp
10160 .Vb 2
10161 \& my @animals = ("cat");
10162 \& unshift(@animals, "mouse"); # ("mouse", "cat")
10163 \&
10164 \& my @colors = ("red");
10165 \& unshift(@colors, ("blue", "green")); # ("blue", "green", "red")
10166 .Ve
10167 .Sp
10168 Returns the new number of elements in the updated array.
10169 .Sp
10170 .Vb 2
10171 \& # Return value is the number of items in the updated array
10172 \& my $color_count = unshift(@colors, ("yellow", "purple"));
10173 \&
10174 \& say "There are $color_count colors in the updated array";
10175 .Ve
10176 .Sp
10177 Note the LIST is prepended whole, not one element at a time, so the
10178 prepended elements stay in the same order. Use
10179 \&\f(CW\*(C`reverse\*(C'\fR to do the reverse.
10180 .Sp
10181 Starting with Perl 5.14, an experimental feature allowed
10182 \&\f(CW\*(C`unshift\*(C'\fR to take
10183 a scalar expression. This experiment has been deemed unsuccessful, and was
10184 removed as of Perl 5.24.
10185 .IP "untie VARIABLE" 4
10186 .IX Xref "untie"
10187 .IX Item "untie VARIABLE"
10188 Breaks the binding between a variable and a package.
10189 (See tie.)
10190 Has no effect if the variable is not tied.
10191 .IP "use Module VERSION LIST" 4
10192 .IX Xref "use module import"
10193 .IX Item "use Module VERSION LIST"
10194 .PD 0
10195 .IP "use Module VERSION" 4
10196 .IX Item "use Module VERSION"
10197 .IP "use Module LIST" 4
10198 .IX Item "use Module LIST"
10199 .IP "use Module" 4
10200 .IX Item "use Module"
10201 .PD
10202 Imports some semantics into the current package from the named module,
10203 generally by aliasing certain subroutine or variable names into your
10204 package. It is exactly equivalent to
10205 .Sp
10206 .Vb 1
10207 \& BEGIN { require Module; Module\->import( LIST ); }
10208 .Ve
10209 .Sp
10210 except that Module \fImust\fR be a bareword.
10211 The importation can be made conditional by using the if module.
10212 .Sp
10213 The \f(CW\*(C`BEGIN\*(C'\fR forces the \f(CW\*(C`require\*(C'\fR and
10214 \&\f(CW\*(C`import\*(C'\fR to happen at compile time. The
10215 \&\f(CW\*(C`require\*(C'\fR makes sure the module is loaded into
10216 memory if it hasn't been yet. The \f(CW\*(C`import\*(C'\fR is not a
10217 builtin; it's just an ordinary static method
10218 call into the \f(CW\*(C`Module\*(C'\fR package to tell the module to import the list of
10219 features back into the current package. The module can implement its
10220 \&\f(CW\*(C`import\*(C'\fR method any way it likes, though most modules
10221 just choose to derive their \f(CW\*(C`import\*(C'\fR method via
10222 inheritance from the \f(CW\*(C`Exporter\*(C'\fR class that is defined in the
10223 \&\f(CW\*(C`Exporter\*(C'\fR module. See Exporter. If no
10224 \&\f(CW\*(C`import\*(C'\fR method can be found, then the call is skipped,
10225 even if there is an AUTOLOAD method.
10226 .Sp
10227 If you do not want to call the package's \f(CW\*(C`import\*(C'\fR
10228 method (for instance,
10229 to stop your namespace from being altered), explicitly supply the empty list:
10230 .Sp
10231 .Vb 1
10232 \& use Module ();
10233 .Ve
10234 .Sp
10235 That is exactly equivalent to
10236 .Sp
10237 .Vb 1
10238 \& BEGIN { require Module }
10239 .Ve
10240 .Sp
10241 If the VERSION argument is present between Module and LIST, then the
10242 \&\f(CW\*(C`use\*(C'\fR will call the \f(CW\*(C`VERSION\*(C'\fR method in
10243 class Module with the given version as an argument:
10244 .Sp
10245 .Vb 1
10246 \& use Module 12.34;
10247 .Ve
10248 .Sp
10249 is equivalent to:
10250 .Sp
10251 .Vb 1
10252 \& BEGIN { require Module; Module\->VERSION(12.34) }
10253 .Ve
10254 .Sp
10255 The default \f(CW\*(C`VERSION\*(C'\fR method,
10256 inherited from the \f(CW\*(C`UNIVERSAL\*(C'\fR class, croaks if the given
10257 version is larger than the value of the variable \f(CW$Module::VERSION\fR.
10258 .Sp
10259 The VERSION argument cannot be an arbitrary expression. It only counts
10260 as a VERSION argument if it is a version number literal, starting with
10261 either a digit or \f(CW\*(C`v\*(C'\fR followed by a digit. Anything that doesn't
10262 look like a version literal will be parsed as the start of the LIST.
10263 Nevertheless, many attempts to use an arbitrary expression as a VERSION
10264 argument will appear to work, because Exporter's \f(CW\*(C`import\*(C'\fR method
10265 handles numeric arguments specially, performing version checks rather
10266 than treating them as things to export.
10267 .Sp
10268 Again, there is a distinction between omitting LIST (\f(CW\*(C`import\*(C'\fR called with no arguments) and an explicit empty LIST \f(CW\*(C`()\*(C'\fR
10269 (\f(CW\*(C`import\*(C'\fR not called). Note that there is no comma
10270 after VERSION!
10271 .Sp
10272 Because this is a wide-open interface, pragmas (compiler directives)
10273 are also implemented this way. Some of the currently implemented
10274 pragmas are:
10275 .Sp
10276 .Vb 8
10277 \& use constant;
10278 \& use diagnostics;
10279 \& use integer;
10280 \& use sigtrap qw(SEGV BUS);
10281 \& use strict qw(subs vars refs);
10282 \& use subs qw(afunc blurfl);
10283 \& use warnings qw(all);
10284 \& use sort qw(stable);
10285 .Ve
10286 .Sp
10287 Some of these pseudo-modules import semantics into the current
10288 block scope (like \f(CW\*(C`strict\*(C'\fR or \f(CW\*(C`integer\*(C'\fR, unlike
10289 ordinary modules, which import symbols into the current package (which
10290 are effective through the end of the file).
10291 .Sp
10292 Because \f(CW\*(C`use\*(C'\fR takes effect at compile time,
10293 it doesn't respect the ordinary flow control of the code being compiled.
10294 In particular, putting a \f(CW\*(C`use\*(C'\fR inside the
10295 false branch of a conditional doesn't prevent it
10296 from being processed. If a module or pragma only needs to be loaded
10297 conditionally, this can be done using the if pragma:
10298 .Sp
10299 .Vb 2
10300 \& use if $] < 5.008, "utf8";
10301 \& use if WANT_WARNINGS, warnings => qw(all);
10302 .Ve
10303 .Sp
10304 There's a corresponding \f(CW\*(C`no\*(C'\fR declaration
10305 that unimports meanings imported by \f(CW\*(C`use\*(C'\fR,
10306 i.e., it calls \f(CW\*(C`Module\->unimport(LIST)\*(C'\fR instead of
10307 \&\f(CW\*(C`import\*(C'\fR. It behaves just as \f(CW\*(C`import\*(C'\fR
10308 does with VERSION, an omitted or empty LIST,
10309 or no unimport method being found.
10310 .Sp
10311 .Vb 3
10312 \& no integer;
10313 \& no strict \*(Aqrefs\*(Aq;
10314 \& no warnings;
10315 .Ve
10316 .Sp
10317 See perlmodlib for a list of standard modules and pragmas. See
10318 perlrun for the \f(CW\*(C`\-M\*(C'\fR and \f(CW\*(C`\-m\*(C'\fR command-line
10319 options to Perl that give \f(CW\*(C`use\*(C'\fR
10320 functionality from the command-line.
10321 .IP "use VERSION" 4
10322 .IX Item "use VERSION"
10323 Lexically enables all features available in the requested version as
10324 defined by the feature pragma, disabling any features not in the
10325 requested version's feature bundle. See feature.
10326 .Sp
10327 VERSION may be either a v\-string such as v5.24.1, which will be compared
10328 to \f(CW$^V\fR (aka \f(CW$PERL_VERSION\fR), or a numeric argument of the
10329 form 5.024001, which will be compared to \f(CW$]\fR. An
10330 exception is raised if VERSION is greater than the version of the current
10331 Perl interpreter; Perl will not attempt to parse the rest of the file.
10332 Compare with \f(CW\*(C`require\*(C'\fR, which can do a similar check
10333 at run time.
10334 .Sp
10335 If the specified Perl version is 5.12 or higher, strictures are enabled
10336 lexically as with \f(CW\*(C`use strict\*(C'\fR.
10337 .Sp
10338 If the specified Perl version is 5.35.0 or higher, warnings are enabled.
10339 .Sp
10340 If the specified Perl version is 5.39.0 or higher, builtin functions are
10341 imported lexically as with \f(CW\*(C`use builtin\*(C'\fR with a corresponding
10342 version bundle.
10343 .Sp
10344 Use of \f(CW\*(C`use VERSION\*(C'\fR while another is in effect is not allowed with a
10345 \&\f(CW\*(C`use v5.39;\*(C'\fR or greater version. For lower versions, \f(CW\*(C`use VERSION\*(C'\fR will
10346 override most behavior of a previous \f(CW\*(C`use VERSION\*(C'\fR, possibly removing
10347 \&\f(CW\*(C`warnings\*(C'\fR and \f(CW\*(C`feature\*(C'\fR effects added by it. This behavior is deprecated,
10348 and a future release of perl will disallow changing the version once one has
10349 been declared. Additionally, a \f(CW\*(C`use VERSION\*(C'\fR with a version less than 5.11
10350 is not allowed after a \f(CW\*(C`use VERSION\*(C'\fR with a version greater than 5.11.
10351 .Sp
10352 \&\f(CW\*(C`use VERSION\*(C'\fR does not load the \fIfeature.pm\fR, \fIstrict.pm\fR, \fIwarnings.pm\fR
10353 or \fIbuiltin.pm\fR files, but instead implements the equivalent functionality
10354 directly.
10355 .Sp
10356 In the current implementation, any explicit use of \f(CW\*(C`no strict\*(C'\fR overrides
10357 \&\f(CW\*(C`use VERSION\*(C'\fR, even if it comes before it. However, this may be subject to
10358 change in a future release of Perl, so new code should not rely on this fact.
10359 It is recommended that a \f(CW\*(C`use VERSION\*(C'\fR declaration be the first significant
10360 statement within a file (possibly after a \f(CW\*(C`package\*(C'\fR statement or any amount
10361 of whitespace or comment), so that its effects happen first, and other pragmata
10362 are applied after it.
10363 .Sp
10364 Specifying VERSION as a numeric argument of the form 5.024001 should
10365 generally be avoided as older less readable syntax compared to
10366 v5.24.1. Before perl 5.8.0 released in 2002 the more verbose numeric
10367 form was the only supported syntax, which is why you might see it in
10368 older code.
10369 .Sp
10370 .Vb 3
10371 \& use v5.24.1; # compile time version check
10372 \& use 5.24.1; # ditto
10373 \& use 5.024_001; # ditto; older syntax compatible with perl 5.6
10374 .Ve
10375 .Sp
10376 This is often useful if you need to check the current Perl version before
10377 \&\f(CW\*(C`use\*(C'\fRing library modules that won't work
10378 with older versions of Perl.
10379 (We try not to do this more than we have to.)
10380 .Sp
10381 Symmetrically, \f(CW\*(C`no VERSION\*(C'\fR allows you to specify that you want a version
10382 of Perl older than the specified one. Historically this was added during
10383 early designs of the Raku language (formerly "Perl 6"), so that a Perl 5
10384 program could begin
10385 .Sp
10386 .Vb 1
10387 \& no 6;
10388 .Ve
10389 .Sp
10390 to declare that it is not a Perl 6 program. As the two languages have
10391 different implementations, file naming conventions, and other
10392 infrastructure, this feature is now little used in practice and should be
10393 avoided in newly-written code.
10394 .Sp
10395 Care should be taken when using the \f(CW\*(C`no VERSION\*(C'\fR form, as it is \fIonly\fR
10396 meant to be used to assert that the running Perl is of a earlier version
10397 than its argument and \fInot\fR to undo the feature-enabling side effects
10398 of \f(CW\*(C`use VERSION\*(C'\fR.
10399 .IP "utime LIST" 4
10400 .IX Xref "utime"
10401 .IX Item "utime LIST"
10402 Changes the access and modification times on each file of a list of
10403 files. The first two elements of the list must be the NUMERIC access
10404 and modification times, in that order. Returns the number of files
10405 successfully changed. The inode change time of each file is set
10406 to the current time. For example, this code has the same effect as the
10407 Unix \fBtouch\fR\|(1) command when the files \fIalready exist\fR and belong to
10408 the user running the program:
10409 .Sp
10410 .Vb 3
10411 \& #!/usr/bin/perl
10412 \& my $atime = my $mtime = time;
10413 \& utime $atime, $mtime, @ARGV;
10414 .Ve
10415 .Sp
10416 Since Perl 5.8.0, if the first two elements of the list are
10417 \&\f(CW\*(C`undef\*(C'\fR,
10418 the \fButime\fR\|(2) syscall from your C library is called with a null second
10419 argument. On most systems, this will set the file's access and
10420 modification times to the current time (i.e., equivalent to the example
10421 above) and will work even on files you don't own provided you have write
10422 permission:
10423 .Sp
10424 .Vb 4
10425 \& for my $file (@ARGV) {
10426 \& utime(undef, undef, $file)
10427 \& || warn "Couldn\*(Aqt touch $file: $!";
10428 \& }
10429 .Ve
10430 .Sp
10431 Under NFS this will use the time of the NFS server, not the time of
10432 the local machine. If there is a time synchronization problem, the
10433 NFS server and local machine will have different times. The Unix
10434 \&\fBtouch\fR\|(1) command will in fact normally use this form instead of the
10435 one shown in the first example.
10436 .Sp
10437 Passing only one of the first two elements as \f(CW\*(C`undef\*(C'\fR is
10438 equivalent to passing a 0 and will not have the effect described when
10439 both are \f(CW\*(C`undef\*(C'\fR. This also triggers an
10440 uninitialized warning.
10441 .Sp
10442 On systems that support \fBfutimes\fR\|(2), you may pass filehandles among the
10443 files. On systems that don't support \fBfutimes\fR\|(2), passing filehandles raises
10444 an exception. Filehandles must be passed as globs or glob references to be
10445 recognized; barewords are considered filenames.
10446 .Sp
10447 Portability issues: "utime" in perlport.
10448 .IP "values HASH" 4
10449 .IX Xref "values"
10450 .IX Item "values HASH"
10451 .PD 0
10452 .IP "values ARRAY" 4
10453 .IX Item "values ARRAY"
10454 .PD
10455 In list context, returns a list consisting of all the values of the named
10456 hash. In Perl 5.12 or later only, will also return a list of the values of
10457 an array; prior to that release, attempting to use an array argument will
10458 produce a syntax error. In scalar context, returns the number of values.
10459 .Sp
10460 Hash entries are returned in an apparently random order. The actual random
10461 order is specific to a given hash; the exact same series of operations
10462 on two hashes may result in a different order for each hash. Any insertion
10463 into the hash may change the order, as will any deletion, with the exception
10464 that the most recent key returned by \f(CW\*(C`each\*(C'\fR or
10465 \&\f(CW\*(C`keys\*(C'\fR may be deleted without changing the order. So
10466 long as a given hash is unmodified you may rely on
10467 \&\f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR and
10468 \&\f(CW\*(C`each\*(C'\fR to repeatedly return the same order
10469 as each other. See "Algorithmic Complexity Attacks" in perlsec for
10470 details on why hash order is randomized. Aside from the guarantees
10471 provided here the exact details of Perl's hash algorithm and the hash
10472 traversal order are subject to change in any release of Perl. Tied hashes
10473 may behave differently to Perl's hashes with respect to changes in order on
10474 insertion and deletion of items.
10475 .Sp
10476 As a side effect, calling \f(CW\*(C`values\*(C'\fR resets the HASH or
10477 ARRAY's internal iterator (see \f(CW\*(C`each\*(C'\fR) before yielding the
10478 values. In particular,
10479 calling \f(CW\*(C`values\*(C'\fR in void context resets the iterator
10480 with no other overhead.
10481 .Sp
10482 Apart from resetting the iterator,
10483 \&\f(CW\*(C`values @array\*(C'\fR in list context is the same as plain \f(CW@array\fR.
10484 (We recommend that you use void context \f(CW\*(C`keys @array\*(C'\fR for this, but
10485 reasoned that taking \f(CW\*(C`values @array\*(C'\fR out would require more
10486 documentation than leaving it in.)
10487 .Sp
10488 Note that the values are not copied, which means modifying them will
10489 modify the contents of the hash:
10490 .Sp
10491 .Vb 2
10492 \& for (values %hash) { s/foo/bar/g } # modifies %hash values
10493 \& for (@hash{keys %hash}) { s/foo/bar/g } # same
10494 .Ve
10495 .Sp
10496 Starting with Perl 5.14, an experimental feature allowed
10497 \&\f(CW\*(C`values\*(C'\fR to take a
10498 scalar expression. This experiment has been deemed unsuccessful, and was
10499 removed as of Perl 5.24.
10500 .Sp
10501 To avoid confusing would-be users of your code who are running earlier
10502 versions of Perl with mysterious syntax errors, put this sort of thing at
10503 the top of your file to signal that your code will work \fIonly\fR on Perls of
10504 a recent vintage:
10505 .Sp
10506 .Vb 1
10507 \& use v5.12; # so keys/values/each work on arrays
10508 .Ve
10509 .Sp
10510 See also \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`each\*(C'\fR, and
10511 \&\f(CW\*(C`sort\*(C'\fR.
10512 .IP "vec EXPR,OFFSET,BITS" 4
10513 .IX Xref "vec bit bit vector"
10514 .IX Item "vec EXPR,OFFSET,BITS"
10515 Treats the string in EXPR as a bit vector made up of elements of
10516 width BITS and returns the value of the element specified by OFFSET
10517 as an unsigned integer. BITS therefore specifies the number of bits
10518 that are reserved for each element in the bit vector. This must
10519 be a power of two from 1 to 32 (or 64, if your platform supports
10520 that).
10521 .Sp
10522 If BITS is 8, "elements" coincide with bytes of the input string.
10523 .Sp
10524 If BITS is 16 or more, bytes of the input string are grouped into chunks
10525 of size BITS/8, and each group is converted to a number as with
10526 \&\f(CW\*(C`pack\*(C'\fR/\f(CW\*(C`unpack\*(C'\fR with
10527 big-endian formats \f(CW\*(C`n\*(C'\fR/\f(CW\*(C`N\*(C'\fR (and analogously for BITS==64). See
10528 \&\f(CW\*(C`pack\*(C'\fR for details.
10529 .Sp
10530 If bits is 4 or less, the string is broken into bytes, then the bits
10531 of each byte are broken into 8/BITS groups. Bits of a byte are
10532 numbered in a little-endian-ish way, as in \f(CW0x01\fR, \f(CW0x02\fR,
10533 \&\f(CW0x04\fR, \f(CW0x08\fR, \f(CW0x10\fR, \f(CW0x20\fR, \f(CW0x40\fR, \f(CW0x80\fR. For example,
10534 breaking the single input byte \f(CWchr(0x36)\fR into two groups gives a list
10535 \&\f(CW\*(C`(0x6, 0x3)\*(C'\fR; breaking it into 4 groups gives \f(CW\*(C`(0x2, 0x1, 0x3, 0x0)\*(C'\fR.
10536 .Sp
10537 \&\f(CW\*(C`vec\*(C'\fR may also be assigned to, in which case
10538 parentheses are needed
10539 to give the expression the correct precedence as in
10540 .Sp
10541 .Vb 1
10542 \& vec($image, $max_x * $x + $y, 8) = 3;
10543 .Ve
10544 .Sp
10545 If the selected element is outside the string, the value 0 is returned.
10546 If an element off the end of the string is written to, Perl will first
10547 extend the string with sufficiently many zero bytes. It is an error
10548 to try to write off the beginning of the string (i.e., negative OFFSET).
10549 .Sp
10550 If the string happens to be encoded as UTF\-8 internally (and thus has
10551 the UTF8 flag set), \f(CW\*(C`vec\*(C'\fR tries to convert it
10552 to use a one-byte-per-character internal representation. However, if the
10553 string contains characters with values of 256 or higher, a fatal error
10554 will occur.
10555 .Sp
10556 Strings created with \f(CW\*(C`vec\*(C'\fR can also be
10557 manipulated with the logical
10558 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
10559 vector operation is desired when both operands are strings.
10560 See "Bitwise String Operators" in perlop.
10561 .Sp
10562 The following code will build up an ASCII string saying \f(CW\*(AqPerlPerlPerl\*(Aq\fR.
10563 The comments show the string after each step. Note that this code works
10564 in the same way on big-endian or little-endian machines.
10565 .Sp
10566 .Vb 2
10567 \& my $foo = \*(Aq\*(Aq;
10568 \& vec($foo, 0, 32) = 0x5065726C; # \*(AqPerl\*(Aq
10569 \&
10570 \& # $foo eq "Perl" eq "\ex50\ex65\ex72\ex6C", 32 bits
10571 \& print vec($foo, 0, 8); # prints 80 == 0x50 == ord(\*(AqP\*(Aq)
10572 \&
10573 \& vec($foo, 2, 16) = 0x5065; # \*(AqPerlPe\*(Aq
10574 \& vec($foo, 3, 16) = 0x726C; # \*(AqPerlPerl\*(Aq
10575 \& vec($foo, 8, 8) = 0x50; # \*(AqPerlPerlP\*(Aq
10576 \& vec($foo, 9, 8) = 0x65; # \*(AqPerlPerlPe\*(Aq
10577 \& vec($foo, 20, 4) = 2; # \*(AqPerlPerlPe\*(Aq . "\ex02"
10578 \& vec($foo, 21, 4) = 7; # \*(AqPerlPerlPer\*(Aq
10579 \& # \*(Aqr\*(Aq is "\ex72"
10580 \& vec($foo, 45, 2) = 3; # \*(AqPerlPerlPer\*(Aq . "\ex0c"
10581 \& vec($foo, 93, 1) = 1; # \*(AqPerlPerlPer\*(Aq . "\ex2c"
10582 \& vec($foo, 94, 1) = 1; # \*(AqPerlPerlPerl\*(Aq
10583 \& # \*(Aql\*(Aq is "\ex6c"
10584 .Ve
10585 .Sp
10586 To transform a bit vector into a string or list of 0's and 1's, use these:
10587 .Sp
10588 .Vb 2
10589 \& my $bits = unpack("b*", $vector);
10590 \& my @bits = split(//, unpack("b*", $vector));
10591 .Ve
10592 .Sp
10593 If you know the exact length in bits, it can be used in place of the \f(CW\*(C`*\*(C'\fR.
10594 .Sp
10595 Here is an example to illustrate how the bits actually fall in place:
10596 .Sp
10597 .Vb 1
10598 \& #!/usr/bin/perl \-wl
10599 \&
10600 \& print <<\*(AqEOT\*(Aq;
10601 \& 0 1 2 3
10602 \& unpack("V",$_) 01234567890123456789012345678901
10603 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
10604 \& EOT
10605 \&
10606 \& for $w (0..3) {
10607 \& $width = 2**$w;
10608 \& for ($shift=0; $shift < $width; ++$shift) {
10609 \& for ($off=0; $off < 32/$width; ++$off) {
10610 \& $str = pack("B*", "0"x32);
10611 \& $bits = (1<<$shift);
10612 \& vec($str, $off, $width) = $bits;
10613 \& $res = unpack("b*",$str);
10614 \& $val = unpack("V", $str);
10615 \& write;
10616 \& }
10617 \& }
10618 \& }
10619 \&
10620 \& format STDOUT =
10621 \& vec($_,@#,@#) = @<< == @######### @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
10622 \& $off, $width, $bits, $val, $res
10623 \& .
10624 \& _\|_END_\|_
10625 .Ve
10626 .Sp
10627 Regardless of the machine architecture on which it runs, the
10628 example above should print the following table:
10629 .Sp
10630 .Vb 10
10631 \& 0 1 2 3
10632 \& unpack("V",$_) 01234567890123456789012345678901
10633 \& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
10634 \& vec($_, 0, 1) = 1 == 1 10000000000000000000000000000000
10635 \& vec($_, 1, 1) = 1 == 2 01000000000000000000000000000000
10636 \& vec($_, 2, 1) = 1 == 4 00100000000000000000000000000000
10637 \& vec($_, 3, 1) = 1 == 8 00010000000000000000000000000000
10638 \& vec($_, 4, 1) = 1 == 16 00001000000000000000000000000000
10639 \& vec($_, 5, 1) = 1 == 32 00000100000000000000000000000000
10640 \& vec($_, 6, 1) = 1 == 64 00000010000000000000000000000000
10641 \& vec($_, 7, 1) = 1 == 128 00000001000000000000000000000000
10642 \& vec($_, 8, 1) = 1 == 256 00000000100000000000000000000000
10643 \& vec($_, 9, 1) = 1 == 512 00000000010000000000000000000000
10644 \& vec($_,10, 1) = 1 == 1024 00000000001000000000000000000000
10645 \& vec($_,11, 1) = 1 == 2048 00000000000100000000000000000000
10646 \& vec($_,12, 1) = 1 == 4096 00000000000010000000000000000000
10647 \& vec($_,13, 1) = 1 == 8192 00000000000001000000000000000000
10648 \& vec($_,14, 1) = 1 == 16384 00000000000000100000000000000000
10649 \& vec($_,15, 1) = 1 == 32768 00000000000000010000000000000000
10650 \& vec($_,16, 1) = 1 == 65536 00000000000000001000000000000000
10651 \& vec($_,17, 1) = 1 == 131072 00000000000000000100000000000000
10652 \& vec($_,18, 1) = 1 == 262144 00000000000000000010000000000000
10653 \& vec($_,19, 1) = 1 == 524288 00000000000000000001000000000000
10654 \& vec($_,20, 1) = 1 == 1048576 00000000000000000000100000000000
10655 \& vec($_,21, 1) = 1 == 2097152 00000000000000000000010000000000
10656 \& vec($_,22, 1) = 1 == 4194304 00000000000000000000001000000000
10657 \& vec($_,23, 1) = 1 == 8388608 00000000000000000000000100000000
10658 \& vec($_,24, 1) = 1 == 16777216 00000000000000000000000010000000
10659 \& vec($_,25, 1) = 1 == 33554432 00000000000000000000000001000000
10660 \& vec($_,26, 1) = 1 == 67108864 00000000000000000000000000100000
10661 \& vec($_,27, 1) = 1 == 134217728 00000000000000000000000000010000
10662 \& vec($_,28, 1) = 1 == 268435456 00000000000000000000000000001000
10663 \& vec($_,29, 1) = 1 == 536870912 00000000000000000000000000000100
10664 \& vec($_,30, 1) = 1 == 1073741824 00000000000000000000000000000010
10665 \& vec($_,31, 1) = 1 == 2147483648 00000000000000000000000000000001
10666 \& vec($_, 0, 2) = 1 == 1 10000000000000000000000000000000
10667 \& vec($_, 1, 2) = 1 == 4 00100000000000000000000000000000
10668 \& vec($_, 2, 2) = 1 == 16 00001000000000000000000000000000
10669 \& vec($_, 3, 2) = 1 == 64 00000010000000000000000000000000
10670 \& vec($_, 4, 2) = 1 == 256 00000000100000000000000000000000
10671 \& vec($_, 5, 2) = 1 == 1024 00000000001000000000000000000000
10672 \& vec($_, 6, 2) = 1 == 4096 00000000000010000000000000000000
10673 \& vec($_, 7, 2) = 1 == 16384 00000000000000100000000000000000
10674 \& vec($_, 8, 2) = 1 == 65536 00000000000000001000000000000000
10675 \& vec($_, 9, 2) = 1 == 262144 00000000000000000010000000000000
10676 \& vec($_,10, 2) = 1 == 1048576 00000000000000000000100000000000
10677 \& vec($_,11, 2) = 1 == 4194304 00000000000000000000001000000000
10678 \& vec($_,12, 2) = 1 == 16777216 00000000000000000000000010000000
10679 \& vec($_,13, 2) = 1 == 67108864 00000000000000000000000000100000
10680 \& vec($_,14, 2) = 1 == 268435456 00000000000000000000000000001000
10681 \& vec($_,15, 2) = 1 == 1073741824 00000000000000000000000000000010
10682 \& vec($_, 0, 2) = 2 == 2 01000000000000000000000000000000
10683 \& vec($_, 1, 2) = 2 == 8 00010000000000000000000000000000
10684 \& vec($_, 2, 2) = 2 == 32 00000100000000000000000000000000
10685 \& vec($_, 3, 2) = 2 == 128 00000001000000000000000000000000
10686 \& vec($_, 4, 2) = 2 == 512 00000000010000000000000000000000
10687 \& vec($_, 5, 2) = 2 == 2048 00000000000100000000000000000000
10688 \& vec($_, 6, 2) = 2 == 8192 00000000000001000000000000000000
10689 \& vec($_, 7, 2) = 2 == 32768 00000000000000010000000000000000
10690 \& vec($_, 8, 2) = 2 == 131072 00000000000000000100000000000000
10691 \& vec($_, 9, 2) = 2 == 524288 00000000000000000001000000000000
10692 \& vec($_,10, 2) = 2 == 2097152 00000000000000000000010000000000
10693 \& vec($_,11, 2) = 2 == 8388608 00000000000000000000000100000000
10694 \& vec($_,12, 2) = 2 == 33554432 00000000000000000000000001000000
10695 \& vec($_,13, 2) = 2 == 134217728 00000000000000000000000000010000
10696 \& vec($_,14, 2) = 2 == 536870912 00000000000000000000000000000100
10697 \& vec($_,15, 2) = 2 == 2147483648 00000000000000000000000000000001
10698 \& vec($_, 0, 4) = 1 == 1 10000000000000000000000000000000
10699 \& vec($_, 1, 4) = 1 == 16 00001000000000000000000000000000
10700 \& vec($_, 2, 4) = 1 == 256 00000000100000000000000000000000
10701 \& vec($_, 3, 4) = 1 == 4096 00000000000010000000000000000000
10702 \& vec($_, 4, 4) = 1 == 65536 00000000000000001000000000000000
10703 \& vec($_, 5, 4) = 1 == 1048576 00000000000000000000100000000000
10704 \& vec($_, 6, 4) = 1 == 16777216 00000000000000000000000010000000
10705 \& vec($_, 7, 4) = 1 == 268435456 00000000000000000000000000001000
10706 \& vec($_, 0, 4) = 2 == 2 01000000000000000000000000000000
10707 \& vec($_, 1, 4) = 2 == 32 00000100000000000000000000000000
10708 \& vec($_, 2, 4) = 2 == 512 00000000010000000000000000000000
10709 \& vec($_, 3, 4) = 2 == 8192 00000000000001000000000000000000
10710 \& vec($_, 4, 4) = 2 == 131072 00000000000000000100000000000000
10711 \& vec($_, 5, 4) = 2 == 2097152 00000000000000000000010000000000
10712 \& vec($_, 6, 4) = 2 == 33554432 00000000000000000000000001000000
10713 \& vec($_, 7, 4) = 2 == 536870912 00000000000000000000000000000100
10714 \& vec($_, 0, 4) = 4 == 4 00100000000000000000000000000000
10715 \& vec($_, 1, 4) = 4 == 64 00000010000000000000000000000000
10716 \& vec($_, 2, 4) = 4 == 1024 00000000001000000000000000000000
10717 \& vec($_, 3, 4) = 4 == 16384 00000000000000100000000000000000
10718 \& vec($_, 4, 4) = 4 == 262144 00000000000000000010000000000000
10719 \& vec($_, 5, 4) = 4 == 4194304 00000000000000000000001000000000
10720 \& vec($_, 6, 4) = 4 == 67108864 00000000000000000000000000100000
10721 \& vec($_, 7, 4) = 4 == 1073741824 00000000000000000000000000000010
10722 \& vec($_, 0, 4) = 8 == 8 00010000000000000000000000000000
10723 \& vec($_, 1, 4) = 8 == 128 00000001000000000000000000000000
10724 \& vec($_, 2, 4) = 8 == 2048 00000000000100000000000000000000
10725 \& vec($_, 3, 4) = 8 == 32768 00000000000000010000000000000000
10726 \& vec($_, 4, 4) = 8 == 524288 00000000000000000001000000000000
10727 \& vec($_, 5, 4) = 8 == 8388608 00000000000000000000000100000000
10728 \& vec($_, 6, 4) = 8 == 134217728 00000000000000000000000000010000
10729 \& vec($_, 7, 4) = 8 == 2147483648 00000000000000000000000000000001
10730 \& vec($_, 0, 8) = 1 == 1 10000000000000000000000000000000
10731 \& vec($_, 1, 8) = 1 == 256 00000000100000000000000000000000
10732 \& vec($_, 2, 8) = 1 == 65536 00000000000000001000000000000000
10733 \& vec($_, 3, 8) = 1 == 16777216 00000000000000000000000010000000
10734 \& vec($_, 0, 8) = 2 == 2 01000000000000000000000000000000
10735 \& vec($_, 1, 8) = 2 == 512 00000000010000000000000000000000
10736 \& vec($_, 2, 8) = 2 == 131072 00000000000000000100000000000000
10737 \& vec($_, 3, 8) = 2 == 33554432 00000000000000000000000001000000
10738 \& vec($_, 0, 8) = 4 == 4 00100000000000000000000000000000
10739 \& vec($_, 1, 8) = 4 == 1024 00000000001000000000000000000000
10740 \& vec($_, 2, 8) = 4 == 262144 00000000000000000010000000000000
10741 \& vec($_, 3, 8) = 4 == 67108864 00000000000000000000000000100000
10742 \& vec($_, 0, 8) = 8 == 8 00010000000000000000000000000000
10743 \& vec($_, 1, 8) = 8 == 2048 00000000000100000000000000000000
10744 \& vec($_, 2, 8) = 8 == 524288 00000000000000000001000000000000
10745 \& vec($_, 3, 8) = 8 == 134217728 00000000000000000000000000010000
10746 \& vec($_, 0, 8) = 16 == 16 00001000000000000000000000000000
10747 \& vec($_, 1, 8) = 16 == 4096 00000000000010000000000000000000
10748 \& vec($_, 2, 8) = 16 == 1048576 00000000000000000000100000000000
10749 \& vec($_, 3, 8) = 16 == 268435456 00000000000000000000000000001000
10750 \& vec($_, 0, 8) = 32 == 32 00000100000000000000000000000000
10751 \& vec($_, 1, 8) = 32 == 8192 00000000000001000000000000000000
10752 \& vec($_, 2, 8) = 32 == 2097152 00000000000000000000010000000000
10753 \& vec($_, 3, 8) = 32 == 536870912 00000000000000000000000000000100
10754 \& vec($_, 0, 8) = 64 == 64 00000010000000000000000000000000
10755 \& vec($_, 1, 8) = 64 == 16384 00000000000000100000000000000000
10756 \& vec($_, 2, 8) = 64 == 4194304 00000000000000000000001000000000
10757 \& vec($_, 3, 8) = 64 == 1073741824 00000000000000000000000000000010
10758 \& vec($_, 0, 8) = 128 == 128 00000001000000000000000000000000
10759 \& vec($_, 1, 8) = 128 == 32768 00000000000000010000000000000000
10760 \& vec($_, 2, 8) = 128 == 8388608 00000000000000000000000100000000
10761 \& vec($_, 3, 8) = 128 == 2147483648 00000000000000000000000000000001
10762 .Ve
10763 .IP wait 4
10764 .IX Xref "wait"
10765 .IX Item "wait"
10766 Behaves like \fBwait\fR\|(2) on your system: it waits for a child
10767 process to terminate and returns the pid of the deceased process, or
10768 \&\f(CW\-1\fR if there are no child processes. The status is returned in
10769 \&\f(CW$?\fR and
10770 \&\f(CW\*(C`${^CHILD_ERROR_NATIVE}\*(C'\fR.
10771 Note that a return value of \f(CW\-1\fR could mean that child processes are
10772 being automatically reaped, as described in perlipc.
10773 .Sp
10774 If you use \f(CW\*(C`wait\*(C'\fR in your handler for
10775 \&\f(CW$SIG{CHLD}\fR, it may accidentally wait for the child
10776 created by \f(CW\*(C`qx\*(C'\fR or \f(CW\*(C`system\*(C'\fR.
10777 See perlipc for details.
10778 .Sp
10779 Equivalent to \f(CW\*(C`waitpid(\-1, 0)\*(C'\fR.
10780 .Sp
10781 Portability issues: "wait" in perlport.
10782 .IP "waitpid PID,FLAGS" 4
10783 .IX Xref "waitpid"
10784 .IX Item "waitpid PID,FLAGS"
10785 Waits for a particular child process to terminate and returns the pid of
10786 the deceased process, or \f(CW\-1\fR if there is no such child process. A
10787 non-blocking wait (with WNOHANG in FLAGS) can return 0 if
10788 there are child processes matching PID but none have terminated yet.
10789 The status is returned in \f(CW$?\fR and
10790 \&\f(CW\*(C`${^CHILD_ERROR_NATIVE}\*(C'\fR.
10791 .Sp
10792 A PID of \f(CW0\fR indicates to wait for any child process whose process group ID is
10793 equal to that of the current process. A PID of less than \f(CW\-1\fR indicates to
10794 wait for any child process whose process group ID is equal to \-PID. A PID of
10795 \&\f(CW\-1\fR indicates to wait for any child process.
10796 .Sp
10797 If you say
10798 .Sp
10799 .Vb 1
10800 \& use POSIX ":sys_wait_h";
10801 \&
10802 \& my $kid;
10803 \& do {
10804 \& $kid = waitpid(\-1, WNOHANG);
10805 \& } while $kid > 0;
10806 .Ve
10807 .Sp
10808 or
10809 .Sp
10810 .Vb 1
10811 \& 1 while waitpid(\-1, WNOHANG) > 0;
10812 .Ve
10813 .Sp
10814 then you can do a non-blocking wait for all pending zombie processes (see
10815 "WAIT" in POSIX).
10816 Non-blocking wait is available on machines supporting either the
10817 \&\fBwaitpid\fR\|(2) or \fBwait4\fR\|(2) syscalls. However, waiting for a particular
10818 pid with FLAGS of \f(CW0\fR is implemented everywhere. (Perl emulates the
10819 system call by remembering the status values of processes that have
10820 exited but have not been harvested by the Perl script yet.)
10821 .Sp
10822 Note that on some systems, a return value of \f(CW\-1\fR could mean that child
10823 processes are being automatically reaped. See perlipc for details,
10824 and for other examples.
10825 .Sp
10826 Portability issues: "waitpid" in perlport.
10827 .IP wantarray 4
10828 .IX Xref "wantarray context"
10829 .IX Item "wantarray"
10830 Returns true if the context of the currently executing subroutine or
10831 \&\f(CW\*(C`eval\*(C'\fR is looking for a list value. Returns false if the
10832 context is
10833 looking for a scalar. Returns the undefined value if the context is
10834 looking for no value (void context).
10835 .Sp
10836 .Vb 3
10837 \& return unless defined wantarray; # don\*(Aqt bother doing more
10838 \& my @a = complex_calculation();
10839 \& return wantarray ? @a : "@a";
10840 .Ve
10841 .Sp
10842 \&\f(CW\*(C`wantarray\*(C'\fR's result is unspecified in the top level of a file,
10843 in a \f(CW\*(C`BEGIN\*(C'\fR, \f(CW\*(C`UNITCHECK\*(C'\fR, \f(CW\*(C`CHECK\*(C'\fR, \f(CW\*(C`INIT\*(C'\fR or \f(CW\*(C`END\*(C'\fR block, or
10844 in a \f(CW\*(C`DESTROY\*(C'\fR method.
10845 .Sp
10846 This function should have been named \fBwantlist()\fR instead.
10847 .IP "warn LIST" 4
10848 .IX Xref "warn warning STDERR"
10849 .IX Item "warn LIST"
10850 Emits a warning, usually by printing it to \f(CW\*(C`STDERR\*(C'\fR. \f(CW\*(C`warn\*(C'\fR interprets
10851 its operand LIST in the same way as \f(CW\*(C`die\*(C'\fR, but is slightly different
10852 in what it defaults to when LIST is empty or makes an empty string.
10853 If it is empty and \f(CW$@\fR already contains an exception
10854 value then that value is used after appending \f(CW"\et...caught"\fR. If it
10855 is empty and \f(CW$@\fR is also empty then the string \f(CW"Warning: Something\*(Aqs
10856 wrong"\fR is used.
10857 .Sp
10858 By default, the exception derived from the operand LIST is stringified
10859 and printed to \f(CW\*(C`STDERR\*(C'\fR. This behaviour can be altered by installing
10860 a \f(CW$SIG{_\|_WARN_\|_}\fR handler. If there is such a
10861 handler then no message is automatically printed; it is the handler's
10862 responsibility to deal with the exception
10863 as it sees fit (like, for instance, converting it into a
10864 \&\f(CW\*(C`die\*(C'\fR). Most
10865 handlers must therefore arrange to actually display the
10866 warnings that they are not prepared to deal with, by calling
10867 \&\f(CW\*(C`warn\*(C'\fR
10868 again in the handler. Note that this is quite safe and will not
10869 produce an endless loop, since \f(CW\*(C`_\|_WARN_\|_\*(C'\fR hooks are not called from
10870 inside one.
10871 .Sp
10872 You will find this behavior is slightly different from that of
10873 \&\f(CW$SIG{_\|_DIE_\|_}\fR handlers (which don't suppress the
10874 error text, but can instead call \f(CW\*(C`die\*(C'\fR again to change
10875 it).
10876 .Sp
10877 Using a \f(CW\*(C`_\|_WARN_\|_\*(C'\fR handler provides a powerful way to silence all
10878 warnings (even the so-called mandatory ones). An example:
10879 .Sp
10880 .Vb 7
10881 \& # wipe out *all* compile\-time warnings
10882 \& BEGIN { $SIG{\*(Aq_\|_WARN_\|_\*(Aq} = sub { warn $_[0] if $DOWARN } }
10883 \& my $foo = 10;
10884 \& my $foo = 20; # no warning about duplicate my $foo,
10885 \& # but hey, you asked for it!
10886 \& # no compile\-time or run\-time warnings before here
10887 \& $DOWARN = 1;
10888 \&
10889 \& # run\-time warnings enabled after here
10890 \& warn "\e$foo is alive and $foo!"; # does show up
10891 .Ve
10892 .Sp
10893 See perlvar for details on setting \f(CW%SIG\fR entries
10894 and for more
10895 examples. See the Carp module for other kinds of warnings using its
10896 \&\f(CW\*(C`carp\*(C'\fR and \f(CW\*(C`cluck\*(C'\fR functions.
10897 .IP "write FILEHANDLE" 4
10898 .IX Xref "write"
10899 .IX Item "write FILEHANDLE"
10900 .PD 0
10901 .IP "write EXPR" 4
10902 .IX Item "write EXPR"
10903 .IP write 4
10904 .IX Item "write"
10905 .PD
10906 Writes a formatted record (possibly multi-line) to the specified FILEHANDLE,
10907 using the format associated with that file. By default the format for
10908 a file is the one having the same name as the filehandle, but the
10909 format for the current output channel (see the
10910 \&\f(CW\*(C`select\*(C'\fR function) may be set explicitly by
10911 assigning the name of the format to the \f(CW$~\fR variable.
10912 .Sp
10913 Top of form processing is handled automatically: if there is insufficient
10914 room on the current page for the formatted record, the page is advanced by
10915 writing a form feed and a special top-of-page
10916 format is used to format the new
10917 page header before the record is written. By default, the top-of-page
10918 format is the name of the filehandle with \f(CW\*(C`_TOP\*(C'\fR appended, or \f(CW\*(C`top\*(C'\fR
10919 in the current package if the former does not exist. This would be a
10920 problem with autovivified filehandles, but it may be dynamically set to the
10921 format of your choice by assigning the name to the \f(CW$^\fR
10922 variable while that filehandle is selected. The number of lines
10923 remaining on the current page is in variable \f(CW\*(C`$\-\*(C'\fR, which
10924 can be set to \f(CW0\fR to force a new page.
10925 .Sp
10926 If FILEHANDLE is unspecified, output goes to the current default output
10927 channel, which starts out as STDOUT but may be changed by the
10928 \&\f(CW\*(C`select\*(C'\fR operator. If the FILEHANDLE is an EXPR,
10929 then the expression
10930 is evaluated and the resulting string is used to look up the name of
10931 the FILEHANDLE at run time. For more on formats, see perlform.
10932 .Sp
10933 Note that write is \fInot\fR the opposite of
10934 \&\f(CW\*(C`read\*(C'\fR. Unfortunately.
10935 .IP y/// 4
10936 .IX Item "y///"
10937 The transliteration operator. Same as
10938 \&\f(CW\*(C`tr///\*(C'\fR. See
10939 "Quote-Like Operators" in perlop.
10940 .SS "Non-function Keywords by Cross-reference"
10941 .IX Subsection "Non-function Keywords by Cross-reference"
10942 \fIperldata\fR
10943 .IX Subsection "perldata"
10944 .IP _\|_DATA_\|_ 4
10945 .IX Item "__DATA__"
10946 .PD 0
10947 .IP _\|_END_\|_ 4
10948 .IX Item "__END__"
10949 .PD
10950 These keywords are documented in "Special Literals" in perldata.
10951 .PP
10952 \fIperlmod\fR
10953 .IX Subsection "perlmod"
10954 .IP BEGIN 4
10955 .IX Item "BEGIN"
10956 .PD 0
10957 .IP CHECK 4
10958 .IX Item "CHECK"
10959 .IP END 4
10960 .IX Item "END"
10961 .IP INIT 4
10962 .IX Item "INIT"
10963 .IP UNITCHECK 4
10964 .IX Item "UNITCHECK"
10965 .PD
10966 These compile phase keywords are documented in "BEGIN, UNITCHECK, CHECK, INIT and END" in perlmod.
10967 .PP
10968 \fIperlobj\fR
10969 .IX Subsection "perlobj"
10970 .IP DESTROY 4
10971 .IX Item "DESTROY"
10972 This method keyword is documented in "Destructors" in perlobj.
10973 .PP
10974 \fIperlop\fR
10975 .IX Subsection "perlop"
10976 .IP and 4
10977 .IX Item "and"
10978 .PD 0
10979 .IP cmp 4
10980 .IX Item "cmp"
10981 .IP eq 4
10982 .IX Item "eq"
10983 .IP ge 4
10984 .IX Item "ge"
10985 .IP gt 4
10986 .IX Item "gt"
10987 .IP isa 4
10988 .IX Item "isa"
10989 .IP le 4
10990 .IX Item "le"
10991 .IP lt 4
10992 .IX Item "lt"
10993 .IP ne 4
10994 .IX Item "ne"
10995 .IP not 4
10996 .IX Item "not"
10997 .IP or 4
10998 .IX Item "or"
10999 .IP x 4
11000 .IX Item "x"
11001 .IP xor 4
11002 .IX Item "xor"
11003 .PD
11004 These operators are documented in perlop.
11005 .PP
11006 \fIperlsub\fR
11007 .IX Subsection "perlsub"
11008 .IP AUTOLOAD 4
11009 .IX Item "AUTOLOAD"
11010 This keyword is documented in "Autoloading" in perlsub.
11011 .PP
11012 \fIperlsyn\fR
11013 .IX Subsection "perlsyn"
11014 .IP else 4
11015 .IX Item "else"
11016 .PD 0
11017 .IP elsif 4
11018 .IX Item "elsif"
11019 .IP for 4
11020 .IX Item "for"
11021 .IP foreach 4
11022 .IX Item "foreach"
11023 .IP if 4
11024 .IX Item "if"
11025 .IP unless 4
11026 .IX Item "unless"
11027 .IP until 4
11028 .IX Item "until"
11029 .IP while 4
11030 .IX Item "while"
11031 .PD
11032 These flow-control keywords are documented in "Compound Statements" in perlsyn.
11033 .IP elseif 4
11034 .IX Item "elseif"
11035 The "else if" keyword is spelled \f(CW\*(C`elsif\*(C'\fR in Perl. There's no \f(CW\*(C`elif\*(C'\fR
11036 or \f(CW\*(C`else if\*(C'\fR either. It does parse \f(CW\*(C`elseif\*(C'\fR, but only to warn you
11037 about not using it.
11038 .Sp
11039 See the documentation for flow-control keywords in "Compound
11040 Statements" in perlsyn.
11041 .IP default 4
11042 .IX Item "default"
11043 .PD 0
11044 .IP given 4
11045 .IX Item "given"
11046 .IP when 4
11047 .IX Item "when"
11048 .PD
11049 These flow-control keywords related to the experimental switch feature are
11050 documented in "Switch Statements" in perlsyn.
11051 .IP try 4
11052 .IX Item "try"
11053 .PD 0
11054 .IP catch 4
11055 .IX Item "catch"
11056 .IP finally 4
11057 .IX Item "finally"
11058 .PD
11059 These flow-control keywords related to the experimental \f(CW\*(C`try\*(C'\fR feature are
11060 documented in "Try Catch Exception Handling" in perlsyn.
11061 .IP defer 4
11062 .IX Item "defer"
11063 This flow-control keyword related to the experimental \f(CW\*(C`defer\*(C'\fR feature is
11064 documented in "defer blocks" in perlsyn.
11065 .IP ADJUST 4
11066 .IX Item "ADJUST"
11067 This class-related phaser block is documented in perlclass.