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