1PERLFUNC(1) Perl Programmers Reference Guide PERLFUNC(1)
2
3
4
6 perlfunc - Perl builtin functions
7
9 The functions in this section can serve as terms in an expression.
10 They fall into two major categories: list operators and named unary
11 operators. These differ in their precedence relationship with a
12 following comma. (See the precedence table in perlop.) List operators
13 take more than one argument, while unary operators can never take more
14 than one argument. Thus, a comma terminates the argument of a unary
15 operator, but merely separates the arguments of a list operator. A
16 unary operator generally provides scalar context to its argument, while
17 a list operator may provide either scalar or list contexts for its
18 arguments. If it does both, scalar arguments come first and list
19 argument follow, and there can only ever be one such list argument.
20 For instance, splice() has three scalar arguments followed by a list,
21 whereas gethostbyname() has four scalar arguments.
22
23 In the syntax descriptions that follow, list operators that expect a
24 list (and provide list context for elements of the list) are shown with
25 LIST as an argument. Such a list may consist of any combination of
26 scalar arguments or list values; the list values will be included in
27 the list as if each individual element were interpolated at that point
28 in the list, forming a longer single-dimensional list value. Commas
29 should separate literal elements of the LIST.
30
31 Any function in the list below may be used either with or without
32 parentheses around its arguments. (The syntax descriptions omit the
33 parentheses.) If you use parentheses, the simple but occasionally
34 surprising rule is this: It looks like a function, therefore it is a
35 function, and precedence doesn't matter. Otherwise it's a list
36 operator or unary operator, and precedence does matter. Whitespace
37 between the function and left parenthesis doesn't count, so sometimes
38 you need to be careful:
39
40 print 1+2+4; # Prints 7.
41 print(1+2) + 4; # Prints 3.
42 print (1+2)+4; # Also prints 3!
43 print +(1+2)+4; # Prints 7.
44 print ((1+2)+4); # Prints 7.
45
46 If you run Perl with the -w switch it can warn you about this. For
47 example, the third line above produces:
48
49 print (...) interpreted as function at - line 1.
50 Useless use of integer addition in void context at - line 1.
51
52 A few functions take no arguments at all, and therefore work as neither
53 unary nor list operators. These include such functions as "time" and
54 "endpwent". For example, "time+86_400" always means "time() + 86_400".
55
56 For functions that can be used in either a scalar or list context,
57 nonabortive failure is generally indicated in scalar context by
58 returning the undefined value, and in list context by returning the
59 empty list.
60
61 Remember the following important rule: There is no rule that relates
62 the behavior of an expression in list context to its behavior in scalar
63 context, or vice versa. It might do two totally different things.
64 Each operator and function decides which sort of value would be most
65 appropriate to return in scalar context. Some operators return the
66 length of the list that would have been returned in list context. Some
67 operators return the first value in the list. Some operators return
68 the last value in the list. Some operators return a count of
69 successful operations. In general, they do what you want, unless you
70 want consistency.
71
72 A named array in scalar context is quite different from what would at
73 first glance appear to be a list in scalar context. You can't get a
74 list like "(1,2,3)" into being in scalar context, because the compiler
75 knows the context at compile time. It would generate the scalar comma
76 operator there, not the list construction version of the comma. That
77 means it was never a list to start with.
78
79 In general, functions in Perl that serve as wrappers for system calls
80 ("syscalls") of the same name (like chown(2), fork(2), closedir(2),
81 etc.) return true when they succeed and "undef" otherwise, as is
82 usually mentioned in the descriptions below. This is different from
83 the C interfaces, which return "-1" on failure. Exceptions to this
84 rule include "wait", "waitpid", and "syscall". System calls also set
85 the special $! variable on failure. Other functions do not, except
86 accidentally.
87
88 Extension modules can also hook into the Perl parser to define new
89 kinds of keyword-headed expression. These may look like functions, but
90 may also look completely different. The syntax following the keyword
91 is defined entirely by the extension. If you are an implementor, see
92 "PL_keyword_plugin" in perlapi for the mechanism. If you are using
93 such a module, see the module's documentation for details of the syntax
94 that it defines.
95
96 Perl Functions by Category
97 Here are Perl's functions (including things that look like functions,
98 like some keywords and named operators) arranged by category. Some
99 functions appear in more than one place.
100
101 Functions for SCALARs or strings
102 "chomp", "chop", "chr", "crypt", "fc", "hex", "index", "lc",
103 "lcfirst", "length", "oct", "ord", "pack", "q//", "qq//",
104 "reverse", "rindex", "sprintf", "substr", "tr///", "uc", "ucfirst",
105 "y///"
106
107 "fc" is available only if the "fc" feature is enabled or if it is
108 prefixed with "CORE::". The "fc" feature is enabled automatically
109 with a "use v5.16" (or higher) declaration in the current scope.
110
111 Regular expressions and pattern matching
112 "m//", "pos", "qr//", "quotemeta", "s///", "split", "study"
113
114 Numeric functions
115 "abs", "atan2", "cos", "exp", "hex", "int", "log", "oct", "rand",
116 "sin", "sqrt", "srand"
117
118 Functions for real @ARRAYs
119 "each", "keys", "pop", "push", "shift", "splice", "unshift",
120 "values"
121
122 Functions for list data
123 "grep", "join", "map", "qw//", "reverse", "sort", "unpack"
124
125 Functions for real %HASHes
126 "delete", "each", "exists", "keys", "values"
127
128 Input and output functions
129 "binmode", "close", "closedir", "dbmclose", "dbmopen", "die",
130 "eof", "fileno", "flock", "format", "getc", "print", "printf",
131 "read", "readdir", "readline" "rewinddir", "say", "seek",
132 "seekdir", "select", "syscall", "sysread", "sysseek", "syswrite",
133 "tell", "telldir", "truncate", "warn", "write"
134
135 "say" is available only if the "say" feature is enabled or if it is
136 prefixed with "CORE::". The "say" feature is enabled automatically
137 with a "use v5.10" (or higher) declaration in the current scope.
138
139 Functions for fixed-length data or records
140 "pack", "read", "syscall", "sysread", "sysseek", "syswrite",
141 "unpack", "vec"
142
143 Functions for filehandles, files, or directories
144 "-X", "chdir", "chmod", "chown", "chroot", "fcntl", "glob",
145 "ioctl", "link", "lstat", "mkdir", "open", "opendir", "readlink",
146 "rename", "rmdir", "stat", "symlink", "sysopen", "umask", "unlink",
147 "utime"
148
149 Keywords related to the control flow of your Perl program
150 "break", "caller", "continue", "die", "do", "dump", "eval",
151 "evalbytes" "exit", "__FILE__", "goto", "last", "__LINE__", "next",
152 "__PACKAGE__", "redo", "return", "sub", "__SUB__", "wantarray"
153
154 "break" is available only if you enable the experimental "switch"
155 feature or use the "CORE::" prefix. The "switch" feature also
156 enables the "default", "given" and "when" statements, which are
157 documented in "Switch Statements" in perlsyn. The "switch" feature
158 is enabled automatically with a "use v5.10" (or higher) declaration
159 in the current scope. In Perl v5.14 and earlier, "continue"
160 required the "switch" feature, like the other keywords.
161
162 "evalbytes" is only available with with the "evalbytes" feature
163 (see feature) or if prefixed with "CORE::". "__SUB__" is only
164 available with with the "current_sub" feature or if prefixed with
165 "CORE::". Both the "evalbytes" and "current_sub" features are
166 enabled automatically with a "use v5.16" (or higher) declaration in
167 the current scope.
168
169 Keywords related to scoping
170 "caller", "import", "local", "my", "our", "package", "state", "use"
171
172 "state" is available only if the "state" feature is enabled or if
173 it is prefixed with "CORE::". The "state" feature is enabled
174 automatically with a "use v5.10" (or higher) declaration in the
175 current scope.
176
177 Miscellaneous functions
178 "defined", "formline", "lock", "prototype", "reset", "scalar",
179 "undef"
180
181 Functions for processes and process groups
182 "alarm", "exec", "fork", "getpgrp", "getppid", "getpriority",
183 "kill", "pipe", "qx//", "readpipe", "setpgrp", "setpriority",
184 "sleep", "system", "times", "wait", "waitpid"
185
186 Keywords related to Perl modules
187 "do", "import", "no", "package", "require", "use"
188
189 Keywords related to classes and object-orientation
190 "bless", "dbmclose", "dbmopen", "package", "ref", "tie", "tied",
191 "untie", "use"
192
193 Low-level socket functions
194 "accept", "bind", "connect", "getpeername", "getsockname",
195 "getsockopt", "listen", "recv", "send", "setsockopt", "shutdown",
196 "socket", "socketpair"
197
198 System V interprocess communication functions
199 "msgctl", "msgget", "msgrcv", "msgsnd", "semctl", "semget",
200 "semop", "shmctl", "shmget", "shmread", "shmwrite"
201
202 Fetching user and group info
203 "endgrent", "endhostent", "endnetent", "endpwent", "getgrent",
204 "getgrgid", "getgrnam", "getlogin", "getpwent", "getpwnam",
205 "getpwuid", "setgrent", "setpwent"
206
207 Fetching network info
208 "endprotoent", "endservent", "gethostbyaddr", "gethostbyname",
209 "gethostent", "getnetbyaddr", "getnetbyname", "getnetent",
210 "getprotobyname", "getprotobynumber", "getprotoent",
211 "getservbyname", "getservbyport", "getservent", "sethostent",
212 "setnetent", "setprotoent", "setservent"
213
214 Time-related functions
215 "gmtime", "localtime", "time", "times"
216
217 Non-function keywords
218 "and", "AUTOLOAD", "BEGIN", "CHECK", "cmp", "CORE", "__DATA__",
219 "default", "DESTROY", "else", "elseif", "elsif", "END", "__END__",
220 "eq", "for", "foreach", "ge", "given", "gt", "if", "INIT", "le",
221 "lt", "ne", "not", "or", "UNITCHECK", "unless", "until", "when",
222 "while", "x", "xor"
223
224 Portability
225 Perl was born in Unix and can therefore access all common Unix system
226 calls. In non-Unix environments, the functionality of some Unix system
227 calls may not be available or details of the available functionality
228 may differ slightly. The Perl functions affected by this are:
229
230 "-X", "binmode", "chmod", "chown", "chroot", "crypt", "dbmclose",
231 "dbmopen", "dump", "endgrent", "endhostent", "endnetent",
232 "endprotoent", "endpwent", "endservent", "exec", "fcntl", "flock",
233 "fork", "getgrent", "getgrgid", "gethostbyname", "gethostent",
234 "getlogin", "getnetbyaddr", "getnetbyname", "getnetent", "getppid",
235 "getpgrp", "getpriority", "getprotobynumber", "getprotoent",
236 "getpwent", "getpwnam", "getpwuid", "getservbyport", "getservent",
237 "getsockopt", "glob", "ioctl", "kill", "link", "lstat", "msgctl",
238 "msgget", "msgrcv", "msgsnd", "open", "pipe", "readlink", "rename",
239 "select", "semctl", "semget", "semop", "setgrent", "sethostent",
240 "setnetent", "setpgrp", "setpriority", "setprotoent", "setpwent",
241 "setservent", "setsockopt", "shmctl", "shmget", "shmread", "shmwrite",
242 "socket", "socketpair", "stat", "symlink", "syscall", "sysopen",
243 "system", "times", "truncate", "umask", "unlink", "utime", "wait",
244 "waitpid"
245
246 For more information about the portability of these functions, see
247 perlport and other available platform-specific documentation.
248
249 Alphabetical Listing of Perl Functions
250 -X FILEHANDLE
251 -X EXPR
252 -X DIRHANDLE
253 -X A file test, where X is one of the letters listed below. This
254 unary operator takes one argument, either a filename, a filehandle,
255 or a dirhandle, and tests the associated file to see if something
256 is true about it. If the argument is omitted, tests $_, except for
257 "-t", which tests STDIN. Unless otherwise documented, it returns 1
258 for true and '' for false, or the undefined value if the file
259 doesn't exist. Despite the funny names, precedence is the same as
260 any other named unary operator. The operator may be any of:
261
262 -r File is readable by effective uid/gid.
263 -w File is writable by effective uid/gid.
264 -x File is executable by effective uid/gid.
265 -o File is owned by effective uid.
266
267 -R File is readable by real uid/gid.
268 -W File is writable by real uid/gid.
269 -X File is executable by real uid/gid.
270 -O File is owned by real uid.
271
272 -e File exists.
273 -z File has zero size (is empty).
274 -s File has nonzero size (returns size in bytes).
275
276 -f File is a plain file.
277 -d File is a directory.
278 -l File is a symbolic link.
279 -p File is a named pipe (FIFO), or Filehandle is a pipe.
280 -S File is a socket.
281 -b File is a block special file.
282 -c File is a character special file.
283 -t Filehandle is opened to a tty.
284
285 -u File has setuid bit set.
286 -g File has setgid bit set.
287 -k File has sticky bit set.
288
289 -T File is an ASCII text file (heuristic guess).
290 -B File is a "binary" file (opposite of -T).
291
292 -M Script start time minus file modification time, in days.
293 -A Same for access time.
294 -C Same for inode change time (Unix, may differ for other platforms)
295
296 Example:
297
298 while (<>) {
299 chomp;
300 next unless -f $_; # ignore specials
301 #...
302 }
303
304 Note that "-s/a/b/" does not do a negated substitution. Saying
305 "-exp($foo)" still works as expected, however: only single letters
306 following a minus are interpreted as file tests.
307
308 These operators are exempt from the "looks like a function rule"
309 described above. That is, an opening parenthesis after the
310 operator does not affect how much of the following code constitutes
311 the argument. Put the opening parentheses before the operator to
312 separate it from code that follows (this applies only to operators
313 with higher precedence than unary operators, of course):
314
315 -s($file) + 1024 # probably wrong; same as -s($file + 1024)
316 (-s $file) + 1024 # correct
317
318 The interpretation of the file permission operators "-r", "-R",
319 "-w", "-W", "-x", and "-X" is by default based solely on the mode
320 of the file and the uids and gids of the user. There may be other
321 reasons you can't actually read, write, or execute the file: for
322 example network filesystem access controls, ACLs (access control
323 lists), read-only filesystems, and unrecognized executable formats.
324 Note that the use of these six specific operators to verify if some
325 operation is possible is usually a mistake, because it may be open
326 to race conditions.
327
328 Also note that, for the superuser on the local filesystems, the
329 "-r", "-R", "-w", and "-W" tests always return 1, and "-x" and "-X"
330 return 1 if any execute bit is set in the mode. Scripts run by the
331 superuser may thus need to do a stat() to determine the actual mode
332 of the file, or temporarily set their effective uid to something
333 else.
334
335 If you are using ACLs, there is a pragma called "filetest" that may
336 produce more accurate results than the bare stat() mode bits. When
337 under "use filetest 'access'" the above-mentioned filetests test
338 whether the permission can(not) be granted using the access(2)
339 family of system calls. Also note that the "-x" and "-X" may under
340 this pragma return true even if there are no execute permission
341 bits set (nor any extra execute permission ACLs). This strangeness
342 is due to the underlying system calls' definitions. Note also
343 that, due to the implementation of "use filetest 'access'", the "_"
344 special filehandle won't cache the results of the file tests when
345 this pragma is in effect. Read the documentation for the
346 "filetest" pragma for more information.
347
348 The "-T" and "-B" switches work as follows. The first block or so
349 of the file is examined for odd characters such as strange control
350 codes or characters with the high bit set. If too many strange
351 characters (>30%) are found, it's a "-B" file; otherwise it's a
352 "-T" file. Also, any file containing a zero byte in the first
353 block is considered a binary file. If "-T" or "-B" is used on a
354 filehandle, the current IO buffer is examined rather than the first
355 block. Both "-T" and "-B" return true on an empty file, or a file
356 at EOF when testing a filehandle. Because you have to read a file
357 to do the "-T" test, on most occasions you want to use a "-f"
358 against the file first, as in "next unless -f $file && -T $file".
359
360 If any of the file tests (or either the "stat" or "lstat" operator)
361 is given the special filehandle consisting of a solitary underline,
362 then the stat structure of the previous file test (or stat
363 operator) is used, saving a system call. (This doesn't work with
364 "-t", and you need to remember that lstat() and "-l" leave values
365 in the stat structure for the symbolic link, not the real file.)
366 (Also, if the stat buffer was filled by an "lstat" call, "-T" and
367 "-B" will reset it with the results of "stat _"). Example:
368
369 print "Can do.\n" if -r $a || -w _ || -x _;
370
371 stat($filename);
372 print "Readable\n" if -r _;
373 print "Writable\n" if -w _;
374 print "Executable\n" if -x _;
375 print "Setuid\n" if -u _;
376 print "Setgid\n" if -g _;
377 print "Sticky\n" if -k _;
378 print "Text\n" if -T _;
379 print "Binary\n" if -B _;
380
381 As of Perl 5.9.1, as a form of purely syntactic sugar, you can
382 stack file test operators, in a way that "-f -w -x $file" is
383 equivalent to "-x $file && -w _ && -f _". (This is only fancy
384 fancy: if you use the return value of "-f $file" as an argument to
385 another filetest operator, no special magic will happen.)
386
387 Portability issues: "-X" in perlport.
388
389 To avoid confusing would-be users of your code with mysterious
390 syntax errors, put something like this at the top of your script:
391
392 use 5.010; # so filetest ops can stack
393
394 abs VALUE
395 abs Returns the absolute value of its argument. If VALUE is omitted,
396 uses $_.
397
398 accept NEWSOCKET,GENERICSOCKET
399 Accepts an incoming socket connect, just as accept(2) does.
400 Returns the packed address if it succeeded, false otherwise. See
401 the example in "Sockets: Client/Server Communication" in perlipc.
402
403 On systems that support a close-on-exec flag on files, the flag
404 will be set for the newly opened file descriptor, as determined by
405 the value of $^F. See "$^F" in perlvar.
406
407 alarm SECONDS
408 alarm
409 Arranges to have a SIGALRM delivered to this process after the
410 specified number of wallclock seconds has elapsed. If SECONDS is
411 not specified, the value stored in $_ is used. (On some machines,
412 unfortunately, the elapsed time may be up to one second less or
413 more than you specified because of how seconds are counted, and
414 process scheduling may delay the delivery of the signal even
415 further.)
416
417 Only one timer may be counting at once. Each call disables the
418 previous timer, and an argument of 0 may be supplied to cancel the
419 previous timer without starting a new one. The returned value is
420 the amount of time remaining on the previous timer.
421
422 For delays of finer granularity than one second, the Time::HiRes
423 module (from CPAN, and starting from Perl 5.8 part of the standard
424 distribution) provides ualarm(). You may also use Perl's four-
425 argument version of select() leaving the first three arguments
426 undefined, or you might be able to use the "syscall" interface to
427 access setitimer(2) if your system supports it. See perlfaq8 for
428 details.
429
430 It is usually a mistake to intermix "alarm" and "sleep" calls,
431 because "sleep" may be internally implemented on your system with
432 "alarm".
433
434 If you want to use "alarm" to time out a system call you need to
435 use an "eval"/"die" pair. You can't rely on the alarm causing the
436 system call to fail with $! set to "EINTR" because Perl sets up
437 signal handlers to restart system calls on some systems. Using
438 "eval"/"die" always works, modulo the caveats given in "Signals" in
439 perlipc.
440
441 eval {
442 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
443 alarm $timeout;
444 $nread = sysread SOCKET, $buffer, $size;
445 alarm 0;
446 };
447 if ($@) {
448 die unless $@ eq "alarm\n"; # propagate unexpected errors
449 # timed out
450 }
451 else {
452 # didn't
453 }
454
455 For more information see perlipc.
456
457 Portability issues: "alarm" in perlport.
458
459 atan2 Y,X
460 Returns the arctangent of Y/X in the range -PI to PI.
461
462 For the tangent operation, you may use the "Math::Trig::tan"
463 function, or use the familiar relation:
464
465 sub tan { sin($_[0]) / cos($_[0]) }
466
467 The return value for "atan2(0,0)" is implementation-defined;
468 consult your atan2(3) manpage for more information.
469
470 Portability issues: "atan2" in perlport.
471
472 bind SOCKET,NAME
473 Binds a network address to a socket, just as bind(2) does. Returns
474 true if it succeeded, false otherwise. NAME should be a packed
475 address of the appropriate type for the socket. See the examples
476 in "Sockets: Client/Server Communication" in perlipc.
477
478 binmode FILEHANDLE, LAYER
479 binmode FILEHANDLE
480 Arranges for FILEHANDLE to be read or written in "binary" or "text"
481 mode on systems where the run-time libraries distinguish between
482 binary and text files. If FILEHANDLE is an expression, the value
483 is taken as the name of the filehandle. Returns true on success,
484 otherwise it returns "undef" and sets $! (errno).
485
486 On some systems (in general, DOS- and Windows-based systems)
487 binmode() is necessary when you're not working with a text file.
488 For the sake of portability it is a good idea always to use it when
489 appropriate, and never to use it when it isn't appropriate. Also,
490 people can set their I/O to be by default UTF8-encoded Unicode, not
491 bytes.
492
493 In other words: regardless of platform, use binmode() on binary
494 data, like images, for example.
495
496 If LAYER is present it is a single string, but may contain multiple
497 directives. The directives alter the behaviour of the filehandle.
498 When LAYER is present, using binmode on a text file makes sense.
499
500 If LAYER is omitted or specified as ":raw" the filehandle is made
501 suitable for passing binary data. This includes turning off
502 possible CRLF translation and marking it as bytes (as opposed to
503 Unicode characters). Note that, despite what may be implied in
504 "Programming Perl" (the Camel, 3rd edition) or elsewhere, ":raw" is
505 not simply the inverse of ":crlf". Other layers that would affect
506 the binary nature of the stream are also disabled. See PerlIO,
507 perlrun, and the discussion about the PERLIO environment variable.
508
509 The ":bytes", ":crlf", ":utf8", and any other directives of the
510 form ":...", are called I/O layers. The "open" pragma can be used
511 to establish default I/O layers. See open.
512
513 The LAYER parameter of the binmode() function is described as
514 "DISCIPLINE" in "Programming Perl, 3rd Edition". However, since
515 the publishing of this book, by many known as "Camel III", the
516 consensus of the naming of this functionality has moved from
517 "discipline" to "layer". All documentation of this version of Perl
518 therefore refers to "layers" rather than to "disciplines". Now
519 back to the regularly scheduled documentation...
520
521 To mark FILEHANDLE as UTF-8, use ":utf8" or ":encoding(UTF-8)".
522 ":utf8" just marks the data as UTF-8 without further checking,
523 while ":encoding(UTF-8)" checks the data for actually being valid
524 UTF-8. More details can be found in PerlIO::encoding.
525
526 In general, binmode() should be called after open() but before any
527 I/O is done on the filehandle. Calling binmode() normally flushes
528 any pending buffered output data (and perhaps pending input data)
529 on the handle. An exception to this is the ":encoding" layer that
530 changes the default character encoding of the handle; see "open".
531 The ":encoding" layer sometimes needs to be called in mid-stream,
532 and it doesn't flush the stream. The ":encoding" also implicitly
533 pushes on top of itself the ":utf8" layer because internally Perl
534 operates on UTF8-encoded Unicode characters.
535
536 The operating system, device drivers, C libraries, and Perl run-
537 time system all conspire to let the programmer treat a single
538 character ("\n") as the line terminator, irrespective of external
539 representation. On many operating systems, the native text file
540 representation matches the internal representation, but on some
541 platforms the external representation of "\n" is made up of more
542 than one character.
543
544 All variants of Unix, Mac OS (old and new), and Stream_LF files on
545 VMS use a single character to end each line in the external
546 representation of text (even though that single character is
547 CARRIAGE RETURN on old, pre-Darwin flavors of Mac OS, and is LINE
548 FEED on Unix and most VMS files). In other systems like OS/2, DOS,
549 and the various flavors of MS-Windows, your program sees a "\n" as
550 a simple "\cJ", but what's stored in text files are the two
551 characters "\cM\cJ". That means that if you don't use binmode() on
552 these systems, "\cM\cJ" sequences on disk will be converted to "\n"
553 on input, and any "\n" in your program will be converted back to
554 "\cM\cJ" on output. This is what you want for text files, but it
555 can be disastrous for binary files.
556
557 Another consequence of using binmode() (on some systems) is that
558 special end-of-file markers will be seen as part of the data
559 stream. For systems from the Microsoft family this means that, if
560 your binary data contain "\cZ", the I/O subsystem will regard it as
561 the end of the file, unless you use binmode().
562
563 binmode() is important not only for readline() and print()
564 operations, but also when using read(), seek(), sysread(),
565 syswrite() and tell() (see perlport for more details). See the $/
566 and "$\" variables in perlvar for how to manually set your input
567 and output line-termination sequences.
568
569 Portability issues: "binmode" in perlport.
570
571 bless REF,CLASSNAME
572 bless REF
573 This function tells the thingy referenced by REF that it is now an
574 object in the CLASSNAME package. If CLASSNAME is omitted, the
575 current package is used. Because a "bless" is often the last thing
576 in a constructor, it returns the reference for convenience. Always
577 use the two-argument version if a derived class might inherit the
578 function doing the blessing. Seeperlobj for more about the
579 blessing (and blessings) of objects.
580
581 Consider always blessing objects in CLASSNAMEs that are mixed case.
582 Namespaces with all lowercase names are considered reserved for
583 Perl pragmata. Builtin types have all uppercase names. To prevent
584 confusion, you may wish to avoid such package names as well. Make
585 sure that CLASSNAME is a true value.
586
587 See "Perl Modules" in perlmod.
588
589 break
590 Break out of a "given()" block.
591
592 This keyword is enabled by the "switch" feature: see feature for
593 more information. You can also access it by prefixing it with
594 "CORE::". Alternately, include a "use v5.10" or later to the
595 current scope.
596
597 caller EXPR
598 caller
599 Returns the context of the current subroutine call. In scalar
600 context, returns the caller's package name if there is a caller
601 (that is, if we're in a subroutine or "eval" or "require") and the
602 undefined value otherwise. In list context, returns
603
604 # 0 1 2
605 ($package, $filename, $line) = caller;
606
607 With EXPR, it returns some extra information that the debugger uses
608 to print a stack trace. The value of EXPR indicates how many call
609 frames to go back before the current one.
610
611 # 0 1 2 3 4
612 ($package, $filename, $line, $subroutine, $hasargs,
613
614 # 5 6 7 8 9 10
615 $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
616 = caller($i);
617
618 Here $subroutine may be "(eval)" if the frame is not a subroutine
619 call, but an "eval". In such a case additional elements $evaltext
620 and $is_require are set: $is_require is true if the frame is
621 created by a "require" or "use" statement, $evaltext contains the
622 text of the "eval EXPR" statement. In particular, for an "eval
623 BLOCK" statement, $subroutine is "(eval)", but $evaltext is
624 undefined. (Note also that each "use" statement creates a
625 "require" frame inside an "eval EXPR" frame.) $subroutine may also
626 be "(unknown)" if this particular subroutine happens to have been
627 deleted from the symbol table. $hasargs is true if a new instance
628 of @_ was set up for the frame. $hints and $bitmask contain
629 pragmatic hints that the caller was compiled with. The $hints and
630 $bitmask values are subject to change between versions of Perl, and
631 are not meant for external use.
632
633 $hinthash is a reference to a hash containing the value of "%^H"
634 when the caller was compiled, or "undef" if "%^H" was empty. Do
635 not modify the values of this hash, as they are the actual values
636 stored in the optree.
637
638 Furthermore, when called from within the DB package in list
639 context, and with an argument, caller returns more detailed
640 information: it sets the list variable @DB::args to be the
641 arguments with which the subroutine was invoked.
642
643 Be aware that the optimizer might have optimized call frames away
644 before "caller" had a chance to get the information. That means
645 that caller(N) might not return information about the call frame
646 you expect it to, for "N > 1". In particular, @DB::args might have
647 information from the previous time "caller" was called.
648
649 Be aware that setting @DB::args is best effort, intended for
650 debugging or generating backtraces, and should not be relied upon.
651 In particular, as @_ contains aliases to the caller's arguments,
652 Perl does not take a copy of @_, so @DB::args will contain
653 modifications the subroutine makes to @_ or its contents, not the
654 original values at call time. @DB::args, like @_, does not hold
655 explicit references to its elements, so under certain cases its
656 elements may have become freed and reallocated for other variables
657 or temporary values. Finally, a side effect of the current
658 implementation is that the effects of "shift @_" can normally be
659 undone (but not "pop @_" or other splicing, and not if a reference
660 to @_ has been taken, and subject to the caveat about reallocated
661 elements), so @DB::args is actually a hybrid of the current state
662 and initial state of @_. Buyer beware.
663
664 chdir EXPR
665 chdir FILEHANDLE
666 chdir DIRHANDLE
667 chdir
668 Changes the working directory to EXPR, if possible. If EXPR is
669 omitted, changes to the directory specified by $ENV{HOME}, if set;
670 if not, changes to the directory specified by $ENV{LOGDIR}. (Under
671 VMS, the variable $ENV{SYS$LOGIN} is also checked, and used if it
672 is set.) If neither is set, "chdir" does nothing. It returns true
673 on success, false otherwise. See the example under "die".
674
675 On systems that support fchdir(2), you may pass a filehandle or
676 directory handle as the argument. On systems that don't support
677 fchdir(2), passing handles raises an exception.
678
679 chmod LIST
680 Changes the permissions of a list of files. The first element of
681 the list must be the numeric mode, which should probably be an
682 octal number, and which definitely should not be a string of octal
683 digits: 0644 is okay, but "0644" is not. Returns the number of
684 files successfully changed. See also "oct" if all you have is a
685 string.
686
687 $cnt = chmod 0755, "foo", "bar";
688 chmod 0755, @executables;
689 $mode = "0644"; chmod $mode, "foo"; # !!! sets mode to
690 # --w----r-T
691 $mode = "0644"; chmod oct($mode), "foo"; # this is better
692 $mode = 0644; chmod $mode, "foo"; # this is best
693
694 On systems that support fchmod(2), you may pass filehandles among
695 the files. On systems that don't support fchmod(2), passing
696 filehandles raises an exception. Filehandles must be passed as
697 globs or glob references to be recognized; barewords are considered
698 filenames.
699
700 open(my $fh, "<", "foo");
701 my $perm = (stat $fh)[2] & 07777;
702 chmod($perm | 0600, $fh);
703
704 You can also import the symbolic "S_I*" constants from the "Fcntl"
705 module:
706
707 use Fcntl qw( :mode );
708 chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables;
709 # Identical to the chmod 0755 of the example above.
710
711 Portability issues: "chmod" in perlport.
712
713 chomp VARIABLE
714 chomp( LIST )
715 chomp
716 This safer version of "chop" removes any trailing string that
717 corresponds to the current value of $/ (also known as
718 $INPUT_RECORD_SEPARATOR in the "English" module). It returns the
719 total number of characters removed from all its arguments. It's
720 often used to remove the newline from the end of an input record
721 when you're worried that the final record may be missing its
722 newline. When in paragraph mode ("$/ = """), it removes all
723 trailing newlines from the string. When in slurp mode ("$/ =
724 undef") or fixed-length record mode ($/ is a reference to an
725 integer or the like; see perlvar) chomp() won't remove anything.
726 If VARIABLE is omitted, it chomps $_. Example:
727
728 while (<>) {
729 chomp; # avoid \n on last field
730 @array = split(/:/);
731 # ...
732 }
733
734 If VARIABLE is a hash, it chomps the hash's values, but not its
735 keys.
736
737 You can actually chomp anything that's an lvalue, including an
738 assignment:
739
740 chomp($cwd = `pwd`);
741 chomp($answer = <STDIN>);
742
743 If you chomp a list, each element is chomped, and the total number
744 of characters removed is returned.
745
746 Note that parentheses are necessary when you're chomping anything
747 that is not a simple variable. This is because "chomp $cwd =
748 `pwd`;" is interpreted as "(chomp $cwd) = `pwd`;", rather than as
749 "chomp( $cwd = `pwd` )" which you might expect. Similarly, "chomp
750 $a, $b" is interpreted as "chomp($a), $b" rather than as "chomp($a,
751 $b)".
752
753 chop VARIABLE
754 chop( LIST )
755 chop
756 Chops off the last character of a string and returns the character
757 chopped. It is much more efficient than "s/.$//s" because it
758 neither scans nor copies the string. If VARIABLE is omitted, chops
759 $_. If VARIABLE is a hash, it chops the hash's values, but not its
760 keys.
761
762 You can actually chop anything that's an lvalue, including an
763 assignment.
764
765 If you chop a list, each element is chopped. Only the value of the
766 last "chop" is returned.
767
768 Note that "chop" returns the last character. To return all but the
769 last character, use "substr($string, 0, -1)".
770
771 See also "chomp".
772
773 chown LIST
774 Changes the owner (and group) of a list of files. The first two
775 elements of the list must be the numeric uid and gid, in that
776 order. A value of -1 in either position is interpreted by most
777 systems to leave that value unchanged. Returns the number of files
778 successfully changed.
779
780 $cnt = chown $uid, $gid, 'foo', 'bar';
781 chown $uid, $gid, @filenames;
782
783 On systems that support fchown(2), you may pass filehandles among
784 the files. On systems that don't support fchown(2), passing
785 filehandles raises an exception. Filehandles must be passed as
786 globs or glob references to be recognized; barewords are considered
787 filenames.
788
789 Here's an example that looks up nonnumeric uids in the passwd file:
790
791 print "User: ";
792 chomp($user = <STDIN>);
793 print "Files: ";
794 chomp($pattern = <STDIN>);
795
796 ($login,$pass,$uid,$gid) = getpwnam($user)
797 or die "$user not in passwd file";
798
799 @ary = glob($pattern); # expand filenames
800 chown $uid, $gid, @ary;
801
802 On most systems, you are not allowed to change the ownership of the
803 file unless you're the superuser, although you should be able to
804 change the group to any of your secondary groups. On insecure
805 systems, these restrictions may be relaxed, but this is not a
806 portable assumption. On POSIX systems, you can detect this
807 condition this way:
808
809 use POSIX qw(sysconf _PC_CHOWN_RESTRICTED);
810 $can_chown_giveaway = not sysconf(_PC_CHOWN_RESTRICTED);
811
812 Portability issues: "chmod" in perlport.
813
814 chr NUMBER
815 chr Returns the character represented by that NUMBER in the character
816 set. For example, "chr(65)" is "A" in either ASCII or Unicode, and
817 chr(0x263a) is a Unicode smiley face.
818
819 Negative values give the Unicode replacement character
820 (chr(0xfffd)), except under the bytes pragma, where the low eight
821 bits of the value (truncated to an integer) are used.
822
823 If NUMBER is omitted, uses $_.
824
825 For the reverse, use "ord".
826
827 Note that characters from 128 to 255 (inclusive) are by default
828 internally not encoded as UTF-8 for backward compatibility reasons.
829
830 See perlunicode for more about Unicode.
831
832 chroot FILENAME
833 chroot
834 This function works like the system call by the same name: it makes
835 the named directory the new root directory for all further
836 pathnames that begin with a "/" by your process and all its
837 children. (It doesn't change your current working directory, which
838 is unaffected.) For security reasons, this call is restricted to
839 the superuser. If FILENAME is omitted, does a "chroot" to $_.
840
841 Portability issues: "chroot" in perlport.
842
843 close FILEHANDLE
844 close
845 Closes the file or pipe associated with the filehandle, flushes the
846 IO buffers, and closes the system file descriptor. Returns true if
847 those operations succeed and if no error was reported by any PerlIO
848 layer. Closes the currently selected filehandle if the argument is
849 omitted.
850
851 You don't have to close FILEHANDLE if you are immediately going to
852 do another "open" on it, because "open" closes it for you. (See
853 open.) However, an explicit "close" on an input file resets the
854 line counter ($.), while the implicit close done by "open" does
855 not.
856
857 If the filehandle came from a piped open, "close" returns false if
858 one of the other syscalls involved fails or if its program exits
859 with non-zero status. If the only problem was that the program
860 exited non-zero, $! will be set to 0. Closing a pipe also waits
861 for the process executing on the pipe to exit--in case you wish to
862 look at the output of the pipe afterwards--and implicitly puts the
863 exit status value of that command into $? and
864 "${^CHILD_ERROR_NATIVE}".
865
866 If there are multiple threads running, "close" on a filehandle from
867 a piped open returns true without waiting for the child process to
868 terminate, if the filehandle is still open in another thread.
869
870 Closing the read end of a pipe before the process writing to it at
871 the other end is done writing results in the writer receiving a
872 SIGPIPE. If the other end can't handle that, be sure to read all
873 the data before closing the pipe.
874
875 Example:
876
877 open(OUTPUT, '|sort >foo') # pipe to sort
878 or die "Can't start sort: $!";
879 #... # print stuff to output
880 close OUTPUT # wait for sort to finish
881 or warn $! ? "Error closing sort pipe: $!"
882 : "Exit status $? from sort";
883 open(INPUT, 'foo') # get sort's results
884 or die "Can't open 'foo' for input: $!";
885
886 FILEHANDLE may be an expression whose value can be used as an
887 indirect filehandle, usually the real filehandle name or an
888 autovivified handle.
889
890 closedir DIRHANDLE
891 Closes a directory opened by "opendir" and returns the success of
892 that system call.
893
894 connect SOCKET,NAME
895 Attempts to connect to a remote socket, just like connect(2).
896 Returns true if it succeeded, false otherwise. NAME should be a
897 packed address of the appropriate type for the socket. See the
898 examples in "Sockets: Client/Server Communication" in perlipc.
899
900 continue BLOCK
901 continue
902 When followed by a BLOCK, "continue" is actually a flow control
903 statement rather than a function. If there is a "continue" BLOCK
904 attached to a BLOCK (typically in a "while" or "foreach"), it is
905 always executed just before the conditional is about to be
906 evaluated again, just like the third part of a "for" loop in C.
907 Thus it can be used to increment a loop variable, even when the
908 loop has been continued via the "next" statement (which is similar
909 to the C "continue" statement).
910
911 "last", "next", or "redo" may appear within a "continue" block;
912 "last" and "redo" behave as if they had been executed within the
913 main block. So will "next", but since it will execute a "continue"
914 block, it may be more entertaining.
915
916 while (EXPR) {
917 ### redo always comes here
918 do_something;
919 } continue {
920 ### next always comes here
921 do_something_else;
922 # then back the top to re-check EXPR
923 }
924 ### last always comes here
925
926 Omitting the "continue" section is equivalent to using an empty
927 one, logically enough, so "next" goes directly back to check the
928 condition at the top of the loop.
929
930 When there is no BLOCK, "continue" is a function that falls through
931 the current "when" or "default" block instead of iterating a
932 dynamically enclosing "foreach" or exiting a lexically enclosing
933 "given". In Perl 5.14 and earlier, this form of "continue" was
934 only available when the "switch" feature was enabled. See feature
935 and "Switch Statements" in perlsyn for more information.
936
937 cos EXPR
938 cos Returns the cosine of EXPR (expressed in radians). If EXPR is
939 omitted, takes the cosine of $_.
940
941 For the inverse cosine operation, you may use the
942 "Math::Trig::acos()" function, or use this relation:
943
944 sub acos { atan2( sqrt(1 - $_[0] * $_[0]), $_[0] ) }
945
946 crypt PLAINTEXT,SALT
947 Creates a digest string exactly like the crypt(3) function in the C
948 library (assuming that you actually have a version there that has
949 not been extirpated as a potential munition).
950
951 crypt() is a one-way hash function. The PLAINTEXT and SALT are
952 turned into a short string, called a digest, which is returned.
953 The same PLAINTEXT and SALT will always return the same string, but
954 there is no (known) way to get the original PLAINTEXT from the
955 hash. Small changes in the PLAINTEXT or SALT will result in large
956 changes in the digest.
957
958 There is no decrypt function. This function isn't all that useful
959 for cryptography (for that, look for Crypt modules on your nearby
960 CPAN mirror) and the name "crypt" is a bit of a misnomer. Instead
961 it is primarily used to check if two pieces of text are the same
962 without having to transmit or store the text itself. An example is
963 checking if a correct password is given. The digest of the
964 password is stored, not the password itself. The user types in a
965 password that is crypt()'d with the same salt as the stored digest.
966 If the two digests match, the password is correct.
967
968 When verifying an existing digest string you should use the digest
969 as the salt (like "crypt($plain, $digest) eq $digest"). The SALT
970 used to create the digest is visible as part of the digest. This
971 ensures crypt() will hash the new string with the same salt as the
972 digest. This allows your code to work with the standard crypt and
973 with more exotic implementations. In other words, assume nothing
974 about the returned string itself nor about how many bytes of SALT
975 may matter.
976
977 Traditionally the result is a string of 13 bytes: two first bytes
978 of the salt, followed by 11 bytes from the set "[./0-9A-Za-z]", and
979 only the first eight bytes of PLAINTEXT mattered. But alternative
980 hashing schemes (like MD5), higher level security schemes (like
981 C2), and implementations on non-Unix platforms may produce
982 different strings.
983
984 When choosing a new salt create a random two character string whose
985 characters come from the set "[./0-9A-Za-z]" (like "join '', ('.',
986 '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]"). This set of
987 characters is just a recommendation; the characters allowed in the
988 salt depend solely on your system's crypt library, and Perl can't
989 restrict what salts "crypt()" accepts.
990
991 Here's an example that makes sure that whoever runs this program
992 knows their password:
993
994 $pwd = (getpwuid($<))[1];
995
996 system "stty -echo";
997 print "Password: ";
998 chomp($word = <STDIN>);
999 print "\n";
1000 system "stty echo";
1001
1002 if (crypt($word, $pwd) ne $pwd) {
1003 die "Sorry...\n";
1004 } else {
1005 print "ok\n";
1006 }
1007
1008 Of course, typing in your own password to whoever asks you for it
1009 is unwise.
1010
1011 The crypt function is unsuitable for hashing large quantities of
1012 data, not least of all because you can't get the information back.
1013 Look at the Digest module for more robust algorithms.
1014
1015 If using crypt() on a Unicode string (which potentially has
1016 characters with codepoints above 255), Perl tries to make sense of
1017 the situation by trying to downgrade (a copy of) the string back to
1018 an eight-bit byte string before calling crypt() (on that copy). If
1019 that works, good. If not, crypt() dies with "Wide character in
1020 crypt".
1021
1022 Portability issues: "crypt" in perlport.
1023
1024 dbmclose HASH
1025 [This function has been largely superseded by the "untie"
1026 function.]
1027
1028 Breaks the binding between a DBM file and a hash.
1029
1030 Portability issues: "dbmclose" in perlport.
1031
1032 dbmopen HASH,DBNAME,MASK
1033 [This function has been largely superseded by the tie function.]
1034
1035 This binds a dbm(3), ndbm(3), sdbm(3), gdbm(3), or Berkeley DB file
1036 to a hash. HASH is the name of the hash. (Unlike normal "open",
1037 the first argument is not a filehandle, even though it looks like
1038 one). DBNAME is the name of the database (without the .dir or .pag
1039 extension if any). If the database does not exist, it is created
1040 with protection specified by MASK (as modified by the "umask"). To
1041 prevent creation of the database if it doesn't exist, you may
1042 specify a MODE of 0, and the function will return a false value if
1043 it can't find an existing database. If your system supports only
1044 the older DBM functions, you may make only one "dbmopen" call in
1045 your program. In older versions of Perl, if your system had
1046 neither DBM nor ndbm, calling "dbmopen" produced a fatal error; it
1047 now falls back to sdbm(3).
1048
1049 If you don't have write access to the DBM file, you can only read
1050 hash variables, not set them. If you want to test whether you can
1051 write, either use file tests or try setting a dummy hash entry
1052 inside an "eval" to trap the error.
1053
1054 Note that functions such as "keys" and "values" may return huge
1055 lists when used on large DBM files. You may prefer to use the
1056 "each" function to iterate over large DBM files. Example:
1057
1058 # print out history file offsets
1059 dbmopen(%HIST,'/usr/lib/news/history',0666);
1060 while (($key,$val) = each %HIST) {
1061 print $key, ' = ', unpack('L',$val), "\n";
1062 }
1063 dbmclose(%HIST);
1064
1065 See also AnyDBM_File for a more general description of the pros and
1066 cons of the various dbm approaches, as well as DB_File for a
1067 particularly rich implementation.
1068
1069 You can control which DBM library you use by loading that library
1070 before you call dbmopen():
1071
1072 use DB_File;
1073 dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")
1074 or die "Can't open netscape history file: $!";
1075
1076 Portability issues: "dbmopen" in perlport.
1077
1078 defined EXPR
1079 defined
1080 Returns a Boolean value telling whether EXPR has a value other than
1081 the undefined value "undef". If EXPR is not present, $_ is
1082 checked.
1083
1084 Many operations return "undef" to indicate failure, end of file,
1085 system error, uninitialized variable, and other exceptional
1086 conditions. This function allows you to distinguish "undef" from
1087 other values. (A simple Boolean test will not distinguish among
1088 "undef", zero, the empty string, and "0", which are all equally
1089 false.) Note that since "undef" is a valid scalar, its presence
1090 doesn't necessarily indicate an exceptional condition: "pop"
1091 returns "undef" when its argument is an empty array, or when the
1092 element to return happens to be "undef".
1093
1094 You may also use "defined(&func)" to check whether subroutine &func
1095 has ever been defined. The return value is unaffected by any
1096 forward declarations of &func. A subroutine that is not defined
1097 may still be callable: its package may have an "AUTOLOAD" method
1098 that makes it spring into existence the first time that it is
1099 called; see perlsub.
1100
1101 Use of "defined" on aggregates (hashes and arrays) is deprecated.
1102 It used to report whether memory for that aggregate had ever been
1103 allocated. This behavior may disappear in future versions of Perl.
1104 You should instead use a simple test for size:
1105
1106 if (@an_array) { print "has array elements\n" }
1107 if (%a_hash) { print "has hash members\n" }
1108
1109 When used on a hash element, it tells you whether the value is
1110 defined, not whether the key exists in the hash. Use "exists" for
1111 the latter purpose.
1112
1113 Examples:
1114
1115 print if defined $switch{D};
1116 print "$val\n" while defined($val = pop(@ary));
1117 die "Can't readlink $sym: $!"
1118 unless defined($value = readlink $sym);
1119 sub foo { defined &$bar ? &$bar(@_) : die "No bar"; }
1120 $debugging = 0 unless defined $debugging;
1121
1122 Note: Many folks tend to overuse "defined" and are then surprised
1123 to discover that the number 0 and "" (the zero-length string) are,
1124 in fact, defined values. For example, if you say
1125
1126 "ab" =~ /a(.*)b/;
1127
1128 The pattern match succeeds and $1 is defined, although it matched
1129 "nothing". It didn't really fail to match anything. Rather, it
1130 matched something that happened to be zero characters long. This
1131 is all very above-board and honest. When a function returns an
1132 undefined value, it's an admission that it couldn't give you an
1133 honest answer. So you should use "defined" only when questioning
1134 the integrity of what you're trying to do. At other times, a
1135 simple comparison to 0 or "" is what you want.
1136
1137 See also "undef", "exists", "ref".
1138
1139 delete EXPR
1140 Given an expression that specifies an element or slice of a hash,
1141 "delete" deletes the specified elements from that hash so that
1142 exists() on that element no longer returns true. Setting a hash
1143 element to the undefined value does not remove its key, but
1144 deleting it does; see "exists".
1145
1146 In list context, returns the value or values deleted, or the last
1147 such element in scalar context. The return list's length always
1148 matches that of the argument list: deleting non-existent elements
1149 returns the undefined value in their corresponding positions.
1150
1151 delete() may also be used on arrays and array slices, but its
1152 behavior is less straightforward. Although exists() will return
1153 false for deleted entries, deleting array elements never changes
1154 indices of existing values; use shift() or splice() for that.
1155 However, if all deleted elements fall at the end of an array, the
1156 array's size shrinks to the position of the highest element that
1157 still tests true for exists(), or to 0 if none do.
1158
1159 WARNING: Calling delete on array values is deprecated and likely to
1160 be removed in a future version of Perl.
1161
1162 Deleting from %ENV modifies the environment. Deleting from a hash
1163 tied to a DBM file deletes the entry from the DBM file. Deleting
1164 from a "tied" hash or array may not necessarily return anything; it
1165 depends on the implementation of the "tied" package's DELETE
1166 method, which may do whatever it pleases.
1167
1168 The "delete local EXPR" construct localizes the deletion to the
1169 current block at run time. Until the block exits, elements locally
1170 deleted temporarily no longer exist. See "Localized deletion of
1171 elements of composite types" in perlsub.
1172
1173 %hash = (foo => 11, bar => 22, baz => 33);
1174 $scalar = delete $hash{foo}; # $scalar is 11
1175 $scalar = delete @hash{qw(foo bar)}; # $scalar is 22
1176 @array = delete @hash{qw(foo bar baz)}; # @array is (undef,undef,33)
1177
1178 The following (inefficiently) deletes all the values of %HASH and
1179 @ARRAY:
1180
1181 foreach $key (keys %HASH) {
1182 delete $HASH{$key};
1183 }
1184
1185 foreach $index (0 .. $#ARRAY) {
1186 delete $ARRAY[$index];
1187 }
1188
1189 And so do these:
1190
1191 delete @HASH{keys %HASH};
1192
1193 delete @ARRAY[0 .. $#ARRAY];
1194
1195 But both are slower than assigning the empty list or undefining
1196 %HASH or @ARRAY, which is the customary way to empty out an
1197 aggregate:
1198
1199 %HASH = (); # completely empty %HASH
1200 undef %HASH; # forget %HASH ever existed
1201
1202 @ARRAY = (); # completely empty @ARRAY
1203 undef @ARRAY; # forget @ARRAY ever existed
1204
1205 The EXPR can be arbitrarily complicated provided its final
1206 operation is an element or slice of an aggregate:
1207
1208 delete $ref->[$x][$y]{$key};
1209 delete @{$ref->[$x][$y]}{$key1, $key2, @morekeys};
1210
1211 delete $ref->[$x][$y][$index];
1212 delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices];
1213
1214 die LIST
1215 "die" raises an exception. Inside an "eval" the error message is
1216 stuffed into $@ and the "eval" is terminated with the undefined
1217 value. If the exception is outside of all enclosing "eval"s, then
1218 the uncaught exception prints LIST to "STDERR" and exits with a
1219 non-zero value. If you need to exit the process with a specific
1220 exit code, see "exit".
1221
1222 Equivalent examples:
1223
1224 die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news';
1225 chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"
1226
1227 If the last element of LIST does not end in a newline, the current
1228 script line number and input line number (if any) are also printed,
1229 and a newline is supplied. Note that the "input line number" (also
1230 known as "chunk") is subject to whatever notion of "line" happens
1231 to be currently in effect, and is also available as the special
1232 variable $.. See "$/" in perlvar and "$." in perlvar.
1233
1234 Hint: sometimes appending ", stopped" to your message will cause it
1235 to make better sense when the string "at foo line 123" is appended.
1236 Suppose you are running script "canasta".
1237
1238 die "/etc/games is no good";
1239 die "/etc/games is no good, stopped";
1240
1241 produce, respectively
1242
1243 /etc/games is no good at canasta line 123.
1244 /etc/games is no good, stopped at canasta line 123.
1245
1246 If the output is empty and $@ already contains a value (typically
1247 from a previous eval) that value is reused after appending
1248 "\t...propagated". This is useful for propagating exceptions:
1249
1250 eval { ... };
1251 die unless $@ =~ /Expected exception/;
1252
1253 If the output is empty and $@ contains an object reference that has
1254 a "PROPAGATE" method, that method will be called with additional
1255 file and line number parameters. The return value replaces the
1256 value in $@; i.e., as if "$@ = eval { $@->PROPAGATE(__FILE__,
1257 __LINE__) };" were called.
1258
1259 If $@ is empty then the string "Died" is used.
1260
1261 If an uncaught exception results in interpreter exit, the exit code
1262 is determined from the values of $! and $? with this pseudocode:
1263
1264 exit $! if $!; # errno
1265 exit $? >> 8 if $? >> 8; # child exit status
1266 exit 255; # last resort
1267
1268 The intent is to squeeze as much possible information about the
1269 likely cause into the limited space of the system exit code.
1270 However, as $! is the value of C's "errno", which can be set by any
1271 system call, this means that the value of the exit code used by
1272 "die" can be non-predictable, so should not be relied upon, other
1273 than to be non-zero.
1274
1275 You can also call "die" with a reference argument, and if this is
1276 trapped within an "eval", $@ contains that reference. This permits
1277 more elaborate exception handling using objects that maintain
1278 arbitrary state about the exception. Such a scheme is sometimes
1279 preferable to matching particular string values of $@ with regular
1280 expressions. Because $@ is a global variable and "eval" may be
1281 used within object implementations, be careful that analyzing the
1282 error object doesn't replace the reference in the global variable.
1283 It's easiest to make a local copy of the reference before any
1284 manipulations. Here's an example:
1285
1286 use Scalar::Util "blessed";
1287
1288 eval { ... ; die Some::Module::Exception->new( FOO => "bar" ) };
1289 if (my $ev_err = $@) {
1290 if (blessed($ev_err) && $ev_err->isa("Some::Module::Exception")) {
1291 # handle Some::Module::Exception
1292 }
1293 else {
1294 # handle all other possible exceptions
1295 }
1296 }
1297
1298 Because Perl stringifies uncaught exception messages before
1299 display, you'll probably want to overload stringification
1300 operations on exception objects. See overload for details about
1301 that.
1302
1303 You can arrange for a callback to be run just before the "die" does
1304 its deed, by setting the $SIG{__DIE__} hook. The associated
1305 handler is called with the error text and can change the error
1306 message, if it sees fit, by calling "die" again. See "%SIG" in
1307 perlvar for details on setting %SIG entries, and "eval BLOCK" for
1308 some examples. Although this feature was to be run only right
1309 before your program was to exit, this is not currently so: the
1310 $SIG{__DIE__} hook is currently called even inside eval()ed
1311 blocks/strings! If one wants the hook to do nothing in such
1312 situations, put
1313
1314 die @_ if $^S;
1315
1316 as the first line of the handler (see "$^S" in perlvar). Because
1317 this promotes strange action at a distance, this counterintuitive
1318 behavior may be fixed in a future release.
1319
1320 See also exit(), warn(), and the Carp module.
1321
1322 do BLOCK
1323 Not really a function. Returns the value of the last command in
1324 the sequence of commands indicated by BLOCK. When modified by the
1325 "while" or "until" loop modifier, executes the BLOCK once before
1326 testing the loop condition. (On other statements the loop
1327 modifiers test the conditional first.)
1328
1329 "do BLOCK" does not count as a loop, so the loop control statements
1330 "next", "last", or "redo" cannot be used to leave or restart the
1331 block. See perlsyn for alternative strategies.
1332
1333 do SUBROUTINE(LIST)
1334 This form of subroutine call is deprecated. SUBROUTINE can be a
1335 bareword, a scalar variable or a subroutine beginning with "&".
1336
1337 do EXPR
1338 Uses the value of EXPR as a filename and executes the contents of
1339 the file as a Perl script.
1340
1341 do 'stat.pl';
1342
1343 is just like
1344
1345 eval `cat stat.pl`;
1346
1347 except that it's more efficient and concise, keeps track of the
1348 current filename for error messages, searches the @INC directories,
1349 and updates %INC if the file is found. See "@INC" in perlvar and
1350 "%INC" in perlvar for these variables. It also differs in that
1351 code evaluated with "do FILENAME" cannot see lexicals in the
1352 enclosing scope; "eval STRING" does. It's the same, however, in
1353 that it does reparse the file every time you call it, so you
1354 probably don't want to do this inside a loop.
1355
1356 If "do" can read the file but cannot compile it, it returns "undef"
1357 and sets an error message in $@. If "do" cannot read the file, it
1358 returns undef and sets $! to the error. Always check $@ first, as
1359 compilation could fail in a way that also sets $!. If the file is
1360 successfully compiled, "do" returns the value of the last
1361 expression evaluated.
1362
1363 Inclusion of library modules is better done with the "use" and
1364 "require" operators, which also do automatic error checking and
1365 raise an exception if there's a problem.
1366
1367 You might like to use "do" to read in a program configuration file.
1368 Manual error checking can be done this way:
1369
1370 # read in config files: system first, then user
1371 for $file ("/share/prog/defaults.rc",
1372 "$ENV{HOME}/.someprogrc")
1373 {
1374 unless ($return = do $file) {
1375 warn "couldn't parse $file: $@" if $@;
1376 warn "couldn't do $file: $!" unless defined $return;
1377 warn "couldn't run $file" unless $return;
1378 }
1379 }
1380
1381 dump LABEL
1382 dump
1383 This function causes an immediate core dump. See also the -u
1384 command-line switch in perlrun, which does the same thing.
1385 Primarily this is so that you can use the undump program (not
1386 supplied) to turn your core dump into an executable binary after
1387 having initialized all your variables at the beginning of the
1388 program. When the new binary is executed it will begin by
1389 executing a "goto LABEL" (with all the restrictions that "goto"
1390 suffers). Think of it as a goto with an intervening core dump and
1391 reincarnation. If "LABEL" is omitted, restarts the program from
1392 the top.
1393
1394 WARNING: Any files opened at the time of the dump will not be open
1395 any more when the program is reincarnated, with possible resulting
1396 confusion by Perl.
1397
1398 This function is now largely obsolete, mostly because it's very
1399 hard to convert a core file into an executable. That's why you
1400 should now invoke it as "CORE::dump()", if you don't want to be
1401 warned against a possible typo.
1402
1403 Portability issues: "dump" in perlport.
1404
1405 each HASH
1406 each ARRAY
1407 each EXPR
1408 When called on a hash in list context, returns a 2-element list
1409 consisting of the key and value for the next element of a hash. In
1410 Perl 5.12 and later only, it will also return the index and value
1411 for the next element of an array so that you can iterate over it;
1412 older Perls consider this a syntax error. When called in scalar
1413 context, returns only the key (not the value) in a hash, or the
1414 index in an array.
1415
1416 Hash entries are returned in an apparently random order. The
1417 actual random order is subject to change in future versions of
1418 Perl, but it is guaranteed to be in the same order as either the
1419 "keys" or "values" function would produce on the same (unmodified)
1420 hash. Since Perl 5.8.2 the ordering can be different even between
1421 different runs of Perl for security reasons (see "Algorithmic
1422 Complexity Attacks" in perlsec).
1423
1424 After "each" has returned all entries from the hash or array, the
1425 next call to "each" returns the empty list in list context and
1426 "undef" in scalar context; the next call following that one
1427 restarts iteration. Each hash or array has its own internal
1428 iterator, accessed by "each", "keys", and "values". The iterator
1429 is implicitly reset when "each" has reached the end as just
1430 described; it can be explicitly reset by calling "keys" or "values"
1431 on the hash or array. If you add or delete a hash's elements while
1432 iterating over it, entries may be skipped or duplicated--so don't
1433 do that. Exception: In the current implementation, it is always
1434 safe to delete the item most recently returned by "each()", so the
1435 following code works properly:
1436
1437 while (($key, $value) = each %hash) {
1438 print $key, "\n";
1439 delete $hash{$key}; # This is safe
1440 }
1441
1442 This prints out your environment like the printenv(1) program, but
1443 in a different order:
1444
1445 while (($key,$value) = each %ENV) {
1446 print "$key=$value\n";
1447 }
1448
1449 Starting with Perl 5.14, "each" can take a scalar EXPR, which must
1450 hold reference to an unblessed hash or array. The argument will be
1451 dereferenced automatically. This aspect of "each" is considered
1452 highly experimental. The exact behaviour may change in a future
1453 version of Perl.
1454
1455 while (($key,$value) = each $hashref) { ... }
1456
1457 To avoid confusing would-be users of your code who are running
1458 earlier versions of Perl with mysterious syntax errors, put this
1459 sort of thing at the top of your file to signal that your code will
1460 work only on Perls of a recent vintage:
1461
1462 use 5.012; # so keys/values/each work on arrays
1463 use 5.014; # so keys/values/each work on scalars (experimental)
1464
1465 See also "keys", "values", and "sort".
1466
1467 eof FILEHANDLE
1468 eof ()
1469 eof Returns 1 if the next read on FILEHANDLE will return end of file or
1470 if FILEHANDLE is not open. FILEHANDLE may be an expression whose
1471 value gives the real filehandle. (Note that this function actually
1472 reads a character and then "ungetc"s it, so isn't useful in an
1473 interactive context.) Do not read from a terminal file (or call
1474 "eof(FILEHANDLE)" on it) after end-of-file is reached. File types
1475 such as terminals may lose the end-of-file condition if you do.
1476
1477 An "eof" without an argument uses the last file read. Using
1478 "eof()" with empty parentheses is different. It refers to the
1479 pseudo file formed from the files listed on the command line and
1480 accessed via the "<>" operator. Since "<>" isn't explicitly
1481 opened, as a normal filehandle is, an "eof()" before "<>" has been
1482 used will cause @ARGV to be examined to determine if input is
1483 available. Similarly, an "eof()" after "<>" has returned end-of-
1484 file will assume you are processing another @ARGV list, and if you
1485 haven't set @ARGV, will read input from "STDIN"; see "I/O
1486 Operators" in perlop.
1487
1488 In a "while (<>)" loop, "eof" or "eof(ARGV)" can be used to detect
1489 the end of each file, whereas "eof()" will detect the end of the
1490 very last file only. Examples:
1491
1492 # reset line numbering on each input file
1493 while (<>) {
1494 next if /^\s*#/; # skip comments
1495 print "$.\t$_";
1496 } continue {
1497 close ARGV if eof; # Not eof()!
1498 }
1499
1500 # insert dashes just before last line of last file
1501 while (<>) {
1502 if (eof()) { # check for end of last file
1503 print "--------------\n";
1504 }
1505 print;
1506 last if eof(); # needed if we're reading from a terminal
1507 }
1508
1509 Practical hint: you almost never need to use "eof" in Perl, because
1510 the input operators typically return "undef" when they run out of
1511 data or encounter an error.
1512
1513 eval EXPR
1514 eval BLOCK
1515 eval
1516 In the first form, the return value of EXPR is parsed and executed
1517 as if it were a little Perl program. The value of the expression
1518 (which is itself determined within scalar context) is first parsed,
1519 and if there were no errors, executed as a block within the lexical
1520 context of the current Perl program. This means, that in
1521 particular, any outer lexical variables are visible to it, and any
1522 package variable settings or subroutine and format definitions
1523 remain afterwards.
1524
1525 Note that the value is parsed every time the "eval" executes. If
1526 EXPR is omitted, evaluates $_. This form is typically used to
1527 delay parsing and subsequent execution of the text of EXPR until
1528 run time.
1529
1530 If the "unicode_eval" feature is enabled (which is the default
1531 under a "use 5.16" or higher declaration), EXPR or $_ is treated as
1532 a string of characters, so "use utf8" declarations have no effect,
1533 and source filters are forbidden. In the absence of the
1534 "unicode_eval" feature, the string will sometimes be treated as
1535 characters and sometimes as bytes, depending on the internal
1536 encoding, and source filters activated within the "eval" exhibit
1537 the erratic, but historical, behaviour of affecting some outer file
1538 scope that is still compiling. See also the "evalbytes" keyword,
1539 which always treats its input as a byte stream and works properly
1540 with source filters, and the feature pragma.
1541
1542 In the second form, the code within the BLOCK is parsed only
1543 once--at the same time the code surrounding the "eval" itself was
1544 parsed--and executed within the context of the current Perl
1545 program. This form is typically used to trap exceptions more
1546 efficiently than the first (see below), while also providing the
1547 benefit of checking the code within BLOCK at compile time.
1548
1549 The final semicolon, if any, may be omitted from the value of EXPR
1550 or within the BLOCK.
1551
1552 In both forms, the value returned is the value of the last
1553 expression evaluated inside the mini-program; a return statement
1554 may be also used, just as with subroutines. The expression
1555 providing the return value is evaluated in void, scalar, or list
1556 context, depending on the context of the "eval" itself. See
1557 "wantarray" for more on how the evaluation context can be
1558 determined.
1559
1560 If there is a syntax error or runtime error, or a "die" statement
1561 is executed, "eval" returns "undef" in scalar context or an empty
1562 list in list context, and $@ is set to the error message. (Prior
1563 to 5.16, a bug caused "undef" to be returned in list context for
1564 syntax errors, but not for runtime errors.) If there was no error,
1565 $@ is set to the empty string. A control flow operator like "last"
1566 or "goto" can bypass the setting of $@. Beware that using "eval"
1567 neither silences Perl from printing warnings to STDERR, nor does it
1568 stuff the text of warning messages into $@. To do either of those,
1569 you have to use the $SIG{__WARN__} facility, or turn off warnings
1570 inside the BLOCK or EXPR using "no warnings 'all'". See "warn",
1571 perlvar, warnings and perllexwarn.
1572
1573 Note that, because "eval" traps otherwise-fatal errors, it is
1574 useful for determining whether a particular feature (such as
1575 "socket" or "symlink") is implemented. It is also Perl's
1576 exception-trapping mechanism, where the die operator is used to
1577 raise exceptions.
1578
1579 If you want to trap errors when loading an XS module, some problems
1580 with the binary interface (such as Perl version skew) may be fatal
1581 even with "eval" unless $ENV{PERL_DL_NONLAZY} is set. See perlrun.
1582
1583 If the code to be executed doesn't vary, you may use the eval-BLOCK
1584 form to trap run-time errors without incurring the penalty of
1585 recompiling each time. The error, if any, is still returned in $@.
1586 Examples:
1587
1588 # make divide-by-zero nonfatal
1589 eval { $answer = $a / $b; }; warn $@ if $@;
1590
1591 # same thing, but less efficient
1592 eval '$answer = $a / $b'; warn $@ if $@;
1593
1594 # a compile-time error
1595 eval { $answer = }; # WRONG
1596
1597 # a run-time error
1598 eval '$answer ='; # sets $@
1599
1600 Using the "eval{}" form as an exception trap in libraries does have
1601 some issues. Due to the current arguably broken state of "__DIE__"
1602 hooks, you may wish not to trigger any "__DIE__" hooks that user
1603 code may have installed. You can use the "local $SIG{__DIE__}"
1604 construct for this purpose, as this example shows:
1605
1606 # a private exception trap for divide-by-zero
1607 eval { local $SIG{'__DIE__'}; $answer = $a / $b; };
1608 warn $@ if $@;
1609
1610 This is especially significant, given that "__DIE__" hooks can call
1611 "die" again, which has the effect of changing their error messages:
1612
1613 # __DIE__ hooks may modify error messages
1614 {
1615 local $SIG{'__DIE__'} =
1616 sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x };
1617 eval { die "foo lives here" };
1618 print $@ if $@; # prints "bar lives here"
1619 }
1620
1621 Because this promotes action at a distance, this counterintuitive
1622 behavior may be fixed in a future release.
1623
1624 With an "eval", you should be especially careful to remember what's
1625 being looked at when:
1626
1627 eval $x; # CASE 1
1628 eval "$x"; # CASE 2
1629
1630 eval '$x'; # CASE 3
1631 eval { $x }; # CASE 4
1632
1633 eval "\$$x++"; # CASE 5
1634 $$x++; # CASE 6
1635
1636 Cases 1 and 2 above behave identically: they run the code contained
1637 in the variable $x. (Although case 2 has misleading double quotes
1638 making the reader wonder what else might be happening (nothing
1639 is).) Cases 3 and 4 likewise behave in the same way: they run the
1640 code '$x', which does nothing but return the value of $x. (Case 4
1641 is preferred for purely visual reasons, but it also has the
1642 advantage of compiling at compile-time instead of at run-time.)
1643 Case 5 is a place where normally you would like to use double
1644 quotes, except that in this particular situation, you can just use
1645 symbolic references instead, as in case 6.
1646
1647 Before Perl 5.14, the assignment to $@ occurred before restoration
1648 of localized variables, which means that for your code to run on
1649 older versions, a temporary is required if you want to mask some
1650 but not all errors:
1651
1652 # alter $@ on nefarious repugnancy only
1653 {
1654 my $e;
1655 {
1656 local $@; # protect existing $@
1657 eval { test_repugnancy() };
1658 # $@ =~ /nefarious/ and die $@; # Perl 5.14 and higher only
1659 $@ =~ /nefarious/ and $e = $@;
1660 }
1661 die $e if defined $e
1662 }
1663
1664 "eval BLOCK" does not count as a loop, so the loop control
1665 statements "next", "last", or "redo" cannot be used to leave or
1666 restart the block.
1667
1668 An "eval ''" executed within the "DB" package doesn't see the usual
1669 surrounding lexical scope, but rather the scope of the first non-DB
1670 piece of code that called it. You don't normally need to worry
1671 about this unless you are writing a Perl debugger.
1672
1673 evalbytes EXPR
1674 evalbytes
1675 This function is like "eval" with a string argument, except it
1676 always parses its argument, or $_ if EXPR is omitted, as a string
1677 of bytes. A string containing characters whose ordinal value
1678 exceeds 255 results in an error. Source filters activated within
1679 the evaluated code apply to the code itself.
1680
1681 This function is only available under the "evalbytes" feature, a
1682 "use v5.16" (or higher) declaration, or with a "CORE::" prefix.
1683 See feature for more information.
1684
1685 exec LIST
1686 exec PROGRAM LIST
1687 The "exec" function executes a system command and never returns;
1688 use "system" instead of "exec" if you want it to return. It fails
1689 and returns false only if the command does not exist and it is
1690 executed directly instead of via your system's command shell (see
1691 below).
1692
1693 Since it's a common mistake to use "exec" instead of "system", Perl
1694 warns you if "exec" is called in void context and if there is a
1695 following statement that isn't "die", "warn", or "exit" (if "-w" is
1696 set--but you always do that, right?). If you really want to follow
1697 an "exec" with some other statement, you can use one of these
1698 styles to avoid the warning:
1699
1700 exec ('foo') or print STDERR "couldn't exec foo: $!";
1701 { exec ('foo') }; print STDERR "couldn't exec foo: $!";
1702
1703 If there is more than one argument in LIST, or if LIST is an array
1704 with more than one value, calls execvp(3) with the arguments in
1705 LIST. If there is only one scalar argument or an array with one
1706 element in it, the argument is checked for shell metacharacters,
1707 and if there are any, the entire argument is passed to the system's
1708 command shell for parsing (this is "/bin/sh -c" on Unix platforms,
1709 but varies on other platforms). If there are no shell
1710 metacharacters in the argument, it is split into words and passed
1711 directly to "execvp", which is more efficient. Examples:
1712
1713 exec '/bin/echo', 'Your arguments are: ', @ARGV;
1714 exec "sort $outfile | uniq";
1715
1716 If you don't really want to execute the first argument, but want to
1717 lie to the program you are executing about its own name, you can
1718 specify the program you actually want to run as an "indirect
1719 object" (without a comma) in front of the LIST. (This always
1720 forces interpretation of the LIST as a multivalued list, even if
1721 there is only a single scalar in the list.) Example:
1722
1723 $shell = '/bin/csh';
1724 exec $shell '-sh'; # pretend it's a login shell
1725
1726 or, more directly,
1727
1728 exec {'/bin/csh'} '-sh'; # pretend it's a login shell
1729
1730 When the arguments get executed via the system shell, results are
1731 subject to its quirks and capabilities. See "`STRING`" in perlop
1732 for details.
1733
1734 Using an indirect object with "exec" or "system" is also more
1735 secure. This usage (which also works fine with system()) forces
1736 interpretation of the arguments as a multivalued list, even if the
1737 list had just one argument. That way you're safe from the shell
1738 expanding wildcards or splitting up words with whitespace in them.
1739
1740 @args = ( "echo surprise" );
1741
1742 exec @args; # subject to shell escapes
1743 # if @args == 1
1744 exec { $args[0] } @args; # safe even with one-arg list
1745
1746 The first version, the one without the indirect object, ran the
1747 echo program, passing it "surprise" an argument. The second
1748 version didn't; it tried to run a program named "echo surprise",
1749 didn't find it, and set $? to a non-zero value indicating failure.
1750
1751 Beginning with v5.6.0, Perl attempts to flush all files opened for
1752 output before the exec, but this may not be supported on some
1753 platforms (see perlport). To be safe, you may need to set $|
1754 ($AUTOFLUSH in English) or call the "autoflush()" method of
1755 "IO::Handle" on any open handles to avoid lost output.
1756
1757 Note that "exec" will not call your "END" blocks, nor will it
1758 invoke "DESTROY" methods on your objects.
1759
1760 Portability issues: "exec" in perlport.
1761
1762 exists EXPR
1763 Given an expression that specifies an element of a hash, returns
1764 true if the specified element in the hash has ever been
1765 initialized, even if the corresponding value is undefined.
1766
1767 print "Exists\n" if exists $hash{$key};
1768 print "Defined\n" if defined $hash{$key};
1769 print "True\n" if $hash{$key};
1770
1771 exists may also be called on array elements, but its behavior is
1772 much less obvious and is strongly tied to the use of "delete" on
1773 arrays. Be aware that calling exists on array values is deprecated
1774 and likely to be removed in a future version of Perl.
1775
1776 print "Exists\n" if exists $array[$index];
1777 print "Defined\n" if defined $array[$index];
1778 print "True\n" if $array[$index];
1779
1780 A hash or array element can be true only if it's defined and
1781 defined only if it exists, but the reverse doesn't necessarily hold
1782 true.
1783
1784 Given an expression that specifies the name of a subroutine,
1785 returns true if the specified subroutine has ever been declared,
1786 even if it is undefined. Mentioning a subroutine name for exists
1787 or defined does not count as declaring it. Note that a subroutine
1788 that does not exist may still be callable: its package may have an
1789 "AUTOLOAD" method that makes it spring into existence the first
1790 time that it is called; see perlsub.
1791
1792 print "Exists\n" if exists &subroutine;
1793 print "Defined\n" if defined &subroutine;
1794
1795 Note that the EXPR can be arbitrarily complicated as long as the
1796 final operation is a hash or array key lookup or subroutine name:
1797
1798 if (exists $ref->{A}->{B}->{$key}) { }
1799 if (exists $hash{A}{B}{$key}) { }
1800
1801 if (exists $ref->{A}->{B}->[$ix]) { }
1802 if (exists $hash{A}{B}[$ix]) { }
1803
1804 if (exists &{$ref->{A}{B}{$key}}) { }
1805
1806 Although the most deeply nested array or hash element will not
1807 spring into existence just because its existence was tested, any
1808 intervening ones will. Thus "$ref->{"A"}" and "$ref->{"A"}->{"B"}"
1809 will spring into existence due to the existence test for the $key
1810 element above. This happens anywhere the arrow operator is used,
1811 including even here:
1812
1813 undef $ref;
1814 if (exists $ref->{"Some key"}) { }
1815 print $ref; # prints HASH(0x80d3d5c)
1816
1817 This surprising autovivification in what does not at first--or even
1818 second--glance appear to be an lvalue context may be fixed in a
1819 future release.
1820
1821 Use of a subroutine call, rather than a subroutine name, as an
1822 argument to exists() is an error.
1823
1824 exists ⊂ # OK
1825 exists &sub(); # Error
1826
1827 exit EXPR
1828 exit
1829 Evaluates EXPR and exits immediately with that value. Example:
1830
1831 $ans = <STDIN>;
1832 exit 0 if $ans =~ /^[Xx]/;
1833
1834 See also "die". If EXPR is omitted, exits with 0 status. The only
1835 universally recognized values for EXPR are 0 for success and 1 for
1836 error; other values are subject to interpretation depending on the
1837 environment in which the Perl program is running. For example,
1838 exiting 69 (EX_UNAVAILABLE) from a sendmail incoming-mail filter
1839 will cause the mailer to return the item undelivered, but that's
1840 not true everywhere.
1841
1842 Don't use "exit" to abort a subroutine if there's any chance that
1843 someone might want to trap whatever error happened. Use "die"
1844 instead, which can be trapped by an "eval".
1845
1846 The exit() function does not always exit immediately. It calls any
1847 defined "END" routines first, but these "END" routines may not
1848 themselves abort the exit. Likewise any object destructors that
1849 need to be called are called before the real exit. "END" routines
1850 and destructors can change the exit status by modifying $?. If
1851 this is a problem, you can call "POSIX::_exit($status)" to avoid
1852 END and destructor processing. See perlmod for details.
1853
1854 Portability issues: "exit" in perlport.
1855
1856 exp EXPR
1857 exp Returns e (the natural logarithm base) to the power of EXPR. If
1858 EXPR is omitted, gives "exp($_)".
1859
1860 fc EXPR
1861 fc Returns the casefolded version of EXPR. This is the internal
1862 function implementing the "\F" escape in double-quoted strings.
1863
1864 Casefolding is the process of mapping strings to a form where case
1865 differences are erased; comparing two strings in their casefolded
1866 form is effectively a way of asking if two strings are equal,
1867 regardless of case.
1868
1869 Roughly, if you ever found yourself writing this
1870
1871 lc($this) eq lc($that) # Wrong!
1872 # or
1873 uc($this) eq uc($that) # Also wrong!
1874 # or
1875 $this =~ /\Q$that/i # Right!
1876
1877 Now you can write
1878
1879 fc($this) eq fc($that)
1880
1881 And get the correct results.
1882
1883 Perl only implements the full form of casefolding. For further
1884 information on casefolding, refer to the Unicode Standard,
1885 specifically sections 3.13 "Default Case Operations", 4.2
1886 "Case-Normative", and 5.18 "Case Mappings", available at
1887 <http://www.unicode.org/versions/latest/>, as well as the Case
1888 Charts available at <http://www.unicode.org/charts/case/>.
1889
1890 If EXPR is omitted, uses $_.
1891
1892 This function behaves the same way under various pragma, such as in
1893 a locale, as "lc" does.
1894
1895 While the Unicode Standard defines two additional forms of
1896 casefolding, one for Turkic languages and one that never maps one
1897 character into multiple characters, these are not provided by the
1898 Perl core; However, the CPAN module "Unicode::Casing" may be used
1899 to provide an implementation.
1900
1901 This keyword is available only when the "fc" feature is enabled, or
1902 when prefixed with "CORE::"; See feature. Alternately, include a
1903 "use v5.16" or later to the current scope.
1904
1905 fcntl FILEHANDLE,FUNCTION,SCALAR
1906 Implements the fcntl(2) function. You'll probably have to say
1907
1908 use Fcntl;
1909
1910 first to get the correct constant definitions. Argument processing
1911 and value returned work just like "ioctl" below. For example:
1912
1913 use Fcntl;
1914 fcntl($filehandle, F_GETFL, $packed_return_buffer)
1915 or die "can't fcntl F_GETFL: $!";
1916
1917 You don't have to check for "defined" on the return from "fcntl".
1918 Like "ioctl", it maps a 0 return from the system call into "0 but
1919 true" in Perl. This string is true in boolean context and 0 in
1920 numeric context. It is also exempt from the normal -w warnings on
1921 improper numeric conversions.
1922
1923 Note that "fcntl" raises an exception if used on a machine that
1924 doesn't implement fcntl(2). See the Fcntl module or your fcntl(2)
1925 manpage to learn what functions are available on your system.
1926
1927 Here's an example of setting a filehandle named "REMOTE" to be non-
1928 blocking at the system level. You'll have to negotiate $| on your
1929 own, though.
1930
1931 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
1932
1933 $flags = fcntl(REMOTE, F_GETFL, 0)
1934 or die "Can't get flags for the socket: $!\n";
1935
1936 $flags = fcntl(REMOTE, F_SETFL, $flags | O_NONBLOCK)
1937 or die "Can't set flags for the socket: $!\n";
1938
1939 Portability issues: "fcntl" in perlport.
1940
1941 __FILE__
1942 A special token that returns the name of the file in which it
1943 occurs.
1944
1945 fileno FILEHANDLE
1946 Returns the file descriptor for a filehandle, or undefined if the
1947 filehandle is not open. If there is no real file descriptor at the
1948 OS level, as can happen with filehandles connected to memory
1949 objects via "open" with a reference for the third argument, -1 is
1950 returned.
1951
1952 This is mainly useful for constructing bitmaps for "select" and
1953 low-level POSIX tty-handling operations. If FILEHANDLE is an
1954 expression, the value is taken as an indirect filehandle, generally
1955 its name.
1956
1957 You can use this to find out whether two handles refer to the same
1958 underlying descriptor:
1959
1960 if (fileno(THIS) == fileno(THAT)) {
1961 print "THIS and THAT are dups\n";
1962 }
1963
1964 flock FILEHANDLE,OPERATION
1965 Calls flock(2), or an emulation of it, on FILEHANDLE. Returns true
1966 for success, false on failure. Produces a fatal error if used on a
1967 machine that doesn't implement flock(2), fcntl(2) locking, or
1968 lockf(3). "flock" is Perl's portable file-locking interface,
1969 although it locks entire files only, not records.
1970
1971 Two potentially non-obvious but traditional "flock" semantics are
1972 that it waits indefinitely until the lock is granted, and that its
1973 locks are merely advisory. Such discretionary locks are more
1974 flexible, but offer fewer guarantees. This means that programs
1975 that do not also use "flock" may modify files locked with "flock".
1976 See perlport, your port's specific documentation, and your system-
1977 specific local manpages for details. It's best to assume
1978 traditional behavior if you're writing portable programs. (But if
1979 you're not, you should as always feel perfectly free to write for
1980 your own system's idiosyncrasies (sometimes called "features").
1981 Slavish adherence to portability concerns shouldn't get in the way
1982 of your getting your job done.)
1983
1984 OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly combined
1985 with LOCK_NB. These constants are traditionally valued 1, 2, 8 and
1986 4, but you can use the symbolic names if you import them from the
1987 Fcntl module, either individually, or as a group using the ":flock"
1988 tag. LOCK_SH requests a shared lock, LOCK_EX requests an exclusive
1989 lock, and LOCK_UN releases a previously requested lock. If LOCK_NB
1990 is bitwise-or'ed with LOCK_SH or LOCK_EX, then "flock" returns
1991 immediately rather than blocking waiting for the lock; check the
1992 return status to see if you got it.
1993
1994 To avoid the possibility of miscoordination, Perl now flushes
1995 FILEHANDLE before locking or unlocking it.
1996
1997 Note that the emulation built with lockf(3) doesn't provide shared
1998 locks, and it requires that FILEHANDLE be open with write intent.
1999 These are the semantics that lockf(3) implements. Most if not all
2000 systems implement lockf(3) in terms of fcntl(2) locking, though, so
2001 the differing semantics shouldn't bite too many people.
2002
2003 Note that the fcntl(2) emulation of flock(3) requires that
2004 FILEHANDLE be open with read intent to use LOCK_SH and requires
2005 that it be open with write intent to use LOCK_EX.
2006
2007 Note also that some versions of "flock" cannot lock things over the
2008 network; you would need to use the more system-specific "fcntl" for
2009 that. If you like you can force Perl to ignore your system's
2010 flock(2) function, and so provide its own fcntl(2)-based emulation,
2011 by passing the switch "-Ud_flock" to the Configure program when you
2012 configure and build a new Perl.
2013
2014 Here's a mailbox appender for BSD systems.
2015
2016 use Fcntl qw(:flock SEEK_END); # import LOCK_* and SEEK_END constants
2017
2018 sub lock {
2019 my ($fh) = @_;
2020 flock($fh, LOCK_EX) or die "Cannot lock mailbox - $!\n";
2021
2022 # and, in case someone appended while we were waiting...
2023 seek($fh, 0, SEEK_END) or die "Cannot seek - $!\n";
2024 }
2025
2026 sub unlock {
2027 my ($fh) = @_;
2028 flock($fh, LOCK_UN) or die "Cannot unlock mailbox - $!\n";
2029 }
2030
2031 open(my $mbox, ">>", "/usr/spool/mail/$ENV{'USER'}")
2032 or die "Can't open mailbox: $!";
2033
2034 lock($mbox);
2035 print $mbox $msg,"\n\n";
2036 unlock($mbox);
2037
2038 On systems that support a real flock(2), locks are inherited across
2039 fork() calls, whereas those that must resort to the more capricious
2040 fcntl(2) function lose their locks, making it seriously harder to
2041 write servers.
2042
2043 See also DB_File for other flock() examples.
2044
2045 Portability issues: "flock" in perlport.
2046
2047 fork
2048 Does a fork(2) system call to create a new process running the same
2049 program at the same point. It returns the child pid to the parent
2050 process, 0 to the child process, or "undef" if the fork is
2051 unsuccessful. File descriptors (and sometimes locks on those
2052 descriptors) are shared, while everything else is copied. On most
2053 systems supporting fork(), great care has gone into making it
2054 extremely efficient (for example, using copy-on-write technology on
2055 data pages), making it the dominant paradigm for multitasking over
2056 the last few decades.
2057
2058 Beginning with v5.6.0, Perl attempts to flush all files opened for
2059 output before forking the child process, but this may not be
2060 supported on some platforms (see perlport). To be safe, you may
2061 need to set $| ($AUTOFLUSH in English) or call the "autoflush()"
2062 method of "IO::Handle" on any open handles to avoid duplicate
2063 output.
2064
2065 If you "fork" without ever waiting on your children, you will
2066 accumulate zombies. On some systems, you can avoid this by setting
2067 $SIG{CHLD} to "IGNORE". See also perlipc for more examples of
2068 forking and reaping moribund children.
2069
2070 Note that if your forked child inherits system file descriptors
2071 like STDIN and STDOUT that are actually connected by a pipe or
2072 socket, even if you exit, then the remote server (such as, say, a
2073 CGI script or a backgrounded job launched from a remote shell)
2074 won't think you're done. You should reopen those to /dev/null if
2075 it's any issue.
2076
2077 On some platforms such as Windows, where the fork() system call is
2078 not available, Perl can be built to emulate fork() in the Perl
2079 interpreter. The emulation is designed, at the level of the Perl
2080 program, to be as compatible as possible with the "Unix" fork().
2081 However it has limitations that have to be considered in code
2082 intended to be portable. See perlfork for more details.
2083
2084 Portability issues: "fork" in perlport.
2085
2086 format
2087 Declare a picture format for use by the "write" function. For
2088 example:
2089
2090 format Something =
2091 Test: @<<<<<<<< @||||| @>>>>>
2092 $str, $%, '$' . int($num)
2093 .
2094
2095 $str = "widget";
2096 $num = $cost/$quantity;
2097 $~ = 'Something';
2098 write;
2099
2100 See perlform for many details and examples.
2101
2102 formline PICTURE,LIST
2103 This is an internal function used by "format"s, though you may call
2104 it, too. It formats (see perlform) a list of values according to
2105 the contents of PICTURE, placing the output into the format output
2106 accumulator, $^A (or $ACCUMULATOR in English). Eventually, when a
2107 "write" is done, the contents of $^A are written to some
2108 filehandle. You could also read $^A and then set $^A back to "".
2109 Note that a format typically does one "formline" per line of form,
2110 but the "formline" function itself doesn't care how many newlines
2111 are embedded in the PICTURE. This means that the "~" and "~~"
2112 tokens treat the entire PICTURE as a single line. You may
2113 therefore need to use multiple formlines to implement a single
2114 record format, just like the "format" compiler.
2115
2116 Be careful if you put double quotes around the picture, because an
2117 "@" character may be taken to mean the beginning of an array name.
2118 "formline" always returns true. See perlform for other examples.
2119
2120 If you are trying to use this instead of "write" to capture the
2121 output, you may find it easier to open a filehandle to a scalar
2122 ("open $fh, ">", \$output") and write to that instead.
2123
2124 getc FILEHANDLE
2125 getc
2126 Returns the next character from the input file attached to
2127 FILEHANDLE, or the undefined value at end of file or if there was
2128 an error (in the latter case $! is set). If FILEHANDLE is omitted,
2129 reads from STDIN. This is not particularly efficient. However, it
2130 cannot be used by itself to fetch single characters without waiting
2131 for the user to hit enter. For that, try something more like:
2132
2133 if ($BSD_STYLE) {
2134 system "stty cbreak </dev/tty >/dev/tty 2>&1";
2135 }
2136 else {
2137 system "stty", '-icanon', 'eol', "\001";
2138 }
2139
2140 $key = getc(STDIN);
2141
2142 if ($BSD_STYLE) {
2143 system "stty -cbreak </dev/tty >/dev/tty 2>&1";
2144 }
2145 else {
2146 system 'stty', 'icanon', 'eol', '^@'; # ASCII NUL
2147 }
2148 print "\n";
2149
2150 Determination of whether $BSD_STYLE should be set is left as an
2151 exercise to the reader.
2152
2153 The "POSIX::getattr" function can do this more portably on systems
2154 purporting POSIX compliance. See also the "Term::ReadKey" module
2155 from your nearest CPAN site; details on CPAN can be found under
2156 "CPAN" in perlmodlib.
2157
2158 getlogin
2159 This implements the C library function of the same name, which on
2160 most systems returns the current login from /etc/utmp, if any. If
2161 it returns the empty string, use "getpwuid".
2162
2163 $login = getlogin || getpwuid($<) || "Kilroy";
2164
2165 Do not consider "getlogin" for authentication: it is not as secure
2166 as "getpwuid".
2167
2168 Portability issues: "getlogin" in perlport.
2169
2170 getpeername SOCKET
2171 Returns the packed sockaddr address of the other end of the SOCKET
2172 connection.
2173
2174 use Socket;
2175 $hersockaddr = getpeername(SOCK);
2176 ($port, $iaddr) = sockaddr_in($hersockaddr);
2177 $herhostname = gethostbyaddr($iaddr, AF_INET);
2178 $herstraddr = inet_ntoa($iaddr);
2179
2180 getpgrp PID
2181 Returns the current process group for the specified PID. Use a PID
2182 of 0 to get the current process group for the current process.
2183 Will raise an exception if used on a machine that doesn't implement
2184 getpgrp(2). If PID is omitted, returns the process group of the
2185 current process. Note that the POSIX version of "getpgrp" does not
2186 accept a PID argument, so only "PID==0" is truly portable.
2187
2188 Portability issues: "getpgrp" in perlport.
2189
2190 getppid
2191 Returns the process id of the parent process.
2192
2193 Note for Linux users: Between v5.8.1 and v5.16.0 Perl would work
2194 around non-POSIX thread semantics the minority of Linux systems
2195 (and Debian GNU/kFreeBSD systems) that used LinuxThreads, this
2196 emulation has since been removed. See the documentation for $$ for
2197 details.
2198
2199 Portability issues: "getppid" in perlport.
2200
2201 getpriority WHICH,WHO
2202 Returns the current priority for a process, a process group, or a
2203 user. (See getpriority(2).) Will raise a fatal exception if used
2204 on a machine that doesn't implement getpriority(2).
2205
2206 Portability issues: "getpriority" in perlport.
2207
2208 getpwnam NAME
2209 getgrnam NAME
2210 gethostbyname NAME
2211 getnetbyname NAME
2212 getprotobyname NAME
2213 getpwuid UID
2214 getgrgid GID
2215 getservbyname NAME,PROTO
2216 gethostbyaddr ADDR,ADDRTYPE
2217 getnetbyaddr ADDR,ADDRTYPE
2218 getprotobynumber NUMBER
2219 getservbyport PORT,PROTO
2220 getpwent
2221 getgrent
2222 gethostent
2223 getnetent
2224 getprotoent
2225 getservent
2226 setpwent
2227 setgrent
2228 sethostent STAYOPEN
2229 setnetent STAYOPEN
2230 setprotoent STAYOPEN
2231 setservent STAYOPEN
2232 endpwent
2233 endgrent
2234 endhostent
2235 endnetent
2236 endprotoent
2237 endservent
2238 These routines are the same as their counterparts in the system C
2239 library. In list context, the return values from the various get
2240 routines are as follows:
2241
2242 ($name,$passwd,$uid,$gid,
2243 $quota,$comment,$gcos,$dir,$shell,$expire) = getpw*
2244 ($name,$passwd,$gid,$members) = getgr*
2245 ($name,$aliases,$addrtype,$length,@addrs) = gethost*
2246 ($name,$aliases,$addrtype,$net) = getnet*
2247 ($name,$aliases,$proto) = getproto*
2248 ($name,$aliases,$port,$proto) = getserv*
2249
2250 (If the entry doesn't exist you get an empty list.)
2251
2252 The exact meaning of the $gcos field varies but it usually contains
2253 the real name of the user (as opposed to the login name) and other
2254 information pertaining to the user. Beware, however, that in many
2255 system users are able to change this information and therefore it
2256 cannot be trusted and therefore the $gcos is tainted (see perlsec).
2257 The $passwd and $shell, user's encrypted password and login shell,
2258 are also tainted, for the same reason.
2259
2260 In scalar context, you get the name, unless the function was a
2261 lookup by name, in which case you get the other thing, whatever it
2262 is. (If the entry doesn't exist you get the undefined value.) For
2263 example:
2264
2265 $uid = getpwnam($name);
2266 $name = getpwuid($num);
2267 $name = getpwent();
2268 $gid = getgrnam($name);
2269 $name = getgrgid($num);
2270 $name = getgrent();
2271 #etc.
2272
2273 In getpw*() the fields $quota, $comment, and $expire are special in
2274 that they are unsupported on many systems. If the $quota is
2275 unsupported, it is an empty scalar. If it is supported, it usually
2276 encodes the disk quota. If the $comment field is unsupported, it
2277 is an empty scalar. If it is supported it usually encodes some
2278 administrative comment about the user. In some systems the $quota
2279 field may be $change or $age, fields that have to do with password
2280 aging. In some systems the $comment field may be $class. The
2281 $expire field, if present, encodes the expiration period of the
2282 account or the password. For the availability and the exact
2283 meaning of these fields in your system, please consult getpwnam(3)
2284 and your system's pwd.h file. You can also find out from within
2285 Perl what your $quota and $comment fields mean and whether you have
2286 the $expire field by using the "Config" module and the values
2287 "d_pwquota", "d_pwage", "d_pwchange", "d_pwcomment", and
2288 "d_pwexpire". Shadow password files are supported only if your
2289 vendor has implemented them in the intuitive fashion that calling
2290 the regular C library routines gets the shadow versions if you're
2291 running under privilege or if there exists the shadow(3) functions
2292 as found in System V (this includes Solaris and Linux). Those
2293 systems that implement a proprietary shadow password facility are
2294 unlikely to be supported.
2295
2296 The $members value returned by getgr*() is a space-separated list
2297 of the login names of the members of the group.
2298
2299 For the gethost*() functions, if the "h_errno" variable is
2300 supported in C, it will be returned to you via $? if the function
2301 call fails. The @addrs value returned by a successful call is a
2302 list of raw addresses returned by the corresponding library call.
2303 In the Internet domain, each address is four bytes long; you can
2304 unpack it by saying something like:
2305
2306 ($a,$b,$c,$d) = unpack('W4',$addr[0]);
2307
2308 The Socket library makes this slightly easier:
2309
2310 use Socket;
2311 $iaddr = inet_aton("127.1"); # or whatever address
2312 $name = gethostbyaddr($iaddr, AF_INET);
2313
2314 # or going the other way
2315 $straddr = inet_ntoa($iaddr);
2316
2317 In the opposite way, to resolve a hostname to the IP address you
2318 can write this:
2319
2320 use Socket;
2321 $packed_ip = gethostbyname("www.perl.org");
2322 if (defined $packed_ip) {
2323 $ip_address = inet_ntoa($packed_ip);
2324 }
2325
2326 Make sure "gethostbyname()" is called in SCALAR context and that
2327 its return value is checked for definedness.
2328
2329 The "getprotobynumber" function, even though it only takes one
2330 argument, has the precedence of a list operator, so beware:
2331
2332 getprotobynumber $number eq 'icmp' # WRONG
2333 getprotobynumber($number eq 'icmp') # actually means this
2334 getprotobynumber($number) eq 'icmp' # better this way
2335
2336 If you get tired of remembering which element of the return list
2337 contains which return value, by-name interfaces are provided in
2338 standard modules: "File::stat", "Net::hostent", "Net::netent",
2339 "Net::protoent", "Net::servent", "Time::gmtime", "Time::localtime",
2340 and "User::grent". These override the normal built-ins, supplying
2341 versions that return objects with the appropriate names for each
2342 field. For example:
2343
2344 use File::stat;
2345 use User::pwent;
2346 $is_his = (stat($filename)->uid == pwent($whoever)->uid);
2347
2348 Even though it looks as though they're the same method calls (uid),
2349 they aren't, because a "File::stat" object is different from a
2350 "User::pwent" object.
2351
2352 Portability issues: "getpwnam" in perlport to "endservent" in
2353 perlport.
2354
2355 getsockname SOCKET
2356 Returns the packed sockaddr address of this end of the SOCKET
2357 connection, in case you don't know the address because you have
2358 several different IPs that the connection might have come in on.
2359
2360 use Socket;
2361 $mysockaddr = getsockname(SOCK);
2362 ($port, $myaddr) = sockaddr_in($mysockaddr);
2363 printf "Connect to %s [%s]\n",
2364 scalar gethostbyaddr($myaddr, AF_INET),
2365 inet_ntoa($myaddr);
2366
2367 getsockopt SOCKET,LEVEL,OPTNAME
2368 Queries the option named OPTNAME associated with SOCKET at a given
2369 LEVEL. Options may exist at multiple protocol levels depending on
2370 the socket type, but at least the uppermost socket level SOL_SOCKET
2371 (defined in the "Socket" module) will exist. To query options at
2372 another level the protocol number of the appropriate protocol
2373 controlling the option should be supplied. For example, to
2374 indicate that an option is to be interpreted by the TCP protocol,
2375 LEVEL should be set to the protocol number of TCP, which you can
2376 get using "getprotobyname".
2377
2378 The function returns a packed string representing the requested
2379 socket option, or "undef" on error, with the reason for the error
2380 placed in $!. Just what is in the packed string depends on LEVEL
2381 and OPTNAME; consult getsockopt(2) for details. A common case is
2382 that the option is an integer, in which case the result is a packed
2383 integer, which you can decode using "unpack" with the "i" (or "I")
2384 format.
2385
2386 Here's an example to test whether Nagle's algorithm is enabled on a
2387 socket:
2388
2389 use Socket qw(:all);
2390
2391 defined(my $tcp = getprotobyname("tcp"))
2392 or die "Could not determine the protocol number for tcp";
2393 # my $tcp = IPPROTO_TCP; # Alternative
2394 my $packed = getsockopt($socket, $tcp, TCP_NODELAY)
2395 or die "getsockopt TCP_NODELAY: $!";
2396 my $nodelay = unpack("I", $packed);
2397 print "Nagle's algorithm is turned ", $nodelay ? "off\n" : "on\n";
2398
2399 Portability issues: "getsockopt" in perlport.
2400
2401 glob EXPR
2402 glob
2403 In list context, returns a (possibly empty) list of filename
2404 expansions on the value of EXPR such as the standard Unix shell
2405 /bin/csh would do. In scalar context, glob iterates through such
2406 filename expansions, returning undef when the list is exhausted.
2407 This is the internal function implementing the "<*.c>" operator,
2408 but you can use it directly. If EXPR is omitted, $_ is used. The
2409 "<*.c>" operator is discussed in more detail in "I/O Operators" in
2410 perlop.
2411
2412 Note that "glob" splits its arguments on whitespace and treats each
2413 segment as separate pattern. As such, "glob("*.c *.h")" matches
2414 all files with a .c or .h extension. The expression "glob(".* *")"
2415 matches all files in the current working directory. If you want to
2416 glob filenames that might contain whitespace, you'll have to use
2417 extra quotes around the spacey filename to protect it. For
2418 example, to glob filenames that have an "e" followed by a space
2419 followed by an "f", use either of:
2420
2421 @spacies = <"*e f*">;
2422 @spacies = glob '"*e f*"';
2423 @spacies = glob q("*e f*");
2424
2425 If you had to get a variable through, you could do this:
2426
2427 @spacies = glob "'*${var}e f*'";
2428 @spacies = glob qq("*${var}e f*");
2429
2430 If non-empty braces are the only wildcard characters used in the
2431 "glob", no filenames are matched, but potentially many strings are
2432 returned. For example, this produces nine strings, one for each
2433 pairing of fruits and colors:
2434
2435 @many = glob "{apple,tomato,cherry}={green,yellow,red}";
2436
2437 Beginning with v5.6.0, this operator is implemented using the
2438 standard "File::Glob" extension. See File::Glob for details,
2439 including "bsd_glob" which does not treat whitespace as a pattern
2440 separator.
2441
2442 Portability issues: "glob" in perlport.
2443
2444 gmtime EXPR
2445 gmtime
2446 Works just like "localtime" but the returned values are localized
2447 for the standard Greenwich time zone.
2448
2449 Note: When called in list context, $isdst, the last value returned
2450 by gmtime, is always 0. There is no Daylight Saving Time in GMT.
2451
2452 Portability issues: "gmtime" in perlport.
2453
2454 goto LABEL
2455 goto EXPR
2456 goto &NAME
2457 The "goto-LABEL" form finds the statement labeled with LABEL and
2458 resumes execution there. It can't be used to get out of a block or
2459 subroutine given to "sort". It can be used to go almost anywhere
2460 else within the dynamic scope, including out of subroutines, but
2461 it's usually better to use some other construct such as "last" or
2462 "die". The author of Perl has never felt the need to use this form
2463 of "goto" (in Perl, that is; C is another matter). (The difference
2464 is that C does not offer named loops combined with loop control.
2465 Perl does, and this replaces most structured uses of "goto" in
2466 other languages.)
2467
2468 The "goto-EXPR" form expects a label name, whose scope will be
2469 resolved dynamically. This allows for computed "goto"s per
2470 FORTRAN, but isn't necessarily recommended if you're optimizing for
2471 maintainability:
2472
2473 goto ("FOO", "BAR", "GLARCH")[$i];
2474
2475 As shown in this example, "goto-EXPR" is exempt from the "looks
2476 like a function" rule. A pair of parentheses following it does not
2477 (necessarily) delimit its argument. "goto("NE")."XT"" is
2478 equivalent to "goto NEXT".
2479
2480 Use of "goto-LABEL" or "goto-EXPR" to jump into a construct is
2481 deprecated and will issue a warning. Even then, it may not be used
2482 to go into any construct that requires initialization, such as a
2483 subroutine or a "foreach" loop. It also can't be used to go into a
2484 construct that is optimized away.
2485
2486 The "goto-&NAME" form is quite different from the other forms of
2487 "goto". In fact, it isn't a goto in the normal sense at all, and
2488 doesn't have the stigma associated with other gotos. Instead, it
2489 exits the current subroutine (losing any changes set by local())
2490 and immediately calls in its place the named subroutine using the
2491 current value of @_. This is used by "AUTOLOAD" subroutines that
2492 wish to load another subroutine and then pretend that the other
2493 subroutine had been called in the first place (except that any
2494 modifications to @_ in the current subroutine are propagated to the
2495 other subroutine.) After the "goto", not even "caller" will be
2496 able to tell that this routine was called first.
2497
2498 NAME needn't be the name of a subroutine; it can be a scalar
2499 variable containing a code reference or a block that evaluates to a
2500 code reference.
2501
2502 grep BLOCK LIST
2503 grep EXPR,LIST
2504 This is similar in spirit to, but not the same as, grep(1) and its
2505 relatives. In particular, it is not limited to using regular
2506 expressions.
2507
2508 Evaluates the BLOCK or EXPR for each element of LIST (locally
2509 setting $_ to each element) and returns the list value consisting
2510 of those elements for which the expression evaluated to true. In
2511 scalar context, returns the number of times the expression was
2512 true.
2513
2514 @foo = grep(!/^#/, @bar); # weed out comments
2515
2516 or equivalently,
2517
2518 @foo = grep {!/^#/} @bar; # weed out comments
2519
2520 Note that $_ is an alias to the list value, so it can be used to
2521 modify the elements of the LIST. While this is useful and
2522 supported, it can cause bizarre results if the elements of LIST are
2523 not variables. Similarly, grep returns aliases into the original
2524 list, much as a for loop's index variable aliases the list
2525 elements. That is, modifying an element of a list returned by grep
2526 (for example, in a "foreach", "map" or another "grep") actually
2527 modifies the element in the original list. This is usually
2528 something to be avoided when writing clear code.
2529
2530 If $_ is lexical in the scope where the "grep" appears (because it
2531 has been declared with "my $_") then, in addition to being locally
2532 aliased to the list elements, $_ keeps being lexical inside the
2533 block; i.e., it can't be seen from the outside, avoiding any
2534 potential side-effects.
2535
2536 See also "map" for a list composed of the results of the BLOCK or
2537 EXPR.
2538
2539 hex EXPR
2540 hex Interprets EXPR as a hex string and returns the corresponding
2541 value. (To convert strings that might start with either 0, "0x",
2542 or "0b", see "oct".) If EXPR is omitted, uses $_.
2543
2544 print hex '0xAf'; # prints '175'
2545 print hex 'aF'; # same
2546
2547 Hex strings may only represent integers. Strings that would cause
2548 integer overflow trigger a warning. Leading whitespace is not
2549 stripped, unlike oct(). To present something as hex, look into
2550 "printf", "sprintf", and "unpack".
2551
2552 import LIST
2553 There is no builtin "import" function. It is just an ordinary
2554 method (subroutine) defined (or inherited) by modules that wish to
2555 export names to another module. The "use" function calls the
2556 "import" method for the package used. See also "use", perlmod, and
2557 Exporter.
2558
2559 index STR,SUBSTR,POSITION
2560 index STR,SUBSTR
2561 The index function searches for one string within another, but
2562 without the wildcard-like behavior of a full regular-expression
2563 pattern match. It returns the position of the first occurrence of
2564 SUBSTR in STR at or after POSITION. If POSITION is omitted, starts
2565 searching from the beginning of the string. POSITION before the
2566 beginning of the string or after its end is treated as if it were
2567 the beginning or the end, respectively. POSITION and the return
2568 value are based at zero. If the substring is not found, "index"
2569 returns -1.
2570
2571 int EXPR
2572 int Returns the integer portion of EXPR. If EXPR is omitted, uses $_.
2573 You should not use this function for rounding: one because it
2574 truncates towards 0, and two because machine representations of
2575 floating-point numbers can sometimes produce counterintuitive
2576 results. For example, "int(-6.725/0.025)" produces -268 rather
2577 than the correct -269; that's because it's really more like
2578 -268.99999999999994315658 instead. Usually, the "sprintf",
2579 "printf", or the "POSIX::floor" and "POSIX::ceil" functions will
2580 serve you better than will int().
2581
2582 ioctl FILEHANDLE,FUNCTION,SCALAR
2583 Implements the ioctl(2) function. You'll probably first have to
2584 say
2585
2586 require "sys/ioctl.ph"; # probably in $Config{archlib}/sys/ioctl.ph
2587
2588 to get the correct function definitions. If sys/ioctl.ph doesn't
2589 exist or doesn't have the correct definitions you'll have to roll
2590 your own, based on your C header files such as <sys/ioctl.h>.
2591 (There is a Perl script called h2ph that comes with the Perl kit
2592 that may help you in this, but it's nontrivial.) SCALAR will be
2593 read and/or written depending on the FUNCTION; a C pointer to the
2594 string value of SCALAR will be passed as the third argument of the
2595 actual "ioctl" call. (If SCALAR has no string value but does have
2596 a numeric value, that value will be passed rather than a pointer to
2597 the string value. To guarantee this to be true, add a 0 to the
2598 scalar before using it.) The "pack" and "unpack" functions may be
2599 needed to manipulate the values of structures used by "ioctl".
2600
2601 The return value of "ioctl" (and "fcntl") is as follows:
2602
2603 if OS returns: then Perl returns:
2604 -1 undefined value
2605 0 string "0 but true"
2606 anything else that number
2607
2608 Thus Perl returns true on success and false on failure, yet you can
2609 still easily determine the actual value returned by the operating
2610 system:
2611
2612 $retval = ioctl(...) || -1;
2613 printf "System returned %d\n", $retval;
2614
2615 The special string "0 but true" is exempt from -w complaints about
2616 improper numeric conversions.
2617
2618 Portability issues: "ioctl" in perlport.
2619
2620 join EXPR,LIST
2621 Joins the separate strings of LIST into a single string with fields
2622 separated by the value of EXPR, and returns that new string.
2623 Example:
2624
2625 $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
2626
2627 Beware that unlike "split", "join" doesn't take a pattern as its
2628 first argument. Compare "split".
2629
2630 keys HASH
2631 keys ARRAY
2632 keys EXPR
2633 Called in list context, returns a list consisting of all the keys
2634 of the named hash, or in Perl 5.12 or later only, the indices of an
2635 array. Perl releases prior to 5.12 will produce a syntax error if
2636 you try to use an array argument. In scalar context, returns the
2637 number of keys or indices.
2638
2639 The keys of a hash are returned in an apparently random order. The
2640 actual random order is subject to change in future versions of
2641 Perl, but it is guaranteed to be the same order as either the
2642 "values" or "each" function produces (given that the hash has not
2643 been modified). Since Perl 5.8.1 the ordering can be different
2644 even between different runs of Perl for security reasons (see
2645 "Algorithmic Complexity Attacks" in perlsec).
2646
2647 As a side effect, calling keys() resets the internal interator of
2648 the HASH or ARRAY (see "each"). In particular, calling keys() in
2649 void context resets the iterator with no other overhead.
2650
2651 Here is yet another way to print your environment:
2652
2653 @keys = keys %ENV;
2654 @values = values %ENV;
2655 while (@keys) {
2656 print pop(@keys), '=', pop(@values), "\n";
2657 }
2658
2659 or how about sorted by key:
2660
2661 foreach $key (sort(keys %ENV)) {
2662 print $key, '=', $ENV{$key}, "\n";
2663 }
2664
2665 The returned values are copies of the original keys in the hash, so
2666 modifying them will not affect the original hash. Compare
2667 "values".
2668
2669 To sort a hash by value, you'll need to use a "sort" function.
2670 Here's a descending numeric sort of a hash by its values:
2671
2672 foreach $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
2673 printf "%4d %s\n", $hash{$key}, $key;
2674 }
2675
2676 Used as an lvalue, "keys" allows you to increase the number of hash
2677 buckets allocated for the given hash. This can gain you a measure
2678 of efficiency if you know the hash is going to get big. (This is
2679 similar to pre-extending an array by assigning a larger number to
2680 $#array.) If you say
2681
2682 keys %hash = 200;
2683
2684 then %hash will have at least 200 buckets allocated for it--256 of
2685 them, in fact, since it rounds up to the next power of two. These
2686 buckets will be retained even if you do "%hash = ()", use "undef
2687 %hash" if you want to free the storage while %hash is still in
2688 scope. You can't shrink the number of buckets allocated for the
2689 hash using "keys" in this way (but you needn't worry about doing
2690 this by accident, as trying has no effect). "keys @array" in an
2691 lvalue context is a syntax error.
2692
2693 Starting with Perl 5.14, "keys" can take a scalar EXPR, which must
2694 contain a reference to an unblessed hash or array. The argument
2695 will be dereferenced automatically. This aspect of "keys" is
2696 considered highly experimental. The exact behaviour may change in
2697 a future version of Perl.
2698
2699 for (keys $hashref) { ... }
2700 for (keys $obj->get_arrayref) { ... }
2701
2702 To avoid confusing would-be users of your code who are running
2703 earlier versions of Perl with mysterious syntax errors, put this
2704 sort of thing at the top of your file to signal that your code will
2705 work only on Perls of a recent vintage:
2706
2707 use 5.012; # so keys/values/each work on arrays
2708 use 5.014; # so keys/values/each work on scalars (experimental)
2709
2710 See also "each", "values", and "sort".
2711
2712 kill SIGNAL, LIST
2713 kill SIGNAL
2714 Sends a signal to a list of processes. Returns the number of
2715 processes successfully signaled (which is not necessarily the same
2716 as the number actually killed).
2717
2718 $cnt = kill 1, $child1, $child2;
2719 kill 9, @goners;
2720
2721 If SIGNAL is zero, no signal is sent to the process, but "kill"
2722 checks whether it's possible to send a signal to it (that means, to
2723 be brief, that the process is owned by the same user, or we are the
2724 super-user). This is useful to check that a child process is still
2725 alive (even if only as a zombie) and hasn't changed its UID. See
2726 perlport for notes on the portability of this construct.
2727
2728 Unlike in the shell, if SIGNAL is negative, it kills process groups
2729 instead of processes. That means you usually want to use positive
2730 not negative signals. You may also use a signal name in quotes.
2731
2732 The behavior of kill when a PROCESS number is zero or negative
2733 depends on the operating system. For example, on POSIX-conforming
2734 systems, zero will signal the current process group and -1 will
2735 signal all processes.
2736
2737 See "Signals" in perlipc for more details.
2738
2739 On some platforms such as Windows where the fork() system call is
2740 not available. Perl can be built to emulate fork() at the
2741 interpreter level. This emulation has limitations related to kill
2742 that have to be considered, for code running on Windows and in code
2743 intended to be portable.
2744
2745 See perlfork for more details.
2746
2747 If there is no LIST of processes, no signal is sent, and the return
2748 value is 0. This form is sometimes used, however, because it
2749 causes tainting checks to be run. But see "Laundering and
2750 Detecting Tainted Data" in perlsec.
2751
2752 Portability issues: "kill" in perlport.
2753
2754 last LABEL
2755 last
2756 The "last" command is like the "break" statement in C (as used in
2757 loops); it immediately exits the loop in question. If the LABEL is
2758 omitted, the command refers to the innermost enclosing loop. The
2759 "continue" block, if any, is not executed:
2760
2761 LINE: while (<STDIN>) {
2762 last LINE if /^$/; # exit when done with header
2763 #...
2764 }
2765
2766 "last" cannot be used to exit a block that returns a value such as
2767 "eval {}", "sub {}", or "do {}", and should not be used to exit a
2768 grep() or map() operation.
2769
2770 Note that a block by itself is semantically identical to a loop
2771 that executes once. Thus "last" can be used to effect an early
2772 exit out of such a block.
2773
2774 See also "continue" for an illustration of how "last", "next", and
2775 "redo" work.
2776
2777 lc EXPR
2778 lc Returns a lowercased version of EXPR. This is the internal
2779 function implementing the "\L" escape in double-quoted strings.
2780
2781 If EXPR is omitted, uses $_.
2782
2783 What gets returned depends on several factors:
2784
2785 If "use bytes" is in effect:
2786 On EBCDIC platforms
2787 The results are what the C language system call "tolower()"
2788 returns.
2789
2790 On ASCII platforms
2791 The results follow ASCII semantics. Only characters "A-Z"
2792 change, to "a-z" respectively.
2793
2794 Otherwise, if "use locale" (but not "use locale ':not_characters'")
2795 is in effect:
2796 Respects current LC_CTYPE locale for code points < 256; and
2797 uses Unicode semantics for the remaining code points (this last
2798 can only happen if the UTF8 flag is also set). See perllocale.
2799
2800 A deficiency in this is that case changes that cross the
2801 255/256 boundary are not well-defined. For example, the lower
2802 case of LATIN CAPITAL LETTER SHARP S (U+1E9E) in Unicode
2803 semantics is U+00DF (on ASCII platforms). But under "use
2804 locale", the lower case of U+1E9E is itself, because 0xDF may
2805 not be LATIN SMALL LETTER SHARP S in the current locale, and
2806 Perl has no way of knowing if that character even exists in the
2807 locale, much less what code point it is. Perl returns the
2808 input character unchanged, for all instances (and there aren't
2809 many) where the 255/256 boundary would otherwise be crossed.
2810
2811 Otherwise, If EXPR has the UTF8 flag set:
2812 Unicode semantics are used for the case change.
2813
2814 Otherwise, if "use feature 'unicode_strings'" or "use locale
2815 ':not_characters'") is in effect:
2816 Unicode semantics are used for the case change.
2817
2818 Otherwise:
2819 On EBCDIC platforms
2820 The results are what the C language system call "tolower()"
2821 returns.
2822
2823 On ASCII platforms
2824 ASCII semantics are used for the case change. The
2825 lowercase of any character outside the ASCII range is the
2826 character itself.
2827
2828 lcfirst EXPR
2829 lcfirst
2830 Returns the value of EXPR with the first character lowercased.
2831 This is the internal function implementing the "\l" escape in
2832 double-quoted strings.
2833
2834 If EXPR is omitted, uses $_.
2835
2836 This function behaves the same way under various pragmata, such as
2837 in a locale, as "lc" does.
2838
2839 length EXPR
2840 length
2841 Returns the length in characters of the value of EXPR. If EXPR is
2842 omitted, returns the length of $_. If EXPR is undefined, returns
2843 "undef".
2844
2845 This function cannot be used on an entire array or hash to find out
2846 how many elements these have. For that, use "scalar @array" and
2847 "scalar keys %hash", respectively.
2848
2849 Like all Perl character operations, length() normally deals in
2850 logical characters, not physical bytes. For how many bytes a
2851 string encoded as UTF-8 would take up, use
2852 "length(Encode::encode_utf8(EXPR))" (you'll have to "use Encode"
2853 first). See Encode and perlunicode.
2854
2855 __LINE__
2856 A special token that compiles to the current line number.
2857
2858 link OLDFILE,NEWFILE
2859 Creates a new filename linked to the old filename. Returns true
2860 for success, false otherwise.
2861
2862 Portability issues: "link" in perlport.
2863
2864 listen SOCKET,QUEUESIZE
2865 Does the same thing that the listen(2) system call does. Returns
2866 true if it succeeded, false otherwise. See the example in
2867 "Sockets: Client/Server Communication" in perlipc.
2868
2869 local EXPR
2870 You really probably want to be using "my" instead, because "local"
2871 isn't what most people think of as "local". See "Private Variables
2872 via my()" in perlsub for details.
2873
2874 A local modifies the listed variables to be local to the enclosing
2875 block, file, or eval. If more than one value is listed, the list
2876 must be placed in parentheses. See "Temporary Values via local()"
2877 in perlsub for details, including issues with tied arrays and
2878 hashes.
2879
2880 The "delete local EXPR" construct can also be used to localize the
2881 deletion of array/hash elements to the current block. See
2882 "Localized deletion of elements of composite types" in perlsub.
2883
2884 localtime EXPR
2885 localtime
2886 Converts a time as returned by the time function to a 9-element
2887 list with the time analyzed for the local time zone. Typically
2888 used as follows:
2889
2890 # 0 1 2 3 4 5 6 7 8
2891 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
2892 localtime(time);
2893
2894 All list elements are numeric and come straight out of the C
2895 `struct tm'. $sec, $min, and $hour are the seconds, minutes, and
2896 hours of the specified time.
2897
2898 $mday is the day of the month and $mon the month in the range
2899 0..11, with 0 indicating January and 11 indicating December. This
2900 makes it easy to get a month name from a list:
2901
2902 my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
2903 print "$abbr[$mon] $mday";
2904 # $mon=9, $mday=18 gives "Oct 18"
2905
2906 $year contains the number of years since 1900. To get a 4-digit
2907 year write:
2908
2909 $year += 1900;
2910
2911 To get the last two digits of the year (e.g., "01" in 2001) do:
2912
2913 $year = sprintf("%02d", $year % 100);
2914
2915 $wday is the day of the week, with 0 indicating Sunday and 3
2916 indicating Wednesday. $yday is the day of the year, in the range
2917 0..364 (or 0..365 in leap years.)
2918
2919 $isdst is true if the specified time occurs during Daylight Saving
2920 Time, false otherwise.
2921
2922 If EXPR is omitted, "localtime()" uses the current time (as
2923 returned by time(3)).
2924
2925 In scalar context, "localtime()" returns the ctime(3) value:
2926
2927 $now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994"
2928
2929 The format of this scalar value is not locale-dependent but built
2930 into Perl. For GMT instead of local time use the "gmtime" builtin.
2931 See also the "Time::Local" module (for converting seconds, minutes,
2932 hours, and such back to the integer value returned by time()), and
2933 the POSIX module's strftime(3) and mktime(3) functions.
2934
2935 To get somewhat similar but locale-dependent date strings, set up
2936 your locale environment variables appropriately (please see
2937 perllocale) and try for example:
2938
2939 use POSIX qw(strftime);
2940 $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
2941 # or for GMT formatted appropriately for your locale:
2942 $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;
2943
2944 Note that the %a and %b, the short forms of the day of the week and
2945 the month of the year, may not necessarily be three characters
2946 wide.
2947
2948 The Time::gmtime and Time::localtime modules provide a convenient,
2949 by-name access mechanism to the gmtime() and localtime() functions,
2950 respectively.
2951
2952 For a comprehensive date and time representation look at the
2953 DateTime module on CPAN.
2954
2955 Portability issues: "localtime" in perlport.
2956
2957 lock THING
2958 This function places an advisory lock on a shared variable or
2959 referenced object contained in THING until the lock goes out of
2960 scope.
2961
2962 The value returned is the scalar itself, if the argument is a
2963 scalar, or a reference, if the argument is a hash, array or
2964 subroutine.
2965
2966 lock() is a "weak keyword" : this means that if you've defined a
2967 function by this name (before any calls to it), that function will
2968 be called instead. If you are not under "use threads::shared" this
2969 does nothing. See threads::shared.
2970
2971 log EXPR
2972 log Returns the natural logarithm (base e) of EXPR. If EXPR is
2973 omitted, returns the log of $_. To get the log of another base,
2974 use basic algebra: The base-N log of a number is equal to the
2975 natural log of that number divided by the natural log of N. For
2976 example:
2977
2978 sub log10 {
2979 my $n = shift;
2980 return log($n)/log(10);
2981 }
2982
2983 See also "exp" for the inverse operation.
2984
2985 lstat FILEHANDLE
2986 lstat EXPR
2987 lstat DIRHANDLE
2988 lstat
2989 Does the same thing as the "stat" function (including setting the
2990 special "_" filehandle) but stats a symbolic link instead of the
2991 file the symbolic link points to. If symbolic links are
2992 unimplemented on your system, a normal "stat" is done. For much
2993 more detailed information, please see the documentation for "stat".
2994
2995 If EXPR is omitted, stats $_.
2996
2997 Portability issues: "lstat" in perlport.
2998
2999 m// The match operator. See "Regexp Quote-Like Operators" in perlop.
3000
3001 map BLOCK LIST
3002 map EXPR,LIST
3003 Evaluates the BLOCK or EXPR for each element of LIST (locally
3004 setting $_ to each element) and returns the list value composed of
3005 the results of each such evaluation. In scalar context, returns
3006 the total number of elements so generated. Evaluates BLOCK or EXPR
3007 in list context, so each element of LIST may produce zero, one, or
3008 more elements in the returned value.
3009
3010 @chars = map(chr, @numbers);
3011
3012 translates a list of numbers to the corresponding characters.
3013
3014 my @squares = map { $_ * $_ } @numbers;
3015
3016 translates a list of numbers to their squared values.
3017
3018 my @squares = map { $_ > 5 ? ($_ * $_) : () } @numbers;
3019
3020 shows that number of returned elements can differ from the number
3021 of input elements. To omit an element, return an empty list ().
3022 This could also be achieved by writing
3023
3024 my @squares = map { $_ * $_ } grep { $_ > 5 } @numbers;
3025
3026 which makes the intention more clear.
3027
3028 Map always returns a list, which can be assigned to a hash such
3029 that the elements become key/value pairs. See perldata for more
3030 details.
3031
3032 %hash = map { get_a_key_for($_) => $_ } @array;
3033
3034 is just a funny way to write
3035
3036 %hash = ();
3037 foreach (@array) {
3038 $hash{get_a_key_for($_)} = $_;
3039 }
3040
3041 Note that $_ is an alias to the list value, so it can be used to
3042 modify the elements of the LIST. While this is useful and
3043 supported, it can cause bizarre results if the elements of LIST are
3044 not variables. Using a regular "foreach" loop for this purpose
3045 would be clearer in most cases. See also "grep" for an array
3046 composed of those items of the original list for which the BLOCK or
3047 EXPR evaluates to true.
3048
3049 If $_ is lexical in the scope where the "map" appears (because it
3050 has been declared with "my $_"), then, in addition to being locally
3051 aliased to the list elements, $_ keeps being lexical inside the
3052 block; that is, it can't be seen from the outside, avoiding any
3053 potential side-effects.
3054
3055 "{" starts both hash references and blocks, so "map { ..." could be
3056 either the start of map BLOCK LIST or map EXPR, LIST. Because Perl
3057 doesn't look ahead for the closing "}" it has to take a guess at
3058 which it's dealing with based on what it finds just after the "{".
3059 Usually it gets it right, but if it doesn't it won't realize
3060 something is wrong until it gets to the "}" and encounters the
3061 missing (or unexpected) comma. The syntax error will be reported
3062 close to the "}", but you'll need to change something near the "{"
3063 such as using a unary "+" to give Perl some help:
3064
3065 %hash = map { "\L$_" => 1 } @array # perl guesses EXPR. wrong
3066 %hash = map { +"\L$_" => 1 } @array # perl guesses BLOCK. right
3067 %hash = map { ("\L$_" => 1) } @array # this also works
3068 %hash = map { lc($_) => 1 } @array # as does this.
3069 %hash = map +( lc($_) => 1 ), @array # this is EXPR and works!
3070
3071 %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array)
3072
3073 or to force an anon hash constructor use "+{":
3074
3075 @hashes = map +{ lc($_) => 1 }, @array # EXPR, so needs comma at end
3076
3077 to get a list of anonymous hashes each with only one entry apiece.
3078
3079 mkdir FILENAME,MASK
3080 mkdir FILENAME
3081 mkdir
3082 Creates the directory specified by FILENAME, with permissions
3083 specified by MASK (as modified by "umask"). If it succeeds it
3084 returns true; otherwise it returns false and sets $! (errno). MASK
3085 defaults to 0777 if omitted, and FILENAME defaults to $_ if
3086 omitted.
3087
3088 In general, it is better to create directories with a permissive
3089 MASK and let the user modify that with their "umask" than it is to
3090 supply a restrictive MASK and give the user no way to be more
3091 permissive. The exceptions to this rule are when the file or
3092 directory should be kept private (mail files, for instance). The
3093 perlfunc(1) entry on "umask" discusses the choice of MASK in more
3094 detail.
3095
3096 Note that according to the POSIX 1003.1-1996 the FILENAME may have
3097 any number of trailing slashes. Some operating and filesystems do
3098 not get this right, so Perl automatically removes all trailing
3099 slashes to keep everyone happy.
3100
3101 To recursively create a directory structure, look at the "mkpath"
3102 function of the File::Path module.
3103
3104 msgctl ID,CMD,ARG
3105 Calls the System V IPC function msgctl(2). You'll probably have to
3106 say
3107
3108 use IPC::SysV;
3109
3110 first to get the correct constant definitions. If CMD is
3111 "IPC_STAT", then ARG must be a variable that will hold the returned
3112 "msqid_ds" structure. Returns like "ioctl": the undefined value
3113 for error, "0 but true" for zero, or the actual return value
3114 otherwise. See also "SysV IPC" in perlipc and the documentation
3115 for "IPC::SysV" and "IPC::Semaphore".
3116
3117 Portability issues: "msgctl" in perlport.
3118
3119 msgget KEY,FLAGS
3120 Calls the System V IPC function msgget(2). Returns the message
3121 queue id, or "undef" on error. See also "SysV IPC" in perlipc and
3122 the documentation for "IPC::SysV" and "IPC::Msg".
3123
3124 Portability issues: "msgget" in perlport.
3125
3126 msgrcv ID,VAR,SIZE,TYPE,FLAGS
3127 Calls the System V IPC function msgrcv to receive a message from
3128 message queue ID into variable VAR with a maximum message size of
3129 SIZE. Note that when a message is received, the message type as a
3130 native long integer will be the first thing in VAR, followed by the
3131 actual message. This packing may be opened with "unpack("l! a*")".
3132 Taints the variable. Returns true if successful, false on error.
3133 See also "SysV IPC" in perlipc and the documentation for
3134 "IPC::SysV" and "IPC::SysV::Msg".
3135
3136 Portability issues: "msgrcv" in perlport.
3137
3138 msgsnd ID,MSG,FLAGS
3139 Calls the System V IPC function msgsnd to send the message MSG to
3140 the message queue ID. MSG must begin with the native long integer
3141 message type, be followed by the length of the actual message, and
3142 then finally the message itself. This kind of packing can be
3143 achieved with "pack("l! a*", $type, $message)". Returns true if
3144 successful, false on error. See also the "IPC::SysV" and
3145 "IPC::SysV::Msg" documentation.
3146
3147 Portability issues: "msgsnd" in perlport.
3148
3149 my EXPR
3150 my TYPE EXPR
3151 my EXPR : ATTRS
3152 my TYPE EXPR : ATTRS
3153 A "my" declares the listed variables to be local (lexically) to the
3154 enclosing block, file, or "eval". If more than one value is
3155 listed, the list must be placed in parentheses.
3156
3157 The exact semantics and interface of TYPE and ATTRS are still
3158 evolving. TYPE is currently bound to the use of the "fields"
3159 pragma, and attributes are handled using the "attributes" pragma,
3160 or starting from Perl 5.8.0 also via the "Attribute::Handlers"
3161 module. See "Private Variables via my()" in perlsub for details,
3162 and fields, attributes, and Attribute::Handlers.
3163
3164 next LABEL
3165 next
3166 The "next" command is like the "continue" statement in C; it starts
3167 the next iteration of the loop:
3168
3169 LINE: while (<STDIN>) {
3170 next LINE if /^#/; # discard comments
3171 #...
3172 }
3173
3174 Note that if there were a "continue" block on the above, it would
3175 get executed even on discarded lines. If LABEL is omitted, the
3176 command refers to the innermost enclosing loop.
3177
3178 "next" cannot be used to exit a block which returns a value such as
3179 "eval {}", "sub {}", or "do {}", and should not be used to exit a
3180 grep() or map() operation.
3181
3182 Note that a block by itself is semantically identical to a loop
3183 that executes once. Thus "next" will exit such a block early.
3184
3185 See also "continue" for an illustration of how "last", "next", and
3186 "redo" work.
3187
3188 no MODULE VERSION LIST
3189 no MODULE VERSION
3190 no MODULE LIST
3191 no MODULE
3192 no VERSION
3193 See the "use" function, of which "no" is the opposite.
3194
3195 oct EXPR
3196 oct Interprets EXPR as an octal string and returns the corresponding
3197 value. (If EXPR happens to start off with "0x", interprets it as a
3198 hex string. If EXPR starts off with "0b", it is interpreted as a
3199 binary string. Leading whitespace is ignored in all three cases.)
3200 The following will handle decimal, binary, octal, and hex in
3201 standard Perl notation:
3202
3203 $val = oct($val) if $val =~ /^0/;
3204
3205 If EXPR is omitted, uses $_. To go the other way (produce a
3206 number in octal), use sprintf() or printf():
3207
3208 $dec_perms = (stat("filename"))[2] & 07777;
3209 $oct_perm_str = sprintf "%o", $perms;
3210
3211 The oct() function is commonly used when a string such as 644 needs
3212 to be converted into a file mode, for example. Although Perl
3213 automatically converts strings into numbers as needed, this
3214 automatic conversion assumes base 10.
3215
3216 Leading white space is ignored without warning, as too are any
3217 trailing non-digits, such as a decimal point ("oct" only handles
3218 non-negative integers, not negative integers or floating point).
3219
3220 open FILEHANDLE,EXPR
3221 open FILEHANDLE,MODE,EXPR
3222 open FILEHANDLE,MODE,EXPR,LIST
3223 open FILEHANDLE,MODE,REFERENCE
3224 open FILEHANDLE
3225 Opens the file whose filename is given by EXPR, and associates it
3226 with FILEHANDLE.
3227
3228 Simple examples to open a file for reading:
3229
3230 open(my $fh, "<", "input.txt")
3231 or die "cannot open < input.txt: $!";
3232
3233 and for writing:
3234
3235 open(my $fh, ">", "output.txt")
3236 or die "cannot open > output.txt: $!";
3237
3238 (The following is a comprehensive reference to open(): for a
3239 gentler introduction you may consider perlopentut.)
3240
3241 If FILEHANDLE is an undefined scalar variable (or array or hash
3242 element), a new filehandle is autovivified, meaning that the
3243 variable is assigned a reference to a newly allocated anonymous
3244 filehandle. Otherwise if FILEHANDLE is an expression, its value is
3245 the real filehandle. (This is considered a symbolic reference, so
3246 "use strict "refs"" should not be in effect.)
3247
3248 If EXPR is omitted, the global (package) scalar variable of the
3249 same name as the FILEHANDLE contains the filename. (Note that
3250 lexical variables--those declared with "my" or "state"--will not
3251 work for this purpose; so if you're using "my" or "state", specify
3252 EXPR in your call to open.)
3253
3254 If three (or more) arguments are specified, the open mode
3255 (including optional encoding) in the second argument are distinct
3256 from the filename in the third. If MODE is "<" or nothing, the
3257 file is opened for input. If MODE is ">", the file is opened for
3258 output, with existing files first being truncated ("clobbered") and
3259 nonexisting files newly created. If MODE is ">>", the file is
3260 opened for appending, again being created if necessary.
3261
3262 You can put a "+" in front of the ">" or "<" to indicate that you
3263 want both read and write access to the file; thus "+<" is almost
3264 always preferred for read/write updates--the "+>" mode would
3265 clobber the file first. You can't usually use either read-write
3266 mode for updating textfiles, since they have variable-length
3267 records. See the -i switch in perlrun for a better approach. The
3268 file is created with permissions of 0666 modified by the process's
3269 "umask" value.
3270
3271 These various prefixes correspond to the fopen(3) modes of "r",
3272 "r+", "w", "w+", "a", and "a+".
3273
3274 In the one- and two-argument forms of the call, the mode and
3275 filename should be concatenated (in that order), preferably
3276 separated by white space. You can--but shouldn't--omit the mode in
3277 these forms when that mode is "<". It is always safe to use the
3278 two-argument form of "open" if the filename argument is a known
3279 literal.
3280
3281 For three or more arguments if MODE is "|-", the filename is
3282 interpreted as a command to which output is to be piped, and if
3283 MODE is "-|", the filename is interpreted as a command that pipes
3284 output to us. In the two-argument (and one-argument) form, one
3285 should replace dash ("-") with the command. See "Using open() for
3286 IPC" in perlipc for more examples of this. (You are not allowed to
3287 "open" to a command that pipes both in and out, but see IPC::Open2,
3288 IPC::Open3, and "Bidirectional Communication with Another Process"
3289 in perlipc for alternatives.)
3290
3291 In the form of pipe opens taking three or more arguments, if LIST
3292 is specified (extra arguments after the command name) then LIST
3293 becomes arguments to the command invoked if the platform supports
3294 it. The meaning of "open" with more than three arguments for non-
3295 pipe modes is not yet defined, but experimental "layers" may give
3296 extra LIST arguments meaning.
3297
3298 In the two-argument (and one-argument) form, opening "<-" or "-"
3299 opens STDIN and opening ">-" opens STDOUT.
3300
3301 You may (and usually should) use the three-argument form of open to
3302 specify I/O layers (sometimes referred to as "disciplines") to
3303 apply to the handle that affect how the input and output are
3304 processed (see open and PerlIO for more details). For example:
3305
3306 open(my $fh, "<:encoding(UTF-8)", "filename")
3307 || die "can't open UTF-8 encoded filename: $!";
3308
3309 opens the UTF8-encoded file containing Unicode characters; see
3310 perluniintro. Note that if layers are specified in the three-
3311 argument form, then default layers stored in ${^OPEN} (see perlvar;
3312 usually set by the open pragma or the switch -CioD) are ignored.
3313 Those layers will also be ignored if you specifying a colon with no
3314 name following it. In that case the default layer for the
3315 operating system (:raw on Unix, :crlf on Windows) is used.
3316
3317 Open returns nonzero on success, the undefined value otherwise. If
3318 the "open" involved a pipe, the return value happens to be the pid
3319 of the subprocess.
3320
3321 If you're running Perl on a system that distinguishes between text
3322 files and binary files, then you should check out "binmode" for
3323 tips for dealing with this. The key distinction between systems
3324 that need "binmode" and those that don't is their text file
3325 formats. Systems like Unix, Mac OS, and Plan 9, that end lines
3326 with a single character and encode that character in C as "\n" do
3327 not need "binmode". The rest need it.
3328
3329 When opening a file, it's seldom a good idea to continue if the
3330 request failed, so "open" is frequently used with "die". Even if
3331 "die" won't do what you want (say, in a CGI script, where you want
3332 to format a suitable error message (but there are modules that can
3333 help with that problem)) always check the return value from opening
3334 a file.
3335
3336 As a special case the three-argument form with a read/write mode
3337 and the third argument being "undef":
3338
3339 open(my $tmp, "+>", undef) or die ...
3340
3341 opens a filehandle to an anonymous temporary file. Also using "+<"
3342 works for symmetry, but you really should consider writing
3343 something to the temporary file first. You will need to seek() to
3344 do the reading.
3345
3346 Since v5.8.0, Perl has built using PerlIO by default. Unless
3347 you've changed this (such as building Perl with "Configure
3348 -Uuseperlio"), you can open filehandles directly to Perl scalars
3349 via:
3350
3351 open($fh, ">", \$variable) || ..
3352
3353 To (re)open "STDOUT" or "STDERR" as an in-memory file, close it
3354 first:
3355
3356 close STDOUT;
3357 open(STDOUT, ">", \$variable)
3358 or die "Can't open STDOUT: $!";
3359
3360 General examples:
3361
3362 $ARTICLE = 100;
3363 open(ARTICLE) or die "Can't find article $ARTICLE: $!\n";
3364 while (<ARTICLE>) {...
3365
3366 open(LOG, ">>/usr/spool/news/twitlog"); # (log is reserved)
3367 # if the open fails, output is discarded
3368
3369 open(my $dbase, "+<", "dbase.mine") # open for update
3370 or die "Can't open 'dbase.mine' for update: $!";
3371
3372 open(my $dbase, "+<dbase.mine") # ditto
3373 or die "Can't open 'dbase.mine' for update: $!";
3374
3375 open(ARTICLE, "-|", "caesar <$article") # decrypt article
3376 or die "Can't start caesar: $!";
3377
3378 open(ARTICLE, "caesar <$article |") # ditto
3379 or die "Can't start caesar: $!";
3380
3381 open(EXTRACT, "|sort >Tmp$$") # $$ is our process id
3382 or die "Can't start sort: $!";
3383
3384 # in-memory files
3385 open(MEMORY, ">", \$var)
3386 or die "Can't open memory file: $!";
3387 print MEMORY "foo!\n"; # output will appear in $var
3388
3389 # process argument list of files along with any includes
3390
3391 foreach $file (@ARGV) {
3392 process($file, "fh00");
3393 }
3394
3395 sub process {
3396 my($filename, $input) = @_;
3397 $input++; # this is a string increment
3398 unless (open($input, "<", $filename)) {
3399 print STDERR "Can't open $filename: $!\n";
3400 return;
3401 }
3402
3403 local $_;
3404 while (<$input>) { # note use of indirection
3405 if (/^#include "(.*)"/) {
3406 process($1, $input);
3407 next;
3408 }
3409 #... # whatever
3410 }
3411 }
3412
3413 See perliol for detailed info on PerlIO.
3414
3415 You may also, in the Bourne shell tradition, specify an EXPR
3416 beginning with ">&", in which case the rest of the string is
3417 interpreted as the name of a filehandle (or file descriptor, if
3418 numeric) to be duped (as dup(2)) and opened. You may use "&" after
3419 ">", ">>", "<", "+>", "+>>", and "+<". The mode you specify should
3420 match the mode of the original filehandle. (Duping a filehandle
3421 does not take into account any existing contents of IO buffers.)
3422 If you use the three-argument form, then you can pass either a
3423 number, the name of a filehandle, or the normal "reference to a
3424 glob".
3425
3426 Here is a script that saves, redirects, and restores "STDOUT" and
3427 "STDERR" using various methods:
3428
3429 #!/usr/bin/perl
3430 open(my $oldout, ">&STDOUT") or die "Can't dup STDOUT: $!";
3431 open(OLDERR, ">&", \*STDERR) or die "Can't dup STDERR: $!";
3432
3433 open(STDOUT, '>', "foo.out") or die "Can't redirect STDOUT: $!";
3434 open(STDERR, ">&STDOUT") or die "Can't dup STDOUT: $!";
3435
3436 select STDERR; $| = 1; # make unbuffered
3437 select STDOUT; $| = 1; # make unbuffered
3438
3439 print STDOUT "stdout 1\n"; # this works for
3440 print STDERR "stderr 1\n"; # subprocesses too
3441
3442 open(STDOUT, ">&", $oldout) or die "Can't dup \$oldout: $!";
3443 open(STDERR, ">&OLDERR") or die "Can't dup OLDERR: $!";
3444
3445 print STDOUT "stdout 2\n";
3446 print STDERR "stderr 2\n";
3447
3448 If you specify '<&=X', where "X" is a file descriptor number or a
3449 filehandle, then Perl will do an equivalent of C's "fdopen" of that
3450 file descriptor (and not call dup(2)); this is more parsimonious of
3451 file descriptors. For example:
3452
3453 # open for input, reusing the fileno of $fd
3454 open(FILEHANDLE, "<&=$fd")
3455
3456 or
3457
3458 open(FILEHANDLE, "<&=", $fd)
3459
3460 or
3461
3462 # open for append, using the fileno of OLDFH
3463 open(FH, ">>&=", OLDFH)
3464
3465 or
3466
3467 open(FH, ">>&=OLDFH")
3468
3469 Being parsimonious on filehandles is also useful (besides being
3470 parsimonious) for example when something is dependent on file
3471 descriptors, like for example locking using flock(). If you do
3472 just "open(A, ">>&B")", the filehandle A will not have the same
3473 file descriptor as B, and therefore flock(A) will not flock(B) nor
3474 vice versa. But with "open(A, ">>&=B")", the filehandles will
3475 share the same underlying system file descriptor.
3476
3477 Note that under Perls older than 5.8.0, Perl uses the standard C
3478 library's' fdopen() to implement the "=" functionality. On many
3479 Unix systems, fdopen() fails when file descriptors exceed a certain
3480 value, typically 255. For Perls 5.8.0 and later, PerlIO is (most
3481 often) the default.
3482
3483 You can see whether your Perl was built with PerlIO by running
3484 "perl -V" and looking for the "useperlio=" line. If "useperlio" is
3485 "define", you have PerlIO; otherwise you don't.
3486
3487 If you open a pipe on the command "-" (that is, specify either "|-"
3488 or "-|" with the one- or two-argument forms of "open"), an implicit
3489 "fork" is done, so "open" returns twice: in the parent process it
3490 returns the pid of the child process, and in the child process it
3491 returns (a defined) 0. Use "defined($pid)" or "//" to determine
3492 whether the open was successful.
3493
3494 For example, use either
3495
3496 $child_pid = open(FROM_KID, "-|") // die "can't fork: $!";
3497
3498 or
3499 $child_pid = open(TO_KID, "|-") // die "can't fork: $!";
3500
3501 followed by
3502
3503 if ($child_pid) {
3504 # am the parent:
3505 # either write TO_KID or else read FROM_KID
3506 ...
3507 wait $child_pid;
3508 } else {
3509 # am the child; use STDIN/STDOUT normally
3510 ...
3511 exit;
3512 }
3513
3514 The filehandle behaves normally for the parent, but I/O to that
3515 filehandle is piped from/to the STDOUT/STDIN of the child process.
3516 In the child process, the filehandle isn't opened--I/O happens
3517 from/to the new STDOUT/STDIN. Typically this is used like the
3518 normal piped open when you want to exercise more control over just
3519 how the pipe command gets executed, such as when running setuid and
3520 you don't want to have to scan shell commands for metacharacters.
3521
3522 The following blocks are more or less equivalent:
3523
3524 open(FOO, "|tr '[a-z]' '[A-Z]'");
3525 open(FOO, "|-", "tr '[a-z]' '[A-Z]'");
3526 open(FOO, "|-") || exec 'tr', '[a-z]', '[A-Z]';
3527 open(FOO, "|-", "tr", '[a-z]', '[A-Z]');
3528
3529 open(FOO, "cat -n '$file'|");
3530 open(FOO, "-|", "cat -n '$file'");
3531 open(FOO, "-|") || exec "cat", "-n", $file;
3532 open(FOO, "-|", "cat", "-n", $file);
3533
3534 The last two examples in each block show the pipe as "list form",
3535 which is not yet supported on all platforms. A good rule of thumb
3536 is that if your platform has a real "fork()" (in other words, if
3537 your platform is Unix, including Linux and MacOS X), you can use
3538 the list form. You would want to use the list form of the pipe so
3539 you can pass literal arguments to the command without risk of the
3540 shell interpreting any shell metacharacters in them. However, this
3541 also bars you from opening pipes to commands that intentionally
3542 contain shell metacharacters, such as:
3543
3544 open(FOO, "|cat -n | expand -4 | lpr")
3545 // die "Can't open pipeline to lpr: $!";
3546
3547 See "Safe Pipe Opens" in perlipc for more examples of this.
3548
3549 Beginning with v5.6.0, Perl will attempt to flush all files opened
3550 for output before any operation that may do a fork, but this may
3551 not be supported on some platforms (see perlport). To be safe, you
3552 may need to set $| ($AUTOFLUSH in English) or call the
3553 "autoflush()" method of "IO::Handle" on any open handles.
3554
3555 On systems that support a close-on-exec flag on files, the flag
3556 will be set for the newly opened file descriptor as determined by
3557 the value of $^F. See "$^F" in perlvar.
3558
3559 Closing any piped filehandle causes the parent process to wait for
3560 the child to finish, then returns the status value in $? and
3561 "${^CHILD_ERROR_NATIVE}".
3562
3563 The filename passed to the one- and two-argument forms of open()
3564 will have leading and trailing whitespace deleted and normal
3565 redirection characters honored. This property, known as "magic
3566 open", can often be used to good effect. A user could specify a
3567 filename of "rsh cat file |", or you could change certain filenames
3568 as needed:
3569
3570 $filename =~ s/(.*\.gz)\s*$/gzip -dc < $1|/;
3571 open(FH, $filename) or die "Can't open $filename: $!";
3572
3573 Use the three-argument form to open a file with arbitrary weird
3574 characters in it,
3575
3576 open(FOO, "<", $file)
3577 || die "can't open < $file: $!";
3578
3579 otherwise it's necessary to protect any leading and trailing
3580 whitespace:
3581
3582 $file =~ s#^(\s)#./$1#;
3583 open(FOO, "< $file\0")
3584 || die "open failed: $!";
3585
3586 (this may not work on some bizarre filesystems). One should
3587 conscientiously choose between the magic and three-argument form of
3588 open():
3589
3590 open(IN, $ARGV[0]) || die "can't open $ARGV[0]: $!";
3591
3592 will allow the user to specify an argument of the form "rsh cat
3593 file |", but will not work on a filename that happens to have a
3594 trailing space, while
3595
3596 open(IN, "<", $ARGV[0])
3597 || die "can't open < $ARGV[0]: $!";
3598
3599 will have exactly the opposite restrictions.
3600
3601 If you want a "real" C "open" (see open(2) on your system), then
3602 you should use the "sysopen" function, which involves no such magic
3603 (but may use subtly different filemodes than Perl open(), which is
3604 mapped to C fopen()). This is another way to protect your
3605 filenames from interpretation. For example:
3606
3607 use IO::Handle;
3608 sysopen(HANDLE, $path, O_RDWR|O_CREAT|O_EXCL)
3609 or die "sysopen $path: $!";
3610 $oldfh = select(HANDLE); $| = 1; select($oldfh);
3611 print HANDLE "stuff $$\n";
3612 seek(HANDLE, 0, 0);
3613 print "File contains: ", <HANDLE>;
3614
3615 Using the constructor from the "IO::Handle" package (or one of its
3616 subclasses, such as "IO::File" or "IO::Socket"), you can generate
3617 anonymous filehandles that have the scope of the variables used to
3618 hold them, then automatically (but silently) close once their
3619 reference counts become zero, typically at scope exit:
3620
3621 use IO::File;
3622 #...
3623 sub read_myfile_munged {
3624 my $ALL = shift;
3625 # or just leave it undef to autoviv
3626 my $handle = IO::File->new;
3627 open($handle, "<", "myfile") or die "myfile: $!";
3628 $first = <$handle>
3629 or return (); # Automatically closed here.
3630 mung($first) or die "mung failed"; # Or here.
3631 return (first, <$handle>) if $ALL; # Or here.
3632 return $first; # Or here.
3633 }
3634
3635 WARNING: The previous example has a bug because the automatic close
3636 that happens when the refcount on "handle" does not properly detect
3637 and report failures. Always close the handle yourself and inspect
3638 the return value.
3639
3640 close($handle)
3641 || warn "close failed: $!";
3642
3643 See "seek" for some details about mixing reading and writing.
3644
3645 Portability issues: "open" in perlport.
3646
3647 opendir DIRHANDLE,EXPR
3648 Opens a directory named EXPR for processing by "readdir",
3649 "telldir", "seekdir", "rewinddir", and "closedir". Returns true if
3650 successful. DIRHANDLE may be an expression whose value can be used
3651 as an indirect dirhandle, usually the real dirhandle name. If
3652 DIRHANDLE is an undefined scalar variable (or array or hash
3653 element), the variable is assigned a reference to a new anonymous
3654 dirhandle; that is, it's autovivified. DIRHANDLEs have their own
3655 namespace separate from FILEHANDLEs.
3656
3657 See the example at "readdir".
3658
3659 ord EXPR
3660 ord Returns the numeric value of the first character of EXPR. If EXPR
3661 is an empty string, returns 0. If EXPR is omitted, uses $_. (Note
3662 character, not byte.)
3663
3664 For the reverse, see "chr". See perlunicode for more about
3665 Unicode.
3666
3667 our EXPR
3668 our TYPE EXPR
3669 our EXPR : ATTRS
3670 our TYPE EXPR : ATTRS
3671 "our" associates a simple name with a package variable in the
3672 current package for use within the current scope. When "use strict
3673 'vars'" is in effect, "our" lets you use declared global variables
3674 without qualifying them with package names, within the lexical
3675 scope of the "our" declaration. In this way "our" differs from
3676 "use vars", which is package-scoped.
3677
3678 Unlike "my" or "state", which allocates storage for a variable and
3679 associates a simple name with that storage for use within the
3680 current scope, "our" associates a simple name with a package (read:
3681 global) variable in the current package, for use within the current
3682 lexical scope. In other words, "our" has the same scoping rules as
3683 "my" or "state", but does not necessarily create a variable.
3684
3685 If more than one value is listed, the list must be placed in
3686 parentheses.
3687
3688 our $foo;
3689 our($bar, $baz);
3690
3691 An "our" declaration declares a global variable that will be
3692 visible across its entire lexical scope, even across package
3693 boundaries. The package in which the variable is entered is
3694 determined at the point of the declaration, not at the point of
3695 use. This means the following behavior holds:
3696
3697 package Foo;
3698 our $bar; # declares $Foo::bar for rest of lexical scope
3699 $bar = 20;
3700
3701 package Bar;
3702 print $bar; # prints 20, as it refers to $Foo::bar
3703
3704 Multiple "our" declarations with the same name in the same lexical
3705 scope are allowed if they are in different packages. If they
3706 happen to be in the same package, Perl will emit warnings if you
3707 have asked for them, just like multiple "my" declarations. Unlike
3708 a second "my" declaration, which will bind the name to a fresh
3709 variable, a second "our" declaration in the same package, in the
3710 same scope, is merely redundant.
3711
3712 use warnings;
3713 package Foo;
3714 our $bar; # declares $Foo::bar for rest of lexical scope
3715 $bar = 20;
3716
3717 package Bar;
3718 our $bar = 30; # declares $Bar::bar for rest of lexical scope
3719 print $bar; # prints 30
3720
3721 our $bar; # emits warning but has no other effect
3722 print $bar; # still prints 30
3723
3724 An "our" declaration may also have a list of attributes associated
3725 with it.
3726
3727 The exact semantics and interface of TYPE and ATTRS are still
3728 evolving. TYPE is currently bound to the use of the "fields"
3729 pragma, and attributes are handled using the "attributes" pragma,
3730 or, starting from Perl 5.8.0, also via the "Attribute::Handlers"
3731 module. See "Private Variables via my()" in perlsub for details,
3732 and fields, attributes, and Attribute::Handlers.
3733
3734 pack TEMPLATE,LIST
3735 Takes a LIST of values and converts it into a string using the
3736 rules given by the TEMPLATE. The resulting string is the
3737 concatenation of the converted values. Typically, each converted
3738 value looks like its machine-level representation. For example, on
3739 32-bit machines an integer may be represented by a sequence of 4
3740 bytes, which will in Perl be presented as a string that's 4
3741 characters long.
3742
3743 See perlpacktut for an introduction to this function.
3744
3745 The TEMPLATE is a sequence of characters that give the order and
3746 type of values, as follows:
3747
3748 a A string with arbitrary binary data, will be null padded.
3749 A A text (ASCII) string, will be space padded.
3750 Z A null-terminated (ASCIZ) string, will be null padded.
3751
3752 b A bit string (ascending bit order inside each byte,
3753 like vec()).
3754 B A bit string (descending bit order inside each byte).
3755 h A hex string (low nybble first).
3756 H A hex string (high nybble first).
3757
3758 c A signed char (8-bit) value.
3759 C An unsigned char (octet) value.
3760 W An unsigned char value (can be greater than 255).
3761
3762 s A signed short (16-bit) value.
3763 S An unsigned short value.
3764
3765 l A signed long (32-bit) value.
3766 L An unsigned long value.
3767
3768 q A signed quad (64-bit) value.
3769 Q An unsigned quad value.
3770 (Quads are available only if your system supports 64-bit
3771 integer values _and_ if Perl has been compiled to support
3772 those. Raises an exception otherwise.)
3773
3774 i A signed integer value.
3775 I A unsigned integer value.
3776 (This 'integer' is _at_least_ 32 bits wide. Its exact
3777 size depends on what a local C compiler calls 'int'.)
3778
3779 n An unsigned short (16-bit) in "network" (big-endian) order.
3780 N An unsigned long (32-bit) in "network" (big-endian) order.
3781 v An unsigned short (16-bit) in "VAX" (little-endian) order.
3782 V An unsigned long (32-bit) in "VAX" (little-endian) order.
3783
3784 j A Perl internal signed integer value (IV).
3785 J A Perl internal unsigned integer value (UV).
3786
3787 f A single-precision float in native format.
3788 d A double-precision float in native format.
3789
3790 F A Perl internal floating-point value (NV) in native format
3791 D A float of long-double precision in native format.
3792 (Long doubles are available only if your system supports
3793 long double values _and_ if Perl has been compiled to
3794 support those. Raises an exception otherwise.)
3795
3796 p A pointer to a null-terminated string.
3797 P A pointer to a structure (fixed-length string).
3798
3799 u A uuencoded string.
3800 U A Unicode character number. Encodes to a character in char-
3801 acter mode and UTF-8 (or UTF-EBCDIC in EBCDIC platforms) in
3802 byte mode.
3803
3804 w A BER compressed integer (not an ASN.1 BER, see perlpacktut
3805 for details). Its bytes represent an unsigned integer in
3806 base 128, most significant digit first, with as few digits
3807 as possible. Bit eight (the high bit) is set on each byte
3808 except the last.
3809
3810 x A null byte (a.k.a ASCII NUL, "\000", chr(0))
3811 X Back up a byte.
3812 @ Null-fill or truncate to absolute position, counted from the
3813 start of the innermost ()-group.
3814 . Null-fill or truncate to absolute position specified by
3815 the value.
3816 ( Start of a ()-group.
3817
3818 One or more modifiers below may optionally follow certain letters
3819 in the TEMPLATE (the second column lists letters for which the
3820 modifier is valid):
3821
3822 ! sSlLiI Forces native (short, long, int) sizes instead
3823 of fixed (16-/32-bit) sizes.
3824
3825 xX Make x and X act as alignment commands.
3826
3827 nNvV Treat integers as signed instead of unsigned.
3828
3829 @. Specify position as byte offset in the internal
3830 representation of the packed string. Efficient
3831 but dangerous.
3832
3833 > sSiIlLqQ Force big-endian byte-order on the type.
3834 jJfFdDpP (The "big end" touches the construct.)
3835
3836 < sSiIlLqQ Force little-endian byte-order on the type.
3837 jJfFdDpP (The "little end" touches the construct.)
3838
3839 The ">" and "<" modifiers can also be used on "()" groups to force
3840 a particular byte-order on all components in that group, including
3841 all its subgroups.
3842
3843 The following rules apply:
3844
3845 · Each letter may optionally be followed by a number indicating
3846 the repeat count. A numeric repeat count may optionally be
3847 enclosed in brackets, as in "pack("C[80]", @arr)". The repeat
3848 count gobbles that many values from the LIST when used with all
3849 format types other than "a", "A", "Z", "b", "B", "h", "H", "@",
3850 ".", "x", "X", and "P", where it means something else,
3851 described below. Supplying a "*" for the repeat count instead
3852 of a number means to use however many items are left, except
3853 for:
3854
3855 · "@", "x", and "X", where it is equivalent to 0.
3856
3857 · <.>, where it means relative to the start of the string.
3858
3859 · "u", where it is equivalent to 1 (or 45, which here is
3860 equivalent).
3861
3862 One can replace a numeric repeat count with a template letter
3863 enclosed in brackets to use the packed byte length of the
3864 bracketed template for the repeat count.
3865
3866 For example, the template "x[L]" skips as many bytes as in a
3867 packed long, and the template "$t X[$t] $t" unpacks twice
3868 whatever $t (when variable-expanded) unpacks. If the template
3869 in brackets contains alignment commands (such as "x![d]"), its
3870 packed length is calculated as if the start of the template had
3871 the maximal possible alignment.
3872
3873 When used with "Z", a "*" as the repeat count is guaranteed to
3874 add a trailing null byte, so the resulting string is always one
3875 byte longer than the byte length of the item itself.
3876
3877 When used with "@", the repeat count represents an offset from
3878 the start of the innermost "()" group.
3879
3880 When used with ".", the repeat count determines the starting
3881 position to calculate the value offset as follows:
3882
3883 · If the repeat count is 0, it's relative to the current
3884 position.
3885
3886 · If the repeat count is "*", the offset is relative to the
3887 start of the packed string.
3888
3889 · And if it's an integer n, the offset is relative to the
3890 start of the nth innermost "( )" group, or to the start of
3891 the string if n is bigger then the group level.
3892
3893 The repeat count for "u" is interpreted as the maximal number
3894 of bytes to encode per line of output, with 0, 1 and 2 replaced
3895 by 45. The repeat count should not be more than 65.
3896
3897 · The "a", "A", and "Z" types gobble just one value, but pack it
3898 as a string of length count, padding with nulls or spaces as
3899 needed. When unpacking, "A" strips trailing whitespace and
3900 nulls, "Z" strips everything after the first null, and "a"
3901 returns data with no stripping at all.
3902
3903 If the value to pack is too long, the result is truncated. If
3904 it's too long and an explicit count is provided, "Z" packs only
3905 "$count-1" bytes, followed by a null byte. Thus "Z" always
3906 packs a trailing null, except when the count is 0.
3907
3908 · Likewise, the "b" and "B" formats pack a string that's that
3909 many bits long. Each such format generates 1 bit of the
3910 result. These are typically followed by a repeat count like
3911 "B8" or "B64".
3912
3913 Each result bit is based on the least-significant bit of the
3914 corresponding input character, i.e., on "ord($char)%2". In
3915 particular, characters "0" and "1" generate bits 0 and 1, as do
3916 characters "\000" and "\001".
3917
3918 Starting from the beginning of the input string, each 8-tuple
3919 of characters is converted to 1 character of output. With
3920 format "b", the first character of the 8-tuple determines the
3921 least-significant bit of a character; with format "B", it
3922 determines the most-significant bit of a character.
3923
3924 If the length of the input string is not evenly divisible by 8,
3925 the remainder is packed as if the input string were padded by
3926 null characters at the end. Similarly during unpacking,
3927 "extra" bits are ignored.
3928
3929 If the input string is longer than needed, remaining characters
3930 are ignored.
3931
3932 A "*" for the repeat count uses all characters of the input
3933 field. On unpacking, bits are converted to a string of 0s and
3934 1s.
3935
3936 · The "h" and "H" formats pack a string that many nybbles (4-bit
3937 groups, representable as hexadecimal digits, "0".."9" "a".."f")
3938 long.
3939
3940 For each such format, pack() generates 4 bits of result. With
3941 non-alphabetical characters, the result is based on the 4
3942 least-significant bits of the input character, i.e., on
3943 "ord($char)%16". In particular, characters "0" and "1"
3944 generate nybbles 0 and 1, as do bytes "\000" and "\001". For
3945 characters "a".."f" and "A".."F", the result is compatible with
3946 the usual hexadecimal digits, so that "a" and "A" both generate
3947 the nybble "0xA==10". Use only these specific hex characters
3948 with this format.
3949
3950 Starting from the beginning of the template to pack(), each
3951 pair of characters is converted to 1 character of output. With
3952 format "h", the first character of the pair determines the
3953 least-significant nybble of the output character; with format
3954 "H", it determines the most-significant nybble.
3955
3956 If the length of the input string is not even, it behaves as if
3957 padded by a null character at the end. Similarly, "extra"
3958 nybbles are ignored during unpacking.
3959
3960 If the input string is longer than needed, extra characters are
3961 ignored.
3962
3963 A "*" for the repeat count uses all characters of the input
3964 field. For unpack(), nybbles are converted to a string of
3965 hexadecimal digits.
3966
3967 · The "p" format packs a pointer to a null-terminated string.
3968 You are responsible for ensuring that the string is not a
3969 temporary value, as that could potentially get deallocated
3970 before you got around to using the packed result. The "P"
3971 format packs a pointer to a structure of the size indicated by
3972 the length. A null pointer is created if the corresponding
3973 value for "p" or "P" is "undef"; similarly with unpack(), where
3974 a null pointer unpacks into "undef".
3975
3976 If your system has a strange pointer size--meaning a pointer is
3977 neither as big as an int nor as big as a long--it may not be
3978 possible to pack or unpack pointers in big- or little-endian
3979 byte order. Attempting to do so raises an exception.
3980
3981 · The "/" template character allows packing and unpacking of a
3982 sequence of items where the packed structure contains a packed
3983 item count followed by the packed items themselves. This is
3984 useful when the structure you're unpacking has encoded the
3985 sizes or repeat counts for some of its fields within the
3986 structure itself as separate fields.
3987
3988 For "pack", you write length-item"/"sequence-item, and the
3989 length-item describes how the length value is packed. Formats
3990 likely to be of most use are integer-packing ones like "n" for
3991 Java strings, "w" for ASN.1 or SNMP, and "N" for Sun XDR.
3992
3993 For "pack", sequence-item may have a repeat count, in which
3994 case the minimum of that and the number of available items is
3995 used as the argument for length-item. If it has no repeat
3996 count or uses a '*', the number of available items is used.
3997
3998 For "unpack", an internal stack of integer arguments unpacked
3999 so far is used. You write "/"sequence-item and the repeat
4000 count is obtained by popping off the last element from the
4001 stack. The sequence-item must not have a repeat count.
4002
4003 If sequence-item refers to a string type ("A", "a", or "Z"),
4004 the length-item is the string length, not the number of
4005 strings. With an explicit repeat count for pack, the packed
4006 string is adjusted to that length. For example:
4007
4008 This code: gives this result:
4009
4010 unpack("W/a", "\004Gurusamy") ("Guru")
4011 unpack("a3/A A*", "007 Bond J ") (" Bond", "J")
4012 unpack("a3 x2 /A A*", "007: Bond, J.") ("Bond, J", ".")
4013
4014 pack("n/a* w/a","hello,","world") "\000\006hello,\005world"
4015 pack("a/W2", ord("a") .. ord("z")) "2ab"
4016
4017 The length-item is not returned explicitly from "unpack".
4018
4019 Supplying a count to the length-item format letter is only
4020 useful with "A", "a", or "Z". Packing with a length-item of
4021 "a" or "Z" may introduce "\000" characters, which Perl does not
4022 regard as legal in numeric strings.
4023
4024 · The integer types "s", "S", "l", and "L" may be followed by a
4025 "!" modifier to specify native shorts or longs. As shown in
4026 the example above, a bare "l" means exactly 32 bits, although
4027 the native "long" as seen by the local C compiler may be
4028 larger. This is mainly an issue on 64-bit platforms. You can
4029 see whether using "!" makes any difference this way:
4030
4031 printf "format s is %d, s! is %d\n",
4032 length pack("s"), length pack("s!");
4033
4034 printf "format l is %d, l! is %d\n",
4035 length pack("l"), length pack("l!");
4036
4037 "i!" and "I!" are also allowed, but only for completeness'
4038 sake: they are identical to "i" and "I".
4039
4040 The actual sizes (in bytes) of native shorts, ints, longs, and
4041 long longs on the platform where Perl was built are also
4042 available from the command line:
4043
4044 $ perl -V:{short,int,long{,long}}size
4045 shortsize='2';
4046 intsize='4';
4047 longsize='4';
4048 longlongsize='8';
4049
4050 or programmatically via the "Config" module:
4051
4052 use Config;
4053 print $Config{shortsize}, "\n";
4054 print $Config{intsize}, "\n";
4055 print $Config{longsize}, "\n";
4056 print $Config{longlongsize}, "\n";
4057
4058 $Config{longlongsize} is undefined on systems without long long
4059 support.
4060
4061 · The integer formats "s", "S", "i", "I", "l", "L", "j", and "J"
4062 are inherently non-portable between processors and operating
4063 systems because they obey native byteorder and endianness. For
4064 example, a 4-byte integer 0x12345678 (305419896 decimal) would
4065 be ordered natively (arranged in and handled by the CPU
4066 registers) into bytes as
4067
4068 0x12 0x34 0x56 0x78 # big-endian
4069 0x78 0x56 0x34 0x12 # little-endian
4070
4071 Basically, Intel and VAX CPUs are little-endian, while
4072 everybody else, including Motorola m68k/88k, PPC, Sparc, HP PA,
4073 Power, and Cray, are big-endian. Alpha and MIPS can be either:
4074 Digital/Compaq uses (well, used) them in little-endian mode,
4075 but SGI/Cray uses them in big-endian mode.
4076
4077 The names big-endian and little-endian are comic references to
4078 the egg-eating habits of the little-endian Lilliputians and the
4079 big-endian Blefuscudians from the classic Jonathan Swift
4080 satire, Gulliver's Travels. This entered computer lingo via
4081 the paper "On Holy Wars and a Plea for Peace" by Danny Cohen,
4082 USC/ISI IEN 137, April 1, 1980.
4083
4084 Some systems may have even weirder byte orders such as
4085
4086 0x56 0x78 0x12 0x34
4087 0x34 0x12 0x78 0x56
4088
4089 You can determine your system endianness with this incantation:
4090
4091 printf("%#02x ", $_) for unpack("W*", pack L=>0x12345678);
4092
4093 The byteorder on the platform where Perl was built is also
4094 available via Config:
4095
4096 use Config;
4097 print "$Config{byteorder}\n";
4098
4099 or from the command line:
4100
4101 $ perl -V:byteorder
4102
4103 Byteorders "1234" and "12345678" are little-endian; "4321" and
4104 "87654321" are big-endian.
4105
4106 For portably packed integers, either use the formats "n", "N",
4107 "v", and "V" or else use the ">" and "<" modifiers described
4108 immediately below. See also perlport.
4109
4110 · Starting with Perl 5.9.2, integer and floating-point formats,
4111 along with the "p" and "P" formats and "()" groups, may all be
4112 followed by the ">" or "<" endianness modifiers to respectively
4113 enforce big- or little-endian byte-order. These modifiers are
4114 especially useful given how "n", "N", "v", and "V" don't cover
4115 signed integers, 64-bit integers, or floating-point values.
4116
4117 Here are some concerns to keep in mind when using an endianness
4118 modifier:
4119
4120 · Exchanging signed integers between different platforms
4121 works only when all platforms store them in the same
4122 format. Most platforms store signed integers in two's-
4123 complement notation, so usually this is not an issue.
4124
4125 · The ">" or "<" modifiers can only be used on floating-point
4126 formats on big- or little-endian machines. Otherwise,
4127 attempting to use them raises an exception.
4128
4129 · Forcing big- or little-endian byte-order on floating-point
4130 values for data exchange can work only if all platforms use
4131 the same binary representation such as IEEE floating-point.
4132 Even if all platforms are using IEEE, there may still be
4133 subtle differences. Being able to use ">" or "<" on
4134 floating-point values can be useful, but also dangerous if
4135 you don't know exactly what you're doing. It is not a
4136 general way to portably store floating-point values.
4137
4138 · When using ">" or "<" on a "()" group, this affects all
4139 types inside the group that accept byte-order modifiers,
4140 including all subgroups. It is silently ignored for all
4141 other types. You are not allowed to override the byte-
4142 order within a group that already has a byte-order modifier
4143 suffix.
4144
4145 · Real numbers (floats and doubles) are in native machine format
4146 only. Due to the multiplicity of floating-point formats and
4147 the lack of a standard "network" representation for them, no
4148 facility for interchange has been made. This means that packed
4149 floating-point data written on one machine may not be readable
4150 on another, even if both use IEEE floating-point arithmetic
4151 (because the endianness of the memory representation is not
4152 part of the IEEE spec). See also perlport.
4153
4154 If you know exactly what you're doing, you can use the ">" or
4155 "<" modifiers to force big- or little-endian byte-order on
4156 floating-point values.
4157
4158 Because Perl uses doubles (or long doubles, if configured)
4159 internally for all numeric calculation, converting from double
4160 into float and thence to double again loses precision, so
4161 "unpack("f", pack("f", $foo)") will not in general equal $foo.
4162
4163 · Pack and unpack can operate in two modes: character mode ("C0"
4164 mode) where the packed string is processed per character, and
4165 UTF-8 mode ("U0" mode) where the packed string is processed in
4166 its UTF-8-encoded Unicode form on a byte-by-byte basis.
4167 Character mode is the default unless the format string starts
4168 with "U". You can always switch mode mid-format with an
4169 explicit "C0" or "U0" in the format. This mode remains in
4170 effect until the next mode change, or until the end of the "()"
4171 group it (directly) applies to.
4172
4173 Using "C0" to get Unicode characters while using "U0" to get
4174 non-Unicode bytes is not necessarily obvious. Probably only
4175 the first of these is what you want:
4176
4177 $ perl -CS -E 'say "\x{3B1}\x{3C9}"' |
4178 perl -CS -ne 'printf "%v04X\n", $_ for unpack("C0A*", $_)'
4179 03B1.03C9
4180 $ perl -CS -E 'say "\x{3B1}\x{3C9}"' |
4181 perl -CS -ne 'printf "%v02X\n", $_ for unpack("U0A*", $_)'
4182 CE.B1.CF.89
4183 $ perl -CS -E 'say "\x{3B1}\x{3C9}"' |
4184 perl -C0 -ne 'printf "%v02X\n", $_ for unpack("C0A*", $_)'
4185 CE.B1.CF.89
4186 $ perl -CS -E 'say "\x{3B1}\x{3C9}"' |
4187 perl -C0 -ne 'printf "%v02X\n", $_ for unpack("U0A*", $_)'
4188 C3.8E.C2.B1.C3.8F.C2.89
4189
4190 Those examples also illustrate that you should not try to use
4191 "pack"/"unpack" as a substitute for the Encode module.
4192
4193 · You must yourself do any alignment or padding by inserting, for
4194 example, enough "x"es while packing. There is no way for
4195 pack() and unpack() to know where characters are going to or
4196 coming from, so they handle their output and input as flat
4197 sequences of characters.
4198
4199 · A "()" group is a sub-TEMPLATE enclosed in parentheses. A
4200 group may take a repeat count either as postfix, or for
4201 unpack(), also via the "/" template character. Within each
4202 repetition of a group, positioning with "@" starts over at 0.
4203 Therefore, the result of
4204
4205 pack("@1A((@2A)@3A)", qw[X Y Z])
4206
4207 is the string "\0X\0\0YZ".
4208
4209 · "x" and "X" accept the "!" modifier to act as alignment
4210 commands: they jump forward or back to the closest position
4211 aligned at a multiple of "count" characters. For example, to
4212 pack() or unpack() a C structure like
4213
4214 struct {
4215 char c; /* one signed, 8-bit character */
4216 double d;
4217 char cc[2];
4218 }
4219
4220 one may need to use the template "c x![d] d c[2]". This
4221 assumes that doubles must be aligned to the size of double.
4222
4223 For alignment commands, a "count" of 0 is equivalent to a
4224 "count" of 1; both are no-ops.
4225
4226 · "n", "N", "v" and "V" accept the "!" modifier to represent
4227 signed 16-/32-bit integers in big-/little-endian order. This
4228 is portable only when all platforms sharing packed data use the
4229 same binary representation for signed integers; for example,
4230 when all platforms use two's-complement representation.
4231
4232 · Comments can be embedded in a TEMPLATE using "#" through the
4233 end of line. White space can separate pack codes from each
4234 other, but modifiers and repeat counts must follow immediately.
4235 Breaking complex templates into individual line-by-line
4236 components, suitably annotated, can do as much to improve
4237 legibility and maintainability of pack/unpack formats as "/x"
4238 can for complicated pattern matches.
4239
4240 · If TEMPLATE requires more arguments than pack() is given,
4241 pack() assumes additional "" arguments. If TEMPLATE requires
4242 fewer arguments than given, extra arguments are ignored.
4243
4244 Examples:
4245
4246 $foo = pack("WWWW",65,66,67,68);
4247 # foo eq "ABCD"
4248 $foo = pack("W4",65,66,67,68);
4249 # same thing
4250 $foo = pack("W4",0x24b6,0x24b7,0x24b8,0x24b9);
4251 # same thing with Unicode circled letters.
4252 $foo = pack("U4",0x24b6,0x24b7,0x24b8,0x24b9);
4253 # same thing with Unicode circled letters. You don't get the
4254 # UTF-8 bytes because the U at the start of the format caused
4255 # a switch to U0-mode, so the UTF-8 bytes get joined into
4256 # characters
4257 $foo = pack("C0U4",0x24b6,0x24b7,0x24b8,0x24b9);
4258 # foo eq "\xe2\x92\xb6\xe2\x92\xb7\xe2\x92\xb8\xe2\x92\xb9"
4259 # This is the UTF-8 encoding of the string in the
4260 # previous example
4261
4262 $foo = pack("ccxxcc",65,66,67,68);
4263 # foo eq "AB\0\0CD"
4264
4265 # NOTE: The examples above featuring "W" and "c" are true
4266 # only on ASCII and ASCII-derived systems such as ISO Latin 1
4267 # and UTF-8. On EBCDIC systems, the first example would be
4268 # $foo = pack("WWWW",193,194,195,196);
4269
4270 $foo = pack("s2",1,2);
4271 # "\001\000\002\000" on little-endian
4272 # "\000\001\000\002" on big-endian
4273
4274 $foo = pack("a4","abcd","x","y","z");
4275 # "abcd"
4276
4277 $foo = pack("aaaa","abcd","x","y","z");
4278 # "axyz"
4279
4280 $foo = pack("a14","abcdefg");
4281 # "abcdefg\0\0\0\0\0\0\0"
4282
4283 $foo = pack("i9pl", gmtime);
4284 # a real struct tm (on my system anyway)
4285
4286 $utmp_template = "Z8 Z8 Z16 L";
4287 $utmp = pack($utmp_template, @utmp1);
4288 # a struct utmp (BSDish)
4289
4290 @utmp2 = unpack($utmp_template, $utmp);
4291 # "@utmp1" eq "@utmp2"
4292
4293 sub bintodec {
4294 unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
4295 }
4296
4297 $foo = pack('sx2l', 12, 34);
4298 # short 12, two zero bytes padding, long 34
4299 $bar = pack('s@4l', 12, 34);
4300 # short 12, zero fill to position 4, long 34
4301 # $foo eq $bar
4302 $baz = pack('s.l', 12, 4, 34);
4303 # short 12, zero fill to position 4, long 34
4304
4305 $foo = pack('nN', 42, 4711);
4306 # pack big-endian 16- and 32-bit unsigned integers
4307 $foo = pack('S>L>', 42, 4711);
4308 # exactly the same
4309 $foo = pack('s<l<', -42, 4711);
4310 # pack little-endian 16- and 32-bit signed integers
4311 $foo = pack('(sl)<', -42, 4711);
4312 # exactly the same
4313
4314 The same template may generally also be used in unpack().
4315
4316 package NAMESPACE
4317 package NAMESPACE VERSION
4318 package NAMESPACE BLOCK
4319 package NAMESPACE VERSION BLOCK
4320 Declares the BLOCK or the rest of the compilation unit as being in
4321 the given namespace. The scope of the package declaration is
4322 either the supplied code BLOCK or, in the absence of a BLOCK, from
4323 the declaration itself through the end of current scope (the
4324 enclosing block, file, or "eval"). That is, the forms without a
4325 BLOCK are operative through the end of the current scope, just like
4326 the "my", "state", and "our" operators. All unqualified dynamic
4327 identifiers in this scope will be in the given namespace, except
4328 where overridden by another "package" declaration or when they're
4329 one of the special identifiers that qualify into "main::", like
4330 "STDOUT", "ARGV", "ENV", and the punctuation variables.
4331
4332 A package statement affects dynamic variables only, including those
4333 you've used "local" on, but not lexical variables, which are
4334 created with "my", "state", or "our". Typically it would be the
4335 first declaration in a file included by "require" or "use". You
4336 can switch into a package in more than one place, since this only
4337 determines which default symbol table the compiler uses for the
4338 rest of that block. You can refer to identifiers in other packages
4339 than the current one by prefixing the identifier with the package
4340 name and a double colon, as in $SomePack::var or
4341 "ThatPack::INPUT_HANDLE". If package name is omitted, the "main"
4342 package as assumed. That is, $::sail is equivalent to $main::sail
4343 (as well as to "$main'sail", still seen in ancient code, mostly
4344 from Perl 4).
4345
4346 If VERSION is provided, "package" sets the $VERSION variable in the
4347 given namespace to a version object with the VERSION provided.
4348 VERSION must be a "strict" style version number as defined by the
4349 version module: a positive decimal number (integer or decimal-
4350 fraction) without exponentiation or else a dotted-decimal v-string
4351 with a leading 'v' character and at least three components. You
4352 should set $VERSION only once per package.
4353
4354 See "Packages" in perlmod for more information about packages,
4355 modules, and classes. See perlsub for other scoping issues.
4356
4357 __PACKAGE__
4358 A special token that returns the name of the package in which it
4359 occurs.
4360
4361 pipe READHANDLE,WRITEHANDLE
4362 Opens a pair of connected pipes like the corresponding system call.
4363 Note that if you set up a loop of piped processes, deadlock can
4364 occur unless you are very careful. In addition, note that Perl's
4365 pipes use IO buffering, so you may need to set $| to flush your
4366 WRITEHANDLE after each command, depending on the application.
4367
4368 See IPC::Open2, IPC::Open3, and "Bidirectional Communication with
4369 Another Process" in perlipc for examples of such things.
4370
4371 On systems that support a close-on-exec flag on files, that flag is
4372 set on all newly opened file descriptors whose "fileno"s are higher
4373 than the current value of $^F (by default 2 for "STDERR"). See
4374 "$^F" in perlvar.
4375
4376 pop ARRAY
4377 pop EXPR
4378 pop Pops and returns the last value of the array, shortening the array
4379 by one element.
4380
4381 Returns the undefined value if the array is empty, although this
4382 may also happen at other times. If ARRAY is omitted, pops the
4383 @ARGV array in the main program, but the @_ array in subroutines,
4384 just like "shift".
4385
4386 Starting with Perl 5.14, "pop" can take a scalar EXPR, which must
4387 hold a reference to an unblessed array. The argument will be
4388 dereferenced automatically. This aspect of "pop" is considered
4389 highly experimental. The exact behaviour may change in a future
4390 version of Perl.
4391
4392 To avoid confusing would-be users of your code who are running
4393 earlier versions of Perl with mysterious syntax errors, put this
4394 sort of thing at the top of your file to signal that your code will
4395 work only on Perls of a recent vintage:
4396
4397 use 5.014; # so push/pop/etc work on scalars (experimental)
4398
4399 pos SCALAR
4400 pos Returns the offset of where the last "m//g" search left off for the
4401 variable in question ($_ is used when the variable is not
4402 specified). Note that 0 is a valid match offset. "undef"
4403 indicates that the search position is reset (usually due to match
4404 failure, but can also be because no match has yet been run on the
4405 scalar).
4406
4407 "pos" directly accesses the location used by the regexp engine to
4408 store the offset, so assigning to "pos" will change that offset,
4409 and so will also influence the "\G" zero-width assertion in regular
4410 expressions. Both of these effects take place for the next match,
4411 so you can't affect the position with "pos" during the current
4412 match, such as in "(?{pos() = 5})" or "s//pos() = 5/e".
4413
4414 Setting "pos" also resets the matched with zero-length flag,
4415 described under "Repeated Patterns Matching a Zero-length
4416 Substring" in perlre.
4417
4418 Because a failed "m//gc" match doesn't reset the offset, the return
4419 from "pos" won't change either in this case. See perlre and
4420 perlop.
4421
4422 print FILEHANDLE LIST
4423 print FILEHANDLE
4424 print LIST
4425 print
4426 Prints a string or a list of strings. Returns true if successful.
4427 FILEHANDLE may be a scalar variable containing the name of or a
4428 reference to the filehandle, thus introducing one level of
4429 indirection. (NOTE: If FILEHANDLE is a variable and the next token
4430 is a term, it may be misinterpreted as an operator unless you
4431 interpose a "+" or put parentheses around the arguments.) If
4432 FILEHANDLE is omitted, prints to the last selected (see "select")
4433 output handle. If LIST is omitted, prints $_ to the currently
4434 selected output handle. To use FILEHANDLE alone to print the
4435 content of $_ to it, you must use a real filehandle like "FH", not
4436 an indirect one like $fh. To set the default output handle to
4437 something other than STDOUT, use the select operation.
4438
4439 The current value of $, (if any) is printed between each LIST item.
4440 The current value of "$\" (if any) is printed after the entire LIST
4441 has been printed. Because print takes a LIST, anything in the LIST
4442 is evaluated in list context, including any subroutines whose
4443 return lists you pass to "print". Be careful not to follow the
4444 print keyword with a left parenthesis unless you want the
4445 corresponding right parenthesis to terminate the arguments to the
4446 print; put parentheses around all arguments (or interpose a "+",
4447 but that doesn't look as good).
4448
4449 If you're storing handles in an array or hash, or in general
4450 whenever you're using any expression more complex than a bareword
4451 handle or a plain, unsubscripted scalar variable to retrieve it,
4452 you will have to use a block returning the filehandle value
4453 instead, in which case the LIST may not be omitted:
4454
4455 print { $files[$i] } "stuff\n";
4456 print { $OK ? STDOUT : STDERR } "stuff\n";
4457
4458 Printing to a closed pipe or socket will generate a SIGPIPE signal.
4459 See perlipc for more on signal handling.
4460
4461 printf FILEHANDLE FORMAT, LIST
4462 printf FILEHANDLE
4463 printf FORMAT, LIST
4464 printf
4465 Equivalent to "print FILEHANDLE sprintf(FORMAT, LIST)", except that
4466 "$\" (the output record separator) is not appended. The first
4467 argument of the list will be interpreted as the "printf" format.
4468 See sprintf for an explanation of the format argument. If you omit
4469 the LIST, $_ is used; to use FILEHANDLE without a LIST, you must
4470 use a real filehandle like "FH", not an indirect one like $fh. If
4471 "use locale" (including "use locale ':not_characters'") is in
4472 effect and POSIX::setlocale() has been called, the character used
4473 for the decimal separator in formatted floating-point numbers is
4474 affected by the LC_NUMERIC locale setting. See perllocale and
4475 POSIX.
4476
4477 Don't fall into the trap of using a "printf" when a simple "print"
4478 would do. The "print" is more efficient and less error prone.
4479
4480 prototype FUNCTION
4481 Returns the prototype of a function as a string (or "undef" if the
4482 function has no prototype). FUNCTION is a reference to, or the
4483 name of, the function whose prototype you want to retrieve.
4484
4485 If FUNCTION is a string starting with "CORE::", the rest is taken
4486 as a name for a Perl builtin. If the builtin is not overridable
4487 (such as "qw//") or if its arguments cannot be adequately expressed
4488 by a prototype (such as "system"), prototype() returns "undef",
4489 because the builtin does not really behave like a Perl function.
4490 Otherwise, the string describing the equivalent prototype is
4491 returned.
4492
4493 push ARRAY,LIST
4494 push EXPR,LIST
4495 Treats ARRAY as a stack by appending the values of LIST to the end
4496 of ARRAY. The length of ARRAY increases by the length of LIST.
4497 Has the same effect as
4498
4499 for $value (LIST) {
4500 $ARRAY[++$#ARRAY] = $value;
4501 }
4502
4503 but is more efficient. Returns the number of elements in the array
4504 following the completed "push".
4505
4506 Starting with Perl 5.14, "push" can take a scalar EXPR, which must
4507 hold a reference to an unblessed array. The argument will be
4508 dereferenced automatically. This aspect of "push" is considered
4509 highly experimental. The exact behaviour may change in a future
4510 version of Perl.
4511
4512 To avoid confusing would-be users of your code who are running
4513 earlier versions of Perl with mysterious syntax errors, put this
4514 sort of thing at the top of your file to signal that your code will
4515 work only on Perls of a recent vintage:
4516
4517 use 5.014; # so push/pop/etc work on scalars (experimental)
4518
4519 q/STRING/
4520 qq/STRING/
4521 qw/STRING/
4522 qx/STRING/
4523 Generalized quotes. See "Quote-Like Operators" in perlop.
4524
4525 qr/STRING/
4526 Regexp-like quote. See "Regexp Quote-Like Operators" in perlop.
4527
4528 quotemeta EXPR
4529 quotemeta
4530 Returns the value of EXPR with all the ASCII non-"word" characters
4531 backslashed. (That is, all ASCII characters not matching
4532 "/[A-Za-z_0-9]/" will be preceded by a backslash in the returned
4533 string, regardless of any locale settings.) This is the internal
4534 function implementing the "\Q" escape in double-quoted strings.
4535 (See below for the behavior on non-ASCII code points.)
4536
4537 If EXPR is omitted, uses $_.
4538
4539 quotemeta (and "\Q" ... "\E") are useful when interpolating strings
4540 into regular expressions, because by default an interpolated
4541 variable will be considered a mini-regular expression. For
4542 example:
4543
4544 my $sentence = 'The quick brown fox jumped over the lazy dog';
4545 my $substring = 'quick.*?fox';
4546 $sentence =~ s{$substring}{big bad wolf};
4547
4548 Will cause $sentence to become 'The big bad wolf jumped over...'.
4549
4550 On the other hand:
4551
4552 my $sentence = 'The quick brown fox jumped over the lazy dog';
4553 my $substring = 'quick.*?fox';
4554 $sentence =~ s{\Q$substring\E}{big bad wolf};
4555
4556 Or:
4557
4558 my $sentence = 'The quick brown fox jumped over the lazy dog';
4559 my $substring = 'quick.*?fox';
4560 my $quoted_substring = quotemeta($substring);
4561 $sentence =~ s{$quoted_substring}{big bad wolf};
4562
4563 Will both leave the sentence as is. Normally, when accepting
4564 literal string input from the user, quotemeta() or "\Q" must be
4565 used.
4566
4567 In Perl v5.14, all non-ASCII characters are quoted in
4568 non-UTF-8-encoded strings, but not quoted in UTF-8 strings.
4569
4570 Starting in Perl v5.16, Perl adopted a Unicode-defined strategy for
4571 quoting non-ASCII characters; the quoting of ASCII characters is
4572 unchanged.
4573
4574 Also unchanged is the quoting of non-UTF-8 strings when outside the
4575 scope of a "use feature 'unicode_strings'", which is to quote all
4576 characters in the upper Latin1 range. This provides complete
4577 backwards compatibility for old programs which do not use Unicode.
4578 (Note that "unicode_strings" is automatically enabled within the
4579 scope of a "use v5.12" or greater.)
4580
4581 Within the scope of "use locale", all non-ASCII Latin1 code points
4582 are quoted whether the string is encoded as UTF-8 or not. As
4583 mentioned above, locale does not affect the quoting of ASCII-range
4584 characters. This protects against those locales where characters
4585 such as "|" are considered to be word characters.
4586
4587 Otherwise, Perl quotes non-ASCII characters using an adaptation
4588 from Unicode (see <http://www.unicode.org/reports/tr31/>.) The
4589 only code points that are quoted are those that have any of the
4590 Unicode properties: Pattern_Syntax, Pattern_White_Space,
4591 White_Space, Default_Ignorable_Code_Point, or
4592 General_Category=Control.
4593
4594 Of these properties, the two important ones are Pattern_Syntax and
4595 Pattern_White_Space. They have been set up by Unicode for exactly
4596 this purpose of deciding which characters in a regular expression
4597 pattern should be quoted. No character that can be in an
4598 identifier has these properties.
4599
4600 Perl promises, that if we ever add regular expression pattern
4601 metacharacters to the dozen already defined ("\ | ( ) [ { ^ $ * + ?
4602 ."), that we will only use ones that have the Pattern_Syntax
4603 property. Perl also promises, that if we ever add characters that
4604 are considered to be white space in regular expressions (currently
4605 mostly affected by "/x"), they will all have the
4606 Pattern_White_Space property.
4607
4608 Unicode promises that the set of code points that have these two
4609 properties will never change, so something that is not quoted in
4610 v5.16 will never need to be quoted in any future Perl release.
4611 (Not all the code points that match Pattern_Syntax have actually
4612 had characters assigned to them; so there is room to grow, but they
4613 are quoted whether assigned or not. Perl, of course, would never
4614 use an unassigned code point as an actual metacharacter.)
4615
4616 Quoting characters that have the other 3 properties is done to
4617 enhance the readability of the regular expression and not because
4618 they actually need to be quoted for regular expression purposes
4619 (characters with the White_Space property are likely to be
4620 indistinguishable on the page or screen from those with the
4621 Pattern_White_Space property; and the other two properties contain
4622 non-printing characters).
4623
4624 rand EXPR
4625 rand
4626 Returns a random fractional number greater than or equal to 0 and
4627 less than the value of EXPR. (EXPR should be positive.) If EXPR
4628 is omitted, the value 1 is used. Currently EXPR with the value 0
4629 is also special-cased as 1 (this was undocumented before Perl 5.8.0
4630 and is subject to change in future versions of Perl).
4631 Automatically calls "srand" unless "srand" has already been called.
4632 See also "srand".
4633
4634 Apply "int()" to the value returned by "rand()" if you want random
4635 integers instead of random fractional numbers. For example,
4636
4637 int(rand(10))
4638
4639 returns a random integer between 0 and 9, inclusive.
4640
4641 (Note: If your rand function consistently returns numbers that are
4642 too large or too small, then your version of Perl was probably
4643 compiled with the wrong number of RANDBITS.)
4644
4645 "rand()" is not cryptographically secure. You should not rely on
4646 it in security-sensitive situations. As of this writing, a number
4647 of third-party CPAN modules offer random number generators intended
4648 by their authors to be cryptographically secure, including:
4649 Data::Entropy, Crypt::Random, Math::Random::Secure, and
4650 Math::TrulyRandom.
4651
4652 read FILEHANDLE,SCALAR,LENGTH,OFFSET
4653 read FILEHANDLE,SCALAR,LENGTH
4654 Attempts to read LENGTH characters of data into variable SCALAR
4655 from the specified FILEHANDLE. Returns the number of characters
4656 actually read, 0 at end of file, or undef if there was an error (in
4657 the latter case $! is also set). SCALAR will be grown or shrunk so
4658 that the last character actually read is the last character of the
4659 scalar after the read.
4660
4661 An OFFSET may be specified to place the read data at some place in
4662 the string other than the beginning. A negative OFFSET specifies
4663 placement at that many characters counting backwards from the end
4664 of the string. A positive OFFSET greater than the length of SCALAR
4665 results in the string being padded to the required size with "\0"
4666 bytes before the result of the read is appended.
4667
4668 The call is implemented in terms of either Perl's or your system's
4669 native fread(3) library function. To get a true read(2) system
4670 call, see sysread.
4671
4672 Note the characters: depending on the status of the filehandle,
4673 either (8-bit) bytes or characters are read. By default, all
4674 filehandles operate on bytes, but for example if the filehandle has
4675 been opened with the ":utf8" I/O layer (see "open", and the "open"
4676 pragma, open), the I/O will operate on UTF8-encoded Unicode
4677 characters, not bytes. Similarly for the ":encoding" pragma: in
4678 that case pretty much any characters can be read.
4679
4680 readdir DIRHANDLE
4681 Returns the next directory entry for a directory opened by
4682 "opendir". If used in list context, returns all the rest of the
4683 entries in the directory. If there are no more entries, returns
4684 the undefined value in scalar context and the empty list in list
4685 context.
4686
4687 If you're planning to filetest the return values out of a
4688 "readdir", you'd better prepend the directory in question.
4689 Otherwise, because we didn't "chdir" there, it would have been
4690 testing the wrong file.
4691
4692 opendir(my $dh, $some_dir) || die "can't opendir $some_dir: $!";
4693 @dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh);
4694 closedir $dh;
4695
4696 As of Perl 5.11.2 you can use a bare "readdir" in a "while" loop,
4697 which will set $_ on every iteration.
4698
4699 opendir(my $dh, $some_dir) || die;
4700 while(readdir $dh) {
4701 print "$some_dir/$_\n";
4702 }
4703 closedir $dh;
4704
4705 To avoid confusing would-be users of your code who are running
4706 earlier versions of Perl with mysterious failures, put this sort of
4707 thing at the top of your file to signal that your code will work
4708 only on Perls of a recent vintage:
4709
4710 use 5.012; # so readdir assigns to $_ in a lone while test
4711
4712 readline EXPR
4713 readline
4714 Reads from the filehandle whose typeglob is contained in EXPR (or
4715 from *ARGV if EXPR is not provided). In scalar context, each call
4716 reads and returns the next line until end-of-file is reached,
4717 whereupon the subsequent call returns "undef". In list context,
4718 reads until end-of-file is reached and returns a list of lines.
4719 Note that the notion of "line" used here is whatever you may have
4720 defined with $/ or $INPUT_RECORD_SEPARATOR). See "$/" in perlvar.
4721
4722 When $/ is set to "undef", when "readline" is in scalar context
4723 (i.e., file slurp mode), and when an empty file is read, it returns
4724 '' the first time, followed by "undef" subsequently.
4725
4726 This is the internal function implementing the "<EXPR>" operator,
4727 but you can use it directly. The "<EXPR>" operator is discussed in
4728 more detail in "I/O Operators" in perlop.
4729
4730 $line = <STDIN>;
4731 $line = readline(*STDIN); # same thing
4732
4733 If "readline" encounters an operating system error, $! will be set
4734 with the corresponding error message. It can be helpful to check
4735 $! when you are reading from filehandles you don't trust, such as a
4736 tty or a socket. The following example uses the operator form of
4737 "readline" and dies if the result is not defined.
4738
4739 while ( ! eof($fh) ) {
4740 defined( $_ = <$fh> ) or die "readline failed: $!";
4741 ...
4742 }
4743
4744 Note that you have can't handle "readline" errors that way with the
4745 "ARGV" filehandle. In that case, you have to open each element of
4746 @ARGV yourself since "eof" handles "ARGV" differently.
4747
4748 foreach my $arg (@ARGV) {
4749 open(my $fh, $arg) or warn "Can't open $arg: $!";
4750
4751 while ( ! eof($fh) ) {
4752 defined( $_ = <$fh> )
4753 or die "readline failed for $arg: $!";
4754 ...
4755 }
4756 }
4757
4758 readlink EXPR
4759 readlink
4760 Returns the value of a symbolic link, if symbolic links are
4761 implemented. If not, raises an exception. If there is a system
4762 error, returns the undefined value and sets $! (errno). If EXPR is
4763 omitted, uses $_.
4764
4765 Portability issues: "readlink" in perlport.
4766
4767 readpipe EXPR
4768 readpipe
4769 EXPR is executed as a system command. The collected standard
4770 output of the command is returned. In scalar context, it comes
4771 back as a single (potentially multi-line) string. In list context,
4772 returns a list of lines (however you've defined lines with $/ or
4773 $INPUT_RECORD_SEPARATOR). This is the internal function
4774 implementing the "qx/EXPR/" operator, but you can use it directly.
4775 The "qx/EXPR/" operator is discussed in more detail in "I/O
4776 Operators" in perlop. If EXPR is omitted, uses $_.
4777
4778 recv SOCKET,SCALAR,LENGTH,FLAGS
4779 Receives a message on a socket. Attempts to receive LENGTH
4780 characters of data into variable SCALAR from the specified SOCKET
4781 filehandle. SCALAR will be grown or shrunk to the length actually
4782 read. Takes the same flags as the system call of the same name.
4783 Returns the address of the sender if SOCKET's protocol supports
4784 this; returns an empty string otherwise. If there's an error,
4785 returns the undefined value. This call is actually implemented in
4786 terms of recvfrom(2) system call. See "UDP: Message Passing" in
4787 perlipc for examples.
4788
4789 Note the characters: depending on the status of the socket, either
4790 (8-bit) bytes or characters are received. By default all sockets
4791 operate on bytes, but for example if the socket has been changed
4792 using binmode() to operate with the ":encoding(utf8)" I/O layer
4793 (see the "open" pragma, open), the I/O will operate on UTF8-encoded
4794 Unicode characters, not bytes. Similarly for the ":encoding"
4795 pragma: in that case pretty much any characters can be read.
4796
4797 redo LABEL
4798 redo
4799 The "redo" command restarts the loop block without evaluating the
4800 conditional again. The "continue" block, if any, is not executed.
4801 If the LABEL is omitted, the command refers to the innermost
4802 enclosing loop. Programs that want to lie to themselves about what
4803 was just input normally use this command:
4804
4805 # a simpleminded Pascal comment stripper
4806 # (warning: assumes no { or } in strings)
4807 LINE: while (<STDIN>) {
4808 while (s|({.*}.*){.*}|$1 |) {}
4809 s|{.*}| |;
4810 if (s|{.*| |) {
4811 $front = $_;
4812 while (<STDIN>) {
4813 if (/}/) { # end of comment?
4814 s|^|$front\{|;
4815 redo LINE;
4816 }
4817 }
4818 }
4819 print;
4820 }
4821
4822 "redo" cannot be used to retry a block that returns a value such as
4823 "eval {}", "sub {}", or "do {}", and should not be used to exit a
4824 grep() or map() operation.
4825
4826 Note that a block by itself is semantically identical to a loop
4827 that executes once. Thus "redo" inside such a block will
4828 effectively turn it into a looping construct.
4829
4830 See also "continue" for an illustration of how "last", "next", and
4831 "redo" work.
4832
4833 ref EXPR
4834 ref Returns a non-empty string if EXPR is a reference, the empty string
4835 otherwise. If EXPR is not specified, $_ will be used. The value
4836 returned depends on the type of thing the reference is a reference
4837 to. Builtin types include:
4838
4839 SCALAR
4840 ARRAY
4841 HASH
4842 CODE
4843 REF
4844 GLOB
4845 LVALUE
4846 FORMAT
4847 IO
4848 VSTRING
4849 Regexp
4850
4851 If the referenced object has been blessed into a package, then that
4852 package name is returned instead. You can think of "ref" as a
4853 "typeof" operator.
4854
4855 if (ref($r) eq "HASH") {
4856 print "r is a reference to a hash.\n";
4857 }
4858 unless (ref($r)) {
4859 print "r is not a reference at all.\n";
4860 }
4861
4862 The return value "LVALUE" indicates a reference to an lvalue that
4863 is not a variable. You get this from taking the reference of
4864 function calls like "pos()" or "substr()". "VSTRING" is returned
4865 if the reference points to a version string.
4866
4867 The result "Regexp" indicates that the argument is a regular
4868 expression resulting from "qr//".
4869
4870 See also perlref.
4871
4872 rename OLDNAME,NEWNAME
4873 Changes the name of a file; an existing file NEWNAME will be
4874 clobbered. Returns true for success, false otherwise.
4875
4876 Behavior of this function varies wildly depending on your system
4877 implementation. For example, it will usually not work across file
4878 system boundaries, even though the system mv command sometimes
4879 compensates for this. Other restrictions include whether it works
4880 on directories, open files, or pre-existing files. Check perlport
4881 and either the rename(2) manpage or equivalent system documentation
4882 for details.
4883
4884 For a platform independent "move" function look at the File::Copy
4885 module.
4886
4887 Portability issues: "rename" in perlport.
4888
4889 require VERSION
4890 require EXPR
4891 require
4892 Demands a version of Perl specified by VERSION, or demands some
4893 semantics specified by EXPR or by $_ if EXPR is not supplied.
4894
4895 VERSION may be either a numeric argument such as 5.006, which will
4896 be compared to $], or a literal of the form v5.6.1, which will be
4897 compared to $^V (aka $PERL_VERSION). An exception is raised if
4898 VERSION is greater than the version of the current Perl
4899 interpreter. Compare with "use", which can do a similar check at
4900 compile time.
4901
4902 Specifying VERSION as a literal of the form v5.6.1 should generally
4903 be avoided, because it leads to misleading error messages under
4904 earlier versions of Perl that do not support this syntax. The
4905 equivalent numeric version should be used instead.
4906
4907 require v5.6.1; # run time version check
4908 require 5.6.1; # ditto
4909 require 5.006_001; # ditto; preferred for backwards compatibility
4910
4911 Otherwise, "require" demands that a library file be included if it
4912 hasn't already been included. The file is included via the do-FILE
4913 mechanism, which is essentially just a variety of "eval" with the
4914 caveat that lexical variables in the invoking script will be
4915 invisible to the included code. Has semantics similar to the
4916 following subroutine:
4917
4918 sub require {
4919 my ($filename) = @_;
4920 if (exists $INC{$filename}) {
4921 return 1 if $INC{$filename};
4922 die "Compilation failed in require";
4923 }
4924 my ($realfilename,$result);
4925 ITER: {
4926 foreach $prefix (@INC) {
4927 $realfilename = "$prefix/$filename";
4928 if (-f $realfilename) {
4929 $INC{$filename} = $realfilename;
4930 $result = do $realfilename;
4931 last ITER;
4932 }
4933 }
4934 die "Can't find $filename in \@INC";
4935 }
4936 if ($@) {
4937 $INC{$filename} = undef;
4938 die $@;
4939 } elsif (!$result) {
4940 delete $INC{$filename};
4941 die "$filename did not return true value";
4942 } else {
4943 return $result;
4944 }
4945 }
4946
4947 Note that the file will not be included twice under the same
4948 specified name.
4949
4950 The file must return true as the last statement to indicate
4951 successful execution of any initialization code, so it's customary
4952 to end such a file with "1;" unless you're sure it'll return true
4953 otherwise. But it's better just to put the "1;", in case you add
4954 more statements.
4955
4956 If EXPR is a bareword, the require assumes a ".pm" extension and
4957 replaces "::" with "/" in the filename for you, to make it easy to
4958 load standard modules. This form of loading of modules does not
4959 risk altering your namespace.
4960
4961 In other words, if you try this:
4962
4963 require Foo::Bar; # a splendid bareword
4964
4965 The require function will actually look for the "Foo/Bar.pm" file
4966 in the directories specified in the @INC array.
4967
4968 But if you try this:
4969
4970 $class = 'Foo::Bar';
4971 require $class; # $class is not a bareword
4972 #or
4973 require "Foo::Bar"; # not a bareword because of the ""
4974
4975 The require function will look for the "Foo::Bar" file in the @INC
4976 array and will complain about not finding "Foo::Bar" there. In
4977 this case you can do:
4978
4979 eval "require $class";
4980
4981 Now that you understand how "require" looks for files with a
4982 bareword argument, there is a little extra functionality going on
4983 behind the scenes. Before "require" looks for a ".pm" extension,
4984 it will first look for a similar filename with a ".pmc" extension.
4985 If this file is found, it will be loaded in place of any file
4986 ending in a ".pm" extension.
4987
4988 You can also insert hooks into the import facility by putting Perl
4989 code directly into the @INC array. There are three forms of hooks:
4990 subroutine references, array references, and blessed objects.
4991
4992 Subroutine references are the simplest case. When the inclusion
4993 system walks through @INC and encounters a subroutine, this
4994 subroutine gets called with two parameters, the first a reference
4995 to itself, and the second the name of the file to be included
4996 (e.g., "Foo/Bar.pm"). The subroutine should return either nothing
4997 or else a list of up to three values in the following order:
4998
4999 1. A filehandle, from which the file will be read.
5000
5001 2. A reference to a subroutine. If there is no filehandle
5002 (previous item), then this subroutine is expected to generate
5003 one line of source code per call, writing the line into $_ and
5004 returning 1, then finally at end of file returning 0. If there
5005 is a filehandle, then the subroutine will be called to act as a
5006 simple source filter, with the line as read in $_. Again,
5007 return 1 for each valid line, and 0 after all lines have been
5008 returned.
5009
5010 3. Optional state for the subroutine. The state is passed in as
5011 $_[1]. A reference to the subroutine itself is passed in as
5012 $_[0].
5013
5014 If an empty list, "undef", or nothing that matches the first 3
5015 values above is returned, then "require" looks at the remaining
5016 elements of @INC. Note that this filehandle must be a real
5017 filehandle (strictly a typeglob or reference to a typeglob, whether
5018 blessed or unblessed); tied filehandles will be ignored and
5019 processing will stop there.
5020
5021 If the hook is an array reference, its first element must be a
5022 subroutine reference. This subroutine is called as above, but the
5023 first parameter is the array reference. This lets you indirectly
5024 pass arguments to the subroutine.
5025
5026 In other words, you can write:
5027
5028 push @INC, \&my_sub;
5029 sub my_sub {
5030 my ($coderef, $filename) = @_; # $coderef is \&my_sub
5031 ...
5032 }
5033
5034 or:
5035
5036 push @INC, [ \&my_sub, $x, $y, ... ];
5037 sub my_sub {
5038 my ($arrayref, $filename) = @_;
5039 # Retrieve $x, $y, ...
5040 my @parameters = @$arrayref[1..$#$arrayref];
5041 ...
5042 }
5043
5044 If the hook is an object, it must provide an INC method that will
5045 be called as above, the first parameter being the object itself.
5046 (Note that you must fully qualify the sub's name, as unqualified
5047 "INC" is always forced into package "main".) Here is a typical
5048 code layout:
5049
5050 # In Foo.pm
5051 package Foo;
5052 sub new { ... }
5053 sub Foo::INC {
5054 my ($self, $filename) = @_;
5055 ...
5056 }
5057
5058 # In the main program
5059 push @INC, Foo->new(...);
5060
5061 These hooks are also permitted to set the %INC entry corresponding
5062 to the files they have loaded. See "%INC" in perlvar.
5063
5064 For a yet-more-powerful import facility, see "use" and perlmod.
5065
5066 reset EXPR
5067 reset
5068 Generally used in a "continue" block at the end of a loop to clear
5069 variables and reset "??" searches so that they work again. The
5070 expression is interpreted as a list of single characters (hyphens
5071 allowed for ranges). All variables and arrays beginning with one
5072 of those letters are reset to their pristine state. If the
5073 expression is omitted, one-match searches ("?pattern?") are reset
5074 to match again. Only resets variables or searches in the current
5075 package. Always returns 1. Examples:
5076
5077 reset 'X'; # reset all X variables
5078 reset 'a-z'; # reset lower case variables
5079 reset; # just reset ?one-time? searches
5080
5081 Resetting "A-Z" is not recommended because you'll wipe out your
5082 @ARGV and @INC arrays and your %ENV hash. Resets only package
5083 variables; lexical variables are unaffected, but they clean
5084 themselves up on scope exit anyway, so you'll probably want to use
5085 them instead. See "my".
5086
5087 return EXPR
5088 return
5089 Returns from a subroutine, "eval", or "do FILE" with the value
5090 given in EXPR. Evaluation of EXPR may be in list, scalar, or void
5091 context, depending on how the return value will be used, and the
5092 context may vary from one execution to the next (see "wantarray").
5093 If no EXPR is given, returns an empty list in list context, the
5094 undefined value in scalar context, and (of course) nothing at all
5095 in void context.
5096
5097 (In the absence of an explicit "return", a subroutine, eval, or do
5098 FILE automatically returns the value of the last expression
5099 evaluated.)
5100
5101 reverse LIST
5102 In list context, returns a list value consisting of the elements of
5103 LIST in the opposite order. In scalar context, concatenates the
5104 elements of LIST and returns a string value with all characters in
5105 the opposite order.
5106
5107 print join(", ", reverse "world", "Hello"); # Hello, world
5108
5109 print scalar reverse "dlrow ,", "olleH"; # Hello, world
5110
5111 Used without arguments in scalar context, reverse() reverses $_.
5112
5113 $_ = "dlrow ,olleH";
5114 print reverse; # No output, list context
5115 print scalar reverse; # Hello, world
5116
5117 Note that reversing an array to itself (as in "@a = reverse @a")
5118 will preserve non-existent elements whenever possible, i.e., for
5119 non magical arrays or tied arrays with "EXISTS" and "DELETE"
5120 methods.
5121
5122 This operator is also handy for inverting a hash, although there
5123 are some caveats. If a value is duplicated in the original hash,
5124 only one of those can be represented as a key in the inverted hash.
5125 Also, this has to unwind one hash and build a whole new one, which
5126 may take some time on a large hash, such as from a DBM file.
5127
5128 %by_name = reverse %by_address; # Invert the hash
5129
5130 rewinddir DIRHANDLE
5131 Sets the current position to the beginning of the directory for the
5132 "readdir" routine on DIRHANDLE.
5133
5134 Portability issues: "rewinddir" in perlport.
5135
5136 rindex STR,SUBSTR,POSITION
5137 rindex STR,SUBSTR
5138 Works just like index() except that it returns the position of the
5139 last occurrence of SUBSTR in STR. If POSITION is specified,
5140 returns the last occurrence beginning at or before that position.
5141
5142 rmdir FILENAME
5143 rmdir
5144 Deletes the directory specified by FILENAME if that directory is
5145 empty. If it succeeds it returns true; otherwise it returns false
5146 and sets $! (errno). If FILENAME is omitted, uses $_.
5147
5148 To remove a directory tree recursively ("rm -rf" on Unix) look at
5149 the "rmtree" function of the File::Path module.
5150
5151 s///
5152 The substitution operator. See "Regexp Quote-Like Operators" in
5153 perlop.
5154
5155 say FILEHANDLE LIST
5156 say FILEHANDLE
5157 say LIST
5158 say Just like "print", but implicitly appends a newline. "say LIST" is
5159 simply an abbreviation for "{ local $\ = "\n"; print LIST }". To
5160 use FILEHANDLE without a LIST to print the contents of $_ to it,
5161 you must use a real filehandle like "FH", not an indirect one like
5162 $fh.
5163
5164 This keyword is available only when the "say" feature is enabled,
5165 or when prefixed with "CORE::"; see feature. Alternately, include
5166 a "use v5.10" or later to the current scope.
5167
5168 scalar EXPR
5169 Forces EXPR to be interpreted in scalar context and returns the
5170 value of EXPR.
5171
5172 @counts = ( scalar @a, scalar @b, scalar @c );
5173
5174 There is no equivalent operator to force an expression to be
5175 interpolated in list context because in practice, this is never
5176 needed. If you really wanted to do so, however, you could use the
5177 construction "@{[ (some expression) ]}", but usually a simple
5178 "(some expression)" suffices.
5179
5180 Because "scalar" is a unary operator, if you accidentally use a
5181 parenthesized list for the EXPR, this behaves as a scalar comma
5182 expression, evaluating all but the last element in void context and
5183 returning the final element evaluated in scalar context. This is
5184 seldom what you want.
5185
5186 The following single statement:
5187
5188 print uc(scalar(&foo,$bar)),$baz;
5189
5190 is the moral equivalent of these two:
5191
5192 &foo;
5193 print(uc($bar),$baz);
5194
5195 See perlop for more details on unary operators and the comma
5196 operator.
5197
5198 seek FILEHANDLE,POSITION,WHENCE
5199 Sets FILEHANDLE's position, just like the "fseek" call of "stdio".
5200 FILEHANDLE may be an expression whose value gives the name of the
5201 filehandle. The values for WHENCE are 0 to set the new position in
5202 bytes to POSITION; 1 to set it to the current position plus
5203 POSITION; and 2 to set it to EOF plus POSITION, typically negative.
5204 For WHENCE you may use the constants "SEEK_SET", "SEEK_CUR", and
5205 "SEEK_END" (start of the file, current position, end of the file)
5206 from the Fcntl module. Returns 1 on success, false otherwise.
5207
5208 Note the in bytes: even if the filehandle has been set to operate
5209 on characters (for example by using the ":encoding(utf8)" open
5210 layer), tell() will return byte offsets, not character offsets
5211 (because implementing that would render seek() and tell() rather
5212 slow).
5213
5214 If you want to position the file for "sysread" or "syswrite", don't
5215 use "seek", because buffering makes its effect on the file's read-
5216 write position unpredictable and non-portable. Use "sysseek"
5217 instead.
5218
5219 Due to the rules and rigors of ANSI C, on some systems you have to
5220 do a seek whenever you switch between reading and writing. Amongst
5221 other things, this may have the effect of calling stdio's
5222 clearerr(3). A WHENCE of 1 ("SEEK_CUR") is useful for not moving
5223 the file position:
5224
5225 seek(TEST,0,1);
5226
5227 This is also useful for applications emulating "tail -f". Once you
5228 hit EOF on your read and then sleep for a while, you (probably)
5229 have to stick in a dummy seek() to reset things. The "seek"
5230 doesn't change the position, but it does clear the end-of-file
5231 condition on the handle, so that the next "<FILE>" makes Perl try
5232 again to read something. (We hope.)
5233
5234 If that doesn't work (some I/O implementations are particularly
5235 cantankerous), you might need something like this:
5236
5237 for (;;) {
5238 for ($curpos = tell(FILE); $_ = <FILE>;
5239 $curpos = tell(FILE)) {
5240 # search for some stuff and put it into files
5241 }
5242 sleep($for_a_while);
5243 seek(FILE, $curpos, 0);
5244 }
5245
5246 seekdir DIRHANDLE,POS
5247 Sets the current position for the "readdir" routine on DIRHANDLE.
5248 POS must be a value returned by "telldir". "seekdir" also has the
5249 same caveats about possible directory compaction as the
5250 corresponding system library routine.
5251
5252 select FILEHANDLE
5253 select
5254 Returns the currently selected filehandle. If FILEHANDLE is
5255 supplied, sets the new current default filehandle for output. This
5256 has two effects: first, a "write" or a "print" without a filehandle
5257 default to this FILEHANDLE. Second, references to variables
5258 related to output will refer to this output channel.
5259
5260 For example, to set the top-of-form format for more than one output
5261 channel, you might do the following:
5262
5263 select(REPORT1);
5264 $^ = 'report1_top';
5265 select(REPORT2);
5266 $^ = 'report2_top';
5267
5268 FILEHANDLE may be an expression whose value gives the name of the
5269 actual filehandle. Thus:
5270
5271 $oldfh = select(STDERR); $| = 1; select($oldfh);
5272
5273 Some programmers may prefer to think of filehandles as objects with
5274 methods, preferring to write the last example as:
5275
5276 use IO::Handle;
5277 STDERR->autoflush(1);
5278
5279 Portability issues: "select" in perlport.
5280
5281 select RBITS,WBITS,EBITS,TIMEOUT
5282 This calls the select(2) syscall with the bit masks specified,
5283 which can be constructed using "fileno" and "vec", along these
5284 lines:
5285
5286 $rin = $win = $ein = '';
5287 vec($rin, fileno(STDIN), 1) = 1;
5288 vec($win, fileno(STDOUT), 1) = 1;
5289 $ein = $rin | $win;
5290
5291 If you want to select on many filehandles, you may wish to write a
5292 subroutine like this:
5293
5294 sub fhbits {
5295 my @fhlist = @_;
5296 my $bits = "";
5297 for my $fh (@fhlist) {
5298 vec($bits, fileno($fh), 1) = 1;
5299 }
5300 return $bits;
5301 }
5302 $rin = fhbits(*STDIN, *TTY, *MYSOCK);
5303
5304 The usual idiom is:
5305
5306 ($nfound,$timeleft) =
5307 select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
5308
5309 or to block until something becomes ready just do this
5310
5311 $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
5312
5313 Most systems do not bother to return anything useful in $timeleft,
5314 so calling select() in scalar context just returns $nfound.
5315
5316 Any of the bit masks can also be undef. The timeout, if specified,
5317 is in seconds, which may be fractional. Note: not all
5318 implementations are capable of returning the $timeleft. If not,
5319 they always return $timeleft equal to the supplied $timeout.
5320
5321 You can effect a sleep of 250 milliseconds this way:
5322
5323 select(undef, undef, undef, 0.25);
5324
5325 Note that whether "select" gets restarted after signals (say,
5326 SIGALRM) is implementation-dependent. See also perlport for notes
5327 on the portability of "select".
5328
5329 On error, "select" behaves just like select(2): it returns -1 and
5330 sets $!.
5331
5332 On some Unixes, select(2) may report a socket file descriptor as
5333 "ready for reading" even when no data is available, and thus any
5334 subsequent "read" would block. This can be avoided if you always
5335 use O_NONBLOCK on the socket. See select(2) and fcntl(2) for
5336 further details.
5337
5338 The standard "IO::Select" module provides a user-friendlier
5339 interface to "select", mostly because it does all the bit-mask work
5340 for you.
5341
5342 WARNING: One should not attempt to mix buffered I/O (like "read" or
5343 <FH>) with "select", except as permitted by POSIX, and even then
5344 only on POSIX systems. You have to use "sysread" instead.
5345
5346 Portability issues: "select" in perlport.
5347
5348 semctl ID,SEMNUM,CMD,ARG
5349 Calls the System V IPC function semctl(2). You'll probably have to
5350 say
5351
5352 use IPC::SysV;
5353
5354 first to get the correct constant definitions. If CMD is IPC_STAT
5355 or GETALL, then ARG must be a variable that will hold the returned
5356 semid_ds structure or semaphore value array. Returns like "ioctl":
5357 the undefined value for error, ""0 but true"" for zero, or the
5358 actual return value otherwise. The ARG must consist of a vector of
5359 native short integers, which may be created with
5360 "pack("s!",(0)x$nsem)". See also "SysV IPC" in perlipc,
5361 "IPC::SysV", "IPC::Semaphore" documentation.
5362
5363 Portability issues: "semctl" in perlport.
5364
5365 semget KEY,NSEMS,FLAGS
5366 Calls the System V IPC function semget(2). Returns the semaphore
5367 id, or the undefined value on error. See also "SysV IPC" in
5368 perlipc, "IPC::SysV", "IPC::SysV::Semaphore" documentation.
5369
5370 Portability issues: "semget" in perlport.
5371
5372 semop KEY,OPSTRING
5373 Calls the System V IPC function semop(2) for semaphore operations
5374 such as signalling and waiting. OPSTRING must be a packed array of
5375 semop structures. Each semop structure can be generated with
5376 "pack("s!3", $semnum, $semop, $semflag)". The length of OPSTRING
5377 implies the number of semaphore operations. Returns true if
5378 successful, false on error. As an example, the following code
5379 waits on semaphore $semnum of semaphore id $semid:
5380
5381 $semop = pack("s!3", $semnum, -1, 0);
5382 die "Semaphore trouble: $!\n" unless semop($semid, $semop);
5383
5384 To signal the semaphore, replace "-1" with 1. See also "SysV IPC"
5385 in perlipc, "IPC::SysV", and "IPC::SysV::Semaphore" documentation.
5386
5387 Portability issues: "semop" in perlport.
5388
5389 send SOCKET,MSG,FLAGS,TO
5390 send SOCKET,MSG,FLAGS
5391 Sends a message on a socket. Attempts to send the scalar MSG to
5392 the SOCKET filehandle. Takes the same flags as the system call of
5393 the same name. On unconnected sockets, you must specify a
5394 destination to send to, in which case it does a sendto(2) syscall.
5395 Returns the number of characters sent, or the undefined value on
5396 error. The sendmsg(2) syscall is currently unimplemented. See
5397 "UDP: Message Passing" in perlipc for examples.
5398
5399 Note the characters: depending on the status of the socket, either
5400 (8-bit) bytes or characters are sent. By default all sockets
5401 operate on bytes, but for example if the socket has been changed
5402 using binmode() to operate with the ":encoding(utf8)" I/O layer
5403 (see "open", or the "open" pragma, open), the I/O will operate on
5404 UTF-8 encoded Unicode characters, not bytes. Similarly for the
5405 ":encoding" pragma: in that case pretty much any characters can be
5406 sent.
5407
5408 setpgrp PID,PGRP
5409 Sets the current process group for the specified PID, 0 for the
5410 current process. Raises an exception when used on a machine that
5411 doesn't implement POSIX setpgid(2) or BSD setpgrp(2). If the
5412 arguments are omitted, it defaults to "0,0". Note that the BSD 4.2
5413 version of "setpgrp" does not accept any arguments, so only
5414 "setpgrp(0,0)" is portable. See also "POSIX::setsid()".
5415
5416 Portability issues: "setpgrp" in perlport.
5417
5418 setpriority WHICH,WHO,PRIORITY
5419 Sets the current priority for a process, a process group, or a
5420 user. (See setpriority(2).) Raises an exception when used on a
5421 machine that doesn't implement setpriority(2).
5422
5423 Portability issues: "setpriority" in perlport.
5424
5425 setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL
5426 Sets the socket option requested. Returns "undef" on error. Use
5427 integer constants provided by the "Socket" module for LEVEL and
5428 OPNAME. Values for LEVEL can also be obtained from getprotobyname.
5429 OPTVAL might either be a packed string or an integer. An integer
5430 OPTVAL is shorthand for pack("i", OPTVAL).
5431
5432 An example disabling Nagle's algorithm on a socket:
5433
5434 use Socket qw(IPPROTO_TCP TCP_NODELAY);
5435 setsockopt($socket, IPPROTO_TCP, TCP_NODELAY, 1);
5436
5437 Portability issues: "setsockopt" in perlport.
5438
5439 shift ARRAY
5440 shift EXPR
5441 shift
5442 Shifts the first value of the array off and returns it, shortening
5443 the array by 1 and moving everything down. If there are no
5444 elements in the array, returns the undefined value. If ARRAY is
5445 omitted, shifts the @_ array within the lexical scope of
5446 subroutines and formats, and the @ARGV array outside a subroutine
5447 and also within the lexical scopes established by the "eval
5448 STRING", "BEGIN {}", "INIT {}", "CHECK {}", "UNITCHECK {}", and
5449 "END {}" constructs.
5450
5451 Starting with Perl 5.14, "shift" can take a scalar EXPR, which must
5452 hold a reference to an unblessed array. The argument will be
5453 dereferenced automatically. This aspect of "shift" is considered
5454 highly experimental. The exact behaviour may change in a future
5455 version of Perl.
5456
5457 To avoid confusing would-be users of your code who are running
5458 earlier versions of Perl with mysterious syntax errors, put this
5459 sort of thing at the top of your file to signal that your code will
5460 work only on Perls of a recent vintage:
5461
5462 use 5.014; # so push/pop/etc work on scalars (experimental)
5463
5464 See also "unshift", "push", and "pop". "shift" and "unshift" do
5465 the same thing to the left end of an array that "pop" and "push" do
5466 to the right end.
5467
5468 shmctl ID,CMD,ARG
5469 Calls the System V IPC function shmctl. You'll probably have to
5470 say
5471
5472 use IPC::SysV;
5473
5474 first to get the correct constant definitions. If CMD is
5475 "IPC_STAT", then ARG must be a variable that will hold the returned
5476 "shmid_ds" structure. Returns like ioctl: "undef" for error; "0
5477 but true" for zero; and the actual return value otherwise. See
5478 also "SysV IPC" in perlipc and "IPC::SysV" documentation.
5479
5480 Portability issues: "shmctl" in perlport.
5481
5482 shmget KEY,SIZE,FLAGS
5483 Calls the System V IPC function shmget. Returns the shared memory
5484 segment id, or "undef" on error. See also "SysV IPC" in perlipc
5485 and "IPC::SysV" documentation.
5486
5487 Portability issues: "shmget" in perlport.
5488
5489 shmread ID,VAR,POS,SIZE
5490 shmwrite ID,STRING,POS,SIZE
5491 Reads or writes the System V shared memory segment ID starting at
5492 position POS for size SIZE by attaching to it, copying in/out, and
5493 detaching from it. When reading, VAR must be a variable that will
5494 hold the data read. When writing, if STRING is too long, only SIZE
5495 bytes are used; if STRING is too short, nulls are written to fill
5496 out SIZE bytes. Return true if successful, false on error.
5497 shmread() taints the variable. See also "SysV IPC" in perlipc,
5498 "IPC::SysV", and the "IPC::Shareable" module from CPAN.
5499
5500 Portability issues: "shmread" in perlport and "shmwrite" in
5501 perlport.
5502
5503 shutdown SOCKET,HOW
5504 Shuts down a socket connection in the manner indicated by HOW,
5505 which has the same interpretation as in the syscall of the same
5506 name.
5507
5508 shutdown(SOCKET, 0); # I/we have stopped reading data
5509 shutdown(SOCKET, 1); # I/we have stopped writing data
5510 shutdown(SOCKET, 2); # I/we have stopped using this socket
5511
5512 This is useful with sockets when you want to tell the other side
5513 you're done writing but not done reading, or vice versa. It's also
5514 a more insistent form of close because it also disables the file
5515 descriptor in any forked copies in other processes.
5516
5517 Returns 1 for success; on error, returns "undef" if the first
5518 argument is not a valid filehandle, or returns 0 and sets $! for
5519 any other failure.
5520
5521 sin EXPR
5522 sin Returns the sine of EXPR (expressed in radians). If EXPR is
5523 omitted, returns sine of $_.
5524
5525 For the inverse sine operation, you may use the "Math::Trig::asin"
5526 function, or use this relation:
5527
5528 sub asin { atan2($_[0], sqrt(1 - $_[0] * $_[0])) }
5529
5530 sleep EXPR
5531 sleep
5532 Causes the script to sleep for (integer) EXPR seconds, or forever
5533 if no argument is given. Returns the integer number of seconds
5534 actually slept.
5535
5536 May be interrupted if the process receives a signal such as
5537 "SIGALRM".
5538
5539 eval {
5540 local $SIG{ALARM} = sub { die "Alarm!\n" };
5541 sleep;
5542 };
5543 die $@ unless $@ eq "Alarm!\n";
5544
5545 You probably cannot mix "alarm" and "sleep" calls, because "sleep"
5546 is often implemented using "alarm".
5547
5548 On some older systems, it may sleep up to a full second less than
5549 what you requested, depending on how it counts seconds. Most
5550 modern systems always sleep the full amount. They may appear to
5551 sleep longer than that, however, because your process might not be
5552 scheduled right away in a busy multitasking system.
5553
5554 For delays of finer granularity than one second, the Time::HiRes
5555 module (from CPAN, and starting from Perl 5.8 part of the standard
5556 distribution) provides usleep(). You may also use Perl's four-
5557 argument version of select() leaving the first three arguments
5558 undefined, or you might be able to use the "syscall" interface to
5559 access setitimer(2) if your system supports it. See perlfaq8 for
5560 details.
5561
5562 See also the POSIX module's "pause" function.
5563
5564 socket SOCKET,DOMAIN,TYPE,PROTOCOL
5565 Opens a socket of the specified kind and attaches it to filehandle
5566 SOCKET. DOMAIN, TYPE, and PROTOCOL are specified the same as for
5567 the syscall of the same name. You should "use Socket" first to get
5568 the proper definitions imported. See the examples in "Sockets:
5569 Client/Server Communication" in perlipc.
5570
5571 On systems that support a close-on-exec flag on files, the flag
5572 will be set for the newly opened file descriptor, as determined by
5573 the value of $^F. See "$^F" in perlvar.
5574
5575 socketpair SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL
5576 Creates an unnamed pair of sockets in the specified domain, of the
5577 specified type. DOMAIN, TYPE, and PROTOCOL are specified the same
5578 as for the syscall of the same name. If unimplemented, raises an
5579 exception. Returns true if successful.
5580
5581 On systems that support a close-on-exec flag on files, the flag
5582 will be set for the newly opened file descriptors, as determined by
5583 the value of $^F. See "$^F" in perlvar.
5584
5585 Some systems defined "pipe" in terms of "socketpair", in which a
5586 call to "pipe(Rdr, Wtr)" is essentially:
5587
5588 use Socket;
5589 socketpair(Rdr, Wtr, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
5590 shutdown(Rdr, 1); # no more writing for reader
5591 shutdown(Wtr, 0); # no more reading for writer
5592
5593 See perlipc for an example of socketpair use. Perl 5.8 and later
5594 will emulate socketpair using IP sockets to localhost if your
5595 system implements sockets but not socketpair.
5596
5597 Portability issues: "socketpair" in perlport.
5598
5599 sort SUBNAME LIST
5600 sort BLOCK LIST
5601 sort LIST
5602 In list context, this sorts the LIST and returns the sorted list
5603 value. In scalar context, the behaviour of "sort()" is undefined.
5604
5605 If SUBNAME or BLOCK is omitted, "sort"s in standard string
5606 comparison order. If SUBNAME is specified, it gives the name of a
5607 subroutine that returns an integer less than, equal to, or greater
5608 than 0, depending on how the elements of the list are to be
5609 ordered. (The "<=>" and "cmp" operators are extremely useful in
5610 such routines.) SUBNAME may be a scalar variable name
5611 (unsubscripted), in which case the value provides the name of (or a
5612 reference to) the actual subroutine to use. In place of a SUBNAME,
5613 you can provide a BLOCK as an anonymous, in-line sort subroutine.
5614
5615 If the subroutine's prototype is "($$)", the elements to be
5616 compared are passed by reference in @_, as for a normal subroutine.
5617 This is slower than unprototyped subroutines, where the elements to
5618 be compared are passed into the subroutine as the package global
5619 variables $a and $b (see example below). Note that in the latter
5620 case, it is usually highly counter-productive to declare $a and $b
5621 as lexicals.
5622
5623 If the subroutine is an XSUB, the elements to be compared are
5624 pushed on to the stack, the way arguments are usually passed to
5625 XSUBs. $a and $b are not set.
5626
5627 The values to be compared are always passed by reference and should
5628 not be modified.
5629
5630 You also cannot exit out of the sort block or subroutine using any
5631 of the loop control operators described in perlsyn or with "goto".
5632
5633 When "use locale" (but not "use locale 'not_characters'") is in
5634 effect, "sort LIST" sorts LIST according to the current collation
5635 locale. See perllocale.
5636
5637 sort() returns aliases into the original list, much as a for loop's
5638 index variable aliases the list elements. That is, modifying an
5639 element of a list returned by sort() (for example, in a "foreach",
5640 "map" or "grep") actually modifies the element in the original
5641 list. This is usually something to be avoided when writing clear
5642 code.
5643
5644 Perl 5.6 and earlier used a quicksort algorithm to implement sort.
5645 That algorithm was not stable, so could go quadratic. (A stable
5646 sort preserves the input order of elements that compare equal.
5647 Although quicksort's run time is O(NlogN) when averaged over all
5648 arrays of length N, the time can be O(N**2), quadratic behavior,
5649 for some inputs.) In 5.7, the quicksort implementation was
5650 replaced with a stable mergesort algorithm whose worst-case
5651 behavior is O(NlogN). But benchmarks indicated that for some
5652 inputs, on some platforms, the original quicksort was faster. 5.8
5653 has a sort pragma for limited control of the sort. Its rather
5654 blunt control of the underlying algorithm may not persist into
5655 future Perls, but the ability to characterize the input or output
5656 in implementation independent ways quite probably will. See the
5657 sort pragma.
5658
5659 Examples:
5660
5661 # sort lexically
5662 @articles = sort @files;
5663
5664 # same thing, but with explicit sort routine
5665 @articles = sort {$a cmp $b} @files;
5666
5667 # now case-insensitively
5668 @articles = sort {fc($a) cmp fc($b)} @files;
5669
5670 # same thing in reversed order
5671 @articles = sort {$b cmp $a} @files;
5672
5673 # sort numerically ascending
5674 @articles = sort {$a <=> $b} @files;
5675
5676 # sort numerically descending
5677 @articles = sort {$b <=> $a} @files;
5678
5679 # this sorts the %age hash by value instead of key
5680 # using an in-line function
5681 @eldest = sort { $age{$b} <=> $age{$a} } keys %age;
5682
5683 # sort using explicit subroutine name
5684 sub byage {
5685 $age{$a} <=> $age{$b}; # presuming numeric
5686 }
5687 @sortedclass = sort byage @class;
5688
5689 sub backwards { $b cmp $a }
5690 @harry = qw(dog cat x Cain Abel);
5691 @george = qw(gone chased yz Punished Axed);
5692 print sort @harry;
5693 # prints AbelCaincatdogx
5694 print sort backwards @harry;
5695 # prints xdogcatCainAbel
5696 print sort @george, 'to', @harry;
5697 # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
5698
5699 # inefficiently sort by descending numeric compare using
5700 # the first integer after the first = sign, or the
5701 # whole record case-insensitively otherwise
5702
5703 my @new = sort {
5704 ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0]
5705 ||
5706 fc($a) cmp fc($b)
5707 } @old;
5708
5709 # same thing, but much more efficiently;
5710 # we'll build auxiliary indices instead
5711 # for speed
5712 my @nums = @caps = ();
5713 for (@old) {
5714 push @nums, ( /=(\d+)/ ? $1 : undef );
5715 push @caps, fc($_);
5716 }
5717
5718 my @new = @old[ sort {
5719 $nums[$b] <=> $nums[$a]
5720 ||
5721 $caps[$a] cmp $caps[$b]
5722 } 0..$#old
5723 ];
5724
5725 # same thing, but without any temps
5726 @new = map { $_->[0] }
5727 sort { $b->[1] <=> $a->[1]
5728 ||
5729 $a->[2] cmp $b->[2]
5730 } map { [$_, /=(\d+)/, fc($_)] } @old;
5731
5732 # using a prototype allows you to use any comparison subroutine
5733 # as a sort subroutine (including other package's subroutines)
5734 package other;
5735 sub backwards ($$) { $_[1] cmp $_[0]; } # $a and $b are not set here
5736
5737 package main;
5738 @new = sort other::backwards @old;
5739
5740 # guarantee stability, regardless of algorithm
5741 use sort 'stable';
5742 @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
5743
5744 # force use of mergesort (not portable outside Perl 5.8)
5745 use sort '_mergesort'; # note discouraging _
5746 @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
5747
5748 Warning: syntactical care is required when sorting the list
5749 returned from a function. If you want to sort the list returned by
5750 the function call "find_records(@key)", you can use:
5751
5752 @contact = sort { $a cmp $b } find_records @key;
5753 @contact = sort +find_records(@key);
5754 @contact = sort &find_records(@key);
5755 @contact = sort(find_records(@key));
5756
5757 If instead you want to sort the array @key with the comparison
5758 routine "find_records()" then you can use:
5759
5760 @contact = sort { find_records() } @key;
5761 @contact = sort find_records(@key);
5762 @contact = sort(find_records @key);
5763 @contact = sort(find_records (@key));
5764
5765 If you're using strict, you must not declare $a and $b as lexicals.
5766 They are package globals. That means that if you're in the "main"
5767 package and type
5768
5769 @articles = sort {$b <=> $a} @files;
5770
5771 then $a and $b are $main::a and $main::b (or $::a and $::b), but if
5772 you're in the "FooPack" package, it's the same as typing
5773
5774 @articles = sort {$FooPack::b <=> $FooPack::a} @files;
5775
5776 The comparison function is required to behave. If it returns
5777 inconsistent results (sometimes saying $x[1] is less than $x[2] and
5778 sometimes saying the opposite, for example) the results are not
5779 well-defined.
5780
5781 Because "<=>" returns "undef" when either operand is "NaN" (not-a-
5782 number), be careful when sorting with a comparison function like
5783 "$a <=> $b" any lists that might contain a "NaN". The following
5784 example takes advantage that "NaN != NaN" to eliminate any "NaN"s
5785 from the input list.
5786
5787 @result = sort { $a <=> $b } grep { $_ == $_ } @input;
5788
5789 splice ARRAY or EXPR,OFFSET,LENGTH,LIST
5790 splice ARRAY or EXPR,OFFSET,LENGTH
5791 splice ARRAY or EXPR,OFFSET
5792 splice ARRAY or EXPR
5793 Removes the elements designated by OFFSET and LENGTH from an array,
5794 and replaces them with the elements of LIST, if any. In list
5795 context, returns the elements removed from the array. In scalar
5796 context, returns the last element removed, or "undef" if no
5797 elements are removed. The array grows or shrinks as necessary. If
5798 OFFSET is negative then it starts that far from the end of the
5799 array. If LENGTH is omitted, removes everything from OFFSET
5800 onward. If LENGTH is negative, removes the elements from OFFSET
5801 onward except for -LENGTH elements at the end of the array. If
5802 both OFFSET and LENGTH are omitted, removes everything. If OFFSET
5803 is past the end of the array, Perl issues a warning, and splices at
5804 the end of the array.
5805
5806 The following equivalences hold (assuming "$#a >= $i" )
5807
5808 push(@a,$x,$y) splice(@a,@a,0,$x,$y)
5809 pop(@a) splice(@a,-1)
5810 shift(@a) splice(@a,0,1)
5811 unshift(@a,$x,$y) splice(@a,0,0,$x,$y)
5812 $a[$i] = $y splice(@a,$i,1,$y)
5813
5814 Example, assuming array lengths are passed before arrays:
5815
5816 sub aeq { # compare two list values
5817 my(@a) = splice(@_,0,shift);
5818 my(@b) = splice(@_,0,shift);
5819 return 0 unless @a == @b; # same len?
5820 while (@a) {
5821 return 0 if pop(@a) ne pop(@b);
5822 }
5823 return 1;
5824 }
5825 if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }
5826
5827 Starting with Perl 5.14, "splice" can take scalar EXPR, which must
5828 hold a reference to an unblessed array. The argument will be
5829 dereferenced automatically. This aspect of "splice" is considered
5830 highly experimental. The exact behaviour may change in a future
5831 version of Perl.
5832
5833 To avoid confusing would-be users of your code who are running
5834 earlier versions of Perl with mysterious syntax errors, put this
5835 sort of thing at the top of your file to signal that your code will
5836 work only on Perls of a recent vintage:
5837
5838 use 5.014; # so push/pop/etc work on scalars (experimental)
5839
5840 split /PATTERN/,EXPR,LIMIT
5841 split /PATTERN/,EXPR
5842 split /PATTERN/
5843 split
5844 Splits the string EXPR into a list of strings and returns the list
5845 in list context, or the size of the list in scalar context.
5846
5847 If only PATTERN is given, EXPR defaults to $_.
5848
5849 Anything in EXPR that matches PATTERN is taken to be a separator
5850 that separates the EXPR into substrings (called "fields") that do
5851 not include the separator. Note that a separator may be longer
5852 than one character or even have no characters at all (the empty
5853 string, which is a zero-width match).
5854
5855 The PATTERN need not be constant; an expression may be used to
5856 specify a pattern that varies at runtime.
5857
5858 If PATTERN matches the empty string, the EXPR is split at the match
5859 position (between characters). As an example, the following:
5860
5861 print join(':', split('b', 'abc')), "\n";
5862
5863 uses the 'b' in 'abc' as a separator to produce the output 'a:c'.
5864 However, this:
5865
5866 print join(':', split('', 'abc')), "\n";
5867
5868 uses empty string matches as separators to produce the output
5869 'a:b:c'; thus, the empty string may be used to split EXPR into a
5870 list of its component characters.
5871
5872 As a special case for "split", the empty pattern given in match
5873 operator syntax ("//") specifically matches the empty string, which
5874 is contrary to its usual interpretation as the last successful
5875 match.
5876
5877 If PATTERN is "/^/", then it is treated as if it used the multiline
5878 modifier ("/^/m"), since it isn't much use otherwise.
5879
5880 As another special case, "split" emulates the default behavior of
5881 the command line tool awk when the PATTERN is either omitted or a
5882 literal string composed of a single space character (such as ' ' or
5883 "\x20", but not e.g. "/ /"). In this case, any leading whitespace
5884 in EXPR is removed before splitting occurs, and the PATTERN is
5885 instead treated as if it were "/\s+/"; in particular, this means
5886 that any contiguous whitespace (not just a single space character)
5887 is used as a separator. However, this special treatment can be
5888 avoided by specifying the pattern "/ /" instead of the string " ",
5889 thereby allowing only a single space character to be a separator.
5890
5891 If omitted, PATTERN defaults to a single space, " ", triggering the
5892 previously described awk emulation.
5893
5894 If LIMIT is specified and positive, it represents the maximum
5895 number of fields into which the EXPR may be split; in other words,
5896 LIMIT is one greater than the maximum number of times EXPR may be
5897 split. Thus, the LIMIT value 1 means that EXPR may be split a
5898 maximum of zero times, producing a maximum of one field (namely,
5899 the entire value of EXPR). For instance:
5900
5901 print join(':', split(//, 'abc', 1)), "\n";
5902
5903 produces the output 'abc', and this:
5904
5905 print join(':', split(//, 'abc', 2)), "\n";
5906
5907 produces the output 'a:bc', and each of these:
5908
5909 print join(':', split(//, 'abc', 3)), "\n";
5910 print join(':', split(//, 'abc', 4)), "\n";
5911
5912 produces the output 'a:b:c'.
5913
5914 If LIMIT is negative, it is treated as if it were instead
5915 arbitrarily large; as many fields as possible are produced.
5916
5917 If LIMIT is omitted (or, equivalently, zero), then it is usually
5918 treated as if it were instead negative but with the exception that
5919 trailing empty fields are stripped (empty leading fields are always
5920 preserved); if all fields are empty, then all fields are considered
5921 to be trailing (and are thus stripped in this case). Thus, the
5922 following:
5923
5924 print join(':', split(',', 'a,b,c,,,')), "\n";
5925
5926 produces the output 'a:b:c', but the following:
5927
5928 print join(':', split(',', 'a,b,c,,,', -1)), "\n";
5929
5930 produces the output 'a:b:c:::'.
5931
5932 In time-critical applications, it is worthwhile to avoid splitting
5933 into more fields than necessary. Thus, when assigning to a list,
5934 if LIMIT is omitted (or zero), then LIMIT is treated as though it
5935 were one larger than the number of variables in the list; for the
5936 following, LIMIT is implicitly 4:
5937
5938 ($login, $passwd, $remainder) = split(/:/);
5939
5940 Note that splitting an EXPR that evaluates to the empty string
5941 always produces zero fields, regardless of the LIMIT specified.
5942
5943 An empty leading field is produced when there is a positive-width
5944 match at the beginning of EXPR. For instance:
5945
5946 print join(':', split(/ /, ' abc')), "\n";
5947
5948 produces the output ':abc'. However, a zero-width match at the
5949 beginning of EXPR never produces an empty field, so that:
5950
5951 print join(':', split(//, ' abc'));
5952
5953 produces the output ' :a:b:c' (rather than ': :a:b:c').
5954
5955 An empty trailing field, on the other hand, is produced when there
5956 is a match at the end of EXPR, regardless of the length of the
5957 match (of course, unless a non-zero LIMIT is given explicitly, such
5958 fields are removed, as in the last example). Thus:
5959
5960 print join(':', split(//, ' abc', -1)), "\n";
5961
5962 produces the output ' :a:b:c:'.
5963
5964 If the PATTERN contains capturing groups, then for each separator,
5965 an additional field is produced for each substring captured by a
5966 group (in the order in which the groups are specified, as per
5967 backreferences); if any group does not match, then it captures the
5968 "undef" value instead of a substring. Also, note that any such
5969 additional field is produced whenever there is a separator (that
5970 is, whenever a split occurs), and such an additional field does not
5971 count towards the LIMIT. Consider the following expressions
5972 evaluated in list context (each returned list is provided in the
5973 associated comment):
5974
5975 split(/-|,/, "1-10,20", 3)
5976 # ('1', '10', '20')
5977
5978 split(/(-|,)/, "1-10,20", 3)
5979 # ('1', '-', '10', ',', '20')
5980
5981 split(/-|(,)/, "1-10,20", 3)
5982 # ('1', undef, '10', ',', '20')
5983
5984 split(/(-)|,/, "1-10,20", 3)
5985 # ('1', '-', '10', undef, '20')
5986
5987 split(/(-)|(,)/, "1-10,20", 3)
5988 # ('1', '-', undef, '10', undef, ',', '20')
5989
5990 sprintf FORMAT, LIST
5991 Returns a string formatted by the usual "printf" conventions of the
5992 C library function "sprintf". See below for more details and see
5993 sprintf(3) or printf(3) on your system for an explanation of the
5994 general principles.
5995
5996 For example:
5997
5998 # Format number with up to 8 leading zeroes
5999 $result = sprintf("%08d", $number);
6000
6001 # Round number to 3 digits after decimal point
6002 $rounded = sprintf("%.3f", $number);
6003
6004 Perl does its own "sprintf" formatting: it emulates the C function
6005 sprintf(3), but doesn't use it except for floating-point numbers,
6006 and even then only standard modifiers are allowed. Non-standard
6007 extensions in your local sprintf(3) are therefore unavailable from
6008 Perl.
6009
6010 Unlike "printf", "sprintf" does not do what you probably mean when
6011 you pass it an array as your first argument. The array is given
6012 scalar context, and instead of using the 0th element of the array
6013 as the format, Perl will use the count of elements in the array as
6014 the format, which is almost never useful.
6015
6016 Perl's "sprintf" permits the following universally-known
6017 conversions:
6018
6019 %% a percent sign
6020 %c a character with the given number
6021 %s a string
6022 %d a signed integer, in decimal
6023 %u an unsigned integer, in decimal
6024 %o an unsigned integer, in octal
6025 %x an unsigned integer, in hexadecimal
6026 %e a floating-point number, in scientific notation
6027 %f a floating-point number, in fixed decimal notation
6028 %g a floating-point number, in %e or %f notation
6029
6030 In addition, Perl permits the following widely-supported
6031 conversions:
6032
6033 %X like %x, but using upper-case letters
6034 %E like %e, but using an upper-case "E"
6035 %G like %g, but with an upper-case "E" (if applicable)
6036 %b an unsigned integer, in binary
6037 %B like %b, but using an upper-case "B" with the # flag
6038 %p a pointer (outputs the Perl value's address in hexadecimal)
6039 %n special: *stores* the number of characters output so far
6040 into the next argument in the parameter list
6041
6042 Finally, for backward (and we do mean "backward") compatibility,
6043 Perl permits these unnecessary but widely-supported conversions:
6044
6045 %i a synonym for %d
6046 %D a synonym for %ld
6047 %U a synonym for %lu
6048 %O a synonym for %lo
6049 %F a synonym for %f
6050
6051 Note that the number of exponent digits in the scientific notation
6052 produced by %e, %E, %g and %G for numbers with the modulus of the
6053 exponent less than 100 is system-dependent: it may be three or less
6054 (zero-padded as necessary). In other words, 1.23 times ten to the
6055 99th may be either "1.23e99" or "1.23e099".
6056
6057 Between the "%" and the format letter, you may specify several
6058 additional attributes controlling the interpretation of the format.
6059 In order, these are:
6060
6061 format parameter index
6062 An explicit format parameter index, such as "2$". By default
6063 sprintf will format the next unused argument in the list, but
6064 this allows you to take the arguments out of order:
6065
6066 printf '%2$d %1$d', 12, 34; # prints "34 12"
6067 printf '%3$d %d %1$d', 1, 2, 3; # prints "3 1 1"
6068
6069 flags
6070 one or more of:
6071
6072 space prefix non-negative number with a space
6073 + prefix non-negative number with a plus sign
6074 - left-justify within the field
6075 0 use zeros, not spaces, to right-justify
6076 # ensure the leading "0" for any octal,
6077 prefix non-zero hexadecimal with "0x" or "0X",
6078 prefix non-zero binary with "0b" or "0B"
6079
6080 For example:
6081
6082 printf '<% d>', 12; # prints "< 12>"
6083 printf '<%+d>', 12; # prints "<+12>"
6084 printf '<%6s>', 12; # prints "< 12>"
6085 printf '<%-6s>', 12; # prints "<12 >"
6086 printf '<%06s>', 12; # prints "<000012>"
6087 printf '<%#o>', 12; # prints "<014>"
6088 printf '<%#x>', 12; # prints "<0xc>"
6089 printf '<%#X>', 12; # prints "<0XC>"
6090 printf '<%#b>', 12; # prints "<0b1100>"
6091 printf '<%#B>', 12; # prints "<0B1100>"
6092
6093 When a space and a plus sign are given as the flags at once, a
6094 plus sign is used to prefix a positive number.
6095
6096 printf '<%+ d>', 12; # prints "<+12>"
6097 printf '<% +d>', 12; # prints "<+12>"
6098
6099 When the # flag and a precision are given in the %o conversion,
6100 the precision is incremented if it's necessary for the leading
6101 "0".
6102
6103 printf '<%#.5o>', 012; # prints "<00012>"
6104 printf '<%#.5o>', 012345; # prints "<012345>"
6105 printf '<%#.0o>', 0; # prints "<0>"
6106
6107 vector flag
6108 This flag tells Perl to interpret the supplied string as a
6109 vector of integers, one for each character in the string. Perl
6110 applies the format to each integer in turn, then joins the
6111 resulting strings with a separator (a dot "." by default).
6112 This can be useful for displaying ordinal values of characters
6113 in arbitrary strings:
6114
6115 printf "%vd", "AB\x{100}"; # prints "65.66.256"
6116 printf "version is v%vd\n", $^V; # Perl's version
6117
6118 Put an asterisk "*" before the "v" to override the string to
6119 use to separate the numbers:
6120
6121 printf "address is %*vX\n", ":", $addr; # IPv6 address
6122 printf "bits are %0*v8b\n", " ", $bits; # random bitstring
6123
6124 You can also explicitly specify the argument number to use for
6125 the join string using something like "*2$v"; for example:
6126
6127 printf '%*4$vX %*4$vX %*4$vX', @addr[1..3], ":"; # 3 IPv6 addresses
6128
6129 (minimum) width
6130 Arguments are usually formatted to be only as wide as required
6131 to display the given value. You can override the width by
6132 putting a number here, or get the width from the next argument
6133 (with "*") or from a specified argument (e.g., with "*2$"):
6134
6135 printf "<%s>", "a"; # prints "<a>"
6136 printf "<%6s>", "a"; # prints "< a>"
6137 printf "<%*s>", 6, "a"; # prints "< a>"
6138 printf "<%*2$s>", "a", 6; # prints "< a>"
6139 printf "<%2s>", "long"; # prints "<long>" (does not truncate)
6140
6141 If a field width obtained through "*" is negative, it has the
6142 same effect as the "-" flag: left-justification.
6143
6144 precision, or maximum width
6145 You can specify a precision (for numeric conversions) or a
6146 maximum width (for string conversions) by specifying a "."
6147 followed by a number. For floating-point formats except "g"
6148 and "G", this specifies how many places right of the decimal
6149 point to show (the default being 6). For example:
6150
6151 # these examples are subject to system-specific variation
6152 printf '<%f>', 1; # prints "<1.000000>"
6153 printf '<%.1f>', 1; # prints "<1.0>"
6154 printf '<%.0f>', 1; # prints "<1>"
6155 printf '<%e>', 10; # prints "<1.000000e+01>"
6156 printf '<%.1e>', 10; # prints "<1.0e+01>"
6157
6158 For "g" and "G", this specifies the maximum number of digits to
6159 show, including those prior to the decimal point and those
6160 after it; for example:
6161
6162 # These examples are subject to system-specific variation.
6163 printf '<%g>', 1; # prints "<1>"
6164 printf '<%.10g>', 1; # prints "<1>"
6165 printf '<%g>', 100; # prints "<100>"
6166 printf '<%.1g>', 100; # prints "<1e+02>"
6167 printf '<%.2g>', 100.01; # prints "<1e+02>"
6168 printf '<%.5g>', 100.01; # prints "<100.01>"
6169 printf '<%.4g>', 100.01; # prints "<100>"
6170
6171 For integer conversions, specifying a precision implies that
6172 the output of the number itself should be zero-padded to this
6173 width, where the 0 flag is ignored:
6174
6175 printf '<%.6d>', 1; # prints "<000001>"
6176 printf '<%+.6d>', 1; # prints "<+000001>"
6177 printf '<%-10.6d>', 1; # prints "<000001 >"
6178 printf '<%10.6d>', 1; # prints "< 000001>"
6179 printf '<%010.6d>', 1; # prints "< 000001>"
6180 printf '<%+10.6d>', 1; # prints "< +000001>"
6181
6182 printf '<%.6x>', 1; # prints "<000001>"
6183 printf '<%#.6x>', 1; # prints "<0x000001>"
6184 printf '<%-10.6x>', 1; # prints "<000001 >"
6185 printf '<%10.6x>', 1; # prints "< 000001>"
6186 printf '<%010.6x>', 1; # prints "< 000001>"
6187 printf '<%#10.6x>', 1; # prints "< 0x000001>"
6188
6189 For string conversions, specifying a precision truncates the
6190 string to fit the specified width:
6191
6192 printf '<%.5s>', "truncated"; # prints "<trunc>"
6193 printf '<%10.5s>', "truncated"; # prints "< trunc>"
6194
6195 You can also get the precision from the next argument using
6196 ".*":
6197
6198 printf '<%.6x>', 1; # prints "<000001>"
6199 printf '<%.*x>', 6, 1; # prints "<000001>"
6200
6201 If a precision obtained through "*" is negative, it counts as
6202 having no precision at all.
6203
6204 printf '<%.*s>', 7, "string"; # prints "<string>"
6205 printf '<%.*s>', 3, "string"; # prints "<str>"
6206 printf '<%.*s>', 0, "string"; # prints "<>"
6207 printf '<%.*s>', -1, "string"; # prints "<string>"
6208
6209 printf '<%.*d>', 1, 0; # prints "<0>"
6210 printf '<%.*d>', 0, 0; # prints "<>"
6211 printf '<%.*d>', -1, 0; # prints "<0>"
6212
6213 You cannot currently get the precision from a specified number,
6214 but it is intended that this will be possible in the future,
6215 for example using ".*2$":
6216
6217 printf "<%.*2$x>", 1, 6; # INVALID, but in future will print "<000001>"
6218
6219 size
6220 For numeric conversions, you can specify the size to interpret
6221 the number as using "l", "h", "V", "q", "L", or "ll". For
6222 integer conversions ("d u o x X b i D U O"), numbers are
6223 usually assumed to be whatever the default integer size is on
6224 your platform (usually 32 or 64 bits), but you can override
6225 this to use instead one of the standard C types, as supported
6226 by the compiler used to build Perl:
6227
6228 hh interpret integer as C type "char" or "unsigned char"
6229 on Perl 5.14 or later
6230 h interpret integer as C type "short" or "unsigned short"
6231 j interpret integer as C type "intmax_t" on Perl 5.14
6232 or later, and only with a C99 compiler (unportable)
6233 l interpret integer as C type "long" or "unsigned long"
6234 q, L, or ll interpret integer as C type "long long", "unsigned long long",
6235 or "quad" (typically 64-bit integers)
6236 t interpret integer as C type "ptrdiff_t" on Perl 5.14 or later
6237 z interpret integer as C type "size_t" on Perl 5.14 or later
6238
6239 As of 5.14, none of these raises an exception if they are not
6240 supported on your platform. However, if warnings are enabled,
6241 a warning of the "printf" warning class is issued on an
6242 unsupported conversion flag. Should you instead prefer an
6243 exception, do this:
6244
6245 use warnings FATAL => "printf";
6246
6247 If you would like to know about a version dependency before you
6248 start running the program, put something like this at its top:
6249
6250 use 5.014; # for hh/j/t/z/ printf modifiers
6251
6252 You can find out whether your Perl supports quads via Config:
6253
6254 use Config;
6255 if ($Config{use64bitint} eq "define" || $Config{longsize} >= 8) {
6256 print "Nice quads!\n";
6257 }
6258
6259 For floating-point conversions ("e f g E F G"), numbers are
6260 usually assumed to be the default floating-point size on your
6261 platform (double or long double), but you can force "long
6262 double" with "q", "L", or "ll" if your platform supports them.
6263 You can find out whether your Perl supports long doubles via
6264 Config:
6265
6266 use Config;
6267 print "long doubles\n" if $Config{d_longdbl} eq "define";
6268
6269 You can find out whether Perl considers "long double" to be the
6270 default floating-point size to use on your platform via Config:
6271
6272 use Config;
6273 if ($Config{uselongdouble} eq "define") {
6274 print "long doubles by default\n";
6275 }
6276
6277 It can also be that long doubles and doubles are the same
6278 thing:
6279
6280 use Config;
6281 ($Config{doublesize} == $Config{longdblsize}) &&
6282 print "doubles are long doubles\n";
6283
6284 The size specifier "V" has no effect for Perl code, but is
6285 supported for compatibility with XS code. It means "use the
6286 standard size for a Perl integer or floating-point number",
6287 which is the default.
6288
6289 order of arguments
6290 Normally, sprintf() takes the next unused argument as the value
6291 to format for each format specification. If the format
6292 specification uses "*" to require additional arguments, these
6293 are consumed from the argument list in the order they appear in
6294 the format specification before the value to format. Where an
6295 argument is specified by an explicit index, this does not
6296 affect the normal order for the arguments, even when the
6297 explicitly specified index would have been the next argument.
6298
6299 So:
6300
6301 printf "<%*.*s>", $a, $b, $c;
6302
6303 uses $a for the width, $b for the precision, and $c as the
6304 value to format; while:
6305
6306 printf "<%*1$.*s>", $a, $b;
6307
6308 would use $a for the width and precision, and $b as the value
6309 to format.
6310
6311 Here are some more examples; be aware that when using an
6312 explicit index, the "$" may need escaping:
6313
6314 printf "%2\$d %d\n", 12, 34; # will print "34 12\n"
6315 printf "%2\$d %d %d\n", 12, 34; # will print "34 12 34\n"
6316 printf "%3\$d %d %d\n", 12, 34, 56; # will print "56 12 34\n"
6317 printf "%2\$*3\$d %d\n", 12, 34, 3; # will print " 34 12\n"
6318
6319 If "use locale" (including "use locale 'not_characters'") is in
6320 effect and POSIX::setlocale() has been called, the character used
6321 for the decimal separator in formatted floating-point numbers is
6322 affected by the LC_NUMERIC locale. See perllocale and POSIX.
6323
6324 sqrt EXPR
6325 sqrt
6326 Return the positive square root of EXPR. If EXPR is omitted, uses
6327 $_. Works only for non-negative operands unless you've loaded the
6328 "Math::Complex" module.
6329
6330 use Math::Complex;
6331 print sqrt(-4); # prints 2i
6332
6333 srand EXPR
6334 srand
6335 Sets and returns the random number seed for the "rand" operator.
6336
6337 The point of the function is to "seed" the "rand" function so that
6338 "rand" can produce a different sequence each time you run your
6339 program. When called with a parameter, "srand" uses that for the
6340 seed; otherwise it (semi-)randomly chooses a seed. In either case,
6341 starting with Perl 5.14, it returns the seed. To signal that your
6342 code will work only on Perls of a recent vintage:
6343
6344 use 5.014; # so srand returns the seed
6345
6346 If "srand()" is not called explicitly, it is called implicitly
6347 without a parameter at the first use of the "rand" operator.
6348 However, this was not true of versions of Perl before 5.004, so if
6349 your script will run under older Perl versions, it should call
6350 "srand"; otherwise most programs won't call "srand()" at all.
6351
6352 But there are a few situations in recent Perls where programs are
6353 likely to want to call "srand". One is for generating predictable
6354 results generally for testing or debugging. There, you use
6355 "srand($seed)", with the same $seed each time. Another case is
6356 that you may want to call "srand()" after a "fork()" to avoid child
6357 processes sharing the same seed value as the parent (and
6358 consequently each other).
6359
6360 Do not call "srand()" (i.e., without an argument) more than once
6361 per process. The internal state of the random number generator
6362 should contain more entropy than can be provided by any seed, so
6363 calling "srand()" again actually loses randomness.
6364
6365 Most implementations of "srand" take an integer and will silently
6366 truncate decimal numbers. This means "srand(42)" will usually
6367 produce the same results as "srand(42.1)". To be safe, always pass
6368 "srand" an integer.
6369
6370 In versions of Perl prior to 5.004 the default seed was just the
6371 current "time". This isn't a particularly good seed, so many old
6372 programs supply their own seed value (often "time ^ $$" or "time ^
6373 ($$ + ($$ << 15))"), but that isn't necessary any more.
6374
6375 Frequently called programs (like CGI scripts) that simply use
6376
6377 time ^ $$
6378
6379 for a seed can fall prey to the mathematical property that
6380
6381 a^b == (a+1)^(b+1)
6382
6383 one-third of the time. So don't do that.
6384
6385 A typical use of the returned seed is for a test program which has
6386 too many combinations to test comprehensively in the time available
6387 to it each run. It can test a random subset each time, and should
6388 there be a failure, log the seed used for that run so that it can
6389 later be used to reproduce the same results.
6390
6391 "rand()" is not cryptographically secure. You should not rely on
6392 it in security-sensitive situations. As of this writing, a number
6393 of third-party CPAN modules offer random number generators intended
6394 by their authors to be cryptographically secure, including:
6395 Data::Entropy, Crypt::Random, Math::Random::Secure, and
6396 Math::TrulyRandom.
6397
6398 stat FILEHANDLE
6399 stat EXPR
6400 stat DIRHANDLE
6401 stat
6402 Returns a 13-element list giving the status info for a file, either
6403 the file opened via FILEHANDLE or DIRHANDLE, or named by EXPR. If
6404 EXPR is omitted, it stats $_ (not "_"!). Returns the empty list if
6405 "stat" fails. Typically used as follows:
6406
6407 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
6408 $atime,$mtime,$ctime,$blksize,$blocks)
6409 = stat($filename);
6410
6411 Not all fields are supported on all filesystem types. Here are the
6412 meanings of the fields:
6413
6414 0 dev device number of filesystem
6415 1 ino inode number
6416 2 mode file mode (type and permissions)
6417 3 nlink number of (hard) links to the file
6418 4 uid numeric user ID of file's owner
6419 5 gid numeric group ID of file's owner
6420 6 rdev the device identifier (special files only)
6421 7 size total size of file, in bytes
6422 8 atime last access time in seconds since the epoch
6423 9 mtime last modify time in seconds since the epoch
6424 10 ctime inode change time in seconds since the epoch (*)
6425 11 blksize preferred block size for file system I/O
6426 12 blocks actual number of blocks allocated
6427
6428 (The epoch was at 00:00 January 1, 1970 GMT.)
6429
6430 (*) Not all fields are supported on all filesystem types. Notably,
6431 the ctime field is non-portable. In particular, you cannot expect
6432 it to be a "creation time"; see "Files and Filesystems" in perlport
6433 for details.
6434
6435 If "stat" is passed the special filehandle consisting of an
6436 underline, no stat is done, but the current contents of the stat
6437 structure from the last "stat", "lstat", or filetest are returned.
6438 Example:
6439
6440 if (-x $file && (($d) = stat(_)) && $d < 0) {
6441 print "$file is executable NFS file\n";
6442 }
6443
6444 (This works on machines only for which the device number is
6445 negative under NFS.)
6446
6447 Because the mode contains both the file type and its permissions,
6448 you should mask off the file type portion and (s)printf using a
6449 "%o" if you want to see the real permissions.
6450
6451 $mode = (stat($filename))[2];
6452 printf "Permissions are %04o\n", $mode & 07777;
6453
6454 In scalar context, "stat" returns a boolean value indicating
6455 success or failure, and, if successful, sets the information
6456 associated with the special filehandle "_".
6457
6458 The File::stat module provides a convenient, by-name access
6459 mechanism:
6460
6461 use File::stat;
6462 $sb = stat($filename);
6463 printf "File is %s, size is %s, perm %04o, mtime %s\n",
6464 $filename, $sb->size, $sb->mode & 07777,
6465 scalar localtime $sb->mtime;
6466
6467 You can import symbolic mode constants ("S_IF*") and functions
6468 ("S_IS*") from the Fcntl module:
6469
6470 use Fcntl ':mode';
6471
6472 $mode = (stat($filename))[2];
6473
6474 $user_rwx = ($mode & S_IRWXU) >> 6;
6475 $group_read = ($mode & S_IRGRP) >> 3;
6476 $other_execute = $mode & S_IXOTH;
6477
6478 printf "Permissions are %04o\n", S_IMODE($mode), "\n";
6479
6480 $is_setuid = $mode & S_ISUID;
6481 $is_directory = S_ISDIR($mode);
6482
6483 You could write the last two using the "-u" and "-d" operators.
6484 Commonly available "S_IF*" constants are:
6485
6486 # Permissions: read, write, execute, for user, group, others.
6487
6488 S_IRWXU S_IRUSR S_IWUSR S_IXUSR
6489 S_IRWXG S_IRGRP S_IWGRP S_IXGRP
6490 S_IRWXO S_IROTH S_IWOTH S_IXOTH
6491
6492 # Setuid/Setgid/Stickiness/SaveText.
6493 # Note that the exact meaning of these is system-dependent.
6494
6495 S_ISUID S_ISGID S_ISVTX S_ISTXT
6496
6497 # File types. Not all are necessarily available on
6498 # your system.
6499
6500 S_IFREG S_IFDIR S_IFLNK S_IFBLK S_IFCHR
6501 S_IFIFO S_IFSOCK S_IFWHT S_ENFMT
6502
6503 # The following are compatibility aliases for S_IRUSR,
6504 # S_IWUSR, and S_IXUSR.
6505
6506 S_IREAD S_IWRITE S_IEXEC
6507
6508 and the "S_IF*" functions are
6509
6510 S_IMODE($mode) the part of $mode containing the permission
6511 bits and the setuid/setgid/sticky bits
6512
6513 S_IFMT($mode) the part of $mode containing the file type
6514 which can be bit-anded with (for example)
6515 S_IFREG or with the following functions
6516
6517 # The operators -f, -d, -l, -b, -c, -p, and -S.
6518
6519 S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)
6520 S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)
6521
6522 # No direct -X operator counterpart, but for the first one
6523 # the -g operator is often equivalent. The ENFMT stands for
6524 # record flocking enforcement, a platform-dependent feature.
6525
6526 S_ISENFMT($mode) S_ISWHT($mode)
6527
6528 See your native chmod(2) and stat(2) documentation for more details
6529 about the "S_*" constants. To get status info for a symbolic link
6530 instead of the target file behind the link, use the "lstat"
6531 function.
6532
6533 Portability issues: "stat" in perlport.
6534
6535 state EXPR
6536 state TYPE EXPR
6537 state EXPR : ATTRS
6538 state TYPE EXPR : ATTRS
6539 "state" declares a lexically scoped variable, just like "my".
6540 However, those variables will never be reinitialized, contrary to
6541 lexical variables that are reinitialized each time their enclosing
6542 block is entered. See "Persistent Private Variables" in perlsub
6543 for details.
6544
6545 "state" variables are enabled only when the "use feature "state""
6546 pragma is in effect, unless the keyword is written as
6547 "CORE::state". See also feature.
6548
6549 study SCALAR
6550 study
6551 Takes extra time to study SCALAR ($_ if unspecified) in
6552 anticipation of doing many pattern matches on the string before it
6553 is next modified. This may or may not save time, depending on the
6554 nature and number of patterns you are searching and the
6555 distribution of character frequencies in the string to be searched;
6556 you probably want to compare run times with and without it to see
6557 which is faster. Those loops that scan for many short constant
6558 strings (including the constant parts of more complex patterns)
6559 will benefit most. (The way "study" works is this: a linked list
6560 of every character in the string to be searched is made, so we
6561 know, for example, where all the 'k' characters are. From each
6562 search string, the rarest character is selected, based on some
6563 static frequency tables constructed from some C programs and
6564 English text. Only those places that contain this "rarest"
6565 character are examined.)
6566
6567 For example, here is a loop that inserts index producing entries
6568 before any line containing a certain pattern:
6569
6570 while (<>) {
6571 study;
6572 print ".IX foo\n" if /\bfoo\b/;
6573 print ".IX bar\n" if /\bbar\b/;
6574 print ".IX blurfl\n" if /\bblurfl\b/;
6575 # ...
6576 print;
6577 }
6578
6579 In searching for "/\bfoo\b/", only locations in $_ that contain "f"
6580 will be looked at, because "f" is rarer than "o". In general, this
6581 is a big win except in pathological cases. The only question is
6582 whether it saves you more time than it took to build the linked
6583 list in the first place.
6584
6585 Note that if you have to look for strings that you don't know till
6586 runtime, you can build an entire loop as a string and "eval" that
6587 to avoid recompiling all your patterns all the time. Together with
6588 undefining $/ to input entire files as one record, this can be
6589 quite fast, often faster than specialized programs like fgrep(1).
6590 The following scans a list of files (@files) for a list of words
6591 (@words), and prints out the names of those files that contain a
6592 match:
6593
6594 $search = 'while (<>) { study;';
6595 foreach $word (@words) {
6596 $search .= "++\$seen{\$ARGV} if /\\b$word\\b/;\n";
6597 }
6598 $search .= "}";
6599 @ARGV = @files;
6600 undef $/;
6601 eval $search; # this screams
6602 $/ = "\n"; # put back to normal input delimiter
6603 foreach $file (sort keys(%seen)) {
6604 print $file, "\n";
6605 }
6606
6607 sub NAME BLOCK
6608 sub NAME (PROTO) BLOCK
6609 sub NAME : ATTRS BLOCK
6610 sub NAME (PROTO) : ATTRS BLOCK
6611 This is subroutine definition, not a real function per se. Without
6612 a BLOCK it's just a forward declaration. Without a NAME, it's an
6613 anonymous function declaration, so does return a value: the CODE
6614 ref of the closure just created.
6615
6616 See perlsub and perlref for details about subroutines and
6617 references; see attributes and Attribute::Handlers for more
6618 information about attributes.
6619
6620 __SUB__
6621 A special token that returns the a reference to the current
6622 subroutine, or "undef" outside of a subroutine.
6623
6624 This token is only available under "use v5.16" or the "current_sub"
6625 feature. See feature.
6626
6627 substr EXPR,OFFSET,LENGTH,REPLACEMENT
6628 substr EXPR,OFFSET,LENGTH
6629 substr EXPR,OFFSET
6630 Extracts a substring out of EXPR and returns it. First character
6631 is at offset zero. If OFFSET is negative, starts that far back
6632 from the end of the string. If LENGTH is omitted, returns
6633 everything through the end of the string. If LENGTH is negative,
6634 leaves that many characters off the end of the string.
6635
6636 my $s = "The black cat climbed the green tree";
6637 my $color = substr $s, 4, 5; # black
6638 my $middle = substr $s, 4, -11; # black cat climbed the
6639 my $end = substr $s, 14; # climbed the green tree
6640 my $tail = substr $s, -4; # tree
6641 my $z = substr $s, -4, 2; # tr
6642
6643 You can use the substr() function as an lvalue, in which case EXPR
6644 must itself be an lvalue. If you assign something shorter than
6645 LENGTH, the string will shrink, and if you assign something longer
6646 than LENGTH, the string will grow to accommodate it. To keep the
6647 string the same length, you may need to pad or chop your value
6648 using "sprintf".
6649
6650 If OFFSET and LENGTH specify a substring that is partly outside the
6651 string, only the part within the string is returned. If the
6652 substring is beyond either end of the string, substr() returns the
6653 undefined value and produces a warning. When used as an lvalue,
6654 specifying a substring that is entirely outside the string raises
6655 an exception. Here's an example showing the behavior for boundary
6656 cases:
6657
6658 my $name = 'fred';
6659 substr($name, 4) = 'dy'; # $name is now 'freddy'
6660 my $null = substr $name, 6, 2; # returns "" (no warning)
6661 my $oops = substr $name, 7; # returns undef, with warning
6662 substr($name, 7) = 'gap'; # raises an exception
6663
6664 An alternative to using substr() as an lvalue is to specify the
6665 replacement string as the 4th argument. This allows you to replace
6666 parts of the EXPR and return what was there before in one
6667 operation, just as you can with splice().
6668
6669 my $s = "The black cat climbed the green tree";
6670 my $z = substr $s, 14, 7, "jumped from"; # climbed
6671 # $s is now "The black cat jumped from the green tree"
6672
6673 Note that the lvalue returned by the three-argument version of
6674 substr() acts as a 'magic bullet'; each time it is assigned to, it
6675 remembers which part of the original string is being modified; for
6676 example:
6677
6678 $x = '1234';
6679 for (substr($x,1,2)) {
6680 $_ = 'a'; print $x,"\n"; # prints 1a4
6681 $_ = 'xyz'; print $x,"\n"; # prints 1xyz4
6682 $x = '56789';
6683 $_ = 'pq'; print $x,"\n"; # prints 5pq9
6684 }
6685
6686 With negative offsets, it remembers its position from the end of
6687 the string when the target string is modified:
6688
6689 $x = '1234';
6690 for (substr($x, -3, 2)) {
6691 $_ = 'a'; print $x,"\n"; # prints 1a4, as above
6692 $x = 'abcdefg';
6693 print $_,"\n"; # prints f
6694 }
6695
6696 Prior to Perl version 5.10, the result of using an lvalue multiple
6697 times was unspecified. Prior to 5.16, the result with negative
6698 offsets was unspecified.
6699
6700 symlink OLDFILE,NEWFILE
6701 Creates a new filename symbolically linked to the old filename.
6702 Returns 1 for success, 0 otherwise. On systems that don't support
6703 symbolic links, raises an exception. To check for that, use eval:
6704
6705 $symlink_exists = eval { symlink("",""); 1 };
6706
6707 Portability issues: "symlink" in perlport.
6708
6709 syscall NUMBER, LIST
6710 Calls the system call specified as the first element of the list,
6711 passing the remaining elements as arguments to the system call. If
6712 unimplemented, raises an exception. The arguments are interpreted
6713 as follows: if a given argument is numeric, the argument is passed
6714 as an int. If not, the pointer to the string value is passed. You
6715 are responsible to make sure a string is pre-extended long enough
6716 to receive any result that might be written into a string. You
6717 can't use a string literal (or other read-only string) as an
6718 argument to "syscall" because Perl has to assume that any string
6719 pointer might be written through. If your integer arguments are
6720 not literals and have never been interpreted in a numeric context,
6721 you may need to add 0 to them to force them to look like numbers.
6722 This emulates the "syswrite" function (or vice versa):
6723
6724 require 'syscall.ph'; # may need to run h2ph
6725 $s = "hi there\n";
6726 syscall(&SYS_write, fileno(STDOUT), $s, length $s);
6727
6728 Note that Perl supports passing of up to only 14 arguments to your
6729 syscall, which in practice should (usually) suffice.
6730
6731 Syscall returns whatever value returned by the system call it
6732 calls. If the system call fails, "syscall" returns "-1" and sets
6733 $! (errno). Note that some system calls can legitimately return
6734 "-1". The proper way to handle such calls is to assign "$!=0"
6735 before the call, then check the value of $! if "syscall" returns
6736 "-1".
6737
6738 There's a problem with "syscall(&SYS_pipe)": it returns the file
6739 number of the read end of the pipe it creates, but there is no way
6740 to retrieve the file number of the other end. You can avoid this
6741 problem by using "pipe" instead.
6742
6743 Portability issues: "syscall" in perlport.
6744
6745 sysopen FILEHANDLE,FILENAME,MODE
6746 sysopen FILEHANDLE,FILENAME,MODE,PERMS
6747 Opens the file whose filename is given by FILENAME, and associates
6748 it with FILEHANDLE. If FILEHANDLE is an expression, its value is
6749 used as the real filehandle wanted; an undefined scalar will be
6750 suitably autovivified. This function calls the underlying
6751 operating system's open(2) function with the parameters FILENAME,
6752 MODE, and PERMS.
6753
6754 The possible values and flag bits of the MODE parameter are system-
6755 dependent; they are available via the standard module "Fcntl". See
6756 the documentation of your operating system's open(2) syscall to see
6757 which values and flag bits are available. You may combine several
6758 flags using the "|"-operator.
6759
6760 Some of the most common values are "O_RDONLY" for opening the file
6761 in read-only mode, "O_WRONLY" for opening the file in write-only
6762 mode, and "O_RDWR" for opening the file in read-write mode.
6763
6764 For historical reasons, some values work on almost every system
6765 supported by Perl: 0 means read-only, 1 means write-only, and 2
6766 means read/write. We know that these values do not work under
6767 OS/390 & VM/ESA Unix and on the Macintosh; you probably don't want
6768 to use them in new code.
6769
6770 If the file named by FILENAME does not exist and the "open" call
6771 creates it (typically because MODE includes the "O_CREAT" flag),
6772 then the value of PERMS specifies the permissions of the newly
6773 created file. If you omit the PERMS argument to "sysopen", Perl
6774 uses the octal value 0666. These permission values need to be in
6775 octal, and are modified by your process's current "umask".
6776
6777 In many systems the "O_EXCL" flag is available for opening files in
6778 exclusive mode. This is not locking: exclusiveness means here that
6779 if the file already exists, sysopen() fails. "O_EXCL" may not work
6780 on network filesystems, and has no effect unless the "O_CREAT" flag
6781 is set as well. Setting "O_CREAT|O_EXCL" prevents the file from
6782 being opened if it is a symbolic link. It does not protect against
6783 symbolic links in the file's path.
6784
6785 Sometimes you may want to truncate an already-existing file. This
6786 can be done using the "O_TRUNC" flag. The behavior of "O_TRUNC"
6787 with "O_RDONLY" is undefined.
6788
6789 You should seldom if ever use 0644 as argument to "sysopen",
6790 because that takes away the user's option to have a more permissive
6791 umask. Better to omit it. See the perlfunc(1) entry on "umask"
6792 for more on this.
6793
6794 Note that "sysopen" depends on the fdopen() C library function. On
6795 many Unix systems, fdopen() is known to fail when file descriptors
6796 exceed a certain value, typically 255. If you need more file
6797 descriptors than that, consider rebuilding Perl to use the "sfio"
6798 library, or perhaps using the POSIX::open() function.
6799
6800 See perlopentut for a kinder, gentler explanation of opening files.
6801
6802 Portability issues: "sysopen" in perlport.
6803
6804 sysread FILEHANDLE,SCALAR,LENGTH,OFFSET
6805 sysread FILEHANDLE,SCALAR,LENGTH
6806 Attempts to read LENGTH bytes of data into variable SCALAR from the
6807 specified FILEHANDLE, using the read(2). It bypasses buffered IO,
6808 so mixing this with other kinds of reads, "print", "write", "seek",
6809 "tell", or "eof" can cause confusion because the perlio or stdio
6810 layers usually buffers data. Returns the number of bytes actually
6811 read, 0 at end of file, or undef if there was an error (in the
6812 latter case $! is also set). SCALAR will be grown or shrunk so
6813 that the last byte actually read is the last byte of the scalar
6814 after the read.
6815
6816 An OFFSET may be specified to place the read data at some place in
6817 the string other than the beginning. A negative OFFSET specifies
6818 placement at that many characters counting backwards from the end
6819 of the string. A positive OFFSET greater than the length of SCALAR
6820 results in the string being padded to the required size with "\0"
6821 bytes before the result of the read is appended.
6822
6823 There is no syseof() function, which is ok, since eof() doesn't
6824 work well on device files (like ttys) anyway. Use sysread() and
6825 check for a return value for 0 to decide whether you're done.
6826
6827 Note that if the filehandle has been marked as ":utf8" Unicode
6828 characters are read instead of bytes (the LENGTH, OFFSET, and the
6829 return value of sysread() are in Unicode characters). The
6830 ":encoding(...)" layer implicitly introduces the ":utf8" layer.
6831 See "binmode", "open", and the "open" pragma, open.
6832
6833 sysseek FILEHANDLE,POSITION,WHENCE
6834 Sets FILEHANDLE's system position in bytes using lseek(2).
6835 FILEHANDLE may be an expression whose value gives the name of the
6836 filehandle. The values for WHENCE are 0 to set the new position to
6837 POSITION; 1 to set the it to the current position plus POSITION;
6838 and 2 to set it to EOF plus POSITION, typically negative.
6839
6840 Note the in bytes: even if the filehandle has been set to operate
6841 on characters (for example by using the ":encoding(utf8)" I/O
6842 layer), tell() will return byte offsets, not character offsets
6843 (because implementing that would render sysseek() unacceptably
6844 slow).
6845
6846 sysseek() bypasses normal buffered IO, so mixing it with reads
6847 other than "sysread" (for example "<>" or read()) "print", "write",
6848 "seek", "tell", or "eof" may cause confusion.
6849
6850 For WHENCE, you may also use the constants "SEEK_SET", "SEEK_CUR",
6851 and "SEEK_END" (start of the file, current position, end of the
6852 file) from the Fcntl module. Use of the constants is also more
6853 portable than relying on 0, 1, and 2. For example to define a
6854 "systell" function:
6855
6856 use Fcntl 'SEEK_CUR';
6857 sub systell { sysseek($_[0], 0, SEEK_CUR) }
6858
6859 Returns the new position, or the undefined value on failure. A
6860 position of zero is returned as the string "0 but true"; thus
6861 "sysseek" returns true on success and false on failure, yet you can
6862 still easily determine the new position.
6863
6864 system LIST
6865 system PROGRAM LIST
6866 Does exactly the same thing as "exec LIST", except that a fork is
6867 done first and the parent process waits for the child process to
6868 exit. Note that argument processing varies depending on the number
6869 of arguments. If there is more than one argument in LIST, or if
6870 LIST is an array with more than one value, starts the program given
6871 by the first element of the list with arguments given by the rest
6872 of the list. If there is only one scalar argument, the argument is
6873 checked for shell metacharacters, and if there are any, the entire
6874 argument is passed to the system's command shell for parsing (this
6875 is "/bin/sh -c" on Unix platforms, but varies on other platforms).
6876 If there are no shell metacharacters in the argument, it is split
6877 into words and passed directly to "execvp", which is more
6878 efficient.
6879
6880 Beginning with v5.6.0, Perl will attempt to flush all files opened
6881 for output before any operation that may do a fork, but this may
6882 not be supported on some platforms (see perlport). To be safe, you
6883 may need to set $| ($AUTOFLUSH in English) or call the
6884 "autoflush()" method of "IO::Handle" on any open handles.
6885
6886 The return value is the exit status of the program as returned by
6887 the "wait" call. To get the actual exit value, shift right by
6888 eight (see below). See also "exec". This is not what you want to
6889 use to capture the output from a command; for that you should use
6890 merely backticks or "qx//", as described in "`STRING`" in perlop.
6891 Return value of -1 indicates a failure to start the program or an
6892 error of the wait(2) system call (inspect $! for the reason).
6893
6894 If you'd like to make "system" (and many other bits of Perl) die on
6895 error, have a look at the autodie pragma.
6896
6897 Like "exec", "system" allows you to lie to a program about its name
6898 if you use the "system PROGRAM LIST" syntax. Again, see "exec".
6899
6900 Since "SIGINT" and "SIGQUIT" are ignored during the execution of
6901 "system", if you expect your program to terminate on receipt of
6902 these signals you will need to arrange to do so yourself based on
6903 the return value.
6904
6905 @args = ("command", "arg1", "arg2");
6906 system(@args) == 0
6907 or die "system @args failed: $?"
6908
6909 If you'd like to manually inspect "system"'s failure, you can check
6910 all possible failure modes by inspecting $? like this:
6911
6912 if ($? == -1) {
6913 print "failed to execute: $!\n";
6914 }
6915 elsif ($? & 127) {
6916 printf "child died with signal %d, %s coredump\n",
6917 ($? & 127), ($? & 128) ? 'with' : 'without';
6918 }
6919 else {
6920 printf "child exited with value %d\n", $? >> 8;
6921 }
6922
6923 Alternatively, you may inspect the value of
6924 "${^CHILD_ERROR_NATIVE}" with the "W*()" calls from the POSIX
6925 module.
6926
6927 When "system"'s arguments are executed indirectly by the shell,
6928 results and return codes are subject to its quirks. See "`STRING`"
6929 in perlop and "exec" for details.
6930
6931 Since "system" does a "fork" and "wait" it may affect a "SIGCHLD"
6932 handler. See perlipc for details.
6933
6934 Portability issues: "system" in perlport.
6935
6936 syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET
6937 syswrite FILEHANDLE,SCALAR,LENGTH
6938 syswrite FILEHANDLE,SCALAR
6939 Attempts to write LENGTH bytes of data from variable SCALAR to the
6940 specified FILEHANDLE, using write(2). If LENGTH is not specified,
6941 writes whole SCALAR. It bypasses buffered IO, so mixing this with
6942 reads (other than sysread()), "print", "write", "seek", "tell", or
6943 "eof" may cause confusion because the perlio and stdio layers
6944 usually buffer data. Returns the number of bytes actually written,
6945 or "undef" if there was an error (in this case the errno variable
6946 $! is also set). If the LENGTH is greater than the data available
6947 in the SCALAR after the OFFSET, only as much data as is available
6948 will be written.
6949
6950 An OFFSET may be specified to write the data from some part of the
6951 string other than the beginning. A negative OFFSET specifies
6952 writing that many characters counting backwards from the end of the
6953 string. If SCALAR is of length zero, you can only use an OFFSET of
6954 0.
6955
6956 WARNING: If the filehandle is marked ":utf8", Unicode characters
6957 encoded in UTF-8 are written instead of bytes, and the LENGTH,
6958 OFFSET, and return value of syswrite() are in (UTF8-encoded
6959 Unicode) characters. The ":encoding(...)" layer implicitly
6960 introduces the ":utf8" layer. Alternately, if the handle is not
6961 marked with an encoding but you attempt to write characters with
6962 code points over 255, raises an exception. See "binmode", "open",
6963 and the "open" pragma, open.
6964
6965 tell FILEHANDLE
6966 tell
6967 Returns the current position in bytes for FILEHANDLE, or -1 on
6968 error. FILEHANDLE may be an expression whose value gives the name
6969 of the actual filehandle. If FILEHANDLE is omitted, assumes the
6970 file last read.
6971
6972 Note the in bytes: even if the filehandle has been set to operate
6973 on characters (for example by using the ":encoding(utf8)" open
6974 layer), tell() will return byte offsets, not character offsets
6975 (because that would render seek() and tell() rather slow).
6976
6977 The return value of tell() for the standard streams like the STDIN
6978 depends on the operating system: it may return -1 or something
6979 else. tell() on pipes, fifos, and sockets usually returns -1.
6980
6981 There is no "systell" function. Use "sysseek(FH, 0, 1)" for that.
6982
6983 Do not use tell() (or other buffered I/O operations) on a
6984 filehandle that has been manipulated by sysread(), syswrite(), or
6985 sysseek(). Those functions ignore the buffering, while tell() does
6986 not.
6987
6988 telldir DIRHANDLE
6989 Returns the current position of the "readdir" routines on
6990 DIRHANDLE. Value may be given to "seekdir" to access a particular
6991 location in a directory. "telldir" has the same caveats about
6992 possible directory compaction as the corresponding system library
6993 routine.
6994
6995 tie VARIABLE,CLASSNAME,LIST
6996 This function binds a variable to a package class that will provide
6997 the implementation for the variable. VARIABLE is the name of the
6998 variable to be enchanted. CLASSNAME is the name of a class
6999 implementing objects of correct type. Any additional arguments are
7000 passed to the "new" method of the class (meaning "TIESCALAR",
7001 "TIEHANDLE", "TIEARRAY", or "TIEHASH"). Typically these are
7002 arguments such as might be passed to the "dbm_open()" function of
7003 C. The object returned by the "new" method is also returned by the
7004 "tie" function, which would be useful if you want to access other
7005 methods in CLASSNAME.
7006
7007 Note that functions such as "keys" and "values" may return huge
7008 lists when used on large objects, like DBM files. You may prefer
7009 to use the "each" function to iterate over such. Example:
7010
7011 # print out history file offsets
7012 use NDBM_File;
7013 tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
7014 while (($key,$val) = each %HIST) {
7015 print $key, ' = ', unpack('L',$val), "\n";
7016 }
7017 untie(%HIST);
7018
7019 A class implementing a hash should have the following methods:
7020
7021 TIEHASH classname, LIST
7022 FETCH this, key
7023 STORE this, key, value
7024 DELETE this, key
7025 CLEAR this
7026 EXISTS this, key
7027 FIRSTKEY this
7028 NEXTKEY this, lastkey
7029 SCALAR this
7030 DESTROY this
7031 UNTIE this
7032
7033 A class implementing an ordinary array should have the following
7034 methods:
7035
7036 TIEARRAY classname, LIST
7037 FETCH this, key
7038 STORE this, key, value
7039 FETCHSIZE this
7040 STORESIZE this, count
7041 CLEAR this
7042 PUSH this, LIST
7043 POP this
7044 SHIFT this
7045 UNSHIFT this, LIST
7046 SPLICE this, offset, length, LIST
7047 EXTEND this, count
7048 DESTROY this
7049 UNTIE this
7050
7051 A class implementing a filehandle should have the following
7052 methods:
7053
7054 TIEHANDLE classname, LIST
7055 READ this, scalar, length, offset
7056 READLINE this
7057 GETC this
7058 WRITE this, scalar, length, offset
7059 PRINT this, LIST
7060 PRINTF this, format, LIST
7061 BINMODE this
7062 EOF this
7063 FILENO this
7064 SEEK this, position, whence
7065 TELL this
7066 OPEN this, mode, LIST
7067 CLOSE this
7068 DESTROY this
7069 UNTIE this
7070
7071 A class implementing a scalar should have the following methods:
7072
7073 TIESCALAR classname, LIST
7074 FETCH this,
7075 STORE this, value
7076 DESTROY this
7077 UNTIE this
7078
7079 Not all methods indicated above need be implemented. See perltie,
7080 Tie::Hash, Tie::Array, Tie::Scalar, and Tie::Handle.
7081
7082 Unlike "dbmopen", the "tie" function will not "use" or "require" a
7083 module for you; you need to do that explicitly yourself. See
7084 DB_File or the Config module for interesting "tie" implementations.
7085
7086 For further details see perltie, "tied VARIABLE".
7087
7088 tied VARIABLE
7089 Returns a reference to the object underlying VARIABLE (the same
7090 value that was originally returned by the "tie" call that bound the
7091 variable to a package.) Returns the undefined value if VARIABLE
7092 isn't tied to a package.
7093
7094 time
7095 Returns the number of non-leap seconds since whatever time the
7096 system considers to be the epoch, suitable for feeding to "gmtime"
7097 and "localtime". On most systems the epoch is 00:00:00 UTC,
7098 January 1, 1970; a prominent exception being Mac OS Classic which
7099 uses 00:00:00, January 1, 1904 in the current local time zone for
7100 its epoch.
7101
7102 For measuring time in better granularity than one second, use the
7103 Time::HiRes module from Perl 5.8 onwards (or from CPAN before
7104 then), or, if you have gettimeofday(2), you may be able to use the
7105 "syscall" interface of Perl. See perlfaq8 for details.
7106
7107 For date and time processing look at the many related modules on
7108 CPAN. For a comprehensive date and time representation look at the
7109 DateTime module.
7110
7111 times
7112 Returns a four-element list giving the user and system times in
7113 seconds for this process and any exited children of this process.
7114
7115 ($user,$system,$cuser,$csystem) = times;
7116
7117 In scalar context, "times" returns $user.
7118
7119 Children's times are only included for terminated children.
7120
7121 Portability issues: "times" in perlport.
7122
7123 tr///
7124 The transliteration operator. Same as "y///". See "Quote and
7125 Quote-like Operators" in perlop.
7126
7127 truncate FILEHANDLE,LENGTH
7128 truncate EXPR,LENGTH
7129 Truncates the file opened on FILEHANDLE, or named by EXPR, to the
7130 specified length. Raises an exception if truncate isn't
7131 implemented on your system. Returns true if successful, "undef" on
7132 error.
7133
7134 The behavior is undefined if LENGTH is greater than the length of
7135 the file.
7136
7137 The position in the file of FILEHANDLE is left unchanged. You may
7138 want to call seek before writing to the file.
7139
7140 Portability issues: "truncate" in perlport.
7141
7142 uc EXPR
7143 uc Returns an uppercased version of EXPR. This is the internal
7144 function implementing the "\U" escape in double-quoted strings. It
7145 does not attempt to do titlecase mapping on initial letters. See
7146 "ucfirst" for that.
7147
7148 If EXPR is omitted, uses $_.
7149
7150 This function behaves the same way under various pragma, such as in
7151 a locale, as "lc" does.
7152
7153 ucfirst EXPR
7154 ucfirst
7155 Returns the value of EXPR with the first character in uppercase
7156 (titlecase in Unicode). This is the internal function implementing
7157 the "\u" escape in double-quoted strings.
7158
7159 If EXPR is omitted, uses $_.
7160
7161 This function behaves the same way under various pragma, such as in
7162 a locale, as "lc" does.
7163
7164 umask EXPR
7165 umask
7166 Sets the umask for the process to EXPR and returns the previous
7167 value. If EXPR is omitted, merely returns the current umask.
7168
7169 The Unix permission "rwxr-x---" is represented as three sets of
7170 three bits, or three octal digits: 0750 (the leading 0 indicates
7171 octal and isn't one of the digits). The "umask" value is such a
7172 number representing disabled permissions bits. The permission (or
7173 "mode") values you pass "mkdir" or "sysopen" are modified by your
7174 umask, so even if you tell "sysopen" to create a file with
7175 permissions 0777, if your umask is 0022, then the file will
7176 actually be created with permissions 0755. If your "umask" were
7177 0027 (group can't write; others can't read, write, or execute),
7178 then passing "sysopen" 0666 would create a file with mode 0640
7179 (because "0666 &~ 027" is 0640).
7180
7181 Here's some advice: supply a creation mode of 0666 for regular
7182 files (in "sysopen") and one of 0777 for directories (in "mkdir")
7183 and executable files. This gives users the freedom of choice: if
7184 they want protected files, they might choose process umasks of 022,
7185 027, or even the particularly antisocial mask of 077. Programs
7186 should rarely if ever make policy decisions better left to the
7187 user. The exception to this is when writing files that should be
7188 kept private: mail files, web browser cookies, .rhosts files, and
7189 so on.
7190
7191 If umask(2) is not implemented on your system and you are trying to
7192 restrict access for yourself (i.e., "(EXPR & 0700) > 0"), raises an
7193 exception. If umask(2) is not implemented and you are not trying
7194 to restrict access for yourself, returns "undef".
7195
7196 Remember that a umask is a number, usually given in octal; it is
7197 not a string of octal digits. See also "oct", if all you have is a
7198 string.
7199
7200 Portability issues: "umask" in perlport.
7201
7202 undef EXPR
7203 undef
7204 Undefines the value of EXPR, which must be an lvalue. Use only on
7205 a scalar value, an array (using "@"), a hash (using "%"), a
7206 subroutine (using "&"), or a typeglob (using "*"). Saying "undef
7207 $hash{$key}" will probably not do what you expect on most
7208 predefined variables or DBM list values, so don't do that; see
7209 "delete". Always returns the undefined value. You can omit the
7210 EXPR, in which case nothing is undefined, but you still get an
7211 undefined value that you could, for instance, return from a
7212 subroutine, assign to a variable, or pass as a parameter.
7213 Examples:
7214
7215 undef $foo;
7216 undef $bar{'blurfl'}; # Compare to: delete $bar{'blurfl'};
7217 undef @ary;
7218 undef %hash;
7219 undef &mysub;
7220 undef *xyz; # destroys $xyz, @xyz, %xyz, &xyz, etc.
7221 return (wantarray ? (undef, $errmsg) : undef) if $they_blew_it;
7222 select undef, undef, undef, 0.25;
7223 ($a, $b, undef, $c) = &foo; # Ignore third value returned
7224
7225 Note that this is a unary operator, not a list operator.
7226
7227 unlink LIST
7228 unlink
7229 Deletes a list of files. On success, it returns the number of
7230 files it successfully deleted. On failure, it returns false and
7231 sets $! (errno):
7232
7233 my $unlinked = unlink 'a', 'b', 'c';
7234 unlink @goners;
7235 unlink glob "*.bak";
7236
7237 On error, "unlink" will not tell you which files it could not
7238 remove. If you want to know which files you could not remove, try
7239 them one at a time:
7240
7241 foreach my $file ( @goners ) {
7242 unlink $file or warn "Could not unlink $file: $!";
7243 }
7244
7245 Note: "unlink" will not attempt to delete directories unless you
7246 are superuser and the -U flag is supplied to Perl. Even if these
7247 conditions are met, be warned that unlinking a directory can
7248 inflict damage on your filesystem. Finally, using "unlink" on
7249 directories is not supported on many operating systems. Use
7250 "rmdir" instead.
7251
7252 If LIST is omitted, "unlink" uses $_.
7253
7254 unpack TEMPLATE,EXPR
7255 unpack TEMPLATE
7256 "unpack" does the reverse of "pack": it takes a string and expands
7257 it out into a list of values. (In scalar context, it returns
7258 merely the first value produced.)
7259
7260 If EXPR is omitted, unpacks the $_ string. See perlpacktut for an
7261 introduction to this function.
7262
7263 The string is broken into chunks described by the TEMPLATE. Each
7264 chunk is converted separately to a value. Typically, either the
7265 string is a result of "pack", or the characters of the string
7266 represent a C structure of some kind.
7267
7268 The TEMPLATE has the same format as in the "pack" function. Here's
7269 a subroutine that does substring:
7270
7271 sub substr {
7272 my($what,$where,$howmuch) = @_;
7273 unpack("x$where a$howmuch", $what);
7274 }
7275
7276 and then there's
7277
7278 sub ordinal { unpack("W",$_[0]); } # same as ord()
7279
7280 In addition to fields allowed in pack(), you may prefix a field
7281 with a %<number> to indicate that you want a <number>-bit checksum
7282 of the items instead of the items themselves. Default is a 16-bit
7283 checksum. Checksum is calculated by summing numeric values of
7284 expanded values (for string fields the sum of "ord($char)" is
7285 taken; for bit fields the sum of zeroes and ones).
7286
7287 For example, the following computes the same number as the System V
7288 sum program:
7289
7290 $checksum = do {
7291 local $/; # slurp!
7292 unpack("%32W*",<>) % 65535;
7293 };
7294
7295 The following efficiently counts the number of set bits in a bit
7296 vector:
7297
7298 $setbits = unpack("%32b*", $selectmask);
7299
7300 The "p" and "P" formats should be used with care. Since Perl has
7301 no way of checking whether the value passed to "unpack()"
7302 corresponds to a valid memory location, passing a pointer value
7303 that's not known to be valid is likely to have disastrous
7304 consequences.
7305
7306 If there are more pack codes or if the repeat count of a field or a
7307 group is larger than what the remainder of the input string allows,
7308 the result is not well defined: the repeat count may be decreased,
7309 or "unpack()" may produce empty strings or zeros, or it may raise
7310 an exception. If the input string is longer than one described by
7311 the TEMPLATE, the remainder of that input string is ignored.
7312
7313 See "pack" for more examples and notes.
7314
7315 unshift ARRAY,LIST
7316 unshift EXPR,LIST
7317 Does the opposite of a "shift". Or the opposite of a "push",
7318 depending on how you look at it. Prepends list to the front of the
7319 array and returns the new number of elements in the array.
7320
7321 unshift(@ARGV, '-e') unless $ARGV[0] =~ /^-/;
7322
7323 Note the LIST is prepended whole, not one element at a time, so the
7324 prepended elements stay in the same order. Use "reverse" to do the
7325 reverse.
7326
7327 Starting with Perl 5.14, "unshift" can take a scalar EXPR, which
7328 must hold a reference to an unblessed array. The argument will be
7329 dereferenced automatically. This aspect of "unshift" is considered
7330 highly experimental. The exact behaviour may change in a future
7331 version of Perl.
7332
7333 To avoid confusing would-be users of your code who are running
7334 earlier versions of Perl with mysterious syntax errors, put this
7335 sort of thing at the top of your file to signal that your code will
7336 work only on Perls of a recent vintage:
7337
7338 use 5.014; # so push/pop/etc work on scalars (experimental)
7339
7340 untie VARIABLE
7341 Breaks the binding between a variable and a package. (See tie.)
7342 Has no effect if the variable is not tied.
7343
7344 use Module VERSION LIST
7345 use Module VERSION
7346 use Module LIST
7347 use Module
7348 use VERSION
7349 Imports some semantics into the current package from the named
7350 module, generally by aliasing certain subroutine or variable names
7351 into your package. It is exactly equivalent to
7352
7353 BEGIN { require Module; Module->import( LIST ); }
7354
7355 except that Module must be a bareword. The importation can be made
7356 conditional; see if.
7357
7358 In the peculiar "use VERSION" form, VERSION may be either a
7359 positive decimal fraction such as 5.006, which will be compared to
7360 $], or a v-string of the form v5.6.1, which will be compared to $^V
7361 (aka $PERL_VERSION). An exception is raised if VERSION is greater
7362 than the version of the current Perl interpreter; Perl will not
7363 attempt to parse the rest of the file. Compare with "require",
7364 which can do a similar check at run time. Symmetrically, "no
7365 VERSION" allows you to specify that you want a version of Perl
7366 older than the specified one.
7367
7368 Specifying VERSION as a literal of the form v5.6.1 should generally
7369 be avoided, because it leads to misleading error messages under
7370 earlier versions of Perl (that is, prior to 5.6.0) that do not
7371 support this syntax. The equivalent numeric version should be used
7372 instead.
7373
7374 use v5.6.1; # compile time version check
7375 use 5.6.1; # ditto
7376 use 5.006_001; # ditto; preferred for backwards compatibility
7377
7378 This is often useful if you need to check the current Perl version
7379 before "use"ing library modules that won't work with older versions
7380 of Perl. (We try not to do this more than we have to.)
7381
7382 "use VERSION" also enables all features available in the requested
7383 version as defined by the "feature" pragma, disabling any features
7384 not in the requested version's feature bundle. See feature.
7385 Similarly, if the specified Perl version is greater than or equal
7386 to 5.11.0, strictures are enabled lexically as with "use strict".
7387 Any explicit use of "use strict" or "no strict" overrides "use
7388 VERSION", even if it comes before it. In both cases, the
7389 feature.pm and strict.pm files are not actually loaded.
7390
7391 The "BEGIN" forces the "require" and "import" to happen at compile
7392 time. The "require" makes sure the module is loaded into memory if
7393 it hasn't been yet. The "import" is not a builtin; it's just an
7394 ordinary static method call into the "Module" package to tell the
7395 module to import the list of features back into the current
7396 package. The module can implement its "import" method any way it
7397 likes, though most modules just choose to derive their "import"
7398 method via inheritance from the "Exporter" class that is defined in
7399 the "Exporter" module. See Exporter. If no "import" method can be
7400 found then the call is skipped, even if there is an AUTOLOAD
7401 method.
7402
7403 If you do not want to call the package's "import" method (for
7404 instance, to stop your namespace from being altered), explicitly
7405 supply the empty list:
7406
7407 use Module ();
7408
7409 That is exactly equivalent to
7410
7411 BEGIN { require Module }
7412
7413 If the VERSION argument is present between Module and LIST, then
7414 the "use" will call the VERSION method in class Module with the
7415 given version as an argument. The default VERSION method,
7416 inherited from the UNIVERSAL class, croaks if the given version is
7417 larger than the value of the variable $Module::VERSION.
7418
7419 Again, there is a distinction between omitting LIST ("import"
7420 called with no arguments) and an explicit empty LIST "()" ("import"
7421 not called). Note that there is no comma after VERSION!
7422
7423 Because this is a wide-open interface, pragmas (compiler
7424 directives) are also implemented this way. Currently implemented
7425 pragmas are:
7426
7427 use constant;
7428 use diagnostics;
7429 use integer;
7430 use sigtrap qw(SEGV BUS);
7431 use strict qw(subs vars refs);
7432 use subs qw(afunc blurfl);
7433 use warnings qw(all);
7434 use sort qw(stable _quicksort _mergesort);
7435
7436 Some of these pseudo-modules import semantics into the current
7437 block scope (like "strict" or "integer", unlike ordinary modules,
7438 which import symbols into the current package (which are effective
7439 through the end of the file).
7440
7441 Because "use" takes effect at compile time, it doesn't respect the
7442 ordinary flow control of the code being compiled. In particular,
7443 putting a "use" inside the false branch of a conditional doesn't
7444 prevent it from being processed. If a module or pragma only needs
7445 to be loaded conditionally, this can be done using the if pragma:
7446
7447 use if $] < 5.008, "utf8";
7448 use if WANT_WARNINGS, warnings => qw(all);
7449
7450 There's a corresponding "no" declaration that unimports meanings
7451 imported by "use", i.e., it calls "unimport Module LIST" instead of
7452 "import". It behaves just as "import" does with VERSION, an
7453 omitted or empty LIST, or no unimport method being found.
7454
7455 no integer;
7456 no strict 'refs';
7457 no warnings;
7458
7459 Care should be taken when using the "no VERSION" form of "no". It
7460 is only meant to be used to assert that the running Perl is of a
7461 earlier version than its argument and not to undo the feature-
7462 enabling side effects of "use VERSION".
7463
7464 See perlmodlib for a list of standard modules and pragmas. See
7465 perlrun for the "-M" and "-m" command-line options to Perl that
7466 give "use" functionality from the command-line.
7467
7468 utime LIST
7469 Changes the access and modification times on each file of a list of
7470 files. The first two elements of the list must be the NUMERIC
7471 access and modification times, in that order. Returns the number
7472 of files successfully changed. The inode change time of each file
7473 is set to the current time. For example, this code has the same
7474 effect as the Unix touch(1) command when the files already exist
7475 and belong to the user running the program:
7476
7477 #!/usr/bin/perl
7478 $atime = $mtime = time;
7479 utime $atime, $mtime, @ARGV;
7480
7481 Since Perl 5.7.2, if the first two elements of the list are
7482 "undef", the utime(2) syscall from your C library is called with a
7483 null second argument. On most systems, this will set the file's
7484 access and modification times to the current time (i.e., equivalent
7485 to the example above) and will work even on files you don't own
7486 provided you have write permission:
7487
7488 for $file (@ARGV) {
7489 utime(undef, undef, $file)
7490 || warn "couldn't touch $file: $!";
7491 }
7492
7493 Under NFS this will use the time of the NFS server, not the time of
7494 the local machine. If there is a time synchronization problem, the
7495 NFS server and local machine will have different times. The Unix
7496 touch(1) command will in fact normally use this form instead of the
7497 one shown in the first example.
7498
7499 Passing only one of the first two elements as "undef" is equivalent
7500 to passing a 0 and will not have the effect described when both are
7501 "undef". This also triggers an uninitialized warning.
7502
7503 On systems that support futimes(2), you may pass filehandles among
7504 the files. On systems that don't support futimes(2), passing
7505 filehandles raises an exception. Filehandles must be passed as
7506 globs or glob references to be recognized; barewords are considered
7507 filenames.
7508
7509 Portability issues: "utime" in perlport.
7510
7511 values HASH
7512 values ARRAY
7513 values EXPR
7514 In list context, returns a list consisting of all the values of the
7515 named hash. In Perl 5.12 or later only, will also return a list of
7516 the values of an array; prior to that release, attempting to use an
7517 array argument will produce a syntax error. In scalar context,
7518 returns the number of values.
7519
7520 When called on a hash, the values are returned in an apparently
7521 random order. The actual random order is subject to change in
7522 future versions of Perl, but it is guaranteed to be the same order
7523 as either the "keys" or "each" function would produce on the same
7524 (unmodified) hash. Since Perl 5.8.1 the ordering is different even
7525 between different runs of Perl for security reasons (see
7526 "Algorithmic Complexity Attacks" in perlsec).
7527
7528 As a side effect, calling values() resets the HASH or ARRAY's
7529 internal iterator, see "each". (In particular, calling values() in
7530 void context resets the iterator with no other overhead. Apart
7531 from resetting the iterator, "values @array" in list context is the
7532 same as plain @array. (We recommend that you use void context
7533 "keys @array" for this, but reasoned that taking "values @array"
7534 out would require more documentation than leaving it in.)
7535
7536 Note that the values are not copied, which means modifying them
7537 will modify the contents of the hash:
7538
7539 for (values %hash) { s/foo/bar/g } # modifies %hash values
7540 for (@hash{keys %hash}) { s/foo/bar/g } # same
7541
7542 Starting with Perl 5.14, "values" can take a scalar EXPR, which
7543 must hold a reference to an unblessed hash or array. The argument
7544 will be dereferenced automatically. This aspect of "values" is
7545 considered highly experimental. The exact behaviour may change in
7546 a future version of Perl.
7547
7548 for (values $hashref) { ... }
7549 for (values $obj->get_arrayref) { ... }
7550
7551 To avoid confusing would-be users of your code who are running
7552 earlier versions of Perl with mysterious syntax errors, put this
7553 sort of thing at the top of your file to signal that your code will
7554 work only on Perls of a recent vintage:
7555
7556 use 5.012; # so keys/values/each work on arrays
7557 use 5.014; # so keys/values/each work on scalars (experimental)
7558
7559 See also "keys", "each", and "sort".
7560
7561 vec EXPR,OFFSET,BITS
7562 Treats the string in EXPR as a bit vector made up of elements of
7563 width BITS and returns the value of the element specified by OFFSET
7564 as an unsigned integer. BITS therefore specifies the number of
7565 bits that are reserved for each element in the bit vector. This
7566 must be a power of two from 1 to 32 (or 64, if your platform
7567 supports that).
7568
7569 If BITS is 8, "elements" coincide with bytes of the input string.
7570
7571 If BITS is 16 or more, bytes of the input string are grouped into
7572 chunks of size BITS/8, and each group is converted to a number as
7573 with pack()/unpack() with big-endian formats "n"/"N" (and
7574 analogously for BITS==64). See "pack" for details.
7575
7576 If bits is 4 or less, the string is broken into bytes, then the
7577 bits of each byte are broken into 8/BITS groups. Bits of a byte
7578 are numbered in a little-endian-ish way, as in 0x01, 0x02, 0x04,
7579 0x08, 0x10, 0x20, 0x40, 0x80. For example, breaking the single
7580 input byte "chr(0x36)" into two groups gives a list "(0x6, 0x3)";
7581 breaking it into 4 groups gives "(0x2, 0x1, 0x3, 0x0)".
7582
7583 "vec" may also be assigned to, in which case parentheses are needed
7584 to give the expression the correct precedence as in
7585
7586 vec($image, $max_x * $x + $y, 8) = 3;
7587
7588 If the selected element is outside the string, the value 0 is
7589 returned. If an element off the end of the string is written to,
7590 Perl will first extend the string with sufficiently many zero
7591 bytes. It is an error to try to write off the beginning of the
7592 string (i.e., negative OFFSET).
7593
7594 If the string happens to be encoded as UTF-8 internally (and thus
7595 has the UTF8 flag set), this is ignored by "vec", and it operates
7596 on the internal byte string, not the conceptual character string,
7597 even if you only have characters with values less than 256.
7598
7599 Strings created with "vec" can also be manipulated with the logical
7600 operators "|", "&", "^", and "~". These operators will assume a
7601 bit vector operation is desired when both operands are strings.
7602 See "Bitwise String Operators" in perlop.
7603
7604 The following code will build up an ASCII string saying
7605 'PerlPerlPerl'. The comments show the string after each step.
7606 Note that this code works in the same way on big-endian or little-
7607 endian machines.
7608
7609 my $foo = '';
7610 vec($foo, 0, 32) = 0x5065726C; # 'Perl'
7611
7612 # $foo eq "Perl" eq "\x50\x65\x72\x6C", 32 bits
7613 print vec($foo, 0, 8); # prints 80 == 0x50 == ord('P')
7614
7615 vec($foo, 2, 16) = 0x5065; # 'PerlPe'
7616 vec($foo, 3, 16) = 0x726C; # 'PerlPerl'
7617 vec($foo, 8, 8) = 0x50; # 'PerlPerlP'
7618 vec($foo, 9, 8) = 0x65; # 'PerlPerlPe'
7619 vec($foo, 20, 4) = 2; # 'PerlPerlPe' . "\x02"
7620 vec($foo, 21, 4) = 7; # 'PerlPerlPer'
7621 # 'r' is "\x72"
7622 vec($foo, 45, 2) = 3; # 'PerlPerlPer' . "\x0c"
7623 vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c"
7624 vec($foo, 94, 1) = 1; # 'PerlPerlPerl'
7625 # 'l' is "\x6c"
7626
7627 To transform a bit vector into a string or list of 0's and 1's, use
7628 these:
7629
7630 $bits = unpack("b*", $vector);
7631 @bits = split(//, unpack("b*", $vector));
7632
7633 If you know the exact length in bits, it can be used in place of
7634 the "*".
7635
7636 Here is an example to illustrate how the bits actually fall in
7637 place:
7638
7639 #!/usr/bin/perl -wl
7640
7641 print <<'EOT';
7642 0 1 2 3
7643 unpack("V",$_) 01234567890123456789012345678901
7644 ------------------------------------------------------------------
7645 EOT
7646
7647 for $w (0..3) {
7648 $width = 2**$w;
7649 for ($shift=0; $shift < $width; ++$shift) {
7650 for ($off=0; $off < 32/$width; ++$off) {
7651 $str = pack("B*", "0"x32);
7652 $bits = (1<<$shift);
7653 vec($str, $off, $width) = $bits;
7654 $res = unpack("b*",$str);
7655 $val = unpack("V", $str);
7656 write;
7657 }
7658 }
7659 }
7660
7661 format STDOUT =
7662 vec($_,@#,@#) = @<< == @######### @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
7663 $off, $width, $bits, $val, $res
7664 .
7665 __END__
7666
7667 Regardless of the machine architecture on which it runs, the
7668 example above should print the following table:
7669
7670 0 1 2 3
7671 unpack("V",$_) 01234567890123456789012345678901
7672 ------------------------------------------------------------------
7673 vec($_, 0, 1) = 1 == 1 10000000000000000000000000000000
7674 vec($_, 1, 1) = 1 == 2 01000000000000000000000000000000
7675 vec($_, 2, 1) = 1 == 4 00100000000000000000000000000000
7676 vec($_, 3, 1) = 1 == 8 00010000000000000000000000000000
7677 vec($_, 4, 1) = 1 == 16 00001000000000000000000000000000
7678 vec($_, 5, 1) = 1 == 32 00000100000000000000000000000000
7679 vec($_, 6, 1) = 1 == 64 00000010000000000000000000000000
7680 vec($_, 7, 1) = 1 == 128 00000001000000000000000000000000
7681 vec($_, 8, 1) = 1 == 256 00000000100000000000000000000000
7682 vec($_, 9, 1) = 1 == 512 00000000010000000000000000000000
7683 vec($_,10, 1) = 1 == 1024 00000000001000000000000000000000
7684 vec($_,11, 1) = 1 == 2048 00000000000100000000000000000000
7685 vec($_,12, 1) = 1 == 4096 00000000000010000000000000000000
7686 vec($_,13, 1) = 1 == 8192 00000000000001000000000000000000
7687 vec($_,14, 1) = 1 == 16384 00000000000000100000000000000000
7688 vec($_,15, 1) = 1 == 32768 00000000000000010000000000000000
7689 vec($_,16, 1) = 1 == 65536 00000000000000001000000000000000
7690 vec($_,17, 1) = 1 == 131072 00000000000000000100000000000000
7691 vec($_,18, 1) = 1 == 262144 00000000000000000010000000000000
7692 vec($_,19, 1) = 1 == 524288 00000000000000000001000000000000
7693 vec($_,20, 1) = 1 == 1048576 00000000000000000000100000000000
7694 vec($_,21, 1) = 1 == 2097152 00000000000000000000010000000000
7695 vec($_,22, 1) = 1 == 4194304 00000000000000000000001000000000
7696 vec($_,23, 1) = 1 == 8388608 00000000000000000000000100000000
7697 vec($_,24, 1) = 1 == 16777216 00000000000000000000000010000000
7698 vec($_,25, 1) = 1 == 33554432 00000000000000000000000001000000
7699 vec($_,26, 1) = 1 == 67108864 00000000000000000000000000100000
7700 vec($_,27, 1) = 1 == 134217728 00000000000000000000000000010000
7701 vec($_,28, 1) = 1 == 268435456 00000000000000000000000000001000
7702 vec($_,29, 1) = 1 == 536870912 00000000000000000000000000000100
7703 vec($_,30, 1) = 1 == 1073741824 00000000000000000000000000000010
7704 vec($_,31, 1) = 1 == 2147483648 00000000000000000000000000000001
7705 vec($_, 0, 2) = 1 == 1 10000000000000000000000000000000
7706 vec($_, 1, 2) = 1 == 4 00100000000000000000000000000000
7707 vec($_, 2, 2) = 1 == 16 00001000000000000000000000000000
7708 vec($_, 3, 2) = 1 == 64 00000010000000000000000000000000
7709 vec($_, 4, 2) = 1 == 256 00000000100000000000000000000000
7710 vec($_, 5, 2) = 1 == 1024 00000000001000000000000000000000
7711 vec($_, 6, 2) = 1 == 4096 00000000000010000000000000000000
7712 vec($_, 7, 2) = 1 == 16384 00000000000000100000000000000000
7713 vec($_, 8, 2) = 1 == 65536 00000000000000001000000000000000
7714 vec($_, 9, 2) = 1 == 262144 00000000000000000010000000000000
7715 vec($_,10, 2) = 1 == 1048576 00000000000000000000100000000000
7716 vec($_,11, 2) = 1 == 4194304 00000000000000000000001000000000
7717 vec($_,12, 2) = 1 == 16777216 00000000000000000000000010000000
7718 vec($_,13, 2) = 1 == 67108864 00000000000000000000000000100000
7719 vec($_,14, 2) = 1 == 268435456 00000000000000000000000000001000
7720 vec($_,15, 2) = 1 == 1073741824 00000000000000000000000000000010
7721 vec($_, 0, 2) = 2 == 2 01000000000000000000000000000000
7722 vec($_, 1, 2) = 2 == 8 00010000000000000000000000000000
7723 vec($_, 2, 2) = 2 == 32 00000100000000000000000000000000
7724 vec($_, 3, 2) = 2 == 128 00000001000000000000000000000000
7725 vec($_, 4, 2) = 2 == 512 00000000010000000000000000000000
7726 vec($_, 5, 2) = 2 == 2048 00000000000100000000000000000000
7727 vec($_, 6, 2) = 2 == 8192 00000000000001000000000000000000
7728 vec($_, 7, 2) = 2 == 32768 00000000000000010000000000000000
7729 vec($_, 8, 2) = 2 == 131072 00000000000000000100000000000000
7730 vec($_, 9, 2) = 2 == 524288 00000000000000000001000000000000
7731 vec($_,10, 2) = 2 == 2097152 00000000000000000000010000000000
7732 vec($_,11, 2) = 2 == 8388608 00000000000000000000000100000000
7733 vec($_,12, 2) = 2 == 33554432 00000000000000000000000001000000
7734 vec($_,13, 2) = 2 == 134217728 00000000000000000000000000010000
7735 vec($_,14, 2) = 2 == 536870912 00000000000000000000000000000100
7736 vec($_,15, 2) = 2 == 2147483648 00000000000000000000000000000001
7737 vec($_, 0, 4) = 1 == 1 10000000000000000000000000000000
7738 vec($_, 1, 4) = 1 == 16 00001000000000000000000000000000
7739 vec($_, 2, 4) = 1 == 256 00000000100000000000000000000000
7740 vec($_, 3, 4) = 1 == 4096 00000000000010000000000000000000
7741 vec($_, 4, 4) = 1 == 65536 00000000000000001000000000000000
7742 vec($_, 5, 4) = 1 == 1048576 00000000000000000000100000000000
7743 vec($_, 6, 4) = 1 == 16777216 00000000000000000000000010000000
7744 vec($_, 7, 4) = 1 == 268435456 00000000000000000000000000001000
7745 vec($_, 0, 4) = 2 == 2 01000000000000000000000000000000
7746 vec($_, 1, 4) = 2 == 32 00000100000000000000000000000000
7747 vec($_, 2, 4) = 2 == 512 00000000010000000000000000000000
7748 vec($_, 3, 4) = 2 == 8192 00000000000001000000000000000000
7749 vec($_, 4, 4) = 2 == 131072 00000000000000000100000000000000
7750 vec($_, 5, 4) = 2 == 2097152 00000000000000000000010000000000
7751 vec($_, 6, 4) = 2 == 33554432 00000000000000000000000001000000
7752 vec($_, 7, 4) = 2 == 536870912 00000000000000000000000000000100
7753 vec($_, 0, 4) = 4 == 4 00100000000000000000000000000000
7754 vec($_, 1, 4) = 4 == 64 00000010000000000000000000000000
7755 vec($_, 2, 4) = 4 == 1024 00000000001000000000000000000000
7756 vec($_, 3, 4) = 4 == 16384 00000000000000100000000000000000
7757 vec($_, 4, 4) = 4 == 262144 00000000000000000010000000000000
7758 vec($_, 5, 4) = 4 == 4194304 00000000000000000000001000000000
7759 vec($_, 6, 4) = 4 == 67108864 00000000000000000000000000100000
7760 vec($_, 7, 4) = 4 == 1073741824 00000000000000000000000000000010
7761 vec($_, 0, 4) = 8 == 8 00010000000000000000000000000000
7762 vec($_, 1, 4) = 8 == 128 00000001000000000000000000000000
7763 vec($_, 2, 4) = 8 == 2048 00000000000100000000000000000000
7764 vec($_, 3, 4) = 8 == 32768 00000000000000010000000000000000
7765 vec($_, 4, 4) = 8 == 524288 00000000000000000001000000000000
7766 vec($_, 5, 4) = 8 == 8388608 00000000000000000000000100000000
7767 vec($_, 6, 4) = 8 == 134217728 00000000000000000000000000010000
7768 vec($_, 7, 4) = 8 == 2147483648 00000000000000000000000000000001
7769 vec($_, 0, 8) = 1 == 1 10000000000000000000000000000000
7770 vec($_, 1, 8) = 1 == 256 00000000100000000000000000000000
7771 vec($_, 2, 8) = 1 == 65536 00000000000000001000000000000000
7772 vec($_, 3, 8) = 1 == 16777216 00000000000000000000000010000000
7773 vec($_, 0, 8) = 2 == 2 01000000000000000000000000000000
7774 vec($_, 1, 8) = 2 == 512 00000000010000000000000000000000
7775 vec($_, 2, 8) = 2 == 131072 00000000000000000100000000000000
7776 vec($_, 3, 8) = 2 == 33554432 00000000000000000000000001000000
7777 vec($_, 0, 8) = 4 == 4 00100000000000000000000000000000
7778 vec($_, 1, 8) = 4 == 1024 00000000001000000000000000000000
7779 vec($_, 2, 8) = 4 == 262144 00000000000000000010000000000000
7780 vec($_, 3, 8) = 4 == 67108864 00000000000000000000000000100000
7781 vec($_, 0, 8) = 8 == 8 00010000000000000000000000000000
7782 vec($_, 1, 8) = 8 == 2048 00000000000100000000000000000000
7783 vec($_, 2, 8) = 8 == 524288 00000000000000000001000000000000
7784 vec($_, 3, 8) = 8 == 134217728 00000000000000000000000000010000
7785 vec($_, 0, 8) = 16 == 16 00001000000000000000000000000000
7786 vec($_, 1, 8) = 16 == 4096 00000000000010000000000000000000
7787 vec($_, 2, 8) = 16 == 1048576 00000000000000000000100000000000
7788 vec($_, 3, 8) = 16 == 268435456 00000000000000000000000000001000
7789 vec($_, 0, 8) = 32 == 32 00000100000000000000000000000000
7790 vec($_, 1, 8) = 32 == 8192 00000000000001000000000000000000
7791 vec($_, 2, 8) = 32 == 2097152 00000000000000000000010000000000
7792 vec($_, 3, 8) = 32 == 536870912 00000000000000000000000000000100
7793 vec($_, 0, 8) = 64 == 64 00000010000000000000000000000000
7794 vec($_, 1, 8) = 64 == 16384 00000000000000100000000000000000
7795 vec($_, 2, 8) = 64 == 4194304 00000000000000000000001000000000
7796 vec($_, 3, 8) = 64 == 1073741824 00000000000000000000000000000010
7797 vec($_, 0, 8) = 128 == 128 00000001000000000000000000000000
7798 vec($_, 1, 8) = 128 == 32768 00000000000000010000000000000000
7799 vec($_, 2, 8) = 128 == 8388608 00000000000000000000000100000000
7800 vec($_, 3, 8) = 128 == 2147483648 00000000000000000000000000000001
7801
7802 wait
7803 Behaves like wait(2) on your system: it waits for a child process
7804 to terminate and returns the pid of the deceased process, or "-1"
7805 if there are no child processes. The status is returned in $? and
7806 "${^CHILD_ERROR_NATIVE}". Note that a return value of "-1" could
7807 mean that child processes are being automatically reaped, as
7808 described in perlipc.
7809
7810 If you use wait in your handler for $SIG{CHLD} it may accidentally
7811 for the child created by qx() or system(). See perlipc for
7812 details.
7813
7814 Portability issues: "wait" in perlport.
7815
7816 waitpid PID,FLAGS
7817 Waits for a particular child process to terminate and returns the
7818 pid of the deceased process, or "-1" if there is no such child
7819 process. On some systems, a value of 0 indicates that there are
7820 processes still running. The status is returned in $? and
7821 "${^CHILD_ERROR_NATIVE}". If you say
7822
7823 use POSIX ":sys_wait_h";
7824 #...
7825 do {
7826 $kid = waitpid(-1, WNOHANG);
7827 } while $kid > 0;
7828
7829 then you can do a non-blocking wait for all pending zombie
7830 processes. Non-blocking wait is available on machines supporting
7831 either the waitpid(2) or wait4(2) syscalls. However, waiting for a
7832 particular pid with FLAGS of 0 is implemented everywhere. (Perl
7833 emulates the system call by remembering the status values of
7834 processes that have exited but have not been harvested by the Perl
7835 script yet.)
7836
7837 Note that on some systems, a return value of "-1" could mean that
7838 child processes are being automatically reaped. See perlipc for
7839 details, and for other examples.
7840
7841 Portability issues: "waitpid" in perlport.
7842
7843 wantarray
7844 Returns true if the context of the currently executing subroutine
7845 or "eval" is looking for a list value. Returns false if the
7846 context is looking for a scalar. Returns the undefined value if
7847 the context is looking for no value (void context).
7848
7849 return unless defined wantarray; # don't bother doing more
7850 my @a = complex_calculation();
7851 return wantarray ? @a : "@a";
7852
7853 "wantarray()"'s result is unspecified in the top level of a file,
7854 in a "BEGIN", "UNITCHECK", "CHECK", "INIT" or "END" block, or in a
7855 "DESTROY" method.
7856
7857 This function should have been named wantlist() instead.
7858
7859 warn LIST
7860 Prints the value of LIST to STDERR. If the last element of LIST
7861 does not end in a newline, it appends the same file/line number
7862 text as "die" does.
7863
7864 If the output is empty and $@ already contains a value (typically
7865 from a previous eval) that value is used after appending
7866 "\t...caught" to $@. This is useful for staying almost, but not
7867 entirely similar to "die".
7868
7869 If $@ is empty then the string "Warning: Something's wrong" is
7870 used.
7871
7872 No message is printed if there is a $SIG{__WARN__} handler
7873 installed. It is the handler's responsibility to deal with the
7874 message as it sees fit (like, for instance, converting it into a
7875 "die"). Most handlers must therefore arrange to actually display
7876 the warnings that they are not prepared to deal with, by calling
7877 "warn" again in the handler. Note that this is quite safe and will
7878 not produce an endless loop, since "__WARN__" hooks are not called
7879 from inside one.
7880
7881 You will find this behavior is slightly different from that of
7882 $SIG{__DIE__} handlers (which don't suppress the error text, but
7883 can instead call "die" again to change it).
7884
7885 Using a "__WARN__" handler provides a powerful way to silence all
7886 warnings (even the so-called mandatory ones). An example:
7887
7888 # wipe out *all* compile-time warnings
7889 BEGIN { $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN } }
7890 my $foo = 10;
7891 my $foo = 20; # no warning about duplicate my $foo,
7892 # but hey, you asked for it!
7893 # no compile-time or run-time warnings before here
7894 $DOWARN = 1;
7895
7896 # run-time warnings enabled after here
7897 warn "\$foo is alive and $foo!"; # does show up
7898
7899 See perlvar for details on setting %SIG entries and for more
7900 examples. See the Carp module for other kinds of warnings using
7901 its carp() and cluck() functions.
7902
7903 write FILEHANDLE
7904 write EXPR
7905 write
7906 Writes a formatted record (possibly multi-line) to the specified
7907 FILEHANDLE, using the format associated with that file. By default
7908 the format for a file is the one having the same name as the
7909 filehandle, but the format for the current output channel (see the
7910 "select" function) may be set explicitly by assigning the name of
7911 the format to the $~ variable.
7912
7913 Top of form processing is handled automatically: if there is
7914 insufficient room on the current page for the formatted record, the
7915 page is advanced by writing a form feed, a special top-of-page
7916 format is used to format the new page header before the record is
7917 written. By default, the top-of-page format is the name of the
7918 filehandle with "_TOP" appended. This would be a problem with
7919 autovivified filehandles, but it may be dynamically set to the
7920 format of your choice by assigning the name to the $^ variable
7921 while that filehandle is selected. The number of lines remaining
7922 on the current page is in variable "$-", which can be set to 0 to
7923 force a new page.
7924
7925 If FILEHANDLE is unspecified, output goes to the current default
7926 output channel, which starts out as STDOUT but may be changed by
7927 the "select" operator. If the FILEHANDLE is an EXPR, then the
7928 expression is evaluated and the resulting string is used to look up
7929 the name of the FILEHANDLE at run time. For more on formats, see
7930 perlform.
7931
7932 Note that write is not the opposite of "read". Unfortunately.
7933
7934 y///
7935 The transliteration operator. Same as "tr///". See "Quote and
7936 Quote-like Operators" in perlop.
7937
7938 Non-function Keywords by Cross-reference
7939 perldata
7940
7941 __DATA__
7942 __END__
7943 These keywords are documented in "Special Literals" in perldata.
7944
7945 perlmod
7946
7947 BEGIN
7948 CHECK
7949 END
7950 INIT
7951 UNITCHECK
7952 These compile phase keywords are documented in "BEGIN, UNITCHECK,
7953 CHECK, INIT and END" in perlmod.
7954
7955 perlobj
7956
7957 DESTROY
7958 This method keyword is documented in "Destructors" in perlobj.
7959
7960 perlop
7961
7962 and
7963 cmp
7964 eq
7965 ge
7966 gt
7967 if
7968 le
7969 lt
7970 ne
7971 not
7972 or
7973 x
7974 xor These operators are documented in perlop.
7975
7976 perlsub
7977
7978 AUTOLOAD
7979 This keyword is documented in "Autoloading" in perlsub.
7980
7981 perlsyn
7982
7983 else
7984 elseif
7985 elsif
7986 for
7987 foreach
7988 unless
7989 until
7990 while
7991 These flow-control keywords are documented in "Compound Statements"
7992 in perlsyn.
7993
7994 default
7995 given
7996 when
7997 These flow-control keywords related to the experimental switch
7998 feature are documented in "Switch Statements" in perlsyn .
7999
8000
8001
8002perl v5.16.3 2013-03-04 PERLFUNC(1)