perl 3.0 patch #13 (combined patch)
[p5sagit/p5-mst-13.2.git] / perl.man.3
CommitLineData
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
27The
28.I next
29command is like the
30.I continue
31statement 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
41Note that if there were a
42.I continue
43block on the above, it would get executed even on discarded lines.
44If the LABEL is omitted, the command refers to the innermost enclosing loop.
45.Ip "oct(EXPR)" 8 4
46.Ip "oct EXPR" 8
47Returns 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.)
49The following will handle decimal, octal and hex in the standard notation:
50.nf
51
52 $val = oct($val) if $val =~ /^0/;
53
54.fi
55If EXPR is omitted, uses $_.
56.Ip "open(FILEHANDLE,EXPR)" 8 8
57.Ip "open(FILEHANDLE)" 8
58.Ip "open FILEHANDLE" 8
59Opens the file whose filename is given by EXPR, and associates it with
60FILEHANDLE.
61If FILEHANDLE is an expression, its value is used as the name of the
62real filehandle wanted.
63If EXPR is omitted, the scalar variable of the same name as the FILEHANDLE
64contains the filename.
65If the filename begins with \*(L"<\*(R" or nothing, the file is opened for
66input.
67If the filename begins with \*(L">\*(R", the file is opened for output.
68If 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
70want both read and write access to the file.)
71If the filename begins with \*(L"|\*(R", the filename is interpreted
72as a command to which output is to be piped, and if the filename ends
73with a \*(L"|\*(R", the filename is interpreted as command which pipes
74input to us.
75(You may not have a command that pipes both in and out.)
76Opening \'\-\' opens
77.I STDIN
78and opening \'>\-\' opens
79.IR STDOUT .
80Open returns non-zero upon success, the undefined value otherwise.
81If the open involved a pipe, the return value happens to be the pid
82of the subprocess.
83Examples:
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
144You may also, in the Bourne shell tradition, specify an EXPR beginning
145with \*(L">&\*(R", in which case the rest of the string
146is interpreted as the name of a filehandle
147(or file descriptor, if numeric) which is to be duped and opened.
ae986130 148You may use & after >, >>, <, +>, +>> and +<.
149The mode you specify should match the mode of the original filehandle.
a687059c 150Here is a script that saves, redirects, and restores
151.I STDOUT
152and
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
180If you open a pipe on the command \*(L"\-\*(R", i.e. either \*(L"|\-\*(R" or \*(L"\-|\*(R",
181then there is an implicit fork done, and the return value of open
182is the pid of the child within the parent process, and 0 within the child
183process.
184(Use defined($pid) to determine if the open was successful.)
185The filehandle behaves normally for the parent, but i/o to that
186filehandle is piped from/to the
187.IR STDOUT / STDIN
188of the child process.
189In the child process the filehandle isn't opened\*(--i/o happens from/to
190the new
191.I STDOUT
192or
193.IR STDIN .
194Typically this is used like the normal piped open when you want to exercise
195more control over just how the pipe command gets executed, such as when
196you are running setuid, and don't want to have to scan shell commands
197for metacharacters.
198The 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
209Explicitly closing any piped filehandle causes the parent process to wait for the
210child to finish, and returns the status value in $?.
ac58e20f 211Note: on any operation which may do a fork,
212unflushed buffers remain unflushed in both
213processes, which means you may need to set $| to
214avoid duplicate output.
215.Sp
216The filename that is passed to open will have leading and trailing
217whitespace deleted.
218In order to open a file with arbitrary weird characters in it, it's necessary
219to 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
228Opens a directory named EXPR for processing by readdir(), telldir(), seekdir(),
229rewinddir() and closedir().
230Returns true if successful.
231DIRHANDLEs have their own namespace separate from FILEHANDLEs.
232.Ip "ord(EXPR)" 8 4
233.Ip "ord EXPR" 8
234Returns the ascii value of the first character of EXPR.
235If EXPR is omitted, uses $_.
236.Ip "pack(TEMPLATE,LIST)" 8 4
237Takes an array or list of values and packs it into a binary structure,
238returning the string containing the structure.
239The TEMPLATE is a sequence of characters that give the order and type
240of 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
259Each letter may optionally be followed by a number which gives a repeat
260count.
261With all types except "a" and "A" the pack function will gobble up that many values
262from the LIST.
263The "a" and "A" types gobble just one value, but pack it as a string that long,
264padding with nulls or spaces as necessary.
265(When unpacking, "A" strips trailing spaces and nulls, but "a" does not.)
266Examples:
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
294The same template may generally also be used in the unpack function.
ac58e20f 295.Ip "pipe(READHANDLE,WRITEHANDLE)" 8 3
296Opens a pair of connected pipes like the corresponding system call.
297Note that if you set up a loop of piped processes, deadlock can occur
298unless you are very careful.
299In addition, note that perl's pipes use stdio buffering, so you may need
300to set $| to flush your WRITEHANDLE after each command, depending on
301the application.
302[Requires version 3.0 patchlevel 9.]
a687059c 303.Ip "pop(ARRAY)" 8
304.Ip "pop ARRAY" 8 6
305Pops and returns the last value of the array, shortening the array by 1.
306Has the same effect as
307.nf
308
309 $tmp = $ARRAY[$#ARRAY\-\|\-];
310
311.fi
312If 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
318Prints a string or a comma-separated list of strings.
319Returns non-zero if successful.
320FILEHANDLE may be a scalar variable name, in which case the variable contains
321the name of the filehandle, thus introducing one level of indirection.
322If FILEHANDLE is omitted, prints by default to standard output (or to the
323last selected output channel\*(--see select()).
324If LIST is also omitted, prints $_ to
325.IR STDOUT .
326To set the default output channel to something other than
327.I STDOUT
328use the select operation.
ffed7fef 329Note that, because print takes a LIST, anything in the LIST is evaluated
330in an array context, and any subroutine that you call will have one or more
331of 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
336Equivalent to a \*(L"print FILEHANDLE sprintf(LIST)\*(R".
337.Ip "push(ARRAY,LIST)" 8 7
338Treats ARRAY (@ is optional) as a stack, and pushes the values of LIST
339onto the end of ARRAY.
340The length of ARRAY increases by the length of LIST.
341Has the same effect as
342.nf
343
344 for $value (LIST) {
345 $ARRAY[++$#ARRAY] = $value;
346 }
347
348.fi
349but is more efficient.
350.Ip "q/STRING/" 8 5
351.Ip "qq/STRING/" 8
352These are not really functions, but simply syntactic sugar to let you
353avoid putting too many backslashes into quoted strings.
354The q operator is a generalized single quote, and the qq operator a
355generalized double quote.
356Any delimiter can be used in place of /, including newline.
357If the delimiter is an opening bracket or parenthesis, the final delimiter
358will be the corresponding closing bracket or parenthesis.
359(Embedded occurrences of the closing bracket need to be backslashed as usual.)
360Examples:
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
374Returns a random fractional number between 0 and the value of EXPR.
375(EXPR should be positive.)
376If EXPR is omitted, returns a value between 0 and 1.
377See also srand().
378.Ip "read(FILEHANDLE,SCALAR,LENGTH)" 8 5
379Attempts to read LENGTH bytes of data into variable SCALAR from the specified
380FILEHANDLE.
381Returns the number of bytes actually read.
382SCALAR will be grown or shrunk to the length actually read.
383.Ip "readdir(DIRHANDLE)" 8 3
ae986130 384.Ip "readdir DIRHANDLE" 8
a687059c 385Returns the next directory entry for a directory opened by opendir().
386If used in an array context, returns all the rest of the entries in the
387directory.
388If there are no more entries, returns an undefined value in a scalar context
389or a null list in an array context.
390.Ip "readlink(EXPR)" 8 6
391.Ip "readlink EXPR" 8
392Returns the value of a symbolic link, if symbolic links are implemented.
393If not, gives a fatal error.
394If there is some system error, returns the undefined value and sets $! (errno).
395If EXPR is omitted, uses $_.
396.Ip "recv(SOCKET,SCALAR,LEN,FLAGS)" 8 4
397Receives a message on a socket.
398Attempts to receive LENGTH bytes of data into variable SCALAR from the specified
399SOCKET filehandle.
400Returns the address of the sender, or the undefined value if there's an error.
401SCALAR will be grown or shrunk to the length actually read.
402Takes the same flags as the system call of the same name.
403.Ip "redo LABEL" 8 8
404.Ip "redo" 8
405The
406.I redo
407command restarts the loop block without evaluating the conditional again.
408The
409.I continue
410block, if any, is not executed.
411If the LABEL is omitted, the command refers to the innermost enclosing loop.
412This command is normally used by programs that want to lie to themselves
413about 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
436Changes the name of a file.
437Returns 1 for success, 0 otherwise.
438Will not work across filesystem boundaries.
439.Ip "reset(EXPR)" 8 6
440.Ip "reset EXPR" 8
441.Ip "reset" 8
442Generally used in a
443.I continue
444block at the end of a loop to clear variables and reset ?? searches
445so that they work again.
446The expression is interpreted as a list of single characters (hyphens allowed
447for ranges).
448All variables and arrays beginning with one of those letters are reset to
449their pristine state.
450If the expression is omitted, one-match searches (?pattern?) are reset to
451match again.
452Only resets variables or searches in the current package.
453Always returns 1.
454Examples:
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
463Note: resetting \*(L"A\-Z\*(R" is not recommended since you'll wipe out your ARGV and ENV
464arrays.
465.Sp
466The 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
468you are sharing the dbm file.
469Then again, maybe not.)
470.Ip "return LIST" 8 3
471Returns from a subroutine with the value specified.
472(Note that a subroutine can automatically return
473the value of the last expression evaluated.
474That's the preferred method\*(--use of an explicit
475.I return
476is a bit slower.)
477.Ip "reverse(LIST)" 8 4
478.Ip "reverse LIST" 8
479Returns an array value consisting of the elements of LIST in the opposite order.
480.Ip "rewinddir(DIRHANDLE)" 8 5
481.Ip "rewinddir DIRHANDLE" 8
482Sets the current position to the beginning of the directory for the readdir() routine on DIRHANDLE.
483.Ip "rindex(STR,SUBSTR)" 8 4
484Works just like index except that it
485returns the position of the LAST occurrence of SUBSTR in STR.
486.Ip "rmdir(FILENAME)" 8 4
487.Ip "rmdir FILENAME" 8
488Deletes the directory specified by FILENAME if it is empty.
489If it succeeds it returns 1, otherwise it returns 0 and sets $! (errno).
490If FILENAME is omitted, uses $_.
491.Ip "s/PATTERN/REPLACEMENT/gieo" 8 3
492Searches a string for a pattern, and if found, replaces that pattern with the
493replacement text and returns the number of substitutions made.
494Otherwise it returns false (0).
495The \*(L"g\*(R" is optional, and if present, indicates that all occurrences
496of the pattern are to be replaced.
497The \*(L"i\*(R" is also optional, and if present, indicates that matching
498is to be done in a case-insensitive manner.
499The \*(L"e\*(R" is likewise optional, and if present, indicates that
500the replacement string is to be evaluated as an expression rather than just
501as a double-quoted string.
502Any delimiter may replace the slashes; if single quotes are used, no
503interpretation is done on the replacement string (the e modifier overrides
663a0e37 504this, however); if backquotes are used, the replacement string is a command
505to execute whose output will be used as the actual replacement text.
a687059c 506If no string is specified via the =~ or !~ operator,
507the $_ string is searched and modified.
508(The string specified with =~ must be a scalar variable, an array element,
509or an assignment to one of those, i.e. an lvalue.)
510If the pattern contains a $ that looks like a variable rather than an
511end-of-string test, the variable will be interpolated into the pattern at
512run-time.
513If you only want the pattern compiled once the first time the variable is
514interpolated, add an \*(L"o\*(R" at the end.
515See also the section on regular expressions.
516Examples:
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
536on regular expressions.)
537.Ip "seek(FILEHANDLE,POSITION,WHENCE)" 8 3
538Randomly positions the file pointer for FILEHANDLE, just like the fseek()
539call of stdio.
540FILEHANDLE may be an expression whose value gives the name of the filehandle.
541Returns 1 upon success, 0 otherwise.
542.Ip "seekdir(DIRHANDLE,POS)" 8 3
543Sets the current position for the readdir() routine on DIRHANDLE.
544POS must be a value returned by seekdir().
545Has the same caveats about possible directory compaction as the corresponding
546system library routine.
547.Ip "select(FILEHANDLE)" 8 3
548.Ip "select" 8 3
549Returns the currently selected filehandle.
550Sets the current default filehandle for output, if FILEHANDLE is supplied.
551This has two effects: first, a
552.I write
553or a
554.I print
555without a filehandle will default to this FILEHANDLE.
556Second, references to variables related to output will refer to this output
557channel.
558For example, if you have to set the top of form format for more than
559one 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
569FILEHANDLE may be an expression whose value gives the name of the actual filehandle.
570Thus:
571.nf
572
573 $oldfh = select(STDERR); $| = 1; select($oldfh);
574
575.fi
576.Ip "select(RBITS,WBITS,EBITS,TIMEOUT)" 8 3
577This calls the select system call with the bitmasks specified, which can
578be 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
587If 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
601The usual idiom is:
602.nf
603
604 ($nfound,$timeleft) =
605 select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
606
607or 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
618Any of the bitmasks can also be undef.
619The timeout, if specified, is in seconds, which may be fractional.
663a0e37 620NOTE: not all implementations are capable of returning the $timeleft.
621If not, they always return $timeleft equal to the supplied $timeout.
a687059c 622.Ip "setpgrp(PID,PGRP)" 8 4
623Sets the current process group for the specified PID, 0 for the current
624process.
625Will produce a fatal error if used on a machine that doesn't implement
626setpgrp(2).
627.Ip "send(SOCKET,MSG,FLAGS,TO)" 8 4
628.Ip "send(SOCKET,MSG,FLAGS)" 8
629Sends a message on a socket.
630Takes the same flags as the system call of the same name.
631On unconnected sockets you must specify a destination to send TO.
632Returns the number of characters sent, or the undefined value if
633there is an error.
634.Ip "setpriority(WHICH,WHO,PRIORITY)" 8 4
635Sets the current priority for a process, a process group, or a user.
636(See setpriority(2).)
637Will produce a fatal error if used on a machine that doesn't implement
638setpriority(2).
639.Ip "setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)" 8 3
640Sets the socket option requested.
641Returns undefined if there is an error.
642OPTVAL 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
646Shifts the first value of the array off and returns it,
647shortening the array by 1 and moving everything down.
648If there are no elements in the array, returns the undefined value.
649If ARRAY is omitted, shifts the @ARGV array in the main program, and the @_
650array in subroutines.
651See also unshift(), push() and pop().
652Shift() and unshift() do the same thing to the left end of an array that push()
653and pop() do to the right end.
654.Ip "shutdown(SOCKET,HOW)" 8 3
655Shuts down a socket connection in the manner indicated by HOW, which has
656the same interpretation as in the system call of the same name.
657.Ip "sin(EXPR)" 8 4
658.Ip "sin EXPR" 8
659Returns the sine of EXPR (expressed in radians).
660If EXPR is omitted, returns sine of $_.
661.Ip "sleep(EXPR)" 8 6
662.Ip "sleep EXPR" 8
663.Ip "sleep" 8
664Causes the script to sleep for EXPR seconds, or forever if no EXPR.
665May be interrupted by sending the process a SIGALARM.
666Returns the number of seconds actually slept.
667.Ip "socket(SOCKET,DOMAIN,TYPE,PROTOCOL)" 8 3
668Opens a socket of the specified kind and attaches it to filehandle SOCKET.
669DOMAIN, TYPE and PROTOCOL are specified the same as for the system call
670of the same name.
671You may need to run makelib on sys/socket.h to get the proper values handy
672in a perl library file.
673Return true if successful.
674See the example in the section on Interprocess Communication.
675.Ip "socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)" 8 3
676Creates an unnamed pair of sockets in the specified domain, of the specified
677type.
678DOMAIN, TYPE and PROTOCOL are specified the same as for the system call
679of the same name.
680If unimplemented, yields a fatal error.
681Return 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
686Sorts the LIST and returns the sorted array value.
687Nonexistent values of arrays are stripped out.
688If SUBROUTINE is omitted, sorts in standard string comparison order.
689If SUBROUTINE is specified, gives the name of a subroutine that returns
690an integer less than, equal to, or greater than 0,
691depending on how the elements of the array are to be ordered.
692In the interests of efficiency the normal calling code for subroutines
693is bypassed, with the following effects: the subroutine may not be a recursive
694subroutine, and the two elements to be compared are passed into the subroutine
695not via @_ but as $a and $b (see example below).
696They are passed by reference so don't modify $a and $b.
697SUBROUTINE may be a scalar variable name, in which case the value provides
698the name of the subroutine to use.
699Examples:
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
724Splits 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 726into the @_ array.
727(In an array context, you can force the split into @_
728by using ?? as the pattern delimiters, but it still returns the array value.))
a687059c 729If EXPR is omitted, splits the $_ string.
730If PATTERN is also omitted, splits on whitespace (/[\ \et\en]+/).
731Anything matching PATTERN is taken to be a delimiter separating the fields.
732(Note that the delimiter may be longer than one character.)
733If LIMIT is specified, splits into no more than that many fields (though it
734may split into fewer).
735If LIMIT is unspecified, trailing null fields are stripped (which
736potential users of pop() would do well to remember).
737A pattern matching the null string (not to be confused with a null pattern,
738which is one member of the set of patterns matching a null string)
739will split the value of EXPR into separate characters at each point it
740matches that way.
741For example:
742.nf
743
744 print join(\':\', split(/ */, \'hi there\'));
745
746.fi
747produces the output \*(L'h:i:t:h:e:r:e\*(R'.
ffed7fef 748.Sp
663a0e37 749The 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 756larger than the number of variables in the list, to avoid unnecessary work.
663a0e37 757For the list above LIMIT would have been 4 by default.
a687059c 758In time critical applications it behooves you not to split into
759more fields than you really need.)
760.Sp
761If the PATTERN contains parentheses, additional array elements are created
762from each matching substring in the delimiter.
763.Sp
764 split(/([,-])/,"1-10,20");
765.Sp
766produces the array value
767.Sp
768 (1,'-',10,',',20)
769.Sp
770The pattern /PATTERN/ may be replaced with an expression to specify patterns
771that vary at runtime.
772(To do runtime compilation only once, use /$variable/o.)
773As a special case, specifying a space (\'\ \') will split on white space
774just as split with no arguments does, but leading white space does NOT
775produce a null first field.
776Thus, split(\'\ \') can be used to emulate
777.IR awk 's
778default behavior, whereas
779split(/\ /) will give you as many null initial fields as there are
780leading spaces.
781.Sp
782Example:
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().)
800See also
801.IR join .
802.Ip "sprintf(FORMAT,LIST)" 8 4
803Returns a string formatted by the usual printf conventions.
804The * character is not supported.
805.Ip "sqrt(EXPR)" 8 4
806.Ip "sqrt EXPR" 8
807Return the square root of EXPR.
808If EXPR is omitted, returns square root of $_.
809.Ip "srand(EXPR)" 8 4
810.Ip "srand EXPR" 8
811Sets the random number seed for the
812.I rand
813operator.
814If 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 819Returns a 13-element array giving the statistics for a file, either the file
820opened via FILEHANDLE, or named by EXPR.
821Typically 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
830If stat is passed the special filehandle consisting of an underline,
831no stat is done, but the current contents of the stat structure from
832the last stat or filetest are returned.
833Example:
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"
845Takes extra time to study SCALAR ($_ if unspecified) in anticipation of
846doing many pattern matches on the string before it is next modified.
847This may or may not save time, depending on the nature and number of patterns
848you are searching on, and on the distribution of character frequencies in
849the string to be searched\*(--you probably want to compare runtimes with and
850without it to see which runs faster.
851Those loops which scan for many short constant strings (including the constant
852parts of more complex patterns) will benefit most.
853You may have only one study active at a time\*(--if you study a different
854scalar the first is \*(L"unstudied\*(R".
855(The way study works is this: a linked list of every character in the string
856to be searched is made, so we know, for example, where all the \*(L'k\*(R' characters
857are.
858From each search string, the rarest character is selected, based on some
859static frequency tables constructed from some C programs and English text.
860Only those places that contain this \*(L"rarest\*(R" character are examined.)
861.Sp
862For example, here is a loop which inserts index producing entries before any line
863containing 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
877In searching for /\ebfoo\eb/, only those locations in $_ that contain \*(L'f\*(R'
878will be looked at, because \*(L'f\*(R' is rarer than \*(L'o\*(R'.
879In general, this is a big win except in pathological cases.
880The only question is whether it saves you more time than it took to build
881the linked list in the first place.
882.Sp
883Note that if you have to look for strings that you don't know till runtime,
884you can build an entire loop as a string and eval that to avoid recompiling
885all your patterns all the time.
886Together with setting $/ to input entire files as one record, this can
887be very fast, often faster than specialized programs like fgrep.
888The following scans a list of files (@files)
889for a list of words (@words), and prints out the names of those files that
890contain 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
909Extracts a substring out of EXPR and returns it.
910First character is at offset 0, or whatever you've set $[ to.
911If OFFSET is negative, starts that far from the end of the string.
912You can use the substr() function as an lvalue, in which case EXPR must
913be an lvalue.
914If you assign something shorter than LEN, the string will shrink, and
ae986130 915if you assign something longer than LEN, the string will grow to accommodate it.
a687059c 916To keep the string the same length you may need to pad or chop your value using
917sprintf().
918.Ip "syscall(LIST)" 8 6
919.Ip "syscall LIST" 8
920Calls the system call specified as the first element of the list, passing
921the remaining elements as arguments to the system call.
922If unimplemented, produces a fatal error.
923The arguments are interpreted as follows: if a given argument is numeric,
924the argument is passed as an int.
925If not, the pointer to the string value is passed.
926You are responsible to make sure a string is pre-extended long enough
927to receive any result that might be written into a string.
928If your integer arguments are not literals and have never been interpreted
929in a numeric context, you may need to add 0 to them to force them to look
930like 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
939Does exactly the same thing as \*(L"exec LIST\*(R" except that a fork
940is done first, and the parent process waits for the child process to complete.
941Note that argument processing varies depending on the number of arguments.
942The return value is the exit status of the program as returned by the wait()
943call.
944To get the actual exit value divide by 256.
945See also
946.IR exec .
947.Ip "symlink(OLDFILE,NEWFILE)" 8 2
948Creates a new filename symbolically linked to the old filename.
949Returns 1 for success, 0 otherwise.
950On systems that don't support symbolic links, produces a fatal error at
951run time.
952To 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
961Returns the current file position for FILEHANDLE.
962FILEHANDLE may be an expression whose value gives the name of the actual
963filehandle.
964If FILEHANDLE is omitted, assumes the file last read.
965.Ip "telldir(DIRHANDLE)" 8 5
966.Ip "telldir DIRHANDLE" 8
967Returns the current position of the readdir() routines on DIRHANDLE.
968Value may be given to seekdir() to access a particular location in
969a directory.
970Has the same caveats about possible directory compaction as the corresponding
971system library routine.
972.Ip "time" 8 4
973Returns the number of non-leap seconds since January 1, 1970, UTC.
974Suitable for feeding to gmtime() and localtime().
975.Ip "times" 8 4
976Returns a four-element array giving the user and system times, in seconds, for this
977process 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
983Translates all occurrences of the characters found in the search list with
984the corresponding character in the replacement list.
985It returns the number of characters replaced.
986If no string is specified via the =~ or !~ operator,
987the $_ string is translated.
988(The string specified with =~ must be a scalar variable, an array element,
989or an assignment to one of those, i.e. an lvalue.)
990For
991.I sed
992devotees,
993.I y
994is provided as a synonym for
995.IR tr .
996Examples:
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 1011Sets the umask for the process and returns the old one.
1012If EXPR is omitted, merely returns current umask.
1013.Ip "undef(EXPR)" 8 6
1014.Ip "undef EXPR" 8
1015.Ip "undef" 8
1016Undefines the value of EXPR, which must be an lvalue.
1017Use 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
1019dbm array values.)
1020Always returns the undefined value.
1021You can omit the EXPR, in which case nothing is undefined, but you still
1022get an undefined value that you could, for instance, return from a subroutine.
1023Examples:
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
1037Deletes a list of files.
1038Returns 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
1047Note: unlink will not delete directories unless you are superuser and the
1048.B \-U
1049flag is supplied to
1050.IR perl .
1051Even if these conditions are met, be warned that unlinking a directory
1052can inflict damage on your filesystem.
1053Use rmdir instead.
1054.Ip "unpack(TEMPLATE,EXPR)" 8 4
1055Unpack does the reverse of pack: it takes a string representing
1056a structure and expands it out into an array value, returning the array
1057value.
1058The TEMPLATE has the same format as in the pack function.
1059Here'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
1069and then there's
1070
1071 sub ord { unpack("c",$_[0]); }
1072
1073.fi
1074.Ip "unshift(ARRAY,LIST)" 8 4
1075Does the opposite of a
1076.IR shift .
1077Or the opposite of a
1078.IR push ,
1079depending on how you look at it.
1080Prepends list to the front of the array, and returns the number of elements
1081in 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
1089Changes the access and modification times on each file of a list of files.
1090The first two elements of the list must be the NUMERICAL access and
1091modification times, in that order.
1092Returns the number of files successfully changed.
1093The inode modification time of each file is set to the current time.
1094Example 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
1105Returns a normal array consisting of all the values of the named associative
1106array.
1107The values are returned in an apparently random order, but it is the same order
1108as either the keys() or each() function would produce on the same array.
1109See also keys() and each().
1110.Ip "vec(EXPR,OFFSET,BITS)" 8 2
1111Treats a string as a vector of unsigned integers, and returns the value
1112of the bitfield specified.
1113May also be assigned to.
1114BITS must be a power of two from 1 to 32.
1115.Sp
1116Vectors created with vec() can also be manipulated with the logical operators
1117|, & and ^,
1118which will assume a bit vector operation is desired when both operands are
1119strings.
1120This interpretation is not enabled unless there is at least one vec() in
1121your program, to protect older programs.
1122.Ip "wait" 8 6
1123Waits for a child process to terminate and returns the pid of the deceased
ae986130 1124process, or -1 if there are no child processes.
a687059c 1125The status is returned in $?.
ae986130 1126If you expected a child and didn't find it, you probably had a call to
1127system, a close on a pipe, or backticks between the fork and the wait.
1128These constructs also do a wait and may have harvested your child process.
a687059c 1129.Ip "wantarray" 8 4
1130Returns true if the context of the currently executing subroutine
1131is looking for an array value.
1132Returns 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
1140Produces 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 1144Writes a formatted record (possibly multi-line) to the specified file,
1145using the format associated with that file.
1146By default the format for a file is the one having the same name is the
1147filehandle, but the format for the current output channel (see
1148.IR select )
1149may be set explicitly
1150by assigning the name of the format to the $~ variable.
1151.Sp
1152Top of form processing is handled automatically:
1153if there is insufficient room on the current page for the formatted
ac58e20f 1154record, the page is advanced by writing a form feed,
1155a special top-of-page format is used
a687059c 1156to format the new page header, and then the record is written.
1157By default the top-of-page format is \*(L"top\*(R", but it
1158may be set to the
1159format of your choice by assigning the name to the $^ variable.
ac58e20f 1160The number of lines remaining on the current page is in variable $-, which
1161can be set to 0 to force a new page.
a687059c 1162.Sp
1163If FILEHANDLE is unspecified, output goes to the current default output channel,
1164which starts out as
1165.I STDOUT
1166but may be changed by the
1167.I select
1168operator.
1169If the FILEHANDLE is an EXPR, then the expression is evaluated and the
1170resulting string is used to look up the name of the FILEHANDLE at run time.
1171For more on formats, see the section on formats later on.
1172.Sp
1173Note that write is NOT the opposite of read.