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