You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

perl.man.2 31KB

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