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