This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 3.0 patch #40 patch #38, continued
[perl5.git] / perl.man.3
CommitLineData
a687059c 1''' Beginning of part 3
34de22dd 2''' $Header: perl_man.3,v 3.0.1.11 90/11/10 01:48:21 lwall Locked $
a687059c
LW
3'''
4''' $Log: perl.man.3,v $
34de22dd
LW
5''' Revision 3.0.1.11 90/11/10 01:48:21 lwall
6''' patch38: random cleanup
7''' patch38: documented tr///cds
8'''
e5d73d77
LW
9''' Revision 3.0.1.10 90/10/20 02:15:17 lwall
10''' patch37: patch37: fixed various typos in man page
11'''
76854fea
LW
12''' Revision 3.0.1.9 90/10/16 10:02:43 lwall
13''' patch29: you can now read into the middle string
14''' patch29: index and substr now have optional 3rd args
15''' patch29: added scalar reverse
16''' patch29: added scalar
17''' patch29: added SysV IPC
18''' patch29: added waitpid
19''' patch29: added sysread and syswrite
20'''
33b78306
LW
21''' Revision 3.0.1.8 90/08/09 04:39:04 lwall
22''' patch19: added require operator
23''' patch19: added truncate operator
24''' patch19: unpack can do checksumming
25'''
26''' Revision 3.0.1.7 90/08/03 11:15:42 lwall
27''' patch19: Intermediate diffs for Randal
28'''
0f85fab0
LW
29''' Revision 3.0.1.6 90/03/27 16:17:56 lwall
30''' patch16: MSDOS support
31'''
79a0689e
LW
32''' Revision 3.0.1.5 90/03/12 16:52:21 lwall
33''' patch13: documented that print $filehandle &foo is ambiguous
34''' patch13: added splice operator: @oldelems = splice(@array,$offset,$len,LIST)
35'''
ac58e20f
LW
36''' Revision 3.0.1.4 90/02/28 18:00:09 lwall
37''' patch9: added pipe function
38''' patch9: documented how to handle arbitrary weird characters in filenames
39''' patch9: documented the unflushed buffers problem on piped opens
40''' patch9: documented how to force top of page
41'''
663a0e37
LW
42''' Revision 3.0.1.3 89/12/21 20:10:12 lwall
43''' patch7: documented that s`pat`repl` does command substitution on replacement
44''' patch7: documented that $timeleft from select() is likely not implemented
45'''
ffed7fef
LW
46''' Revision 3.0.1.2 89/11/17 15:31:05 lwall
47''' patch5: fixed some manual typos and indent problems
48''' patch5: added warning about print making an array context
49'''
ae986130
LW
50''' Revision 3.0.1.1 89/11/11 04:45:06 lwall
51''' patch2: made some line breaks depend on troff vs. nroff
52'''
a687059c
LW
53''' Revision 3.0 89/10/18 15:21:46 lwall
54''' 3.0 baseline
55'''
56.Ip "next LABEL" 8 8
57.Ip "next" 8
58The
59.I next
60command is like the
61.I continue
62statement in C; it starts the next iteration of the loop:
63.nf
64
65.ne 4
66 line: while (<STDIN>) {
67 next line if /\|^#/; # discard comments
68 .\|.\|.
69 }
70
71.fi
72Note that if there were a
73.I continue
74block on the above, it would get executed even on discarded lines.
75If the LABEL is omitted, the command refers to the innermost enclosing loop.
76.Ip "oct(EXPR)" 8 4
77.Ip "oct EXPR" 8
78Returns the decimal value of EXPR interpreted as an octal string.
79(If EXPR happens to start off with 0x, interprets it as a hex string instead.)
80The following will handle decimal, octal and hex in the standard notation:
81.nf
82
83 $val = oct($val) if $val =~ /^0/;
84
85.fi
86If EXPR is omitted, uses $_.
87.Ip "open(FILEHANDLE,EXPR)" 8 8
88.Ip "open(FILEHANDLE)" 8
89.Ip "open FILEHANDLE" 8
90Opens the file whose filename is given by EXPR, and associates it with
91FILEHANDLE.
92If FILEHANDLE is an expression, its value is used as the name of the
93real filehandle wanted.
94If EXPR is omitted, the scalar variable of the same name as the FILEHANDLE
95contains the filename.
96If the filename begins with \*(L"<\*(R" or nothing, the file is opened for
97input.
98If the filename begins with \*(L">\*(R", the file is opened for output.
99If the filename begins with \*(L">>\*(R", the file is opened for appending.
100(You can put a \'+\' in front of the \'>\' or \'<\' to indicate that you
101want both read and write access to the file.)
102If the filename begins with \*(L"|\*(R", the filename is interpreted
103as a command to which output is to be piped, and if the filename ends
104with a \*(L"|\*(R", the filename is interpreted as command which pipes
105input to us.
106(You may not have a command that pipes both in and out.)
107Opening \'\-\' opens
108.I STDIN
109and opening \'>\-\' opens
110.IR STDOUT .
111Open returns non-zero upon success, the undefined value otherwise.
112If the open involved a pipe, the return value happens to be the pid
113of the subprocess.
114Examples:
115.nf
116
117.ne 3
118 $article = 100;
119 open article || die "Can't find article $article: $!\en";
120 while (<article>) {\|.\|.\|.
121
ae986130 122.ie t \{\
a687059c 123 open(LOG, \'>>/usr/spool/news/twitlog\'\|); # (log is reserved)
ae986130
LW
124'br\}
125.el \{\
126 open(LOG, \'>>/usr/spool/news/twitlog\'\|);
127 # (log is reserved)
128'br\}
a687059c 129
ae986130 130.ie t \{\
a687059c 131 open(article, "caesar <$article |"\|); # decrypt article
ae986130
LW
132'br\}
133.el \{\
134 open(article, "caesar <$article |"\|);
135 # decrypt article
136'br\}
a687059c 137
ae986130 138.ie t \{\
a687059c 139 open(extract, "|sort >/tmp/Tmp$$"\|); # $$ is our process#
ae986130
LW
140'br\}
141.el \{\
142 open(extract, "|sort >/tmp/Tmp$$"\|);
143 # $$ is our process#
144'br\}
a687059c
LW
145
146.ne 7
147 # process argument list of files along with any includes
148
149 foreach $file (@ARGV) {
150 do process($file, \'fh00\'); # no pun intended
151 }
152
153 sub process {
154 local($filename, $input) = @_;
155 $input++; # this is a string increment
156 unless (open($input, $filename)) {
157 print STDERR "Can't open $filename: $!\en";
158 return;
159 }
ae986130 160.ie t \{\
a687059c 161 while (<$input>) { # note the use of indirection
ae986130
LW
162'br\}
163.el \{\
164 while (<$input>) { # note use of indirection
165'br\}
a687059c
LW
166 if (/^#include "(.*)"/) {
167 do process($1, $input);
168 next;
169 }
170 .\|.\|. # whatever
171 }
172 }
173
174.fi
175You may also, in the Bourne shell tradition, specify an EXPR beginning
176with \*(L">&\*(R", in which case the rest of the string
177is interpreted as the name of a filehandle
178(or file descriptor, if numeric) which is to be duped and opened.
ae986130
LW
179You may use & after >, >>, <, +>, +>> and +<.
180The mode you specify should match the mode of the original filehandle.
a687059c
LW
181Here is a script that saves, redirects, and restores
182.I STDOUT
183and
ae986130 184.IR STDERR :
a687059c
LW
185.nf
186
187.ne 21
188 #!/usr/bin/perl
189 open(SAVEOUT, ">&STDOUT");
190 open(SAVEERR, ">&STDERR");
191
192 open(STDOUT, ">foo.out") || die "Can't redirect stdout";
193 open(STDERR, ">&STDOUT") || die "Can't dup stdout";
194
195 select(STDERR); $| = 1; # make unbuffered
196 select(STDOUT); $| = 1; # make unbuffered
197
198 print STDOUT "stdout 1\en"; # this works for
199 print STDERR "stderr 1\en"; # subprocesses too
200
201 close(STDOUT);
202 close(STDERR);
203
204 open(STDOUT, ">&SAVEOUT");
205 open(STDERR, ">&SAVEERR");
206
207 print STDOUT "stdout 2\en";
208 print STDERR "stderr 2\en";
209
210.fi
211If you open a pipe on the command \*(L"\-\*(R", i.e. either \*(L"|\-\*(R" or \*(L"\-|\*(R",
212then there is an implicit fork done, and the return value of open
213is the pid of the child within the parent process, and 0 within the child
214process.
215(Use defined($pid) to determine if the open was successful.)
216The filehandle behaves normally for the parent, but i/o to that
217filehandle is piped from/to the
218.IR STDOUT / STDIN
219of the child process.
220In the child process the filehandle isn't opened\*(--i/o happens from/to
221the new
222.I STDOUT
223or
224.IR STDIN .
225Typically this is used like the normal piped open when you want to exercise
226more control over just how the pipe command gets executed, such as when
227you are running setuid, and don't want to have to scan shell commands
228for metacharacters.
33b78306 229The following pairs are more or less equivalent:
a687059c
LW
230.nf
231
232.ne 5
233 open(FOO, "|tr \'[a\-z]\' \'[A\-Z]\'");
234 open(FOO, "|\-") || exec \'tr\', \'[a\-z]\', \'[A\-Z]\';
235
33b78306 236 open(FOO, "cat \-n '$file'|");
a687059c
LW
237 open(FOO, "\-|") || exec \'cat\', \'\-n\', $file;
238
239.fi
240Explicitly closing any piped filehandle causes the parent process to wait for the
241child to finish, and returns the status value in $?.
ac58e20f
LW
242Note: on any operation which may do a fork,
243unflushed buffers remain unflushed in both
244processes, which means you may need to set $| to
245avoid duplicate output.
246.Sp
247The filename that is passed to open will have leading and trailing
248whitespace deleted.
249In order to open a file with arbitrary weird characters in it, it's necessary
250to protect any leading and trailing whitespace thusly:
251.nf
252
253.ne 2
254 $file =~ s#^(\es)#./$1#;
255 open(FOO, "< $file\e0");
256
257.fi
a687059c
LW
258.Ip "opendir(DIRHANDLE,EXPR)" 8 3
259Opens a directory named EXPR for processing by readdir(), telldir(), seekdir(),
260rewinddir() and closedir().
261Returns true if successful.
262DIRHANDLEs have their own namespace separate from FILEHANDLEs.
263.Ip "ord(EXPR)" 8 4
264.Ip "ord EXPR" 8
0f85fab0 265Returns the numeric ascii value of the first character of EXPR.
a687059c 266If EXPR is omitted, uses $_.
33b78306 267''' Comments on f & d by gnb@melba.bby.oz.au 22/11/89
a687059c
LW
268.Ip "pack(TEMPLATE,LIST)" 8 4
269Takes an array or list of values and packs it into a binary structure,
270returning the string containing the structure.
271The TEMPLATE is a sequence of characters that give the order and type
272of values, as follows:
273.nf
274
275 A An ascii string, will be space padded.
276 a An ascii string, will be null padded.
33b78306 277 c A signed char value.
a687059c
LW
278 C An unsigned char value.
279 s A signed short value.
280 S An unsigned short value.
281 i A signed integer value.
282 I An unsigned integer value.
283 l A signed long value.
284 L An unsigned long value.
285 n A short in \*(L"network\*(R" order.
286 N A long in \*(L"network\*(R" order.
33b78306
LW
287 f A single-precision float in the native format.
288 d A double-precision float in the native format.
a687059c
LW
289 p A pointer to a string.
290 x A null byte.
33b78306
LW
291 X Back up a byte.
292 @ Null fill to absolute position.
293 u A uuencoded string.
a687059c
LW
294
295.fi
296Each letter may optionally be followed by a number which gives a repeat
297count.
298With all types except "a" and "A" the pack function will gobble up that many values
299from the LIST.
33b78306
LW
300A * for the repeat count means to use however many items are left.
301The "a" and "A" types gobble just one value, but pack it as a string of length
302count,
a687059c
LW
303padding with nulls or spaces as necessary.
304(When unpacking, "A" strips trailing spaces and nulls, but "a" does not.)
34de22dd 305Real numbers (floats and doubles) are in the native machine format
33b78306
LW
306only; due to the multiplicity of floating formats around, and the lack
307of a standard \*(L"network\*(R" representation, no facility for
308interchange has been made.
309This means that packed floating point data
310written on one machine may not be readable on another - even if both
311use IEEE floating point arithmetic (as the endian-ness of the memory
312representation is not part of the IEEE spec).
313Note that perl uses
314doubles internally for all numeric calculation, and converting from
34de22dd 315double -> float -> double will lose precision (i.e. unpack("f",
33b78306
LW
316pack("f", $foo)) will not in general equal $foo).
317.br
a687059c
LW
318Examples:
319.nf
320
321 $foo = pack("cccc",65,66,67,68);
322 # foo eq "ABCD"
323 $foo = pack("c4",65,66,67,68);
324 # same thing
325
326 $foo = pack("ccxxcc",65,66,67,68);
327 # foo eq "AB\e0\e0CD"
328
329 $foo = pack("s2",1,2);
330 # "\e1\e0\e2\e0" on little-endian
331 # "\e0\e1\e0\e2" on big-endian
332
333 $foo = pack("a4","abcd","x","y","z");
334 # "abcd"
335
336 $foo = pack("aaaa","abcd","x","y","z");
337 # "axyz"
338
339 $foo = pack("a14","abcdefg");
340 # "abcdefg\e0\e0\e0\e0\e0\e0\e0"
341
ae986130 342 $foo = pack("i9pl", gmtime);
a687059c
LW
343 # a real struct tm (on my system anyway)
344
345.fi
346The same template may generally also be used in the unpack function.
ac58e20f
LW
347.Ip "pipe(READHANDLE,WRITEHANDLE)" 8 3
348Opens a pair of connected pipes like the corresponding system call.
349Note that if you set up a loop of piped processes, deadlock can occur
350unless you are very careful.
351In addition, note that perl's pipes use stdio buffering, so you may need
352to set $| to flush your WRITEHANDLE after each command, depending on
353the application.
354[Requires version 3.0 patchlevel 9.]
a687059c
LW
355.Ip "pop(ARRAY)" 8
356.Ip "pop ARRAY" 8 6
357Pops and returns the last value of the array, shortening the array by 1.
358Has the same effect as
359.nf
360
361 $tmp = $ARRAY[$#ARRAY\-\|\-];
362
363.fi
364If there are no elements in the array, returns the undefined value.
365.Ip "print(FILEHANDLE LIST)" 8 10
366.Ip "print(LIST)" 8
367.Ip "print FILEHANDLE LIST" 8
368.Ip "print LIST" 8
369.Ip "print" 8
370Prints a string or a comma-separated list of strings.
371Returns non-zero if successful.
372FILEHANDLE may be a scalar variable name, in which case the variable contains
373the name of the filehandle, thus introducing one level of indirection.
79a0689e
LW
374(NOTE: If FILEHANDLE is a variable and the next token is a term, it may be
375misinterpreted as an operator unless you interpose a + or put parens around
376the arguments.)
a687059c
LW
377If FILEHANDLE is omitted, prints by default to standard output (or to the
378last selected output channel\*(--see select()).
379If LIST is also omitted, prints $_ to
380.IR STDOUT .
381To set the default output channel to something other than
382.I STDOUT
383use the select operation.
ffed7fef
LW
384Note that, because print takes a LIST, anything in the LIST is evaluated
385in an array context, and any subroutine that you call will have one or more
386of its expressions evaluated in an array context.
79a0689e
LW
387Also be careful not to follow the print keyword with a left parenthesis
388unless you want the corresponding right parenthesis to terminate the
34de22dd 389arguments to the print\*(--interpose a + or put parens around all the arguments.
a687059c
LW
390.Ip "printf(FILEHANDLE LIST)" 8 10
391.Ip "printf(LIST)" 8
392.Ip "printf FILEHANDLE LIST" 8
393.Ip "printf LIST" 8
394Equivalent to a \*(L"print FILEHANDLE sprintf(LIST)\*(R".
395.Ip "push(ARRAY,LIST)" 8 7
396Treats ARRAY (@ is optional) as a stack, and pushes the values of LIST
397onto the end of ARRAY.
398The length of ARRAY increases by the length of LIST.
399Has the same effect as
400.nf
401
402 for $value (LIST) {
403 $ARRAY[++$#ARRAY] = $value;
404 }
405
406.fi
407but is more efficient.
408.Ip "q/STRING/" 8 5
409.Ip "qq/STRING/" 8
410These are not really functions, but simply syntactic sugar to let you
411avoid putting too many backslashes into quoted strings.
412The q operator is a generalized single quote, and the qq operator a
413generalized double quote.
33b78306 414Any non-alphanumeric delimiter can be used in place of /, including newline.
a687059c
LW
415If the delimiter is an opening bracket or parenthesis, the final delimiter
416will be the corresponding closing bracket or parenthesis.
417(Embedded occurrences of the closing bracket need to be backslashed as usual.)
418Examples:
419.nf
420
421.ne 5
422 $foo = q!I said, "You said, \'She said it.\'"!;
423 $bar = q(\'This is it.\');
424 $_ .= qq
425*** The previous line contains the naughty word "$&".\en
426 if /(ibm|apple|awk)/; # :-)
427
428.fi
429.Ip "rand(EXPR)" 8 8
430.Ip "rand EXPR" 8
431.Ip "rand" 8
432Returns a random fractional number between 0 and the value of EXPR.
433(EXPR should be positive.)
434If EXPR is omitted, returns a value between 0 and 1.
435See also srand().
76854fea 436.Ip "read(FILEHANDLE,SCALAR,LENGTH,OFFSET)" 8 5
a687059c
LW
437.Ip "read(FILEHANDLE,SCALAR,LENGTH)" 8 5
438Attempts to read LENGTH bytes of data into variable SCALAR from the specified
439FILEHANDLE.
76854fea 440Returns the number of bytes actually read, or undef if there was an error.
a687059c 441SCALAR will be grown or shrunk to the length actually read.
76854fea
LW
442An OFFSET may be specified to place the read data at some other place
443than the beginning of the string.
444This call is actually implemented in terms of stdio's fread call. To get
445a true read system call, see sysread.
a687059c 446.Ip "readdir(DIRHANDLE)" 8 3
ae986130 447.Ip "readdir DIRHANDLE" 8
a687059c
LW
448Returns the next directory entry for a directory opened by opendir().
449If used in an array context, returns all the rest of the entries in the
450directory.
451If there are no more entries, returns an undefined value in a scalar context
452or a null list in an array context.
453.Ip "readlink(EXPR)" 8 6
454.Ip "readlink EXPR" 8
455Returns the value of a symbolic link, if symbolic links are implemented.
456If not, gives a fatal error.
457If there is some system error, returns the undefined value and sets $! (errno).
458If EXPR is omitted, uses $_.
459.Ip "recv(SOCKET,SCALAR,LEN,FLAGS)" 8 4
460Receives a message on a socket.
461Attempts to receive LENGTH bytes of data into variable SCALAR from the specified
462SOCKET filehandle.
463Returns the address of the sender, or the undefined value if there's an error.
464SCALAR will be grown or shrunk to the length actually read.
465Takes the same flags as the system call of the same name.
466.Ip "redo LABEL" 8 8
467.Ip "redo" 8
468The
469.I redo
470command restarts the loop block without evaluating the conditional again.
471The
472.I continue
473block, if any, is not executed.
474If the LABEL is omitted, the command refers to the innermost enclosing loop.
475This command is normally used by programs that want to lie to themselves
476about what was just input:
477.nf
478
479.ne 16
480 # a simpleminded Pascal comment stripper
481 # (warning: assumes no { or } in strings)
482 line: while (<STDIN>) {
483 while (s|\|({.*}.*\|){.*}|$1 \||) {}
484 s|{.*}| \||;
485 if (s|{.*| \||) {
486 $front = $_;
487 while (<STDIN>) {
488 if (\|/\|}/\|) { # end of comment?
489 s|^|$front{|;
490 redo line;
491 }
492 }
493 }
494 print;
495 }
496
497.fi
498.Ip "rename(OLDNAME,NEWNAME)" 8 2
499Changes the name of a file.
500Returns 1 for success, 0 otherwise.
501Will not work across filesystem boundaries.
33b78306
LW
502.Ip "require(EXPR)" 8 6
503.Ip "require EXPR" 8
504.Ip "require" 8
505Includes the library file specified by EXPR, or by $_ if EXPR is not supplied.
506Has semantics similar to the following subroutine:
507.nf
508
509 sub require {
510 local($filename) = @_;
511 return 1 if $INC{$filename};
512 local($realfilename,$result);
513 ITER: {
514 foreach $prefix (@INC) {
515 $realfilename = "$prefix/$filename";
516 if (-f $realfilename) {
517 $result = do $realfilename;
518 last ITER;
519 }
520 }
521 die "Can't find $filename in \e@INC";
522 }
523 die $@ if $@;
524 die "$filename did not return true value" unless $result;
525 $INC{$filename} = $realfilename;
526 $result;
527 }
528
529.fi
530Note that the file will not be included twice under the same specified name.
a687059c
LW
531.Ip "reset(EXPR)" 8 6
532.Ip "reset EXPR" 8
533.Ip "reset" 8
534Generally used in a
535.I continue
536block at the end of a loop to clear variables and reset ?? searches
537so that they work again.
538The expression is interpreted as a list of single characters (hyphens allowed
539for ranges).
540All variables and arrays beginning with one of those letters are reset to
541their pristine state.
542If the expression is omitted, one-match searches (?pattern?) are reset to
543match again.
544Only resets variables or searches in the current package.
545Always returns 1.
546Examples:
547.nf
548
549.ne 3
550 reset \'X\'; \h'|2i'# reset all X variables
551 reset \'a\-z\';\h'|2i'# reset lower case variables
552 reset; \h'|2i'# just reset ?? searches
553
554.fi
555Note: resetting \*(L"A\-Z\*(R" is not recommended since you'll wipe out your ARGV and ENV
556arrays.
557.Sp
558The use of reset on dbm associative arrays does not change the dbm file.
559(It does, however, flush any entries cached by perl, which may be useful if
560you are sharing the dbm file.
561Then again, maybe not.)
562.Ip "return LIST" 8 3
563Returns from a subroutine with the value specified.
564(Note that a subroutine can automatically return
565the value of the last expression evaluated.
566That's the preferred method\*(--use of an explicit
567.I return
568is a bit slower.)
569.Ip "reverse(LIST)" 8 4
570.Ip "reverse LIST" 8
76854fea
LW
571In an array context, returns an array value consisting of the elements
572of LIST in the opposite order.
573In a scalar context, returns a string value consisting of the bytes of
574the first element of LIST in the opposite order.
a687059c
LW
575.Ip "rewinddir(DIRHANDLE)" 8 5
576.Ip "rewinddir DIRHANDLE" 8
577Sets the current position to the beginning of the directory for the readdir() routine on DIRHANDLE.
76854fea 578.Ip "rindex(STR,SUBSTR,POSITION)" 8 6
a687059c
LW
579.Ip "rindex(STR,SUBSTR)" 8 4
580Works just like index except that it
581returns the position of the LAST occurrence of SUBSTR in STR.
76854fea
LW
582If POSITION is specified, returns the last occurrence at or before that
583position.
a687059c
LW
584.Ip "rmdir(FILENAME)" 8 4
585.Ip "rmdir FILENAME" 8
586Deletes the directory specified by FILENAME if it is empty.
587If it succeeds it returns 1, otherwise it returns 0 and sets $! (errno).
588If FILENAME is omitted, uses $_.
589.Ip "s/PATTERN/REPLACEMENT/gieo" 8 3
590Searches a string for a pattern, and if found, replaces that pattern with the
591replacement text and returns the number of substitutions made.
592Otherwise it returns false (0).
593The \*(L"g\*(R" is optional, and if present, indicates that all occurrences
594of the pattern are to be replaced.
595The \*(L"i\*(R" is also optional, and if present, indicates that matching
596is to be done in a case-insensitive manner.
597The \*(L"e\*(R" is likewise optional, and if present, indicates that
598the replacement string is to be evaluated as an expression rather than just
599as a double-quoted string.
33b78306
LW
600Any non-alphanumeric delimiter may replace the slashes;
601if single quotes are used, no
a687059c 602interpretation is done on the replacement string (the e modifier overrides
663a0e37
LW
603this, however); if backquotes are used, the replacement string is a command
604to execute whose output will be used as the actual replacement text.
a687059c
LW
605If no string is specified via the =~ or !~ operator,
606the $_ string is searched and modified.
607(The string specified with =~ must be a scalar variable, an array element,
608or an assignment to one of those, i.e. an lvalue.)
609If the pattern contains a $ that looks like a variable rather than an
610end-of-string test, the variable will be interpolated into the pattern at
611run-time.
612If you only want the pattern compiled once the first time the variable is
613interpolated, add an \*(L"o\*(R" at the end.
614See also the section on regular expressions.
615Examples:
616.nf
617
618 s/\|\e\|bgreen\e\|b/mauve/g; # don't change wintergreen
619
620 $path \|=~ \|s|\|/usr/bin|\|/usr/local/bin|;
621
622 s/Login: $foo/Login: $bar/; # run-time pattern
623
624 ($foo = $bar) =~ s/bar/foo/;
625
626 $_ = \'abc123xyz\';
627 s/\ed+/$&*2/e; # yields \*(L'abc246xyz\*(R'
628 s/\ed+/sprintf("%5d",$&)/e; # yields \*(L'abc 246xyz\*(R'
629 s/\ew/$& x 2/eg; # yields \*(L'aabbcc 224466xxyyzz\*(R'
630
631 s/\|([^ \|]*\|) *\|([^ \|]*\|)\|/\|$2 $1/; # reverse 1st two fields
632
633.fi
634(Note the use of $ instead of \|\e\| in the last example. See section
635on regular expressions.)
76854fea
LW
636.Ip "scalar(EXPR)" 8 3
637Forces EXPR to be interpreted in a scalar context and returns the value
638of EXPR.
a687059c
LW
639.Ip "seek(FILEHANDLE,POSITION,WHENCE)" 8 3
640Randomly positions the file pointer for FILEHANDLE, just like the fseek()
641call of stdio.
642FILEHANDLE may be an expression whose value gives the name of the filehandle.
643Returns 1 upon success, 0 otherwise.
644.Ip "seekdir(DIRHANDLE,POS)" 8 3
645Sets the current position for the readdir() routine on DIRHANDLE.
34de22dd 646POS must be a value returned by telldir().
a687059c
LW
647Has the same caveats about possible directory compaction as the corresponding
648system library routine.
649.Ip "select(FILEHANDLE)" 8 3
650.Ip "select" 8 3
651Returns the currently selected filehandle.
652Sets the current default filehandle for output, if FILEHANDLE is supplied.
653This has two effects: first, a
654.I write
655or a
656.I print
657without a filehandle will default to this FILEHANDLE.
658Second, references to variables related to output will refer to this output
659channel.
660For example, if you have to set the top of form format for more than
661one output channel, you might do the following:
662.nf
663
664.ne 4
665 select(REPORT1);
666 $^ = \'report1_top\';
667 select(REPORT2);
668 $^ = \'report2_top\';
669
670.fi
671FILEHANDLE may be an expression whose value gives the name of the actual filehandle.
672Thus:
673.nf
674
675 $oldfh = select(STDERR); $| = 1; select($oldfh);
676
677.fi
678.Ip "select(RBITS,WBITS,EBITS,TIMEOUT)" 8 3
679This calls the select system call with the bitmasks specified, which can
680be constructed using fileno() and vec(), along these lines:
681.nf
682
683 $rin = $win = $ein = '';
684 vec($rin,fileno(STDIN),1) = 1;
685 vec($win,fileno(STDOUT),1) = 1;
686 $ein = $rin | $win;
687
688.fi
689If you want to select on many filehandles you might wish to write a subroutine:
690.nf
691
692 sub fhbits {
693 local(@fhlist) = split(' ',$_[0]);
694 local($bits);
695 for (@fhlist) {
696 vec($bits,fileno($_),1) = 1;
697 }
698 $bits;
699 }
700 $rin = &fhbits('STDIN TTY SOCK');
701
702.fi
703The usual idiom is:
704.nf
705
706 ($nfound,$timeleft) =
707 select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
708
709or to block until something becomes ready:
710
ae986130 711.ie t \{\
a687059c 712 $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
ae986130
LW
713'br\}
714.el \{\
715 $nfound = select($rout=$rin, $wout=$win,
716 $eout=$ein, undef);
717'br\}
a687059c
LW
718
719.fi
720Any of the bitmasks can also be undef.
721The timeout, if specified, is in seconds, which may be fractional.
663a0e37
LW
722NOTE: not all implementations are capable of returning the $timeleft.
723If not, they always return $timeleft equal to the supplied $timeout.
76854fea
LW
724.Ip "semctl(ID,SEMNUM,CMD,ARG)" 8 4
725Calls the System V IPC function semctl. If CMD is &IPC_STAT or
726&GETALL, then ARG must be a variable which will hold the returned
727semid_ds structure or semaphore value array. Returns like ioctl: the
728undefined value for error, "0 but true" for zero, or the actual return
729value otherwise.
730.Ip "semget(KEY,NSEMS,SIZE,FLAGS)" 8 4
731Calls the System V IPC function semget. Returns the semaphore id, or
732the undefined value if there is an error.
733.Ip "semop(KEY,OPSTRING)" 8 4
734Calls the System V IPC function semop to perform semaphore operations
735such as signaling and waiting. OPSTRING must be a packed array of
736semop structures. Each semop structure can be generated with
e5d73d77 737\&'pack("sss", $semnum, $semop, $semflag)'. The number of semaphore
76854fea
LW
738operations is implied by the length of OPSTRING. Returns true if
739successful, or false if there is an error. As an example, the
740following code waits on semaphore $semnum of semaphore id $semid:
741.nf
742
743 $semop = pack("sss", $semnum, -1, 0);
e5d73d77 744 die "Semaphore trouble: $!\en" unless semop($semid, $semop);
76854fea
LW
745
746.fi
747To signal the semaphore, replace "-1" with "1".
a687059c
LW
748.Ip "send(SOCKET,MSG,FLAGS,TO)" 8 4
749.Ip "send(SOCKET,MSG,FLAGS)" 8
750Sends a message on a socket.
751Takes the same flags as the system call of the same name.
752On unconnected sockets you must specify a destination to send TO.
753Returns the number of characters sent, or the undefined value if
754there is an error.
33b78306
LW
755.Ip "setpgrp(PID,PGRP)" 8 4
756Sets the current process group for the specified PID, 0 for the current
757process.
758Will produce a fatal error if used on a machine that doesn't implement
759setpgrp(2).
a687059c
LW
760.Ip "setpriority(WHICH,WHO,PRIORITY)" 8 4
761Sets the current priority for a process, a process group, or a user.
762(See setpriority(2).)
763Will produce a fatal error if used on a machine that doesn't implement
764setpriority(2).
765.Ip "setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)" 8 3
766Sets the socket option requested.
767Returns undefined if there is an error.
768OPTVAL may be specified as undef if you don't want to pass an argument.
769.Ip "shift(ARRAY)" 8 6
770.Ip "shift ARRAY" 8
771.Ip "shift" 8
772Shifts the first value of the array off and returns it,
773shortening the array by 1 and moving everything down.
774If there are no elements in the array, returns the undefined value.
775If ARRAY is omitted, shifts the @ARGV array in the main program, and the @_
776array in subroutines.
76854fea 777(This is determined lexically.)
a687059c
LW
778See also unshift(), push() and pop().
779Shift() and unshift() do the same thing to the left end of an array that push()
780and pop() do to the right end.
76854fea
LW
781.Ip "shmctl(ID,CMD,ARG)" 8 4
782Calls the System V IPC function shmctl. If CMD is &IPC_STAT, then ARG
783must be a variable which will hold the returned shmid_ds structure.
784Returns like ioctl: the undefined value for error, "0 but true" for
785zero, or the actual return value otherwise.
786.Ip "shmget(KEY,SIZE,FLAGS)" 8 4
787Calls the System V IPC function shmget. Returns the shared memory
788segment id, or the undefined value if there is an error.
789.Ip "shmread(ID,VAR,POS,SIZE)" 8 4
790.Ip "shmwrite(ID,STRING,POS,SIZE)" 8
791Reads or writes the System V shared memory segment ID starting at
792position POS for size SIZE by attaching to it, copying in/out, and
793detaching from it. When reading, VAR must be a variable which
794will hold the data read. When writing, if STRING is too long,
795only SIZE bytes are used; if STRING is too short, nulls are
796written to fill out SIZE bytes. Return true if successful, or
797false if there is an error.
a687059c
LW
798.Ip "shutdown(SOCKET,HOW)" 8 3
799Shuts down a socket connection in the manner indicated by HOW, which has
800the same interpretation as in the system call of the same name.
801.Ip "sin(EXPR)" 8 4
802.Ip "sin EXPR" 8
803Returns the sine of EXPR (expressed in radians).
804If EXPR is omitted, returns sine of $_.
805.Ip "sleep(EXPR)" 8 6
806.Ip "sleep EXPR" 8
807.Ip "sleep" 8
808Causes the script to sleep for EXPR seconds, or forever if no EXPR.
809May be interrupted by sending the process a SIGALARM.
810Returns the number of seconds actually slept.
811.Ip "socket(SOCKET,DOMAIN,TYPE,PROTOCOL)" 8 3
812Opens a socket of the specified kind and attaches it to filehandle SOCKET.
813DOMAIN, TYPE and PROTOCOL are specified the same as for the system call
814of the same name.
34de22dd 815You may need to run h2ph on sys/socket.h to get the proper values handy
a687059c
LW
816in a perl library file.
817Return true if successful.
818See the example in the section on Interprocess Communication.
819.Ip "socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)" 8 3
820Creates an unnamed pair of sockets in the specified domain, of the specified
821type.
822DOMAIN, TYPE and PROTOCOL are specified the same as for the system call
823of the same name.
824If unimplemented, yields a fatal error.
825Return true if successful.
826.Ip "sort(SUBROUTINE LIST)" 8 9
827.Ip "sort(LIST)" 8
828.Ip "sort SUBROUTINE LIST" 8
829.Ip "sort LIST" 8
830Sorts the LIST and returns the sorted array value.
831Nonexistent values of arrays are stripped out.
832If SUBROUTINE is omitted, sorts in standard string comparison order.
833If SUBROUTINE is specified, gives the name of a subroutine that returns
834an integer less than, equal to, or greater than 0,
835depending on how the elements of the array are to be ordered.
836In the interests of efficiency the normal calling code for subroutines
837is bypassed, with the following effects: the subroutine may not be a recursive
838subroutine, and the two elements to be compared are passed into the subroutine
839not via @_ but as $a and $b (see example below).
840They are passed by reference so don't modify $a and $b.
841SUBROUTINE may be a scalar variable name, in which case the value provides
842the name of the subroutine to use.
843Examples:
844.nf
845
846.ne 4
847 sub byage {
848 $age{$a} - $age{$b}; # presuming integers
849 }
850 @sortedclass = sort byage @class;
851
852.ne 9
853 sub reverse { $a lt $b ? 1 : $a gt $b ? \-1 : 0; }
854 @harry = (\'dog\',\'cat\',\'x\',\'Cain\',\'Abel\');
855 @george = (\'gone\',\'chased\',\'yz\',\'Punished\',\'Axed\');
856 print sort @harry;
857 # prints AbelCaincatdogx
858 print sort reverse @harry;
859 # prints xdogcatCainAbel
860 print sort @george, \'to\', @harry;
861 # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
862
863.fi
79a0689e
LW
864.Ip "splice(ARRAY,OFFSET,LENGTH,LIST)" 8 8
865.Ip "splice(ARRAY,OFFSET,LENGTH)" 8
866.Ip "splice(ARRAY,OFFSET)" 8
867Removes the elements designated by OFFSET and LENGTH from an array, and
868replaces them with the elements of LIST, if any.
869Returns the elements removed from the array.
870The array grows or shrinks as necessary.
871If LENGTH is omitted, removes everything from OFFSET onward.
872The following equivalencies hold (assuming $[ == 0):
873.nf
874
76854fea 875 push(@a,$x,$y)\h'|3.5i'splice(@a,$#a+1,0,$x,$y)
79a0689e
LW
876 pop(@a)\h'|3.5i'splice(@a,-1)
877 shift(@a)\h'|3.5i'splice(@a,0,1)
878 unshift(@a,$x,$y)\h'|3.5i'splice(@a,0,0,$x,$y)
879 $a[$x] = $y\h'|3.5i'splice(@a,$x,1,$y);
880
881Example, assuming array lengths are passed before arrays:
882
883 sub aeq { # compare two array values
884 local(@a) = splice(@_,0,shift);
885 local(@b) = splice(@_,0,shift);
886 return 0 unless @a == @b; # same len?
887 while (@a) {
888 return 0 if pop(@a) ne pop(@b);
889 }
890 return 1;
891 }
892 if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }
893
894.fi
a687059c
LW
895.Ip "split(/PATTERN/,EXPR,LIMIT)" 8 8
896.Ip "split(/PATTERN/,EXPR)" 8 8
897.Ip "split(/PATTERN/)" 8
898.Ip "split" 8
899Splits a string into an array of strings, and returns it.
900(If not in an array context, returns the number of fields found and splits
ac58e20f
LW
901into the @_ array.
902(In an array context, you can force the split into @_
903by using ?? as the pattern delimiters, but it still returns the array value.))
a687059c
LW
904If EXPR is omitted, splits the $_ string.
905If PATTERN is also omitted, splits on whitespace (/[\ \et\en]+/).
906Anything matching PATTERN is taken to be a delimiter separating the fields.
907(Note that the delimiter may be longer than one character.)
908If LIMIT is specified, splits into no more than that many fields (though it
909may split into fewer).
910If LIMIT is unspecified, trailing null fields are stripped (which
911potential users of pop() would do well to remember).
33b78306
LW
912A pattern matching the null string (not to be confused with a null pattern //,
913which is just one member of the set of patterns matching a null string)
a687059c
LW
914will split the value of EXPR into separate characters at each point it
915matches that way.
916For example:
917.nf
918
919 print join(\':\', split(/ */, \'hi there\'));
920
921.fi
922produces the output \*(L'h:i:t:h:e:r:e\*(R'.
ffed7fef 923.Sp
663a0e37 924The LIMIT parameter can be used to partially split a line
a687059c
LW
925.nf
926
927 ($login, $passwd, $remainder) = split(\|/\|:\|/\|, $_, 3);
928
929.fi
663a0e37 930(When assigning to a list, if LIMIT is omitted, perl supplies a LIMIT one
a687059c 931larger than the number of variables in the list, to avoid unnecessary work.
663a0e37 932For the list above LIMIT would have been 4 by default.
a687059c
LW
933In time critical applications it behooves you not to split into
934more fields than you really need.)
935.Sp
936If the PATTERN contains parentheses, additional array elements are created
937from each matching substring in the delimiter.
938.Sp
939 split(/([,-])/,"1-10,20");
940.Sp
941produces the array value
942.Sp
943 (1,'-',10,',',20)
944.Sp
945The pattern /PATTERN/ may be replaced with an expression to specify patterns
946that vary at runtime.
947(To do runtime compilation only once, use /$variable/o.)
948As a special case, specifying a space (\'\ \') will split on white space
949just as split with no arguments does, but leading white space does NOT
950produce a null first field.
951Thus, split(\'\ \') can be used to emulate
952.IR awk 's
953default behavior, whereas
954split(/\ /) will give you as many null initial fields as there are
955leading spaces.
956.Sp
957Example:
958.nf
959
960.ne 5
961 open(passwd, \'/etc/passwd\');
962 while (<passwd>) {
963.ie t \{\
964 ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(\|/\|:\|/\|);
965'br\}
966.el \{\
967 ($login, $passwd, $uid, $gid, $gcos, $home, $shell)
968 = split(\|/\|:\|/\|);
969'br\}
970 .\|.\|.
971 }
972
973.fi
974(Note that $shell above will still have a newline on it. See chop().)
975See also
976.IR join .
977.Ip "sprintf(FORMAT,LIST)" 8 4
978Returns a string formatted by the usual printf conventions.
979The * character is not supported.
980.Ip "sqrt(EXPR)" 8 4
981.Ip "sqrt EXPR" 8
982Return the square root of EXPR.
983If EXPR is omitted, returns square root of $_.
984.Ip "srand(EXPR)" 8 4
985.Ip "srand EXPR" 8
986Sets the random number seed for the
987.I rand
988operator.
989If EXPR is omitted, does srand(time).
ae986130 990.Ip "stat(FILEHANDLE)" 8 8
a687059c
LW
991.Ip "stat FILEHANDLE" 8
992.Ip "stat(EXPR)" 8
ae986130 993.Ip "stat SCALARVARIABLE" 8
a687059c
LW
994Returns a 13-element array giving the statistics for a file, either the file
995opened via FILEHANDLE, or named by EXPR.
996Typically used as follows:
997.nf
998
999.ne 3
1000 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
1001 $atime,$mtime,$ctime,$blksize,$blocks)
1002 = stat($filename);
1003
1004.fi
1005If stat is passed the special filehandle consisting of an underline,
1006no stat is done, but the current contents of the stat structure from
1007the last stat or filetest are returned.
1008Example:
1009.nf
1010
1011.ne 3
1012 if (-x $file && (($d) = stat(_)) && $d < 0) {
1013 print "$file is executable NFS file\en";
1014 }
1015
1016.fi
1017.Ip "study(SCALAR)" 8 6
1018.Ip "study SCALAR" 8
1019.Ip "study"
1020Takes extra time to study SCALAR ($_ if unspecified) in anticipation of
1021doing many pattern matches on the string before it is next modified.
1022This may or may not save time, depending on the nature and number of patterns
1023you are searching on, and on the distribution of character frequencies in
1024the string to be searched\*(--you probably want to compare runtimes with and
1025without it to see which runs faster.
1026Those loops which scan for many short constant strings (including the constant
1027parts of more complex patterns) will benefit most.
1028You may have only one study active at a time\*(--if you study a different
1029scalar the first is \*(L"unstudied\*(R".
1030(The way study works is this: a linked list of every character in the string
1031to be searched is made, so we know, for example, where all the \*(L'k\*(R' characters
1032are.
1033From each search string, the rarest character is selected, based on some
1034static frequency tables constructed from some C programs and English text.
1035Only those places that contain this \*(L"rarest\*(R" character are examined.)
1036.Sp
1037For example, here is a loop which inserts index producing entries before any line
1038containing a certain pattern:
1039.nf
1040
1041.ne 8
1042 while (<>) {
1043 study;
1044 print ".IX foo\en" if /\ebfoo\eb/;
1045 print ".IX bar\en" if /\ebbar\eb/;
1046 print ".IX blurfl\en" if /\ebblurfl\eb/;
1047 .\|.\|.
1048 print;
1049 }
1050
1051.fi
1052In searching for /\ebfoo\eb/, only those locations in $_ that contain \*(L'f\*(R'
1053will be looked at, because \*(L'f\*(R' is rarer than \*(L'o\*(R'.
1054In general, this is a big win except in pathological cases.
1055The only question is whether it saves you more time than it took to build
1056the linked list in the first place.
1057.Sp
1058Note that if you have to look for strings that you don't know till runtime,
1059you can build an entire loop as a string and eval that to avoid recompiling
1060all your patterns all the time.
1061Together with setting $/ to input entire files as one record, this can
1062be very fast, often faster than specialized programs like fgrep.
1063The following scans a list of files (@files)
1064for a list of words (@words), and prints out the names of those files that
1065contain a match:
1066.nf
1067
1068.ne 12
1069 $search = \'while (<>) { study;\';
1070 foreach $word (@words) {
1071 $search .= "++\e$seen{\e$ARGV} if /\eb$word\eb/;\en";
1072 }
1073 $search .= "}";
1074 @ARGV = @files;
1075 $/ = "\e177"; # something that doesn't occur
1076 eval $search; # this screams
1077 $/ = "\en"; # put back to normal input delim
1078 foreach $file (sort keys(%seen)) {
1079 print $file, "\en";
1080 }
1081
1082.fi
1083.Ip "substr(EXPR,OFFSET,LEN)" 8 2
76854fea 1084.Ip "substr(EXPR,OFFSET)" 8 2
a687059c
LW
1085Extracts a substring out of EXPR and returns it.
1086First character is at offset 0, or whatever you've set $[ to.
1087If OFFSET is negative, starts that far from the end of the string.
76854fea 1088If LEN is omitted, returns everything to the end of the string.
a687059c
LW
1089You can use the substr() function as an lvalue, in which case EXPR must
1090be an lvalue.
1091If you assign something shorter than LEN, the string will shrink, and
ae986130 1092if you assign something longer than LEN, the string will grow to accommodate it.
a687059c
LW
1093To keep the string the same length you may need to pad or chop your value using
1094sprintf().
33b78306
LW
1095.Ip "symlink(OLDFILE,NEWFILE)" 8 2
1096Creates a new filename symbolically linked to the old filename.
1097Returns 1 for success, 0 otherwise.
1098On systems that don't support symbolic links, produces a fatal error at
1099run time.
1100To check for that, use eval:
1101.nf
1102
1103 $symlink_exists = (eval \'symlink("","");\', $@ eq \'\');
1104
1105.fi
a687059c
LW
1106.Ip "syscall(LIST)" 8 6
1107.Ip "syscall LIST" 8
1108Calls the system call specified as the first element of the list, passing
1109the remaining elements as arguments to the system call.
1110If unimplemented, produces a fatal error.
1111The arguments are interpreted as follows: if a given argument is numeric,
1112the argument is passed as an int.
1113If not, the pointer to the string value is passed.
1114You are responsible to make sure a string is pre-extended long enough
1115to receive any result that might be written into a string.
1116If your integer arguments are not literals and have never been interpreted
1117in a numeric context, you may need to add 0 to them to force them to look
1118like numbers.
1119.nf
1120
34de22dd 1121 require 'syscall.ph'; # may need to run h2ph
a687059c
LW
1122 syscall(&SYS_write, fileno(STDOUT), "hi there\en", 9);
1123
1124.fi
76854fea
LW
1125.Ip "sysread(FILEHANDLE,SCALAR,LENGTH,OFFSET)" 8 5
1126.Ip "sysread(FILEHANDLE,SCALAR,LENGTH)" 8 5
1127Attempts to read LENGTH bytes of data into variable SCALAR from the specified
1128FILEHANDLE, using the system call read(2).
1129It bypasses stdio, so mixing this with other kinds of reads may cause
1130confusion.
1131Returns the number of bytes actually read, or undef if there was an error.
1132SCALAR will be grown or shrunk to the length actually read.
1133An OFFSET may be specified to place the read data at some other place
1134than the beginning of the string.
a687059c
LW
1135.Ip "system(LIST)" 8 6
1136.Ip "system LIST" 8
1137Does exactly the same thing as \*(L"exec LIST\*(R" except that a fork
1138is done first, and the parent process waits for the child process to complete.
1139Note that argument processing varies depending on the number of arguments.
1140The return value is the exit status of the program as returned by the wait()
1141call.
1142To get the actual exit value divide by 256.
1143See also
1144.IR exec .
76854fea
LW
1145.Ip "syswrite(FILEHANDLE,SCALAR,LENGTH,OFFSET)" 8 5
1146.Ip "syswrite(FILEHANDLE,SCALAR,LENGTH)" 8 5
1147Attempts to write LENGTH bytes of data from variable SCALAR to the specified
1148FILEHANDLE, using the system call write(2).
1149It bypasses stdio, so mixing this with prints may cause
1150confusion.
1151Returns the number of bytes actually written, or undef if there was an error.
1152An OFFSET may be specified to place the read data at some other place
1153than the beginning of the string.
a687059c
LW
1154.Ip "tell(FILEHANDLE)" 8 6
1155.Ip "tell FILEHANDLE" 8 6
1156.Ip "tell" 8
1157Returns the current file position for FILEHANDLE.
1158FILEHANDLE may be an expression whose value gives the name of the actual
1159filehandle.
1160If FILEHANDLE is omitted, assumes the file last read.
1161.Ip "telldir(DIRHANDLE)" 8 5
1162.Ip "telldir DIRHANDLE" 8
1163Returns the current position of the readdir() routines on DIRHANDLE.
1164Value may be given to seekdir() to access a particular location in
1165a directory.
1166Has the same caveats about possible directory compaction as the corresponding
1167system library routine.
1168.Ip "time" 8 4
34de22dd 1169Returns the number of non-leap seconds since 00:00:00 UTC, January 1, 1970.
a687059c
LW
1170Suitable for feeding to gmtime() and localtime().
1171.Ip "times" 8 4
1172Returns a four-element array giving the user and system times, in seconds, for this
1173process and the children of this process.
1174.Sp
1175 ($user,$system,$cuser,$csystem) = times;
1176.Sp
34de22dd
LW
1177.Ip "tr/SEARCHLIST/REPLACEMENTLIST/cds" 8 5
1178.Ip "y/SEARCHLIST/REPLACEMENTLIST/cds" 8
a687059c
LW
1179Translates all occurrences of the characters found in the search list with
1180the corresponding character in the replacement list.
34de22dd 1181It returns the number of characters replaced or deleted.
a687059c
LW
1182If no string is specified via the =~ or !~ operator,
1183the $_ string is translated.
1184(The string specified with =~ must be a scalar variable, an array element,
1185or an assignment to one of those, i.e. an lvalue.)
1186For
1187.I sed
1188devotees,
1189.I y
1190is provided as a synonym for
1191.IR tr .
34de22dd
LW
1192.Sp
1193If the c modifier is specified, the SEARCHLIST character set is complemented.
1194If the d modifier is specified, any characters specified by SEARCHLIST that
1195are not found in REPLACEMENTLIST are deleted.
1196(Note that this is slightly more flexible than the behavior of some
1197.I tr
1198programs, which delete anything they find in the SEARCHLIST, period.)
1199If the s modifier is specified, sequences of characters that were translated
1200to the same character are squashed down to 1 instance of the character.
1201.Sp
1202If the d modifier was used, the REPLACEMENTLIST is always interpreted exactly
1203as specified.
1204Otherwise, if the REPLACEMENTLIST is shorter than the SEARCHLIST,
1205the final character is replicated till it is long enough.
1206If the REPLACEMENTLIST is null, the SEARCHLIST is replicated.
1207This latter is useful for counting characters in a class, or for squashing
1208character sequences in a class.
1209.Sp
a687059c
LW
1210Examples:
1211.nf
1212
1213 $ARGV[1] \|=~ \|y/A\-Z/a\-z/; \h'|3i'# canonicalize to lower case
1214
1215 $cnt = tr/*/*/; \h'|3i'# count the stars in $_
1216
34de22dd
LW
1217 $cnt = tr/0\-9//; \h'|3i'# count the digits in $_
1218
1219 tr/a\-zA\-Z//s; \h'|3i'# bookkeeper \-> bokeper
1220
a687059c
LW
1221 ($HOST = $host) =~ tr/a\-z/A\-Z/;
1222
34de22dd
LW
1223 y/a\-zA\-Z/ /cs; \h'|3i'# change non-alphas to single space
1224
1225 tr/\e200\-\e377/\e0\-\e177/;\h'|3i'# delete 8th bit
a687059c
LW
1226
1227.fi
33b78306
LW
1228.Ip "truncate(FILEHANDLE,LENGTH)" 8 4
1229.Ip "truncate(EXPR,LENGTH)" 8
1230Truncates the file opened on FILEHANDLE, or named by EXPR, to the specified
1231length.
1232Produces a fatal error if truncate isn't implemented on your system.
a687059c
LW
1233.Ip "umask(EXPR)" 8 4
1234.Ip "umask EXPR" 8
ae986130 1235.Ip "umask" 8
a687059c
LW
1236Sets the umask for the process and returns the old one.
1237If EXPR is omitted, merely returns current umask.
1238.Ip "undef(EXPR)" 8 6
1239.Ip "undef EXPR" 8
1240.Ip "undef" 8
1241Undefines the value of EXPR, which must be an lvalue.
1242Use only on a scalar value, an entire array, or a subroutine name (using &).
1243(Undef will probably not do what you expect on most predefined variables or
1244dbm array values.)
1245Always returns the undefined value.
1246You can omit the EXPR, in which case nothing is undefined, but you still
1247get an undefined value that you could, for instance, return from a subroutine.
1248Examples:
1249.nf
1250
1251.ne 6
1252 undef $foo;
1253 undef $bar{'blurfl'};
1254 undef @ary;
1255 undef %assoc;
1256 undef &mysub;
1257 return (wantarray ? () : undef) if $they_blew_it;
1258
1259.fi
1260.Ip "unlink(LIST)" 8 4
1261.Ip "unlink LIST" 8
1262Deletes a list of files.
1263Returns the number of files successfully deleted.
1264.nf
1265
1266.ne 2
1267 $cnt = unlink \'a\', \'b\', \'c\';
1268 unlink @goners;
1269 unlink <*.bak>;
1270
1271.fi
1272Note: unlink will not delete directories unless you are superuser and the
1273.B \-U
1274flag is supplied to
1275.IR perl .
1276Even if these conditions are met, be warned that unlinking a directory
1277can inflict damage on your filesystem.
1278Use rmdir instead.
1279.Ip "unpack(TEMPLATE,EXPR)" 8 4
1280Unpack does the reverse of pack: it takes a string representing
1281a structure and expands it out into an array value, returning the array
1282value.
33b78306 1283(In a scalar context, it merely returns the first value produced.)
a687059c
LW
1284The TEMPLATE has the same format as in the pack function.
1285Here's a subroutine that does substring:
1286.nf
1287
1288.ne 4
1289 sub substr {
1290 local($what,$where,$howmuch) = @_;
1291 unpack("x$where a$howmuch", $what);
1292 }
1293
1294.ne 3
1295and then there's
1296
1297 sub ord { unpack("c",$_[0]); }
1298
1299.fi
33b78306
LW
1300In addition, you may prefix a field with a %<number> to indicate that
1301you want a <number>-bit checksum of the items instead of the items themselves.
1302Default is a 16-bit checksum.
1303For example, the following computes the same number as the System V sum program:
1304.nf
1305
1306.ne 4
1307 while (<>) {
1308 $checksum += unpack("%16C*", $_);
1309 }
1310 $checksum %= 65536;
1311
1312.fi
a687059c
LW
1313.Ip "unshift(ARRAY,LIST)" 8 4
1314Does the opposite of a
1315.IR shift .
1316Or the opposite of a
1317.IR push ,
1318depending on how you look at it.
1319Prepends list to the front of the array, and returns the number of elements
1320in the new array.
1321.nf
1322
1323 unshift(ARGV, \'\-e\') unless $ARGV[0] =~ /^\-/;
1324
1325.fi
1326.Ip "utime(LIST)" 8 2
1327.Ip "utime LIST" 8 2
1328Changes the access and modification times on each file of a list of files.
1329The first two elements of the list must be the NUMERICAL access and
1330modification times, in that order.
1331Returns the number of files successfully changed.
1332The inode modification time of each file is set to the current time.
1333Example of a \*(L"touch\*(R" command:
1334.nf
1335
1336.ne 3
1337 #!/usr/bin/perl
1338 $now = time;
1339 utime $now, $now, @ARGV;
1340
1341.fi
1342.Ip "values(ASSOC_ARRAY)" 8 6
1343.Ip "values ASSOC_ARRAY" 8
1344Returns a normal array consisting of all the values of the named associative
1345array.
1346The values are returned in an apparently random order, but it is the same order
1347as either the keys() or each() function would produce on the same array.
1348See also keys() and each().
1349.Ip "vec(EXPR,OFFSET,BITS)" 8 2
1350Treats a string as a vector of unsigned integers, and returns the value
1351of the bitfield specified.
1352May also be assigned to.
1353BITS must be a power of two from 1 to 32.
1354.Sp
1355Vectors created with vec() can also be manipulated with the logical operators
1356|, & and ^,
1357which will assume a bit vector operation is desired when both operands are
1358strings.
1359This interpretation is not enabled unless there is at least one vec() in
1360your program, to protect older programs.
1361.Ip "wait" 8 6
1362Waits for a child process to terminate and returns the pid of the deceased
ae986130 1363process, or -1 if there are no child processes.
a687059c 1364The status is returned in $?.
76854fea
LW
1365.Ip "waitpid(PID,FLAGS)" 8 6
1366Waits for a particular child process to terminate and returns the pid of the deceased
1367process, or -1 if there is no such child process.
1368The status is returned in $?.
1369If you say
1370.nf
1371
1372 require "sys/wait.h";
1373 .\|.\|.
1374 waitpid(-1,&WNOHANG);
1375
1376.fi
1377then you can do a non-blocking wait for any process. Non-blocking wait
1378is only available on machines supporting either the
1379.I waitpid (2)
1380or
1381.I wait4 (2)
1382system calls.
1383However, waiting for a particular pid with FLAGS of 0 is implemented
1384everywhere. (Perl emulates the system call by remembering the status
1385values of processes that have exited but have not been harvested by the
1386Perl script yet.)
a687059c
LW
1387.Ip "wantarray" 8 4
1388Returns true if the context of the currently executing subroutine
1389is looking for an array value.
1390Returns false if the context is looking for a scalar.
1391.nf
1392
1393 return wantarray ? () : undef;
1394
1395.fi
1396.Ip "warn(LIST)" 8 4
1397.Ip "warn LIST" 8
1398Produces a message on STDERR just like \*(L"die\*(R", but doesn't exit.
1399.Ip "write(FILEHANDLE)" 8 6
1400.Ip "write(EXPR)" 8
ae986130 1401.Ip "write" 8
a687059c
LW
1402Writes a formatted record (possibly multi-line) to the specified file,
1403using the format associated with that file.
1404By default the format for a file is the one having the same name is the
1405filehandle, but the format for the current output channel (see
1406.IR select )
1407may be set explicitly
1408by assigning the name of the format to the $~ variable.
1409.Sp
1410Top of form processing is handled automatically:
1411if there is insufficient room on the current page for the formatted
ac58e20f
LW
1412record, the page is advanced by writing a form feed,
1413a special top-of-page format is used
a687059c
LW
1414to format the new page header, and then the record is written.
1415By default the top-of-page format is \*(L"top\*(R", but it
1416may be set to the
1417format of your choice by assigning the name to the $^ variable.
ac58e20f
LW
1418The number of lines remaining on the current page is in variable $-, which
1419can be set to 0 to force a new page.
a687059c
LW
1420.Sp
1421If FILEHANDLE is unspecified, output goes to the current default output channel,
1422which starts out as
1423.I STDOUT
1424but may be changed by the
1425.I select
1426operator.
1427If the FILEHANDLE is an EXPR, then the expression is evaluated and the
1428resulting string is used to look up the name of the FILEHANDLE at run time.
1429For more on formats, see the section on formats later on.
1430.Sp
1431Note that write is NOT the opposite of read.