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