perl 1.0 patch 12: scripts made by a2p doen't handle leading white space right on...
[p5sagit/p5-mst-13.2.git] / perl.man.2
1 ''' Beginning of part 2
2 ''' $Header: perl.man.2,v 1.0.1.3 88/02/01 17:33:03 root Exp $
3 '''
4 ''' $Log:       perl.man.2,v $
5 ''' Revision 1.0.1.3  88/02/01  17:33:03  root
6 ''' patch12: documented split more adequately.
7 ''' 
8 ''' Revision 1.0.1.2  88/01/30  17:04:28  root
9 ''' patch 11: random cleanup
10 ''' 
11 ''' Revision 1.0.1.1  88/01/28  10:25:11  root
12 ''' patch8: added $@ variable.
13 ''' 
14 ''' Revision 1.0  87/12/18  16:18:41  root
15 ''' Initial revision
16 ''' 
17 '''
18 .Ip "goto LABEL" 8 6
19 Finds the statement labeled with LABEL and resumes execution there.
20 Currently you may only go to statements in the main body of the program
21 that are not nested inside a do {} construct.
22 This statement is not implemented very efficiently, and is here only to make
23 the sed-to-perl translator easier.
24 Use at your own risk.
25 .Ip "hex(EXPR)" 8 2
26 Returns the decimal value of EXPR interpreted as an hex string.
27 (To interpret strings that might start with 0 or 0x see oct().)
28 .Ip "index(STR,SUBSTR)" 8 4
29 Returns the position of SUBSTR in STR, based at 0, or whatever you've
30 set the $[ variable to.
31 If the substring is not found, returns one less than the base, ordinarily -1.
32 .Ip "int(EXPR)" 8 3
33 Returns the integer portion of EXPR.
34 .Ip "join(EXPR,LIST)" 8 8
35 .Ip "join(EXPR,ARRAY)" 8
36 Joins the separate strings of LIST or ARRAY into a single string with fields
37 separated by the value of EXPR, and returns the string.
38 Example:
39 .nf
40     
41     $_ = join(\|':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
42
43 .fi
44 See
45 .IR split .
46 .Ip "keys(ASSOC_ARRAY)" 8 6
47 Returns a normal array consisting of all the keys of the named associative
48 array.
49 The keys are returned in an apparently random order, but it is the same order
50 as either the values() or each() function produces (given that the associative array
51 has not been modified).
52 Here is yet another way to print your environment:
53 .nf
54
55 .ne 5
56         @keys = keys(ENV);
57         @values = values(ENV);
58         while ($#keys >= 0) {
59                 print pop(keys),'=',pop(values),"\n";
60         }
61
62 .fi
63 .Ip "kill LIST" 8 2
64 Sends a signal to a list of processes.
65 The first element of the list must be the (numerical) signal to send.
66 LIST may be an array, in which case you may wish to use the unshift
67 command to put the signal on the front of the array.
68 Returns the number of processes successfully signaled.
69 Note: in order to use the value you must put the whole thing in parentheses:
70 .nf
71
72         $cnt = (kill 9,$child1,$child2);
73
74 .fi
75 .Ip "last LABEL" 8 8
76 .Ip "last" 8
77 The
78 .I last
79 command is like the
80 .I break
81 statement in C (as used in loops); it immediately exits the loop in question.
82 If the LABEL is omitted, the command refers to the innermost enclosing loop.
83 The
84 .I continue
85 block, if any, is not executed:
86 .nf
87
88 .ne 4
89         line: while (<stdin>) {
90                 last line if /\|^$/;    # exit when done with header
91                 .\|.\|.
92         }
93
94 .fi
95 .Ip "localtime(EXPR)" 8 4
96 Converts a time as returned by the time function to a 9-element array with
97 the time analyzed for the local timezone.
98 Typically used as follows:
99 .nf
100
101 .ne 3
102     ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
103        = localtime(time);
104
105 .fi
106 All array elements are numeric.
107 .Ip "log(EXPR)" 8 3
108 Returns logarithm (base e) of EXPR.
109 .Ip "next LABEL" 8 8
110 .Ip "next" 8
111 The
112 .I next
113 command is like the
114 .I continue
115 statement in C; it starts the next iteration of the loop:
116 .nf
117
118 .ne 4
119         line: while (<stdin>) {
120                 next line if /\|^#/;    # discard comments
121                 .\|.\|.
122         }
123
124 .fi
125 Note that if there were a
126 .I continue
127 block on the above, it would get executed even on discarded lines.
128 If the LABEL is omitted, the command refers to the innermost enclosing loop.
129 .Ip "length(EXPR)" 8 2
130 Returns the length in characters of the value of EXPR.
131 .Ip "link(OLDFILE,NEWFILE)" 8 2
132 Creates a new filename linked to the old filename.
133 Returns 1 for success, 0 otherwise.
134 .Ip "oct(EXPR)" 8 2
135 Returns the decimal value of EXPR interpreted as an octal string.
136 (If EXPR happens to start off with 0x, interprets it as a hex string instead.)
137 The following will handle decimal, octal and hex in the standard notation:
138 .nf
139
140         $val = oct($val) if $val =~ /^0/;
141
142 .fi
143 .Ip "open(FILEHANDLE,EXPR)" 8 8
144 .Ip "open(FILEHANDLE)" 8
145 .Ip "open FILEHANDLE" 8
146 Opens the file whose filename is given by EXPR, and associates it with
147 FILEHANDLE.
148 If EXPR is omitted, the string variable of the same name as the FILEHANDLE
149 contains the filename.
150 If the filename begins with \*(L">\*(R", the file is opened for output.
151 If the filename begins with \*(L">>\*(R", the file is opened for appending.
152 If the filename begins with \*(L"|\*(R", the filename is interpreted
153 as a command to which output is to be piped, and if the filename ends
154 with a \*(L"|\*(R", the filename is interpreted as command which pipes
155 input to us.
156 (You may not have a command that pipes both in and out.)
157 Opening '\-' opens stdin and opening '>\-' opens stdout.
158 Open returns 1 upon success, '' otherwise.
159 Examples:
160 .nf
161     
162 .ne 3
163     $article = 100;
164     open article || die "Can't find article $article";
165     while (<article>) {\|.\|.\|.
166
167     open(log, '>>/usr/spool/news/twitlog'\|);
168
169     open(article, "caeser <$article |"\|);              # decrypt article
170
171     open(extract, "|sort >/tmp/Tmp$$"\|);               # $$ is our process#
172
173 .fi
174 .Ip "ord(EXPR)" 8 3
175 Returns the ascii value of the first character of EXPR.
176 .Ip "pop ARRAY" 8 6
177 .Ip "pop(ARRAY)" 8
178 Pops and returns the last value of the array, shortening the array by 1.
179 ''' $tmp = $ARRAY[$#ARRAY--]
180 .Ip "print FILEHANDLE LIST" 8 9
181 .Ip "print LIST" 8
182 .Ip "print" 8
183 Prints a string or comma-separated list of strings.
184 If FILEHANDLE is omitted, prints by default to standard output (or to the
185 last selected output channel\*(--see select()).
186 If LIST is also omitted, prints $_ to stdout.
187 LIST may also be an array value.
188 To set the default output channel to something other than stdout use the select operation.
189 .Ip "printf FILEHANDLE LIST" 8 9
190 .Ip "printf LIST" 8
191 Equivalent to a "print FILEHANDLE sprintf(LIST)".
192 .Ip "push(ARRAY,EXPR)" 8 7
193 Treats ARRAY (@ is optional) as a stack, and pushes the value of EXPR
194 onto the end of ARRAY.
195 The length of ARRAY increases by 1.
196 Has the same effect as
197 .nf
198
199     $ARRAY[$#ARRAY+1] = EXPR;
200
201 .fi
202 but is more efficient.
203 .Ip "redo LABEL" 8 8
204 .Ip "redo" 8
205 The
206 .I redo
207 command restarts the loop block without evaluating the conditional again.
208 The
209 .I continue
210 block, if any, is not executed.
211 If the LABEL is omitted, the command refers to the innermost enclosing loop.
212 This command is normally used by programs that want to lie to themselves
213 about what was just input:
214 .nf
215
216 .ne 16
217         # a simpleminded Pascal comment stripper
218         # (warning: assumes no { or } in strings)
219         line: while (<stdin>) {
220                 while (s|\|({.*}.*\|){.*}|$1 \||) {}
221                 s|{.*}| \||;
222                 if (s|{.*| \||) {
223                         $front = $_;
224                         while (<stdin>) {
225                                 if (\|/\|}/\|) {        # end of comment?
226                                         s|^|$front{|;
227                                         redo line;
228                                 }
229                         }
230                 }
231                 print;
232         }
233
234 .fi
235 .Ip "rename(OLDNAME,NEWNAME)" 8 2
236 Changes the name of a file.
237 Returns 1 for success, 0 otherwise.
238 .Ip "reset EXPR" 8 3
239 Generally used in a
240 .I continue
241 block at the end of a loop to clear variables and reset ?? searches
242 so that they work again.
243 The expression is interpreted as a list of single characters (hyphens allowed
244 for ranges).
245 All string variables beginning with one of those letters are set to the null
246 string.
247 If the expression is omitted, one-match searches (?pattern?) are reset to
248 match again.
249 Always returns 1.
250 Examples:
251 .nf
252
253 .ne 3
254     reset 'X';  \h'|2i'# reset all X variables
255     reset 'a-z';\h'|2i'# reset lower case variables
256     reset;      \h'|2i'# just reset ?? searches
257
258 .fi
259 .Ip "s/PATTERN/REPLACEMENT/g" 8 3
260 Searches a string for a pattern, and if found, replaces that pattern with the
261 replacement text and returns the number of substitutions made.
262 Otherwise it returns false (0).
263 The \*(L"g\*(R" is optional, and if present, indicates that all occurences
264 of the pattern are to be replaced.
265 Any delimiter may replace the slashes; if single quotes are used, no
266 interpretation is done on the replacement string.
267 If no string is specified via the =~ or !~ operator,
268 the $_ string is searched and modified.
269 (The string specified with =~ must be a string variable or array element,
270 i.e. an lvalue.)
271 If the pattern contains a $ that looks like a variable rather than an
272 end-of-string test, the variable will be interpolated into the pattern at
273 run-time.
274 See also the section on regular expressions.
275 Examples:
276 .nf
277
278     s/\|\e\|bgreen\e\|b/mauve/g;                # don't change wintergreen
279
280     $path \|=~ \|s|\|/usr/bin|\|/usr/local/bin|;
281
282     s/Login: $foo/Login: $bar/; # run-time pattern
283
284     s/\|([^ \|]*\|) *\|([^ \|]*\|)\|/\|$2 $1/;  # reverse 1st two fields
285
286 .fi
287 (Note the use of $ instead of \|\e\| in the last example.  See section
288 on regular expressions.)
289 .Ip "seek(FILEHANDLE,POSITION,WHENCE)" 8 3
290 Randomly positions the file pointer for FILEHANDLE, just like the fseek()
291 call of stdio.
292 Returns 1 upon success, 0 otherwise.
293 .Ip "select(FILEHANDLE)" 8 3
294 Sets the current default filehandle for output.
295 This has two effects: first, a
296 .I write
297 or a
298 .I print
299 without a filehandle will default to this FILEHANDLE.
300 Second, references to variables related to output will refer to this output
301 channel.
302 For example, if you have to set the top of form format for more than
303 one output channel, you might do the following:
304 .nf
305
306 .ne 4
307     select(report1);
308     $^ = 'report1_top';
309     select(report2);
310     $^ = 'report2_top';
311
312 .fi
313 Select happens to return TRUE if the file is currently open and FALSE otherwise,
314 but this has no effect on its operation.
315 .Ip "shift(ARRAY)" 8 6
316 .Ip "shift ARRAY" 8
317 .Ip "shift" 8
318 Shifts the first value of the array off, shortening the array by 1 and
319 moving everything down.
320 If ARRAY is omitted, shifts the ARGV array.
321 See also unshift(), push() and pop().
322 Shift() and unshift() do the same thing to the left end of an array that push()
323 and pop() do to the right end.
324 .Ip "sleep EXPR" 8 6
325 .Ip "sleep" 8
326 Causes the script to sleep for EXPR seconds, or forever if no EXPR.
327 May be interrupted by sending the process a SIGALARM.
328 Returns the number of seconds actually slept.
329 .Ip "split(/PATTERN/,EXPR)" 8 8
330 .Ip "split(/PATTERN/)" 8
331 .Ip "split" 8
332 Splits a string into an array of strings, and returns it.
333 If EXPR is omitted, splits the $_ string.
334 If PATTERN is also omitted, splits on whitespace (/[\ \et\en]+/).
335 Anything matching PATTERN is taken to be a delimiter separating the fields.
336 (Note that the delimiter may be longer than one character.)
337 Trailing null fields are stripped, which potential users of pop() would
338 do well to remember.
339 A pattern matching the null string (not to be confused with a null pattern)
340 will split the value of EXPR into separate characters at each point it
341 matches that way.
342 For example:
343 .nf
344
345         print join(':',split(/ */,'hi there'));
346
347 .fi
348 produces the output 'h:i:t:h:e:r:e'.
349
350 The pattern /PATTERN/ may be replaced with an expression to specify patterns
351 that vary at runtime.
352 As a special case, specifying a space ('\ ') will split on white space
353 just as split with no arguments does, but leading white space does NOT
354 produce a null first field.
355 Thus, split('\ ') can be used to emulate awk's default behavior, whereas
356 split(/\ /) will give you as many null initial fields as there are
357 leading spaces.
358 .sp
359 Example:
360 .nf
361
362 .ne 5
363         open(passwd, '/etc/passwd');
364         while (<passwd>) {
365 .ie t \{\
366                 ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(\|/\|:\|/\|);
367 'br\}
368 .el \{\
369                 ($login, $passwd, $uid, $gid, $gcos, $home, $shell)
370                         = split(\|/\|:\|/\|);
371 'br\}
372                 .\|.\|.
373         }
374
375 .fi
376 (Note that $shell above will still have a newline on it.  See chop().)
377 See also
378 .IR join .
379 .Ip "sprintf(FORMAT,LIST)" 8 4
380 Returns a string formatted by the usual printf conventions.
381 The * character is not supported.
382 .Ip "sqrt(EXPR)" 8 3
383 Return the square root of EXPR.
384 .Ip "stat(FILEHANDLE)" 8 6
385 .Ip "stat(EXPR)" 8
386 Returns a 13-element array giving the statistics for a file, either the file
387 opened via FILEHANDLE, or named by EXPR.
388 Typically used as follows:
389 .nf
390
391 .ne 3
392     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
393        $atime,$mtime,$ctime,$blksize,$blocks)
394            = stat($filename);
395
396 .fi
397 .Ip "substr(EXPR,OFFSET,LEN)" 8 2
398 Extracts a substring out of EXPR and returns it.
399 First character is at offset 0, or whatever you've set $[ to.
400 .Ip "system LIST" 8 6
401 Does exactly the same thing as \*(L"exec LIST\*(R" except that a fork
402 is done first, and the parent process waits for the child process to complete.
403 Note that argument processing varies depending on the number of arguments.
404 The return value is the exit status of the program.
405 See exec.
406 .Ip "tell(FILEHANDLE)" 8 6
407 .Ip "tell" 8
408 Returns the current file position for FILEHANDLE.
409 If FILEHANDLE is omitted, assumes the file last read.
410 .Ip "time" 8 4
411 Returns the number of seconds since January 1, 1970.
412 Suitable for feeding to gmtime() and localtime().
413 .Ip "times" 8 4
414 Returns a four-element array giving the user and system times, in seconds, for this
415 process and the children of this process.
416 .sp
417     ($user,$system,$cuser,$csystem) = times;
418 .sp
419 .Ip "tr/SEARCHLIST/REPLACEMENTLIST/" 8 5
420 .Ip "y/SEARCHLIST/REPLACEMENTLIST/" 8
421 Translates all occurences of the characters found in the search list with
422 the corresponding character in the replacement list.
423 It returns the number of characters replaced.
424 If no string is specified via the =~ or !~ operator,
425 the $_ string is translated.
426 (The string specified with =~ must be a string variable or array element,
427 i.e. an lvalue.)
428 For
429 .I sed
430 devotees,
431 .I y
432 is provided as a synonym for
433 .IR tr .
434 Examples:
435 .nf
436
437     $ARGV[1] \|=~ \|y/A-Z/a-z/; \h'|3i'# canonicalize to lower case
438
439     $cnt = tr/*/*/;             \h'|3i'# count the stars in $_
440
441 .fi
442 .Ip "umask(EXPR)" 8 3
443 Sets the umask for the process and returns the old one.
444 .Ip "unlink LIST" 8 2
445 Deletes a list of files.
446 LIST may be an array.
447 Returns the number of files successfully deleted.
448 Note: in order to use the value you must put the whole thing in parentheses:
449 .nf
450
451         $cnt = (unlink 'a','b','c');
452
453 .fi
454 .ne 7
455 .Ip "unshift(ARRAY,LIST)" 8 4
456 Does the opposite of a shift.
457 Prepends list to the front of the array, and returns the number of elements
458 in the new array.
459 .nf
460
461         unshift(ARGV,'-e') unless $ARGV[0] =~ /^-/;
462
463 .fi
464 .Ip "values(ASSOC_ARRAY)" 8 6
465 Returns a normal array consisting of all the values of the named associative
466 array.
467 The values are returned in an apparently random order, but it is the same order
468 as either the keys() or each() function produces (given that the associative array
469 has not been modified).
470 See also keys() and each().
471 .Ip "write(FILEHANDLE)" 8 6
472 .Ip "write(EXPR)" 8
473 .Ip "write(\|)" 8
474 Writes a formatted record (possibly multi-line) to the specified file,
475 using the format associated with that file.
476 By default the format for a file is the one having the same name is the
477 filehandle, but the format for the current output channel (see
478 .IR select )
479 may be set explicitly
480 by assigning the name of the format to the $~ variable.
481 .sp
482 Top of form processing is handled automatically:
483 if there is insufficient room on the current page for the formatted 
484 record, the page is advanced, a special top-of-page format is used
485 to format the new page header, and then the record is written.
486 By default the top-of-page format is \*(L"top\*(R", but it
487 may be set to the
488 format of your choice by assigning the name to the $^ variable.
489 .sp
490 If FILEHANDLE is unspecified, output goes to the current default output channel,
491 which starts out as stdout but may be changed by the
492 .I select
493 operator.
494 If the FILEHANDLE is an EXPR, then the expression is evaluated and the
495 resulting string is used to look up the name of the FILEHANDLE at run time.
496 For more on formats, see the section on formats later on.
497 .Sh "Subroutines"
498 A subroutine may be declared as follows:
499 .nf
500
501     sub NAME BLOCK
502
503 .fi
504 .PP
505 Any arguments passed to the routine come in as array @_,
506 that is ($_[0], $_[1], .\|.\|.).
507 The return value of the subroutine is the value of the last expression
508 evaluated.
509 There are no local variables\*(--everything is a global variable.
510 .PP
511 A subroutine is called using the
512 .I do
513 operator.
514 (CAVEAT: For efficiency reasons recursive subroutine calls are not currently
515 supported.
516 This restriction may go away in the future.  Then again, it may not.)
517 .nf
518
519 .ne 12
520 Example:
521
522         sub MAX {
523                 $max = pop(@_);
524                 while ($foo = pop(@_)) {
525                         $max = $foo \|if \|$max < $foo;
526                 }
527                 $max;
528         }
529
530         .\|.\|.
531         $bestday = do MAX($mon,$tue,$wed,$thu,$fri);
532
533 .ne 21
534 Example:
535
536         # get a line, combining continuation lines
537         #  that start with whitespace
538         sub get_line {
539                 $thisline = $lookahead;
540                 line: while ($lookahead = <stdin>) {
541                         if ($lookahead \|=~ \|/\|^[ \^\e\|t]\|/\|) {
542                                 $thisline \|.= \|$lookahead;
543                         }
544                         else {
545                                 last line;
546                         }
547                 }
548                 $thisline;
549         }
550
551         $lookahead = <stdin>;   # get first line
552         while ($_ = get_line(\|)) {
553                 .\|.\|.
554         }
555
556 .fi
557 .nf
558 .ne 6
559 Use array assignment to name your formal arguments:
560
561         sub maybeset {
562                 ($key,$value) = @_;
563                 $foo{$key} = $value unless $foo{$key};
564         }
565
566 .fi
567 .Sh "Regular Expressions"
568 The patterns used in pattern matching are regular expressions such as
569 those used by
570 .IR egrep (1).
571 In addition, \ew matches an alphanumeric character and \eW a nonalphanumeric.
572 Word boundaries may be matched by \eb, and non-boundaries by \eB.
573 The bracketing construct \|(\ .\|.\|.\ \|) may also be used, $<digit>
574 matches the digit'th substring, where digit can range from 1 to 9.
575 (You can also use the old standby \e<digit> in search patterns,
576 but $<digit> also works in replacement patterns and in the block controlled
577 by the current conditional.)
578 $+ returns whatever the last bracket match matched.
579 $& returns the entire matched string.
580 Up to 10 alternatives may given in a pattern, separated by |, with the
581 caveat that \|(\ .\|.\|.\ |\ .\|.\|.\ \|) is illegal.
582 Examples:
583 .nf
584     
585         s/\|^\|([^ \|]*\|) \|*([^ \|]*\|)\|/\|$2 $1\|/; # swap first two words
586
587 .ne 5
588         if (/\|Time: \|(.\|.\|):\|(.\|.\|):\|(.\|.\|)\|/\|) {
589                 $hours = $1;
590                 $minutes = $2;
591                 $seconds = $3;
592         }
593
594 .fi
595 By default, the ^ character matches only the beginning of the string, and
596 .I perl
597 does certain optimizations with the assumption that the string contains
598 only one line.
599 You may, however, wish to treat a string as a multi-line buffer, such that
600 the ^ will match after any newline within the string.
601 At the cost of a little more overhead, you can do this by setting the variable
602 $* to 1.
603 Setting it back to 0 makes
604 .I perl
605 revert to its old behavior.
606 .Sh "Formats"
607 Output record formats for use with the
608 .I write
609 operator may declared as follows:
610 .nf
611
612 .ne 3
613     format NAME =
614     FORMLIST
615     .
616
617 .fi
618 If name is omitted, format \*(L"stdout\*(R" is defined.
619 FORMLIST consists of a sequence of lines, each of which may be of one of three
620 types:
621 .Ip 1. 4
622 A comment.
623 .Ip 2. 4
624 A \*(L"picture\*(R" line giving the format for one output line.
625 .Ip 3. 4
626 An argument line supplying values to plug into a picture line.
627 .PP
628 Picture lines are printed exactly as they look, except for certain fields
629 that substitute values into the line.
630 Each picture field starts with either @ or ^.
631 The @ field (not to be confused with the array marker @) is the normal
632 case; ^ fields are used
633 to do rudimentary multi-line text block filling.
634 The length of the field is supplied by padding out the field
635 with multiple <, >, or | characters to specify, respectively, left justfication,
636 right justification, or centering.
637 If any of the values supplied for these fields contains a newline, only
638 the text up to the newline is printed.
639 The special field @* can be used for printing multi-line values.
640 It should appear by itself on a line.
641 .PP
642 The values are specified on the following line, in the same order as
643 the picture fields.
644 They must currently be either string variable names or string literals (or
645 pseudo-literals).
646 Currently you can separate values with spaces, but commas may be placed
647 between values to prepare for possible future versions in which full expressions
648 are allowed as values.
649 .PP
650 Picture fields that begin with ^ rather than @ are treated specially.
651 The value supplied must be a string variable name which contains a text
652 string.
653 .I Perl
654 puts as much text as it can into the field, and then chops off the front
655 of the string so that the next time the string variable is referenced,
656 more of the text can be printed.
657 Normally you would use a sequence of fields in a vertical stack to print
658 out a block of text.
659 If you like, you can end the final field with .\|.\|., which will appear in the
660 output if the text was too long to appear in its entirety.
661 .PP
662 Since use of ^ fields can produce variable length records if the text to be
663 formatted is short, you can suppress blank lines by putting the tilde (~)
664 character anywhere in the line.
665 (Normally you should put it in the front if possible.)
666 The tilde will be translated to a space upon output.
667 .PP
668 Examples:
669 .nf
670 .lg 0
671 .cs R 25
672
673 .ne 10
674 # a report on the /etc/passwd file
675 format top =
676 \&                        Passwd File
677 Name                Login    Office   Uid   Gid Home
678 ------------------------------------------------------------------
679 \&.
680 format stdout =
681 @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
682 $name               $login   $office $uid $gid  $home
683 \&.
684
685 .ne 29
686 # a report from a bug report form
687 format top =
688 \&                        Bug Reports
689 @<<<<<<<<<<<<<<<<<<<<<<<     @|||         @>>>>>>>>>>>>>>>>>>>>>>>
690 $system;                      $%;         $date
691 ------------------------------------------------------------------
692 \&.
693 format stdout =
694 Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
695 \&         $subject
696 Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
697 \&       $index                        $description
698 Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
699 \&          $priority         $date    $description
700 From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
701 \&      $from                          $description
702 Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
703 \&             $programmer             $description
704 \&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
705 \&                                     $description
706 \&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
707 \&                                     $description
708 \&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
709 \&                                     $description
710 \&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
711 \&                                     $description
712 \&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<...
713 \&                                     $description
714 \&.
715
716 .cs R
717 .lg
718 It is possible to intermix prints with writes on the same output channel,
719 but you'll have to handle $\- (lines left on the page) yourself.
720 .fi
721 .PP
722 If you are printing lots of fields that are usually blank, you should consider
723 using the reset operator between records.
724 Not only is it more efficient, but it can prevent the bug of adding another
725 field and forgetting to zero it.
726 .Sh "Predefined Names"
727 The following names have special meaning to
728 .IR perl .
729 I could have used alphabetic symbols for some of these, but I didn't want
730 to take the chance that someone would say reset "a-zA-Z" and wipe them all
731 out.
732 You'll just have to suffer along with these silly symbols.
733 Most of them have reasonable mnemonics, or analogues in one of the shells.
734 .Ip $_ 8
735 The default input and pattern-searching space.
736 The following pairs are equivalent:
737 .nf
738
739 .ne 2
740         while (<>) {\|.\|.\|.   # only equivalent in while!
741         while ($_ = <>) {\|.\|.\|.
742
743 .ne 2
744         /\|^Subject:/
745         $_ \|=~ \|/\|^Subject:/
746
747 .ne 2
748         y/a-z/A-Z/
749         $_ =~ y/a-z/A-Z/
750
751 .ne 2
752         chop
753         chop($_)
754
755 .fi 
756 (Mnemonic: underline is understood in certain operations.)
757 .Ip $. 8
758 The current input line number of the last file that was read.
759 Readonly.
760 (Mnemonic: many programs use . to mean the current line number.)
761 .Ip $/ 8
762 The input record separator, newline by default.
763 Works like awk's RS variable, including treating blank lines as delimiters
764 if set to the null string.
765 If set to a value longer than one character, only the first character is used.
766 (Mnemonic: / is used to delimit line boundaries when quoting poetry.)
767 .Ip $, 8
768 The output field separator for the print operator.
769 Ordinarily the print operator simply prints out the comma separated fields
770 you specify.
771 In order to get behavior more like awk, set this variable as you would set
772 awk's OFS variable to specify what is printed between fields.
773 (Mnemonic: what is printed when there is a , in your print statement.)
774 .Ip $\e 8
775 The output record separator for the print operator.
776 Ordinarily the print operator simply prints out the comma separated fields
777 you specify, with no trailing newline or record separator assumed.
778 In order to get behavior more like awk, set this variable as you would set
779 awk's ORS variable to specify what is printed at the end of the print.
780 (Mnemonic: you set $\e instead of adding \en at the end of the print.
781 Also, it's just like /, but it's what you get \*(L"back\*(R" from perl.)
782 .Ip $# 8
783 The output format for printed numbers.
784 This variable is a half-hearted attempt to emulate awk's OFMT variable.
785 There are times, however, when awk and perl have differing notions of what
786 is in fact numeric.
787 Also, the initial value is %.20g rather than %.6g, so you need to set $#
788 explicitly to get awk's value.
789 (Mnemonic: # is the number sign.)
790 .Ip $% 8
791 The current page number of the currently selected output channel.
792 (Mnemonic: % is page number in nroff.)
793 .Ip $= 8
794 The current page length (printable lines) of the currently selected output
795 channel.
796 Default is 60.
797 (Mnemonic: = has horizontal lines.)
798 .Ip $\- 8
799 The number of lines left on the page of the currently selected output channel.
800 (Mnemonic: lines_on_page - lines_printed.)
801 .Ip $~ 8
802 The name of the current report format for the currently selected output
803 channel.
804 (Mnemonic: brother to $^.)
805 .Ip $^ 8
806 The name of the current top-of-page format for the currently selected output
807 channel.
808 (Mnemonic: points to top of page.)
809 .Ip $| 8
810 If set to nonzero, forces a flush after every write or print on the currently
811 selected output channel.
812 Default is 0.
813 Note that stdout will typically be line buffered if output is to the
814 terminal and block buffered otherwise.
815 Setting this variable is useful primarily when you are outputting to a pipe,
816 such as when you are running a perl script under rsh and want to see the
817 output as it's happening.
818 (Mnemonic: when you want your pipes to be piping hot.)
819 .Ip $$ 8
820 The process number of the
821 .I perl
822 running this script.
823 (Mnemonic: same as shells.)
824 .Ip $? 8
825 The status returned by the last backtick (``) command.
826 (Mnemonic: same as sh and ksh.)
827 .Ip $+ 8 4
828 The last bracket matched by the last search pattern.
829 This is useful if you don't know which of a set of alternative patterns
830 matched.
831 For example:
832 .nf
833
834     /Version: \|(.*\|)|Revision: \|(.*\|)\|/ \|&& \|($rev = $+);
835
836 .fi
837 (Mnemonic: be positive and forward looking.)
838 .Ip $* 8 2
839 Set to 1 to do multiline matching within a string, 0 to assume strings contain
840 a single line.
841 Default is 0.
842 (Mnemonic: * matches multiple things.)
843 .Ip $0 8
844 Contains the name of the file containing the
845 .I perl
846 script being executed.
847 The value should be copied elsewhere before any pattern matching happens, which
848 clobbers $0.
849 (Mnemonic: same as sh and ksh.)
850 .Ip $<digit> 8
851 Contains the subpattern from the corresponding set of parentheses in the last
852 pattern matched, not counting patterns matched in nested blocks that have
853 been exited already.
854 (Mnemonic: like \edigit.)
855 .Ip $[ 8 2
856 The index of the first element in an array, and of the first character in
857 a substring.
858 Default is 0, but you could set it to 1 to make
859 .I perl
860 behave more like
861 .I awk
862 (or Fortran)
863 when subscripting and when evaluating the index() and substr() functions.
864 (Mnemonic: [ begins subscripts.)
865 .Ip $! 8 2
866 The current value of errno, with all the usual caveats.
867 (Mnemonic: What just went bang?)
868 .Ip $@ 8 2
869 The error message from the last eval command.
870 If null, the last eval parsed and executed correctly.
871 (Mnemonic: Where was the syntax error "at"?)
872 .Ip @ARGV 8 3
873 The array ARGV contains the command line arguments intended for the script.
874 Note that $#ARGV is the generally number of arguments minus one, since
875 $ARGV[0] is the first argument, NOT the command name.
876 See $0 for the command name.
877 .Ip $ENV{expr} 8 2
878 The associative array ENV contains your current environment.
879 Setting a value in ENV changes the environment for child processes.
880 .Ip $SIG{expr} 8 2
881 The associative array SIG is used to set signal handlers for various signals.
882 Example:
883 .nf
884
885 .ne 12
886         sub handler {   # 1st argument is signal name
887                 ($sig) = @_;
888                 print "Caught a SIG$sig--shutting down\n";
889                 close(log);
890                 exit(0);
891         }
892
893         $SIG{'INT'} = 'handler';
894         $SIG{'QUIT'} = 'handler';
895         ...
896         $SIG{'INT'} = 'DEFAULT';        # restore default action
897         $SIG{'QUIT'} = 'IGNORE';        # ignore SIGQUIT
898
899 .fi
900 .SH ENVIRONMENT
901 .I Perl
902 currently uses no environment variables, except to make them available
903 to the script being executed, and to child processes.
904 However, scripts running setuid would do well to execute the following lines
905 before doing anything else, just to keep people honest:
906 .nf
907
908 .ne 3
909     $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
910     $ENV{'SHELL'} = '/bin/sh' if $ENV{'SHELL'};
911     $ENV{'IFS'} = '' if $ENV{'IFS'};
912
913 .fi
914 .SH AUTHOR
915 Larry Wall <lwall@jpl-devvax.Jpl.Nasa.Gov>
916 .SH FILES
917 /tmp/perl\-eXXXXXX      temporary file for
918 .B \-e
919 commands.
920 .SH SEE ALSO
921 a2p     awk to perl translator
922 .br
923 s2p     sed to perl translator
924 .br
925 perldb  interactive perl debugger
926 .SH DIAGNOSTICS
927 Compilation errors will tell you the line number of the error, with an
928 indication of the next token or token type that was to be examined.
929 (In the case of a script passed to
930 .I perl
931 via
932 .B \-e
933 switches, each
934 .B \-e
935 is counted as one line.)
936 .SH TRAPS
937 Accustomed awk users should take special note of the following:
938 .Ip * 4 2
939 Semicolons are required after all simple statements in perl.  Newline
940 is not a statement delimiter.
941 .Ip * 4 2
942 Curly brackets are required on ifs and whiles.
943 .Ip * 4 2
944 Variables begin with $ or @ in perl.
945 .Ip * 4 2
946 Arrays index from 0 unless you set $[.
947 Likewise string positions in substr() and index().
948 .Ip * 4 2
949 You have to decide whether your array has numeric or string indices.
950 .Ip * 4 2
951 You have to decide whether you want to use string or numeric comparisons.
952 .Ip * 4 2
953 Reading an input line does not split it for you.  You get to split it yourself
954 to an array.
955 And split has different arguments.
956 .Ip * 4 2
957 The current input line is normally in $_, not $0.
958 It generally does not have the newline stripped.
959 ($0 is initially the name of the program executed, then the last matched
960 string.)
961 .Ip * 4 2
962 The current filename is $ARGV, not $FILENAME.
963 NR, RS, ORS, OFS, and OFMT have equivalents with other symbols.
964 FS doesn't have an equivalent, since you have to be explicit about
965 split statements.
966 .Ip * 4 2
967 $<digit> does not refer to fields--it refers to substrings matched by the last
968 match pattern.
969 .Ip * 4 2
970 The print statement does not add field and record separators unless you set
971 $, and $\e.
972 .Ip * 4 2
973 You must open your files before you print to them.
974 .Ip * 4 2
975 The range operator is \*(L"..\*(R", not comma.
976 (The comma operator works as in C.)
977 .Ip * 4 2
978 The match operator is \*(L"=~\*(R", not \*(L"~\*(R".
979 (\*(L"~\*(R" is the one's complement operator.)
980 .Ip * 4 2
981 The concatenation operator is \*(L".\*(R", not the null string.
982 (Using the null string would render \*(L"/pat/ /pat/\*(R" unparseable,
983 since the third slash would be interpreted as a division operator\*(--the
984 tokener is in fact slightly context sensitive for operators like /, ?, and <.
985 And in fact, . itself can be the beginning of a number.)
986 .Ip * 4 2
987 The \ennn construct in patterns must be given as [\ennn] to avoid interpretation
988 as a backreference.
989 .Ip * 4 2
990 Next, exit, and continue work differently.
991 .Ip * 4 2
992 When in doubt, run the awk construct through a2p and see what it gives you.
993 .PP
994 Cerebral C programmers should take note of the following:
995 .Ip * 4 2
996 Curly brackets are required on ifs and whiles.
997 .Ip * 4 2
998 You should use \*(L"elsif\*(R" rather than \*(L"else if\*(R"
999 .Ip * 4 2
1000 Break and continue become last and next, respectively.
1001 .Ip * 4 2
1002 There's no switch statement.
1003 .Ip * 4 2
1004 Variables begin with $ or @ in perl.
1005 .Ip * 4 2
1006 Printf does not implement *.
1007 .Ip * 4 2
1008 Comments begin with #, not /*.
1009 .Ip * 4 2
1010 You can't take the address of anything.
1011 .Ip * 4 2
1012 Subroutines are not reentrant.
1013 .Ip * 4 2
1014 ARGV must be capitalized.
1015 .Ip * 4 2
1016 The \*(L"system\*(R" calls link, unlink, rename, etc. return 1 for success, not 0.
1017 .Ip * 4 2
1018 Signal handlers deal with signal names, not numbers.
1019 .PP
1020 Seasoned sed programmers should take note of the following:
1021 .Ip * 4 2
1022 Backreferences in substitutions use $ rather than \e.
1023 .Ip * 4 2
1024 The pattern matching metacharacters (, ), and | do not have backslashes in front.
1025 .SH BUGS
1026 .PP
1027 You can't currently dereference array elements inside a double-quoted string.
1028 You must assign them to a temporary and interpolate that.
1029 .PP
1030 Associative arrays really ought to be first class objects.
1031 .PP
1032 Recursive subroutines are not currently supported, due to the way temporary
1033 values are stored in the syntax tree.
1034 .PP
1035 Arrays ought to be passable to subroutines just as strings are.
1036 .PP
1037 The array literal consisting of one element is currently misinterpreted, i.e.
1038 .nf
1039
1040         @array = (123);
1041
1042 .fi
1043 doesn't work right.
1044 .PP
1045 .I Perl
1046 actually stands for Pathologically Eclectic Rubbish Lister, but don't tell
1047 anyone I said that.
1048 .rn }` ''