home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / perl2 / part03 < prev    next >
Encoding:
Internet Message Format  |  1989-01-05  |  48.4 KB

  1. Subject:  v15i092:  Perl, release 2, Part03/15
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  7. Posting-number: Volume 15, Issue 92
  8. Archive-name: perl2/part03
  9.  
  10. #! /bin/sh
  11.  
  12. # Make a new directory for the perl sources, cd to it, and run kits 1
  13. # thru 15 through sh.  When all 15 kits have been run, read README.
  14.  
  15. echo "This is perl 2.0 kit 3 (of 15).  If kit 3 is complete, the line"
  16. echo '"'"End of kit 3 (of 15)"'" will echo at the end.'
  17. echo ""
  18. export PATH || (echo "You didn't use sh, you clunch." ; kill $$)
  19. mkdir eg eg/g t 2>/dev/null
  20. echo Extracting perl.man.2
  21. sed >perl.man.2 <<'!STUFFY!FUNK!' -e 's/X//'
  22. X''' Beginning of part 2
  23. X''' $Header: perl.man.2,v 2.0 88/06/05 00:09:30 root Exp $
  24. X'''
  25. X''' $Log:    perl.man.2,v $
  26. X''' Revision 2.0  88/06/05  00:09:30  root
  27. X''' Baseline version 2.0.
  28. X''' 
  29. X'''
  30. X.Ip "goto LABEL" 8 6
  31. XFinds the statement labeled with LABEL and resumes execution there.
  32. XCurrently you may only go to statements in the main body of the program
  33. Xthat are not nested inside a do {} construct.
  34. XThis statement is not implemented very efficiently, and is here only to make
  35. Xthe sed-to-perl translator easier.
  36. XUse at your own risk.
  37. X.Ip "hex(EXPR)" 8 2
  38. XReturns the decimal value of EXPR interpreted as an hex string.
  39. X(To interpret strings that might start with 0 or 0x see oct().)
  40. X.Ip "index(STR,SUBSTR)" 8 4
  41. XReturns the position of SUBSTR in STR, based at 0, or whatever you've
  42. Xset the $[ variable to.
  43. XIf the substring is not found, returns one less than the base, ordinarily -1.
  44. X.Ip "int(EXPR)" 8 3
  45. XReturns the integer portion of EXPR.
  46. X.Ip "join(EXPR,LIST)" 8 8
  47. X.Ip "join(EXPR,ARRAY)" 8
  48. XJoins the separate strings of LIST or ARRAY into a single string with fields
  49. Xseparated by the value of EXPR, and returns the string.
  50. XExample:
  51. X.nf
  52. X    
  53. X    $_ = join(\|':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
  54. X
  55. X.fi
  56. XSee
  57. X.IR split .
  58. X.Ip "keys(ASSOC_ARRAY)" 8 6
  59. XReturns a normal array consisting of all the keys of the named associative
  60. Xarray.
  61. XThe keys are returned in an apparently random order, but it is the same order
  62. Xas either the values() or each() function produces (given that the associative array
  63. Xhas not been modified).
  64. XHere is yet another way to print your environment:
  65. X.nf
  66. X
  67. X.ne 5
  68. X    @keys = keys(ENV);
  69. X    @values = values(ENV);
  70. X    while ($#keys >= 0) {
  71. X        print pop(keys),'=',pop(values),"\en";
  72. X    }
  73. X
  74. Xor how about sorted by key:
  75. X
  76. X.ne 3
  77. X    foreach $key (sort keys(ENV)) {
  78. X        print $key,'=',$ENV{$key},"\en";
  79. X    }
  80. X
  81. X.fi
  82. X.Ip "kill LIST" 8 2
  83. XSends a signal to a list of processes.
  84. XThe first element of the list must be the (numerical) signal to send.
  85. XReturns the number of processes successfully signaled.
  86. X.nf
  87. X
  88. X    $cnt = kill 1,$child1,$child2;
  89. X    kill 9,@goners;
  90. X
  91. X.fi
  92. XIf the signal is negative, kills process groups instead of processes.
  93. X(On System V, a negative \fIprocess\fR number will also kill process groups,
  94. Xbut that's not portable.)
  95. X.Ip "last LABEL" 8 8
  96. X.Ip "last" 8
  97. XThe
  98. X.I last
  99. Xcommand is like the
  100. X.I break
  101. Xstatement in C (as used in loops); it immediately exits the loop in question.
  102. XIf the LABEL is omitted, the command refers to the innermost enclosing loop.
  103. XThe
  104. X.I continue
  105. Xblock, if any, is not executed:
  106. X.nf
  107. X
  108. X.ne 4
  109. X    line: while (<stdin>) {
  110. X        last line if /\|^$/;    # exit when done with header
  111. X        .\|.\|.
  112. X    }
  113. X
  114. X.fi
  115. X.Ip "length(EXPR)" 8 2
  116. XReturns the length in characters of the value of EXPR.
  117. X.Ip "link(OLDFILE,NEWFILE)" 8 2
  118. XCreates a new filename linked to the old filename.
  119. XReturns 1 for success, 0 otherwise.
  120. X.Ip "local(LIST)" 8 4
  121. XDeclares the listed (scalar) variables to be local to the enclosing block,
  122. Xsubroutine or eval.
  123. X(The "do 'filename';" operator also counts as an eval.)
  124. XThis operator works by saving the current values of those variables in LIST
  125. Xon a hidden stack and restoring them upon exiting the block, subroutine or eval.
  126. XThe LIST may be assigned to if desired, which allows you to initialize
  127. Xyour local variables.
  128. XCommonly this is used to name the parameters to a subroutine.
  129. XExamples:
  130. X.nf
  131. X
  132. X.ne 13
  133. X    sub RANGEVAL {
  134. X        local($min, $max, $thunk) = @_;
  135. X        local($result) = '';
  136. X        local($i);
  137. X
  138. X        # Presumably $thunk makes reference to $i
  139. X
  140. X        for ($i = $min; $i < $max; $i++) {
  141. X            $result .= eval $thunk;
  142. X        }
  143. X
  144. X        $result;
  145. X    }
  146. X
  147. X.fi
  148. X.Ip "localtime(EXPR)" 8 4
  149. XConverts a time as returned by the time function to a 9-element array with
  150. Xthe time analyzed for the local timezone.
  151. XTypically used as follows:
  152. X.nf
  153. X
  154. X.ne 3
  155. X    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
  156. X       = localtime(time);
  157. X
  158. X.fi
  159. XAll array elements are numeric, and come straight out of a struct tm.
  160. XIn particular this means that $mon has the range 0..11 and $wday has the
  161. Xrange 0..6.
  162. X.Ip "log(EXPR)" 8 3
  163. XReturns logarithm (base e) of EXPR.
  164. X.Ip "next LABEL" 8 8
  165. X.Ip "next" 8
  166. XThe
  167. X.I next
  168. Xcommand is like the
  169. X.I continue
  170. Xstatement in C; it starts the next iteration of the loop:
  171. X.nf
  172. X
  173. X.ne 4
  174. X    line: while (<stdin>) {
  175. X        next line if /\|^#/;    # discard comments
  176. X        .\|.\|.
  177. X    }
  178. X
  179. X.fi
  180. XNote that if there were a
  181. X.I continue
  182. Xblock on the above, it would get executed even on discarded lines.
  183. XIf the LABEL is omitted, the command refers to the innermost enclosing loop.
  184. X.Ip "oct(EXPR)" 8 2
  185. XReturns the decimal value of EXPR interpreted as an octal string.
  186. X(If EXPR happens to start off with 0x, interprets it as a hex string instead.)
  187. XThe following will handle decimal, octal and hex in the standard notation:
  188. X.nf
  189. X
  190. X    $val = oct($val) if $val =~ /^0/;
  191. X
  192. X.fi
  193. X.Ip "open(FILEHANDLE,EXPR)" 8 8
  194. X.Ip "open(FILEHANDLE)" 8
  195. X.Ip "open FILEHANDLE" 8
  196. XOpens the file whose filename is given by EXPR, and associates it with
  197. XFILEHANDLE.
  198. XIf FILEHANDLE is an expression, its value is used as the name of the
  199. Xreal filehandle wanted.
  200. XIf EXPR is omitted, the scalar variable of the same name as the FILEHANDLE
  201. Xcontains the filename.
  202. XIf the filename begins with \*(L">\*(R", the file is opened for output.
  203. XIf the filename begins with \*(L">>\*(R", the file is opened for appending.
  204. XIf the filename begins with \*(L"|\*(R", the filename is interpreted
  205. Xas a command to which output is to be piped, and if the filename ends
  206. Xwith a \*(L"|\*(R", the filename is interpreted as command which pipes
  207. Xinput to us.
  208. X(You may not have a command that pipes both in and out.)
  209. XOpening '\-' opens stdin and opening '>\-' opens stdout.
  210. XOpen returns 1 upon success, '' otherwise.
  211. XExamples:
  212. X.nf
  213. X    
  214. X.ne 3
  215. X    $article = 100;
  216. X    open article || die "Can't find article $article";
  217. X    while (<article>) {\|.\|.\|.
  218. X
  219. X    open(LOG, '>>/usr/spool/news/twitlog'\|);    # (log is reserved)
  220. X
  221. X    open(article, "caeser <$article |"\|);        # decrypt article
  222. X
  223. X    open(extract, "|sort >/tmp/Tmp$$"\|);        # $$ is our process#
  224. X
  225. X.ne 7
  226. X    # process argument list of files along with any includes
  227. X
  228. X    foreach $file (@ARGV) {
  229. X        do process($file,'fh00');    # no pun intended
  230. X    }
  231. X
  232. X    sub process {{
  233. X        local($filename,$input) = @_;
  234. X        $input++;        # this is a string increment
  235. X        unless (open($input,$filename)) {
  236. X            print stderr "Can't open $filename\en";
  237. X            last;        # note block inside sub
  238. X        }
  239. X        while (<$input>) {        # note the use of indirection
  240. X            if (/^#include "(.*)"/) {
  241. X                do process($1,$input);
  242. X                next;
  243. X            }
  244. X            .\|.\|.        # whatever
  245. X        }
  246. X    }}
  247. X
  248. X.fi
  249. XYou may also, in the Bourne shell tradition, specify an EXPR beginning
  250. Xwith ">&", in which case the rest of the string
  251. Xis interpreted as the name of a filehandle
  252. X(or file descriptor, if numeric) which is to be duped and opened.
  253. XHere is a script that saves, redirects, and restores stdout and stdin:
  254. X.nf
  255. X
  256. X.ne 21
  257. X    #!/usr/bin/perl
  258. X    open(saveout,">&stdout");
  259. X    open(saveerr,">&stderr");
  260. X
  261. X    open(stdout,">foo.out") || die "Can't redirect stdout";
  262. X    open(stderr,">&stdout") || die "Can't dup stdout";
  263. X
  264. X    select(stderr); $| = 1;        # make unbuffered
  265. X    select(stdout); $| = 1;        # make unbuffered
  266. X
  267. X    print stdout "stdout 1\en";    # this works for
  268. X    print stderr "stderr 1\en";    # subprocesses too
  269. X
  270. X    close(stdout);
  271. X    close(stderr);
  272. X
  273. X    open(stdout,">&saveout");
  274. X    open(stderr,">&saveerr");
  275. X
  276. X    print stdout "stdout 2\en";
  277. X    print stderr "stderr 2\en";
  278. X
  279. X.fi
  280. XIf you open a pipe on the command "-", i.e. either "|-" or "-|",
  281. Xthen there is an implicit fork done, and the return value of open
  282. Xis the pid of the child within the parent process, and 0 within the child
  283. Xprocess.
  284. XThe filehandle behaves normally for the parent, but i/o to that
  285. Xfilehandle is piped from/to the stdout/stdin of the child process.
  286. XIn the child process the filehandle isn't opened--i/o happens from/to
  287. Xthe new stdout or stdin.
  288. XTypically this is used like the normal piped open when you want to exercise
  289. Xmore control over just how the pipe command gets executed, such as when
  290. Xyou are running setuid, and don't want to have to scan shell commands
  291. Xfor metacharacters.
  292. XThe following pairs are equivalent:
  293. X.nf
  294. X
  295. X.ne 5
  296. X    open(FOO,"|tr '[a-z]' '[A-Z]'");
  297. X    open(FOO,"|-") || exec 'tr', '[a-z]', '[A-Z]';
  298. X
  299. X    open(FOO,"cat -n $file|");
  300. X    open(FOO,"-|") || exec 'cat', '-n', $file;
  301. X
  302. X.fi
  303. XExplicitly closing the filehandle causes the parent process to wait for the
  304. Xchild to finish, and returns the status value in $?.
  305. X.Ip "ord(EXPR)" 8 3
  306. XReturns the ascii value of the first character of EXPR.
  307. X.Ip "pop ARRAY" 8 6
  308. X.Ip "pop(ARRAY)" 8
  309. XPops and returns the last value of the array, shortening the array by 1.
  310. XHas the same effect as
  311. X.nf
  312. X
  313. X    $tmp = $ARRAY[$#ARRAY]; $#ARRAY--;
  314. X
  315. X.fi
  316. X.Ip "print FILEHANDLE LIST" 8 9
  317. X.Ip "print LIST" 8
  318. X.Ip "print" 8
  319. XPrints a string or a comma-separated list of strings.
  320. XFILEHANDLE may be a scalar variable name, in which case the variable contains
  321. Xthe name of the filehandle, thus introducing one level of indirection.
  322. XIf FILEHANDLE is omitted, prints by default to standard output (or to the
  323. Xlast selected output channel\*(--see select()).
  324. XIf LIST is also omitted, prints $_ to stdout.
  325. XTo set the default output channel to something other than stdout use the select operation.
  326. X.Ip "printf FILEHANDLE LIST" 8 9
  327. X.Ip "printf LIST" 8
  328. XEquivalent to a "print FILEHANDLE sprintf(LIST)".
  329. X.Ip "push(ARRAY,LIST)" 8 7
  330. XTreats ARRAY (@ is optional) as a stack, and pushes the values of LIST
  331. Xonto the end of ARRAY.
  332. XThe length of ARRAY increases by the length of LIST.
  333. XHas the same effect as
  334. X.nf
  335. X
  336. X    for $value (LIST) {
  337. X        $ARRAY[$#ARRAY+1] = $value;
  338. X    }
  339. X
  340. X.fi
  341. Xbut is more efficient.
  342. X.Ip "redo LABEL" 8 8
  343. X.Ip "redo" 8
  344. XThe
  345. X.I redo
  346. Xcommand restarts the loop block without evaluating the conditional again.
  347. XThe
  348. X.I continue
  349. Xblock, if any, is not executed.
  350. XIf the LABEL is omitted, the command refers to the innermost enclosing loop.
  351. XThis command is normally used by programs that want to lie to themselves
  352. Xabout what was just input:
  353. X.nf
  354. X
  355. X.ne 16
  356. X    # a simpleminded Pascal comment stripper
  357. X    # (warning: assumes no { or } in strings)
  358. X    line: while (<stdin>) {
  359. X        while (s|\|({.*}.*\|){.*}|$1 \||) {}
  360. X        s|{.*}| \||;
  361. X        if (s|{.*| \||) {
  362. X            $front = $_;
  363. X            while (<stdin>) {
  364. X                if (\|/\|}/\|) {    # end of comment?
  365. X                    s|^|$front{|;
  366. X                    redo line;
  367. X                }
  368. X            }
  369. X        }
  370. X        print;
  371. X    }
  372. X
  373. X.fi
  374. X.Ip "rename(OLDNAME,NEWNAME)" 8 2
  375. XChanges the name of a file.
  376. XReturns 1 for success, 0 otherwise.
  377. X.Ip "reset EXPR" 8 3
  378. XGenerally used in a
  379. X.I continue
  380. Xblock at the end of a loop to clear variables and reset ?? searches
  381. Xso that they work again.
  382. XThe expression is interpreted as a list of single characters (hyphens allowed
  383. Xfor ranges).
  384. XAll variables and arrays beginning with one of those letters are reset to
  385. Xtheir pristine state.
  386. XIf the expression is omitted, one-match searches (?pattern?) are reset to
  387. Xmatch again.
  388. XAlways returns 1.
  389. XExamples:
  390. X.nf
  391. X
  392. X.ne 3
  393. X    reset 'X';    \h'|2i'# reset all X variables
  394. X    reset 'a-z';\h'|2i'# reset lower case variables
  395. X    reset;    \h'|2i'# just reset ?? searches
  396. X
  397. X.fi
  398. XNote: resetting "A-Z" is not recommended since you'll wipe out your ARGV and ENV
  399. Xarrays.
  400. X.Ip "s/PATTERN/REPLACEMENT/gi" 8 3
  401. XSearches a string for a pattern, and if found, replaces that pattern with the
  402. Xreplacement text and returns the number of substitutions made.
  403. XOtherwise it returns false (0).
  404. XThe \*(L"g\*(R" is optional, and if present, indicates that all occurences
  405. Xof the pattern are to be replaced.
  406. XThe \*(L"i\*(R" is also optional, and if present, indicates that matching
  407. Xis to be done in a case-insensitive manner.
  408. XAny delimiter may replace the slashes; if single quotes are used, no
  409. Xinterpretation is done on the replacement string.
  410. XIf no string is specified via the =~ or !~ operator,
  411. Xthe $_ string is searched and modified.
  412. X(The string specified with =~ must be a scalar variable, an array element,
  413. Xor an assignment to one of those, i.e. an lvalue.)
  414. XIf the pattern contains a $ that looks like a variable rather than an
  415. Xend-of-string test, the variable will be interpolated into the pattern at
  416. Xrun-time.
  417. XSee also the section on regular expressions.
  418. XExamples:
  419. X.nf
  420. X
  421. X    s/\|\e\|bgreen\e\|b/mauve/g;        # don't change wintergreen
  422. X
  423. X    $path \|=~ \|s|\|/usr/bin|\|/usr/local/bin|;
  424. X
  425. X    s/Login: $foo/Login: $bar/; # run-time pattern
  426. X
  427. X    s/\|([^ \|]*\|) *\|([^ \|]*\|)\|/\|$2 $1/;    # reverse 1st two fields
  428. X
  429. X    ($foo = $bar) =~ s/bar/foo/;
  430. X
  431. X.fi
  432. X(Note the use of $ instead of \|\e\| in the last example.  See section
  433. Xon regular expressions.)
  434. X.Ip "seek(FILEHANDLE,POSITION,WHENCE)" 8 3
  435. XRandomly positions the file pointer for FILEHANDLE, just like the fseek()
  436. Xcall of stdio.
  437. XFILEHANDLE may be an expression whose value gives the name of the filehandle.
  438. XReturns 1 upon success, 0 otherwise.
  439. X.Ip "select(FILEHANDLE)" 8 3
  440. XSets the current default filehandle for output.
  441. XThis has two effects: first, a
  442. X.I write
  443. Xor a
  444. X.I print
  445. Xwithout a filehandle will default to this FILEHANDLE.
  446. XSecond, references to variables related to output will refer to this output
  447. Xchannel.
  448. XFor example, if you have to set the top of form format for more than
  449. Xone output channel, you might do the following:
  450. X.nf
  451. X
  452. X.ne 4
  453. X    select(report1);
  454. X    $^ = 'report1_top';
  455. X    select(report2);
  456. X    $^ = 'report2_top';
  457. X
  458. X.fi
  459. XSelect happens to return TRUE if the file is currently open and FALSE otherwise,
  460. Xbut this has no effect on its operation.
  461. XFILEHANDLE may be an expression whose value gives the name of the actual filehandle.
  462. X.Ip "shift(ARRAY)" 8 6
  463. X.Ip "shift ARRAY" 8
  464. X.Ip "shift" 8
  465. XShifts the first value of the array off and returns it,
  466. Xshortening the array by 1 and moving everything down.
  467. XIf ARRAY is omitted, shifts the ARGV array.
  468. XSee also unshift(), push() and pop().
  469. XShift() and unshift() do the same thing to the left end of an array that push()
  470. Xand pop() do to the right end.
  471. X.Ip "sleep EXPR" 8 6
  472. X.Ip "sleep" 8
  473. XCauses the script to sleep for EXPR seconds, or forever if no EXPR.
  474. XMay be interrupted by sending the process a SIGALARM.
  475. XReturns the number of seconds actually slept.
  476. X.Ip "sort SUBROUTINE LIST" 8 7
  477. X.Ip "sort LIST" 8
  478. XSorts the LIST and returns the sorted array value.
  479. XNonexistent values of arrays are stripped out.
  480. XIf SUBROUTINE is omitted, sorts in standard string comparison order.
  481. XIf SUBROUTINE is specified, gives the name of a subroutine that returns
  482. Xa -1, 0, or 1, depending on how the elements of the array are to be ordered.
  483. XIn the interests of efficiency the normal calling code for subroutines
  484. Xis bypassed, with the following effects: the subroutine may not be a recursive
  485. Xsubroutine, and the two elements to be compared are passed into the subroutine
  486. Xnot via @_ but as $a and $b (see example below).
  487. XSUBROUTINE may be a scalar variable name, in which case the value provides
  488. Xthe name of the subroutine to use.
  489. XExamples:
  490. X.nf
  491. X
  492. X.ne 4
  493. X    sub byage {
  494. X        $age{$a} < $age{$b} ? -1 : $age{$a} > $age{$b} ? 1 : 0;
  495. X    }
  496. X    @sortedclass = sort byage @class;
  497. X
  498. X.ne 9
  499. X    sub reverse { $a lt $b ? 1 : $a gt $b ? -1 : 0; }
  500. X    @harry = ('dog','cat','x','Cain','Abel');
  501. X    @george = ('gone','chased','yz','Punished','Axed');
  502. X    print sort @harry;
  503. X        # prints AbelCaincatdogx
  504. X    print sort reverse @harry;
  505. X        # prints xdogcatCainAbel
  506. X    print sort @george,'to',@harry;
  507. X        # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
  508. X
  509. X.fi
  510. X.Ip "split(/PATTERN/,EXPR)" 8 8
  511. X.Ip "split(/PATTERN/)" 8
  512. X.Ip "split" 8
  513. XSplits a string into an array of strings, and returns it.
  514. XIf EXPR is omitted, splits the $_ string.
  515. XIf PATTERN is also omitted, splits on whitespace (/[\ \et\en]+/).
  516. XAnything matching PATTERN is taken to be a delimiter separating the fields.
  517. X(Note that the delimiter may be longer than one character.)
  518. XTrailing null fields are stripped, which potential users of pop() would
  519. Xdo well to remember.
  520. XA pattern matching the null string (not to be confused with a null pattern)
  521. Xwill split the value of EXPR into separate characters at each point it
  522. Xmatches that way.
  523. XFor example:
  524. X.nf
  525. X
  526. X    print join(':',split(/ */,'hi there'));
  527. X
  528. X.fi
  529. Xproduces the output 'h:i:t:h:e:r:e'.
  530. X
  531. XThe pattern /PATTERN/ may be replaced with an expression to specify patterns
  532. Xthat vary at runtime.
  533. XAs a special case, specifying a space ('\ ') will split on white space
  534. Xjust as split with no arguments does, but leading white space does NOT
  535. Xproduce a null first field.
  536. XThus, split('\ ') can be used to emulate awk's default behavior, whereas
  537. Xsplit(/\ /) will give you as many null initial fields as there are
  538. Xleading spaces.
  539. X.sp
  540. XExample:
  541. X.nf
  542. X
  543. X.ne 5
  544. X    open(passwd, '/etc/passwd');
  545. X    while (<passwd>) {
  546. X.ie t \{\
  547. X        ($login, $passwd, $uid, $gid, $gcos, $home, $shell) = split(\|/\|:\|/\|);
  548. X'br\}
  549. X.el \{\
  550. X        ($login, $passwd, $uid, $gid, $gcos, $home, $shell)
  551. X            = split(\|/\|:\|/\|);
  552. X'br\}
  553. X        .\|.\|.
  554. X    }
  555. X
  556. X.fi
  557. X(Note that $shell above will still have a newline on it.  See chop().)
  558. XSee also
  559. X.IR join .
  560. X.Ip "sprintf(FORMAT,LIST)" 8 4
  561. XReturns a string formatted by the usual printf conventions.
  562. XThe * character is not supported.
  563. X.Ip "sqrt(EXPR)" 8 3
  564. XReturn the square root of EXPR.
  565. X.Ip "stat(FILEHANDLE)" 8 6
  566. X.Ip "stat(EXPR)" 8
  567. XReturns a 13-element array giving the statistics for a file, either the file
  568. Xopened via FILEHANDLE, or named by EXPR.
  569. XTypically used as follows:
  570. X.nf
  571. X
  572. X.ne 3
  573. X    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  574. X       $atime,$mtime,$ctime,$blksize,$blocks)
  575. X           = stat($filename);
  576. X
  577. X.fi
  578. X.Ip "study(SCALAR)" 8 6
  579. X.Ip "study"
  580. XTakes extra time to study SCALAR ($_ if unspecified) in anticipation of
  581. Xdoing many pattern matches on the string before it is next modified.
  582. XThis may or may not save time, depending on the nature and number of patterns
  583. Xyou are searching on\*(--you probably want to compare runtimes with and
  584. Xwithout it to see which runs faster.
  585. XThose loops which scan for many short constant strings (including the constant
  586. Xparts of more complex patterns) will benefit most.
  587. XFor example, a loop which inserts index producing entries before an line
  588. Xcontaining a certain pattern:
  589. X.nf
  590. X
  591. X.ne 8
  592. X    while (<>) {
  593. X        study;
  594. X        print ".IX foo\en" if /\ebfoo\eb/;
  595. X        print ".IX bar\en" if /\ebbar\eb/;
  596. X        print ".IX blurfl\en" if /\ebblurfl\eb/;
  597. X        .\|.\|.
  598. X        print;
  599. X    }
  600. X
  601. X.fi
  602. X.Ip "substr(EXPR,OFFSET,LEN)" 8 2
  603. XExtracts a substring out of EXPR and returns it.
  604. XFirst character is at offset 0, or whatever you've set $[ to.
  605. X.Ip "system LIST" 8 6
  606. XDoes exactly the same thing as \*(L"exec LIST\*(R" except that a fork
  607. Xis done first, and the parent process waits for the child process to complete.
  608. XNote that argument processing varies depending on the number of arguments.
  609. XThe return value is the exit status of the program as returned by the wait()
  610. Xcall.
  611. XTo get the actual exit value divide by 256.
  612. XSee also exec.
  613. X.Ip "symlink(OLDFILE,NEWFILE)" 8 2
  614. XCreates a new filename symbolically linked to the old filename.
  615. XReturns 1 for success, 0 otherwise.
  616. XOn systems that don't support symbolic links, produces a fatal error at
  617. Xrun time.
  618. XTo check for that, use eval:
  619. X.nf
  620. X
  621. X    $symlink_exists = (eval 'symlink("","");', $@ eq '');
  622. X
  623. X.fi
  624. X.Ip "tell(FILEHANDLE)" 8 6
  625. X.Ip "tell" 8
  626. XReturns the current file position for FILEHANDLE.
  627. XFILEHANDLE may be an expression whose value gives the name of the actual
  628. Xfilehandle.
  629. XIf FILEHANDLE is omitted, assumes the file last read.
  630. X.Ip "time" 8 4
  631. XReturns the number of seconds since January 1, 1970.
  632. XSuitable for feeding to gmtime() and localtime().
  633. X.Ip "times" 8 4
  634. XReturns a four-element array giving the user and system times, in seconds, for this
  635. Xprocess and the children of this process.
  636. X.sp
  637. X    ($user,$system,$cuser,$csystem) = times;
  638. X.sp
  639. X.Ip "tr/SEARCHLIST/REPLACEMENTLIST/" 8 5
  640. X.Ip "y/SEARCHLIST/REPLACEMENTLIST/" 8
  641. XTranslates all occurences of the characters found in the search list with
  642. Xthe corresponding character in the replacement list.
  643. XIt returns the number of characters replaced.
  644. XIf no string is specified via the =~ or !~ operator,
  645. Xthe $_ string is translated.
  646. X(The string specified with =~ must be a scalar variable, an array element,
  647. Xor an assignment to one of those, i.e. an lvalue.)
  648. XFor
  649. X.I sed
  650. Xdevotees,
  651. X.I y
  652. Xis provided as a synonym for
  653. X.IR tr .
  654. XExamples:
  655. X.nf
  656. X
  657. X    $ARGV[1] \|=~ \|y/A-Z/a-z/;    \h'|3i'# canonicalize to lower case
  658. X
  659. X    $cnt = tr/*/*/;        \h'|3i'# count the stars in $_
  660. X
  661. X    ($HOST = $host) =~ tr/a-z/A-Z/;
  662. X
  663. X.fi
  664. X.Ip "umask(EXPR)" 8 3
  665. XSets the umask for the process and returns the old one.
  666. X.Ip "unlink LIST" 8 2
  667. XDeletes a list of files.
  668. XReturns the number of files successfully deleted.
  669. X.nf
  670. X
  671. X.ne 2
  672. X    $cnt = unlink 'a','b','c';
  673. X    unlink @goners;
  674. X
  675. X.fi
  676. XNote: unlink will not delete directories unless you are superuser and the \-U
  677. Xflag is supplied to perl.
  678. X.ne 7
  679. X.Ip "unshift(ARRAY,LIST)" 8 4
  680. XDoes the opposite of a shift.
  681. XOr the opposite of a push, depending on how you look at it.
  682. XPrepends list to the front of the array, and returns the number of elements
  683. Xin the new array.
  684. X.nf
  685. X
  686. X    unshift(ARGV,'-e') unless $ARGV[0] =~ /^-/;
  687. X
  688. X.fi
  689. X.Ip "utime LIST" 8 2
  690. XChanges the access and modification times on each file of a list of files.
  691. XThe first two elements of the list must be the NUMERICAL access and
  692. Xmodification times, in that order.
  693. XReturns the number of files successfully changed.
  694. XThe inode modification time of each file is set to the current time.
  695. XExample of a "touch" command:
  696. X.nf
  697. X
  698. X.ne 3
  699. X    #!/usr/bin/perl
  700. X    $now = time;
  701. X    utime $now,$now,@ARGV;
  702. X
  703. X.fi
  704. X.Ip "values(ASSOC_ARRAY)" 8 6
  705. XReturns a normal array consisting of all the values of the named associative
  706. Xarray.
  707. XThe values are returned in an apparently random order, but it is the same order
  708. Xas either the keys() or each() function produces (given that the associative array
  709. Xhas not been modified).
  710. XSee also keys() and each().
  711. X.Ip "wait" 8 6
  712. XWaits for a child process to terminate and returns the pid of the deceased
  713. Xprocess.
  714. XThe status is returned in $?.
  715. X.Ip "write(FILEHANDLE)" 8 6
  716. X.Ip "write(EXPR)" 8
  717. X.Ip "write(\|)" 8
  718. XWrites a formatted record (possibly multi-line) to the specified file,
  719. Xusing the format associated with that file.
  720. XBy default the format for a file is the one having the same name is the
  721. Xfilehandle, but the format for the current output channel (see
  722. X.IR select )
  723. Xmay be set explicitly
  724. Xby assigning the name of the format to the $~ variable.
  725. X.sp
  726. XTop of form processing is handled automatically:
  727. Xif there is insufficient room on the current page for the formatted 
  728. Xrecord, the page is advanced, a special top-of-page format is used
  729. Xto format the new page header, and then the record is written.
  730. XBy default the top-of-page format is \*(L"top\*(R", but it
  731. Xmay be set to the
  732. Xformat of your choice by assigning the name to the $^ variable.
  733. X.sp
  734. XIf FILEHANDLE is unspecified, output goes to the current default output channel,
  735. Xwhich starts out as stdout but may be changed by the
  736. X.I select
  737. Xoperator.
  738. XIf the FILEHANDLE is an EXPR, then the expression is evaluated and the
  739. Xresulting string is used to look up the name of the FILEHANDLE at run time.
  740. XFor more on formats, see the section on formats later on.
  741. X.Sh "Precedence"
  742. XPerl operators have the following associativity and precedence:
  743. X.nf
  744. X
  745. Xnonassoc\h'|1i'print printf exec system sort
  746. X\h'1.5i'chmod chown kill unlink utime
  747. Xleft\h'|1i',
  748. Xright\h'|1i'=
  749. Xright\h'|1i'?:
  750. Xnonassoc\h'|1i'..
  751. Xleft\h'|1i'||
  752. Xleft\h'|1i'&&
  753. Xleft\h'|1i'| ^
  754. Xleft\h'|1i'&
  755. Xnonassoc\h'|1i'== != eq ne
  756. Xnonassoc\h'|1i'< > <= >= lt gt le ge
  757. Xnonassoc\h'|1i'chdir die exit eval reset sleep
  758. Xnonassoc\h'|1i'-r -w -x etc.
  759. Xleft\h'|1i'<< >>
  760. Xleft\h'|1i'+ - .
  761. Xleft\h'|1i'* / % x
  762. Xleft\h'|1i'=~ !~ 
  763. Xright\h'|1i'! ~ and unary minus
  764. Xnonassoc\h'|1i'++ --
  765. Xleft\h'|1i''('
  766. X
  767. X.fi
  768. XActually, the precedence of list operators such as print, sort or chmod is
  769. Xeither very high or very low depending on whether you look at the left
  770. Xside of operator or the right side of it.
  771. XFor example, in
  772. X
  773. X    @ary = (1, 3, sort 4, 2);
  774. X    print @ary;        # prints 1324
  775. X
  776. Xthe commas on the right of the sort are evaluated before the sort, but
  777. Xthe commas on the left are evaluated after.
  778. XIn other words, list operators tend to gobble up all the arguments that
  779. Xfollow them, and then act like a simple term with regard to the preceding
  780. Xexpression.
  781. X.Sh "Subroutines"
  782. XA subroutine may be declared as follows:
  783. X.nf
  784. X
  785. X    sub NAME BLOCK
  786. X
  787. X.fi
  788. X.PP
  789. XAny arguments passed to the routine come in as array @_,
  790. Xthat is ($_[0], $_[1], .\|.\|.).
  791. XThe return value of the subroutine is the value of the last expression
  792. Xevaluated.
  793. XTo create local variables see the "local" operator.
  794. X.PP
  795. XA subroutine is called using the
  796. X.I do
  797. Xoperator.
  798. X.nf
  799. X
  800. X.ne 12
  801. XExample:
  802. X
  803. X    sub MAX {
  804. X        local($max) = pop(@_);
  805. X        foreach $foo (@_) {
  806. X            $max = $foo \|if \|$max < $foo;
  807. X        }
  808. X        $max;
  809. X    }
  810. X
  811. X    .\|.\|.
  812. X    $bestday = do MAX($mon,$tue,$wed,$thu,$fri);
  813. X
  814. X.ne 21
  815. XExample:
  816. X
  817. X    # get a line, combining continuation lines
  818. X    #  that start with whitespace
  819. X    sub get_line {
  820. X        $thisline = $lookahead;
  821. X        line: while ($lookahead = <stdin>) {
  822. X            if ($lookahead \|=~ \|/\|^[ \^\e\|t]\|/\|) {
  823. X                $thisline \|.= \|$lookahead;
  824. X            }
  825. X            else {
  826. X                last line;
  827. X            }
  828. X        }
  829. X        $thisline;
  830. X    }
  831. X
  832. X    $lookahead = <stdin>;    # get first line
  833. X    while ($_ = get_line(\|)) {
  834. X        .\|.\|.
  835. X    }
  836. X
  837. X.fi
  838. X.nf
  839. X.ne 6
  840. XUse array assignment to local list to name your formal arguments:
  841. X
  842. X    sub maybeset {
  843. X        local($key,$value) = @_;
  844. X        $foo{$key} = $value unless $foo{$key};
  845. X    }
  846. X
  847. X.fi
  848. XSubroutines may be called recursively.
  849. X.Sh "Regular Expressions"
  850. XThe patterns used in pattern matching are regular expressions such as
  851. Xthose supplied in the Version 8 regexp routines.
  852. X(In fact, the routines are derived from Henry Spencer's freely redistributable
  853. Xreimplementation of the V8 routines.)
  854. XIn addition, \ew matches an alphanumeric character (including "_") and \eW a nonalphanumeric.
  855. XWord boundaries may be matched by \eb, and non-boundaries by \eB.
  856. XA whitespace character is matched by \es, non-whitespace by \eS.
  857. XA numeric character is matched by \ed, non-numeric by \eD.
  858. XYou may use \ew, \es and \ed within character classes.
  859. XAlso, \en, \er, \ef, \et and \eNNN have their normal interpretations.
  860. XWithin character classes \eb represents backspace rather than a word boundary.
  861. XThe bracketing construct \|(\ .\|.\|.\ \|) may also be used, in which case \e<digit>
  862. Xmatches the digit'th substring, where digit can range from 1 to 9.
  863. X(Outside of patterns, use $ instead of \e in front of the digit.
  864. XThe scope of $<digit> extends to the end of the enclosing BLOCK, or to
  865. Xthe next pattern match with subexpressions.)
  866. X$+ returns whatever the last bracket match matched.
  867. X$& returns the entire matched string.
  868. X($0 normally returns the same thing, but don't depend on it.)
  869. XAlternatives may be separated by |.
  870. XExamples:
  871. X.nf
  872. X    
  873. X    s/\|^\|([^ \|]*\|) \|*([^ \|]*\|)\|/\|$2 $1\|/;    # swap first two words
  874. X
  875. X.ne 5
  876. X    if (/\|Time: \|(.\|.\|):\|(.\|.\|):\|(.\|.\|)\|/\|) {
  877. X        $hours = $1;
  878. X        $minutes = $2;
  879. X        $seconds = $3;
  880. X    }
  881. X
  882. X.fi
  883. XBy default, the ^ character matches only the beginning of the string, and
  884. X.I perl
  885. Xdoes certain optimizations with the assumption that the string contains
  886. Xonly one line.
  887. XYou may, however, wish to treat a string as a multi-line buffer, such that
  888. Xthe ^ will match after any newline within the string.
  889. XAt the cost of a little more overhead, you can do this by setting the variable
  890. X$* to 1.
  891. XSetting it back to 0 makes
  892. X.I perl
  893. Xrevert to its old behavior.
  894. X.PP
  895. XTo facilitate multi-line substitutions, the . character never matches a newline.
  896. XIn particular, the following leaves a newline on the $_ string:
  897. X.nf
  898. X
  899. X    $_ = <stdin>;
  900. X    s/.*(some_string).*/$1/;
  901. X
  902. XIf the newline is unwanted, try one of
  903. X
  904. X    s/.*(some_string).*\en/$1/;
  905. X    s/.*(some_string)[^\000]*/$1/;
  906. X    s/.*(some_string)(.|\en)*/$1/;
  907. X    chop; s/.*(some_string).*/$1/;
  908. X    /(some_string)/ && ($_ = $1);
  909. X
  910. X.fi
  911. X.Sh "Formats"
  912. XOutput record formats for use with the
  913. X.I write
  914. Xoperator may declared as follows:
  915. X.nf
  916. X
  917. X.ne 3
  918. X    format NAME =
  919. X    FORMLIST
  920. X    .
  921. X
  922. X.fi
  923. XIf name is omitted, format \*(L"stdout\*(R" is defined.
  924. XFORMLIST consists of a sequence of lines, each of which may be of one of three
  925. Xtypes:
  926. X.Ip 1. 4
  927. XA comment.
  928. X.Ip 2. 4
  929. XA \*(L"picture\*(R" line giving the format for one output line.
  930. X.Ip 3. 4
  931. XAn argument line supplying values to plug into a picture line.
  932. X.PP
  933. XPicture lines are printed exactly as they look, except for certain fields
  934. Xthat substitute values into the line.
  935. XEach picture field starts with either @ or ^.
  936. XThe @ field (not to be confused with the array marker @) is the normal
  937. Xcase; ^ fields are used
  938. Xto do rudimentary multi-line text block filling.
  939. XThe length of the field is supplied by padding out the field
  940. Xwith multiple <, >, or | characters to specify, respectively, left justfication,
  941. Xright justification, or centering.
  942. XIf any of the values supplied for these fields contains a newline, only
  943. Xthe text up to the newline is printed.
  944. XThe special field @* can be used for printing multi-line values.
  945. XIt should appear by itself on a line.
  946. X.PP
  947. XThe values are specified on the following line, in the same order as
  948. Xthe picture fields.
  949. XThey must currently be either scalar variable names or literals (or
  950. Xpseudo-literals).
  951. XCurrently you can separate values with spaces, but commas may be placed
  952. Xbetween values to prepare for possible future versions in which full expressions
  953. Xare allowed as values.
  954. X.PP
  955. XPicture fields that begin with ^ rather than @ are treated specially.
  956. XThe value supplied must be a scalar variable name which contains a text
  957. Xstring.
  958. X.I Perl
  959. Xputs as much text as it can into the field, and then chops off the front
  960. Xof the string so that the next time the variable is referenced,
  961. Xmore of the text can be printed.
  962. XNormally you would use a sequence of fields in a vertical stack to print
  963. Xout a block of text.
  964. XIf you like, you can end the final field with .\|.\|., which will appear in the
  965. Xoutput if the text was too long to appear in its entirety.
  966. X.PP
  967. XSince use of ^ fields can produce variable length records if the text to be
  968. Xformatted is short, you can suppress blank lines by putting the tilde (~)
  969. Xcharacter anywhere in the line.
  970. X(Normally you should put it in the front if possible.)
  971. XThe tilde will be translated to a space upon output.
  972. X.PP
  973. XExamples:
  974. X.nf
  975. X.lg 0
  976. X.cs R 25
  977. X
  978. X.ne 10
  979. X# a report on the /etc/passwd file
  980. Xformat top =
  981. X\&                        Passwd File
  982. XName                Login    Office   Uid   Gid Home
  983. X------------------------------------------------------------------
  984. X\&.
  985. Xformat stdout =
  986. X@<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
  987. X$name               $login   $office $uid $gid  $home
  988. X\&.
  989. X
  990. X.ne 29
  991. X# a report from a bug report form
  992. Xformat top =
  993. X\&                        Bug Reports
  994. X@<<<<<<<<<<<<<<<<<<<<<<<     @|||         @>>>>>>>>>>>>>>>>>>>>>>>
  995. X$system;                      $%;         $date
  996. X------------------------------------------------------------------
  997. X\&.
  998. Xformat stdout =
  999. XSubject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1000. X\&         $subject
  1001. XIndex: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1002. X\&       $index                        $description
  1003. XPriority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1004. X\&          $priority         $date    $description
  1005. XFrom: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1006. X\&      $from                          $description
  1007. XAssigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1008. X\&             $programmer             $description
  1009. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1010. X\&                                     $description
  1011. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1012. X\&                                     $description
  1013. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1014. X\&                                     $description
  1015. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  1016. X\&                                     $description
  1017. X\&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<...
  1018. X\&                                     $description
  1019. X\&.
  1020. X
  1021. X.cs R
  1022. X.lg
  1023. XIt is possible to intermix prints with writes on the same output channel,
  1024. Xbut you'll have to handle $\- (lines left on the page) yourself.
  1025. X.fi
  1026. X.PP
  1027. XIf you are printing lots of fields that are usually blank, you should consider
  1028. Xusing the reset operator between records.
  1029. XNot only is it more efficient, but it can prevent the bug of adding another
  1030. Xfield and forgetting to zero it.
  1031. X.Sh "Predefined Names"
  1032. XThe following names have special meaning to
  1033. X.IR perl .
  1034. XI could have used alphabetic symbols for some of these, but I didn't want
  1035. Xto take the chance that someone would say reset "a-zA-Z" and wipe them all
  1036. Xout.
  1037. XYou'll just have to suffer along with these silly symbols.
  1038. XMost of them have reasonable mnemonics, or analogues in one of the shells.
  1039. X.Ip $_ 8
  1040. XThe default input and pattern-searching space.
  1041. XThe following pairs are equivalent:
  1042. X.nf
  1043. X
  1044. X.ne 2
  1045. X    while (<>) {\|.\|.\|.    # only equivalent in while!
  1046. X    while ($_ = <>) {\|.\|.\|.
  1047. X
  1048. X.ne 2
  1049. X    /\|^Subject:/
  1050. X    $_ \|=~ \|/\|^Subject:/
  1051. X
  1052. X.ne 2
  1053. X    y/a-z/A-Z/
  1054. X    $_ =~ y/a-z/A-Z/
  1055. X
  1056. X.ne 2
  1057. X    chop
  1058. X    chop($_)
  1059. X
  1060. X.fi 
  1061. X(Mnemonic: underline is understood in certain operations.)
  1062. X.Ip $. 8
  1063. XThe current input line number of the last filehandle that was read.
  1064. XReadonly.
  1065. XRemember that only an explicit close on the filehandle resets the line number.
  1066. XSince <> never does an explicit close, line numbers increase across ARGV files
  1067. X(but see examples under eof).
  1068. X(Mnemonic: many programs use . to mean the current line number.)
  1069. X.Ip $/ 8
  1070. XThe input record separator, newline by default.
  1071. XWorks like awk's RS variable, including treating blank lines as delimiters
  1072. Xif set to the null string.
  1073. XIf set to a value longer than one character, only the first character is used.
  1074. X(Mnemonic: / is used to delimit line boundaries when quoting poetry.)
  1075. X.Ip $, 8
  1076. XThe output field separator for the print operator.
  1077. XOrdinarily the print operator simply prints out the comma separated fields
  1078. Xyou specify.
  1079. XIn order to get behavior more like awk, set this variable as you would set
  1080. Xawk's OFS variable to specify what is printed between fields.
  1081. X(Mnemonic: what is printed when there is a , in your print statement.)
  1082. X.Ip $\e 8
  1083. XThe output record separator for the print operator.
  1084. XOrdinarily the print operator simply prints out the comma separated fields
  1085. Xyou specify, with no trailing newline or record separator assumed.
  1086. XIn order to get behavior more like awk, set this variable as you would set
  1087. Xawk's ORS variable to specify what is printed at the end of the print.
  1088. X(Mnemonic: you set $\e instead of adding \en at the end of the print.
  1089. XAlso, it's just like /, but it's what you get \*(L"back\*(R" from perl.)
  1090. X.Ip $# 8
  1091. XThe output format for printed numbers.
  1092. XThis variable is a half-hearted attempt to emulate awk's OFMT variable.
  1093. XThere are times, however, when awk and perl have differing notions of what
  1094. Xis in fact numeric.
  1095. XAlso, the initial value is %.20g rather than %.6g, so you need to set $#
  1096. Xexplicitly to get awk's value.
  1097. X(Mnemonic: # is the number sign.)
  1098. X.Ip $% 8
  1099. XThe current page number of the currently selected output channel.
  1100. X(Mnemonic: % is page number in nroff.)
  1101. X.Ip $= 8
  1102. XThe current page length (printable lines) of the currently selected output
  1103. Xchannel.
  1104. XDefault is 60.
  1105. X(Mnemonic: = has horizontal lines.)
  1106. X.Ip $\- 8
  1107. XThe number of lines left on the page of the currently selected output channel.
  1108. X(Mnemonic: lines_on_page - lines_printed.)
  1109. X.Ip $~ 8
  1110. XThe name of the current report format for the currently selected output
  1111. Xchannel.
  1112. X(Mnemonic: brother to $^.)
  1113. X.Ip $^ 8
  1114. XThe name of the current top-of-page format for the currently selected output
  1115. Xchannel.
  1116. X(Mnemonic: points to top of page.)
  1117. X.Ip $| 8
  1118. XIf set to nonzero, forces a flush after every write or print on the currently
  1119. Xselected output channel.
  1120. XDefault is 0.
  1121. XNote that stdout will typically be line buffered if output is to the
  1122. Xterminal and block buffered otherwise.
  1123. XSetting this variable is useful primarily when you are outputting to a pipe,
  1124. Xsuch as when you are running a perl script under rsh and want to see the
  1125. Xoutput as it's happening.
  1126. X(Mnemonic: when you want your pipes to be piping hot.)
  1127. X.Ip $$ 8
  1128. XThe process number of the
  1129. X.I perl
  1130. Xrunning this script.
  1131. X(Mnemonic: same as shells.)
  1132. X.Ip $? 8
  1133. XThe status returned by the last backtick (``) command or system operator.
  1134. XNote that this is the status word returned by the wait() system
  1135. Xcall, so the exit value of the subprocess is actually ($? >> 8).
  1136. X$? & 255 gives which signal, if any, the process died from, and whether
  1137. Xthere was a core dump.
  1138. X(Mnemonic: similar to sh and ksh.)
  1139. X.Ip $& 8 4
  1140. XThe string matched by the last pattern match.
  1141. X(Mnemonic: like & in some editors.)
  1142. X.Ip $+ 8 4
  1143. XThe last bracket matched by the last search pattern.
  1144. XThis is useful if you don't know which of a set of alternative patterns
  1145. Xmatched.
  1146. XFor example:
  1147. X.nf
  1148. X
  1149. X    /Version: \|(.*\|)|Revision: \|(.*\|)\|/ \|&& \|($rev = $+);
  1150. X
  1151. X.fi
  1152. X(Mnemonic: be positive and forward looking.)
  1153. X.Ip $* 8 2
  1154. XSet to 1 to do multiline matching within a string, 0 to assume strings contain
  1155. Xa single line.
  1156. XDefault is 0.
  1157. X(Mnemonic: * matches multiple things.)
  1158. X.Ip $0 8
  1159. XContains the name of the file containing the
  1160. X.I perl
  1161. Xscript being executed.
  1162. XThe value should be copied elsewhere before any pattern matching happens, which
  1163. Xclobbers $0.
  1164. X(Mnemonic: same as sh and ksh.)
  1165. X.Ip $<digit> 8
  1166. XContains the subpattern from the corresponding set of parentheses in the last
  1167. Xpattern matched, not counting patterns matched in nested blocks that have
  1168. Xbeen exited already.
  1169. X(Mnemonic: like \edigit.)
  1170. X.Ip $[ 8 2
  1171. XThe index of the first element in an array, and of the first character in
  1172. Xa substring.
  1173. XDefault is 0, but you could set it to 1 to make
  1174. X.I perl
  1175. Xbehave more like
  1176. X.I awk
  1177. X(or Fortran)
  1178. Xwhen subscripting and when evaluating the index() and substr() functions.
  1179. X(Mnemonic: [ begins subscripts.)
  1180. X.Ip $! 8 2
  1181. XIf used in a numeric context, yields the current value of errno, with all the
  1182. Xusual caveats.
  1183. XIf used in a string context, yields the corresponding system error string.
  1184. XYou can assign to $! in order to set errno
  1185. Xif, for instance, you want $! to return the string for error n, or you want
  1186. Xto set the exit value for the die operator.
  1187. X(Mnemonic: What just went bang?)
  1188. X.Ip $@ 8 2
  1189. XThe error message from the last eval command.
  1190. XIf null, the last eval parsed and executed correctly.
  1191. X(Mnemonic: Where was the syntax error "at"?)
  1192. X.Ip $< 8 2
  1193. XThe real uid of this process.
  1194. X(Mnemonic: it's the uid you came FROM, if you're running setuid.)
  1195. X.Ip $> 8 2
  1196. XThe effective uid of this process.
  1197. XExample:
  1198. X.nf
  1199. X
  1200. X    $< = $>;    # set real uid to the effective uid
  1201. X
  1202. X.fi
  1203. X(Mnemonic: it's the uid you went TO, if you're running setuid.)
  1204. X.Ip $( 8 2
  1205. XThe real gid of this process.
  1206. XIf you are on a machine that supports membership in multiple groups
  1207. Xsimultaneously, gives a space separated list of groups you are in.
  1208. XThe first number is the one returned by getgid(), and the subsequent ones
  1209. Xby getgroups(), one of which may be the same as the first number.
  1210. X(Mnemonic: parens are used to GROUP things.
  1211. XThe real gid is the group you LEFT, if you're running setgid.)
  1212. X.Ip $) 8 2
  1213. XThe effective gid of this process.
  1214. XIf you are on a machine that supports membership in multiple groups
  1215. Xsimultaneously, gives a space separated list of groups you are in.
  1216. XThe first number is the one returned by getegid(), and the subsequent ones
  1217. Xby getgroups(), one of which may be the same as the first number.
  1218. X(Mnemonic: parens are used to GROUP things.
  1219. XThe effective gid is the group that's RIGHT for you, if you're running setgid.)
  1220. X.Sp
  1221. XNote: $<, $>, $( and $) can only be set on machines that support the
  1222. Xcorresponding set[re][ug]id() routine.
  1223. X.Ip @ARGV 8 3
  1224. XThe array ARGV contains the command line arguments intended for the script.
  1225. XNote that $#ARGV is the generally number of arguments minus one, since
  1226. X$ARGV[0] is the first argument, NOT the command name.
  1227. XSee $0 for the command name.
  1228. X.Ip @INC 8 3
  1229. XThe array INC contains the list of places to look for perl scripts to be
  1230. Xevaluated by the "do EXPR" command.
  1231. XIt initially consists of the arguments to any -I command line switches, followed
  1232. Xby the default perl library, probably "/usr/local/lib/perl".
  1233. X.Ip $ENV{expr} 8 2
  1234. XThe associative array ENV contains your current environment.
  1235. XSetting a value in ENV changes the environment for child processes.
  1236. X.Ip $SIG{expr} 8 2
  1237. XThe associative array SIG is used to set signal handlers for various signals.
  1238. XExample:
  1239. X.nf
  1240. X
  1241. X.ne 12
  1242. X    sub handler {    # 1st argument is signal name
  1243. X        local($sig) = @_;
  1244. X        print "Caught a SIG$sig--shutting down\en";
  1245. X        close(LOG);
  1246. X        exit(0);
  1247. X    }
  1248. X
  1249. X    $SIG{'INT'} = 'handler';
  1250. X    $SIG{'QUIT'} = 'handler';
  1251. X    .\|.\|.
  1252. X    $SIG{'INT'} = 'DEFAULT';    # restore default action
  1253. X    $SIG{'QUIT'} = 'IGNORE';    # ignore SIGQUIT
  1254. X
  1255. X.fi
  1256. X.SH ENVIRONMENT
  1257. X.I Perl
  1258. Xcurrently uses no environment variables, except to make them available
  1259. Xto the script being executed, and to child processes.
  1260. XHowever, scripts running setuid would do well to execute the following lines
  1261. Xbefore doing anything else, just to keep people honest:
  1262. X.nf
  1263. X
  1264. X.ne 3
  1265. X    $ENV{'PATH'} = '/bin:/usr/bin';    # or whatever you need
  1266. X    $ENV{'SHELL'} = '/bin/sh' if $ENV{'SHELL'};
  1267. X    $ENV{'IFS'} = '' if $ENV{'IFS'};
  1268. X
  1269. X.fi
  1270. X.SH AUTHOR
  1271. XLarry Wall <lwall@jpl-devvax.Jpl.Nasa.Gov>
  1272. X.SH FILES
  1273. X/tmp/perl\-eXXXXXX    temporary file for
  1274. X.B \-e
  1275. Xcommands.
  1276. X.SH SEE ALSO
  1277. Xa2p    awk to perl translator
  1278. X.br
  1279. Xs2p    sed to perl translator
  1280. X.br
  1281. Xperldb    interactive perl debugger
  1282. X.SH DIAGNOSTICS
  1283. XCompilation errors will tell you the line number of the error, with an
  1284. Xindication of the next token or token type that was to be examined.
  1285. X(In the case of a script passed to
  1286. X.I perl
  1287. Xvia
  1288. X.B \-e
  1289. Xswitches, each
  1290. X.B \-e
  1291. Xis counted as one line.)
  1292. X.SH TRAPS
  1293. XAccustomed awk users should take special note of the following:
  1294. X.Ip * 4 2
  1295. XSemicolons are required after all simple statements in perl.  Newline
  1296. Xis not a statement delimiter.
  1297. X.Ip * 4 2
  1298. XCurly brackets are required on ifs and whiles.
  1299. X.Ip * 4 2
  1300. XVariables begin with $ or @ in perl.
  1301. X.Ip * 4 2
  1302. XArrays index from 0 unless you set $[.
  1303. XLikewise string positions in substr() and index().
  1304. X.Ip * 4 2
  1305. XYou have to decide whether your array has numeric or string indices.
  1306. X.Ip * 4 2
  1307. XAssociative array values do not spring into existence upon mere reference.
  1308. X.Ip * 4 2
  1309. XYou have to decide whether you want to use string or numeric comparisons.
  1310. X.Ip * 4 2
  1311. XReading an input line does not split it for you.  You get to split it yourself
  1312. Xto an array.
  1313. XAnd split has different arguments.
  1314. X.Ip * 4 2
  1315. XThe current input line is normally in $_, not $0.
  1316. XIt generally does not have the newline stripped.
  1317. X($0 is initially the name of the program executed, then the last matched
  1318. Xstring.)
  1319. X.Ip * 4 2
  1320. XThe current filename is $ARGV, not $FILENAME.
  1321. XNR, RS, ORS, OFS, and OFMT have equivalents with other symbols.
  1322. XFS doesn't have an equivalent, since you have to be explicit about
  1323. Xsplit statements.
  1324. X.Ip * 4 2
  1325. X$<digit> does not refer to fields--it refers to substrings matched by the last
  1326. Xmatch pattern.
  1327. X.Ip * 4 2
  1328. XThe print statement does not add field and record separators unless you set
  1329. X$, and $\e.
  1330. X.Ip * 4 2
  1331. XYou must open your files before you print to them.
  1332. X.Ip * 4 2
  1333. XThe range operator is \*(L"..\*(R", not comma.
  1334. X(The comma operator works as in C.)
  1335. X.Ip * 4 2
  1336. XThe match operator is \*(L"=~\*(R", not \*(L"~\*(R".
  1337. X(\*(L"~\*(R" is the one's complement operator.)
  1338. X.Ip * 4 2
  1339. XThe concatenation operator is \*(L".\*(R", not the null string.
  1340. X(Using the null string would render \*(L"/pat/ /pat/\*(R" unparseable,
  1341. Xsince the third slash would be interpreted as a division operator\*(--the
  1342. Xtokener is in fact slightly context sensitive for operators like /, ?, and <.
  1343. XAnd in fact, . itself can be the beginning of a number.)
  1344. X.Ip * 4 2
  1345. XNext, exit, and continue work differently.
  1346. X.Ip * 4 2
  1347. XWhen in doubt, run the awk construct through a2p and see what it gives you.
  1348. X.PP
  1349. XCerebral C programmers should take note of the following:
  1350. X.Ip * 4 2
  1351. XCurly brackets are required on ifs and whiles.
  1352. X.Ip * 4 2
  1353. XYou should use \*(L"elsif\*(R" rather than \*(L"else if\*(R"
  1354. X.Ip * 4 2
  1355. XBreak and continue become last and next, respectively.
  1356. X.Ip * 4 2
  1357. XThere's no switch statement.
  1358. X.Ip * 4 2
  1359. XVariables begin with $ or @ in perl.
  1360. X.Ip * 4 2
  1361. XPrintf does not implement *.
  1362. X.Ip * 4 2
  1363. XComments begin with #, not /*.
  1364. X.Ip * 4 2
  1365. XYou can't take the address of anything.
  1366. X.Ip * 4 2
  1367. XARGV must be capitalized.
  1368. X.Ip * 4 2
  1369. XThe \*(L"system\*(R" calls link, unlink, rename, etc. return nonzero for success, not 0.
  1370. X.Ip * 4 2
  1371. XSignal handlers deal with signal names, not numbers.
  1372. X.PP
  1373. XSeasoned sed programmers should take note of the following:
  1374. X.Ip * 4 2
  1375. XBackreferences in substitutions use $ rather than \e.
  1376. X.Ip * 4 2
  1377. XThe pattern matching metacharacters (, ), and | do not have backslashes in front.
  1378. X.Ip * 4 2
  1379. XThe range operator is .. rather than comma.
  1380. X.PP
  1381. XSharp shell programmers should take note of the following:
  1382. X.Ip * 4 2
  1383. XThe backtick operator does variable interpretation without regard to the
  1384. Xpresence of single quotes in the command.
  1385. X.Ip * 4 2
  1386. XThe backtick operator does no translation of the return value, unlike csh.
  1387. X.Ip * 4 2
  1388. XShells (especially csh) do several levels of substitution on each command line.
  1389. XPerl does substitution only in certain constructs such as double quotes,
  1390. Xbackticks, angle brackets and search patterns.
  1391. X.Ip * 4 2
  1392. XShells interpret scripts a little bit at a time.
  1393. XPerl compiles the whole program before executing it.
  1394. X.Ip * 4 2
  1395. XThe arguments are available via @ARGV, not $1, $2, etc.
  1396. X.Ip * 4 2
  1397. XThe environment is not automatically made available as variables.
  1398. X.SH BUGS
  1399. X.PP
  1400. XYou can't currently dereference arrays or array elements inside a
  1401. Xdouble-quoted string.
  1402. XYou must assign them to a scalar and interpolate that.
  1403. X.PP
  1404. XAssociative arrays really ought to be first class objects.
  1405. X.PP
  1406. XPerl is at the mercy of the C compiler's definitions of various operations
  1407. Xsuch as % and atof().
  1408. XIn particular, don't trust % on negative numbers.
  1409. X.PP
  1410. X.I Perl
  1411. Xactually stands for Pathologically Eclectic Rubbish Lister, but don't tell
  1412. Xanyone I said that.
  1413. X.rn }` ''
  1414. !STUFFY!FUNK!
  1415. echo Extracting stab.h
  1416. sed >stab.h <<'!STUFFY!FUNK!' -e 's/X//'
  1417. X/* $Header: stab.h,v 2.0 88/06/05 00:11:05 root Exp $
  1418. X *
  1419. X * $Log:    stab.h,v $
  1420. X * Revision 2.0  88/06/05  00:11:05  root
  1421. X * Baseline version 2.0.
  1422. X * 
  1423. X */
  1424. X
  1425. Xstruct stab {
  1426. X    struct stab *stab_next;
  1427. X    char    *stab_name;
  1428. X    STR        *stab_val;
  1429. X    struct stio *stab_io;
  1430. X    FCMD    *stab_form;
  1431. X    ARRAY    *stab_array;
  1432. X    HASH    *stab_hash;
  1433. X    SUBR    *stab_sub;
  1434. X    char    stab_flags;
  1435. X};
  1436. X
  1437. X#define SF_VMAGIC 1        /* call routine to dereference STR val */
  1438. X#define SF_MULTI 2        /* seen more than once */
  1439. X
  1440. Xstruct stio {
  1441. X    FILE    *fp;
  1442. X    long    lines;
  1443. X    long    page;
  1444. X    long    page_len;
  1445. X    long    lines_left;
  1446. X    char    *top_name;
  1447. X    STAB    *top_stab;
  1448. X    char    *fmt_name;
  1449. X    STAB    *fmt_stab;
  1450. X    short    subprocess;
  1451. X    char    type;
  1452. X    char    flags;
  1453. X};
  1454. X
  1455. X#define IOF_ARGV 1    /* this fp iterates over ARGV */
  1456. X#define IOF_START 2    /* check for null ARGV and substitute '-' */
  1457. X#define IOF_FLUSH 4    /* this fp wants a flush after write op */
  1458. X
  1459. Xstruct sub {
  1460. X    CMD        *cmd;
  1461. X    char    *filename;
  1462. X    long    depth;    /* >= 2 indicates recursive call */
  1463. X    ARRAY    *tosave;
  1464. X};
  1465. X
  1466. X#define Nullstab Null(STAB*)
  1467. X
  1468. X#define STAB_STR(s) (tmpstab = (s), tmpstab->stab_flags & SF_VMAGIC ? stab_str(tmpstab) : tmpstab->stab_val)
  1469. X#define STAB_GET(s) (tmpstab = (s), str_get(tmpstab->stab_flags & SF_VMAGIC ? stab_str(tmpstab) : tmpstab->stab_val))
  1470. X#define STAB_GNUM(s) (tmpstab = (s), str_gnum(tmpstab->stab_flags & SF_VMAGIC ? stab_str(tmpstab) : tmpstab->stab_val))
  1471. X
  1472. XEXT STAB *tmpstab;
  1473. X
  1474. XEXT STAB *stab_index[128];
  1475. X
  1476. XEXT char *envname;    /* place for ENV name being assigned--gross cheat */
  1477. XEXT char *signame;    /* place for SIG name being assigned--gross cheat */
  1478. X
  1479. XEXT unsigned short statusvalue;
  1480. X
  1481. XSTAB *aadd();
  1482. XSTAB *hadd();
  1483. !STUFFY!FUNK!
  1484. echo ""
  1485. echo "End of kit 3 (of 15)"
  1486. cat /dev/null >kit3isdone
  1487. run=''
  1488. config=''
  1489. for iskit in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
  1490.     if test -f kit${iskit}isdone; then
  1491.     run="$run $iskit"
  1492.     else
  1493.     todo="$todo $iskit"
  1494.     fi
  1495. done
  1496. case $todo in
  1497.     '')
  1498.     echo "You have run all your kits.  Please read README and then type Configure."
  1499.     chmod 755 Configure
  1500.     ;;
  1501.     *)  echo "You have run$run."
  1502.     echo "You still need to run$todo."
  1503.     ;;
  1504. esac
  1505. : Someone might mail this, so...
  1506. exit
  1507.  
  1508.