home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume18 / perl / part05 < prev    next >
Internet Message Format  |  1991-04-14  |  51KB

  1. From: lwall@netlabs.com (Larry Wall)
  2. Newsgroups: comp.sources.misc
  3. Subject: v18i023:  perl - The perl programming language, Part05/36
  4. Message-ID: <1991Apr15.015323.6720@sparky.IMD.Sterling.COM>
  5. Date: 15 Apr 91 01:53:23 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7. X-Checksum-Snefru: be7faf3e 21cb7867 f5dfc446 e5eac8ab
  8.  
  9. Submitted-by: Larry Wall <lwall@netlabs.com>
  10. Posting-number: Volume 18, Issue 23
  11. Archive-name: perl/part05
  12.  
  13. [There are 36 kits for perl version 4.0.]
  14.  
  15. #! /bin/sh
  16.  
  17. # Make a new directory for the perl sources, cd to it, and run kits 1
  18. # thru 36 through sh.  When all 36 kits have been run, read README.
  19.  
  20. echo "This is perl 4.0 kit 5 (of 36).  If kit 5 is complete, the line"
  21. echo '"'"End of kit 5 (of 36)"'" will echo at the end.'
  22. echo ""
  23. export PATH || (echo "You didn't use sh, you clunch." ; kill $$)
  24. mkdir  2>/dev/null
  25. echo Extracting perl.man:AB
  26. sed >perl.man:AB <<'!STUFFY!FUNK!' -e 's/X//'
  27. X''' 
  28. X''' Revision 3.0.1.10  90/11/10  01:46:29  lwall
  29. X''' patch38: random cleanup
  30. X''' patch38: added alarm function
  31. X''' 
  32. X''' Revision 3.0.1.9  90/10/15  18:17:37  lwall
  33. X''' patch29: added caller
  34. X''' patch29: index and substr now have optional 3rd args
  35. X''' patch29: added SysV IPC
  36. X''' 
  37. X''' Revision 3.0.1.8  90/08/13  22:21:00  lwall
  38. X''' patch28: documented that you can't interpolate $) or $| in pattern
  39. X''' 
  40. X''' Revision 3.0.1.7  90/08/09  04:27:04  lwall
  41. X''' patch19: added require operator
  42. X''' 
  43. X''' Revision 3.0.1.6  90/08/03  11:15:29  lwall
  44. X''' patch19: Intermediate diffs for Randal
  45. X''' 
  46. X''' Revision 3.0.1.5  90/03/27  16:15:17  lwall
  47. X''' patch16: MSDOS support
  48. X''' 
  49. X''' Revision 3.0.1.4  90/03/12  16:46:02  lwall
  50. X''' patch13: documented behavior of @array = /noparens/
  51. X''' 
  52. X''' Revision 3.0.1.3  90/02/28  17:55:58  lwall
  53. X''' patch9: grep now returns number of items matched in scalar context
  54. X''' patch9: documented in-place modification capabilites of grep
  55. X''' 
  56. X''' Revision 3.0.1.2  89/11/17  15:30:16  lwall
  57. X''' patch5: fixed some manual typos and indent problems
  58. X''' 
  59. X''' Revision 3.0.1.1  89/11/11  04:43:10  lwall
  60. X''' patch2: made some line breaks depend on troff vs. nroff
  61. X''' patch2: example of unshift had args backwards
  62. X''' 
  63. X''' Revision 3.0  89/10/18  15:21:37  lwall
  64. X''' 3.0 baseline
  65. X''' 
  66. X'''
  67. X.PP
  68. XAlong with the literals and variables mentioned earlier,
  69. Xthe operations in the following section can serve as terms in an expression.
  70. XSome of these operations take a LIST as an argument.
  71. XSuch a list can consist of any combination of scalar arguments or array values;
  72. Xthe array values will be included in the list as if each individual element were
  73. Xinterpolated at that point in the list, forming a longer single-dimensional
  74. Xarray value.
  75. XElements of the LIST should be separated by commas.
  76. XIf an operation is listed both with and without parentheses around its
  77. Xarguments, it means you can either use it as a unary operator or
  78. Xas a function call.
  79. XTo use it as a function call, the next token on the same line must
  80. Xbe a left parenthesis.
  81. X(There may be intervening white space.)
  82. XSuch a function then has highest precedence, as you would expect from
  83. Xa function.
  84. XIf any token other than a left parenthesis follows, then it is a
  85. Xunary operator, with a precedence depending only on whether it is a LIST
  86. Xoperator or not.
  87. XLIST operators have lowest precedence.
  88. XAll other unary operators have a precedence greater than relational operators
  89. Xbut less than arithmetic operators.
  90. XSee the section on Precedence.
  91. X.Ip "/PATTERN/" 8 4
  92. XSee m/PATTERN/.
  93. X.Ip "?PATTERN?" 8 4
  94. XThis is just like the /pattern/ search, except that it matches only once between
  95. Xcalls to the
  96. X.I reset
  97. Xoperator.
  98. XThis is a useful optimization when you only want to see the first occurrence of
  99. Xsomething in each file of a set of files, for instance.
  100. XOnly ?? patterns local to the current package are reset.
  101. X.Ip "accept(NEWSOCKET,GENERICSOCKET)" 8 2
  102. XDoes the same thing that the accept system call does.
  103. XReturns true if it succeeded, false otherwise.
  104. XSee example in section on Interprocess Communication.
  105. X.Ip "alarm(SECONDS)" 8 4
  106. X.Ip "alarm SECONDS" 8
  107. XArranges to have a SIGALRM delivered to this process after the specified number
  108. Xof seconds (minus 1, actually) have elapsed.  Thus, alarm(15) will cause
  109. Xa SIGALRM at some point more than 14 seconds in the future.
  110. XOnly one timer may be counting at once.  Each call disables the previous
  111. Xtimer, and an argument of 0 may be supplied to cancel the previous timer
  112. Xwithout starting a new one.
  113. XThe returned value is the amount of time remaining on the previous timer.
  114. X.Ip "atan2(Y,X)" 8 2
  115. XReturns the arctangent of Y/X in the range
  116. X.if t \-\(*p to \(*p.
  117. X.if n \-PI to PI.
  118. X.Ip "bind(SOCKET,NAME)" 8 2
  119. XDoes the same thing that the bind system call does.
  120. XReturns true if it succeeded, false otherwise.
  121. XNAME should be a packed address of the proper type for the socket.
  122. XSee example in section on Interprocess Communication.
  123. X.Ip "binmode(FILEHANDLE)" 8 4
  124. X.Ip "binmode FILEHANDLE" 8 4
  125. XArranges for the file to be read in \*(L"binary\*(R" mode in operating systems
  126. Xthat distinguish between binary and text files.
  127. XFiles that are not read in binary mode have CR LF sequences translated
  128. Xto LF on input and LF translated to CR LF on output.
  129. XBinmode has no effect under Unix.
  130. XIf FILEHANDLE is an expression, the value is taken as the name of
  131. Xthe filehandle.
  132. X.Ip "caller(EXPR)"
  133. X.Ip "caller"
  134. XReturns the context of the current subroutine call:
  135. X.nf
  136. X
  137. X    ($package,$filename,$line) = caller;
  138. X
  139. X.fi
  140. XWith EXPR, returns some extra information that the debugger uses to print
  141. Xa stack trace.  The value of EXPR indicates how many call frames to go
  142. Xback before the current one.
  143. X.Ip "chdir(EXPR)" 8 2
  144. X.Ip "chdir EXPR" 8 2
  145. XChanges the working directory to EXPR, if possible.
  146. XIf EXPR is omitted, changes to home directory.
  147. XReturns 1 upon success, 0 otherwise.
  148. XSee example under
  149. X.IR die .
  150. X.Ip "chmod(LIST)" 8 2
  151. X.Ip "chmod LIST" 8 2
  152. XChanges the permissions of a list of files.
  153. XThe first element of the list must be the numerical mode.
  154. XReturns the number of files successfully changed.
  155. X.nf
  156. X
  157. X.ne 2
  158. X    $cnt = chmod 0755, \'foo\', \'bar\';
  159. X    chmod 0755, @executables;
  160. X
  161. X.fi
  162. X.Ip "chop(LIST)" 8 7
  163. X.Ip "chop(VARIABLE)" 8
  164. X.Ip "chop VARIABLE" 8
  165. X.Ip "chop" 8
  166. XChops off the last character of a string and returns the character chopped.
  167. XIt's used primarily to remove the newline from the end of an input record,
  168. Xbut is much more efficient than s/\en// because it neither scans nor copies
  169. Xthe string.
  170. XIf VARIABLE is omitted, chops $_.
  171. XExample:
  172. X.nf
  173. X
  174. X.ne 5
  175. X    while (<>) {
  176. X        chop;    # avoid \en on last field
  177. X        @array = split(/:/);
  178. X        .\|.\|.
  179. X    }
  180. X
  181. X.fi
  182. XYou can actually chop anything that's an lvalue, including an assignment:
  183. X.nf
  184. X
  185. X    chop($cwd = \`pwd\`);
  186. X    chop($answer = <STDIN>);
  187. X
  188. X.fi
  189. XIf you chop a list, each element is chopped.
  190. XOnly the value of the last chop is returned.
  191. X.Ip "chown(LIST)" 8 2
  192. X.Ip "chown LIST" 8 2
  193. XChanges the owner (and group) of a list of files.
  194. XThe first two elements of the list must be the NUMERICAL uid and gid,
  195. Xin that order.
  196. XReturns the number of files successfully changed.
  197. X.nf
  198. X
  199. X.ne 2
  200. X    $cnt = chown $uid, $gid, \'foo\', \'bar\';
  201. X    chown $uid, $gid, @filenames;
  202. X
  203. X.fi
  204. X.ne 23
  205. XHere's an example of looking up non-numeric uids:
  206. X.nf
  207. X
  208. X    print "User: ";
  209. X    $user = <STDIN>;
  210. X    chop($user);
  211. X    print "Files: "
  212. X    $pattern = <STDIN>;
  213. X    chop($pattern);
  214. X.ie t \{\
  215. X    open(pass, \'/etc/passwd\') || die "Can't open passwd: $!\en";
  216. X'br\}
  217. X.el \{\
  218. X    open(pass, \'/etc/passwd\')
  219. X        || die "Can't open passwd: $!\en";
  220. X'br\}
  221. X    while (<pass>) {
  222. X        ($login,$pass,$uid,$gid) = split(/:/);
  223. X        $uid{$login} = $uid;
  224. X        $gid{$login} = $gid;
  225. X    }
  226. X    @ary = <${pattern}>;    # get filenames
  227. X    if ($uid{$user} eq \'\') {
  228. X        die "$user not in passwd file";
  229. X    }
  230. X    else {
  231. X        chown $uid{$user}, $gid{$user}, @ary;
  232. X    }
  233. X
  234. X.fi
  235. X.Ip "chroot(FILENAME)" 8 5
  236. X.Ip "chroot FILENAME" 8
  237. XDoes the same as the system call of that name.
  238. XIf you don't know what it does, don't worry about it.
  239. XIf FILENAME is omitted, does chroot to $_.
  240. X.Ip "close(FILEHANDLE)" 8 5
  241. X.Ip "close FILEHANDLE" 8
  242. XCloses the file or pipe associated with the file handle.
  243. XYou don't have to close FILEHANDLE if you are immediately going to
  244. Xdo another open on it, since open will close it for you.
  245. X(See
  246. X.IR open .)
  247. XHowever, an explicit close on an input file resets the line counter ($.), while
  248. Xthe implicit close done by
  249. X.I open
  250. Xdoes not.
  251. XAlso, closing a pipe will wait for the process executing on the pipe to complete,
  252. Xin case you want to look at the output of the pipe afterwards.
  253. XClosing a pipe explicitly also puts the status value of the command into $?.
  254. XExample:
  255. X.nf
  256. X
  257. X.ne 4
  258. X    open(OUTPUT, \'|sort >foo\');    # pipe to sort
  259. X    .\|.\|.    # print stuff to output
  260. X    close OUTPUT;        # wait for sort to finish
  261. X    open(INPUT, \'foo\');    # get sort's results
  262. X
  263. X.fi
  264. XFILEHANDLE may be an expression whose value gives the real filehandle name.
  265. X.Ip "closedir(DIRHANDLE)" 8 5
  266. X.Ip "closedir DIRHANDLE" 8
  267. XCloses a directory opened by opendir().
  268. X.Ip "connect(SOCKET,NAME)" 8 2
  269. XDoes the same thing that the connect system call does.
  270. XReturns true if it succeeded, false otherwise.
  271. XNAME should be a package address of the proper type for the socket.
  272. XSee example in section on Interprocess Communication.
  273. X.Ip "cos(EXPR)" 8 6
  274. X.Ip "cos EXPR" 8 6
  275. XReturns the cosine of EXPR (expressed in radians).
  276. XIf EXPR is omitted takes cosine of $_.
  277. X.Ip "crypt(PLAINTEXT,SALT)" 8 6
  278. XEncrypts a string exactly like the crypt() function in the C library.
  279. XUseful for checking the password file for lousy passwords.
  280. XOnly the guys wearing white hats should do this.
  281. X.Ip "dbmclose(ASSOC_ARRAY)" 8 6
  282. X.Ip "dbmclose ASSOC_ARRAY" 8
  283. XBreaks the binding between a dbm file and an associative array.
  284. XThe values remaining in the associative array are meaningless unless
  285. Xyou happen to want to know what was in the cache for the dbm file.
  286. XThis function is only useful if you have ndbm.
  287. X.Ip "dbmopen(ASSOC,DBNAME,MODE)" 8 6
  288. XThis binds a dbm or ndbm file to an associative array.
  289. XASSOC is the name of the associative array.
  290. X(Unlike normal open, the first argument is NOT a filehandle, even though
  291. Xit looks like one).
  292. XDBNAME is the name of the database (without the .dir or .pag extension).
  293. XIf the database does not exist, it is created with protection specified
  294. Xby MODE (as modified by the umask).
  295. XIf your system only supports the older dbm functions, you may only have one
  296. Xdbmopen in your program.
  297. XIf your system has neither dbm nor ndbm, calling dbmopen produces a fatal
  298. Xerror.
  299. X.Sp
  300. XValues assigned to the associative array prior to the dbmopen are lost.
  301. XA certain number of values from the dbm file are cached in memory.
  302. XBy default this number is 64, but you can increase it by preallocating
  303. Xthat number of garbage entries in the associative array before the dbmopen.
  304. XYou can flush the cache if necessary with the reset command.
  305. X.Sp
  306. XIf you don't have write access to the dbm file, you can only read
  307. Xassociative array variables, not set them.
  308. XIf you want to test whether you can write, either use file tests or
  309. Xtry setting a dummy array entry inside an eval, which will trap the error.
  310. X.Sp
  311. XNote that functions such as keys() and values() may return huge array values
  312. Xwhen used on large dbm files.
  313. XYou may prefer to use the each() function to iterate over large dbm files.
  314. XExample:
  315. X.nf
  316. X
  317. X.ne 6
  318. X    # print out history file offsets
  319. X    dbmopen(HIST,'/usr/lib/news/history',0666);
  320. X    while (($key,$val) = each %HIST) {
  321. X        print $key, ' = ', unpack('L',$val), "\en";
  322. X    }
  323. X    dbmclose(HIST);
  324. X
  325. X.fi
  326. X.Ip "defined(EXPR)" 8 6
  327. X.Ip "defined EXPR" 8
  328. XReturns a boolean value saying whether the lvalue EXPR has a real value
  329. Xor not.
  330. XMany operations return the undefined value under exceptional conditions,
  331. Xsuch as end of file, uninitialized variable, system error and such.
  332. XThis function allows you to distinguish between an undefined null string
  333. Xand a defined null string with operations that might return a real null
  334. Xstring, in particular referencing elements of an array.
  335. XYou may also check to see if arrays or subroutines exist.
  336. XUse on predefined variables is not guaranteed to produce intuitive results.
  337. XExamples:
  338. X.nf
  339. X
  340. X.ne 7
  341. X    print if defined $switch{'D'};
  342. X    print "$val\en" while defined($val = pop(@ary));
  343. X    die "Can't readlink $sym: $!"
  344. X        unless defined($value = readlink $sym);
  345. X    eval '@foo = ()' if defined(@foo);
  346. X    die "No XYZ package defined" unless defined %_XYZ;
  347. X    sub foo { defined &bar ? &bar(@_) : die "No bar"; }
  348. X
  349. X.fi
  350. XSee also undef.
  351. X.Ip "delete $ASSOC{KEY}" 8 6
  352. XDeletes the specified value from the specified associative array.
  353. XReturns the deleted value, or the undefined value if nothing was deleted.
  354. XDeleting from $ENV{} modifies the environment.
  355. XDeleting from an array bound to a dbm file deletes the entry from the dbm
  356. Xfile.
  357. X.Sp
  358. XThe following deletes all the values of an associative array:
  359. X.nf
  360. X
  361. X.ne 3
  362. X    foreach $key (keys %ARRAY) {
  363. X        delete $ARRAY{$key};
  364. X    }
  365. X
  366. X.fi
  367. X(But it would be faster to use the
  368. X.I reset
  369. Xcommand.
  370. XSaying undef %ARRAY is faster yet.)
  371. X.Ip "die(LIST)" 8
  372. X.Ip "die LIST" 8
  373. XOutside of an eval, prints the value of LIST to
  374. X.I STDERR
  375. Xand exits with the current value of $!
  376. X(errno).
  377. XIf $! is 0, exits with the value of ($? >> 8) (\`command\` status).
  378. XIf ($? >> 8) is 0, exits with 255.
  379. XInside an eval, the error message is stuffed into $@ and the eval is terminated
  380. Xwith the undefined value.
  381. X.Sp
  382. XEquivalent examples:
  383. X.nf
  384. X
  385. X.ne 3
  386. X.ie t \{\
  387. X    die "Can't cd to spool: $!\en" unless chdir \'/usr/spool/news\';
  388. X'br\}
  389. X.el \{\
  390. X    die "Can't cd to spool: $!\en"
  391. X        unless chdir \'/usr/spool/news\';
  392. X'br\}
  393. X
  394. X    chdir \'/usr/spool/news\' || die "Can't cd to spool: $!\en" 
  395. X
  396. X.fi
  397. X.Sp
  398. XIf the value of EXPR does not end in a newline, the current script line
  399. Xnumber and input line number (if any) are also printed, and a newline is
  400. Xsupplied.
  401. XHint: sometimes appending \*(L", stopped\*(R" to your message will cause it to make
  402. Xbetter sense when the string \*(L"at foo line 123\*(R" is appended.
  403. XSuppose you are running script \*(L"canasta\*(R".
  404. X.nf
  405. X
  406. X.ne 7
  407. X    die "/etc/games is no good";
  408. X    die "/etc/games is no good, stopped";
  409. X
  410. Xproduce, respectively
  411. X
  412. X    /etc/games is no good at canasta line 123.
  413. X    /etc/games is no good, stopped at canasta line 123.
  414. X
  415. X.fi
  416. XSee also
  417. X.IR exit .
  418. X.Ip "do BLOCK" 8 4
  419. XReturns the value of the last command in the sequence of commands indicated
  420. Xby BLOCK.
  421. XWhen modified by a loop modifier, executes the BLOCK once before testing the
  422. Xloop condition.
  423. X(On other statements the loop modifiers test the conditional first.)
  424. X.Ip "do SUBROUTINE (LIST)" 8 3
  425. XExecutes a SUBROUTINE declared by a
  426. X.I sub
  427. Xdeclaration, and returns the value
  428. Xof the last expression evaluated in SUBROUTINE.
  429. XIf there is no subroutine by that name, produces a fatal error.
  430. X(You may use the \*(L"defined\*(R" operator to determine if a subroutine
  431. Xexists.)
  432. XIf you pass arrays as part of LIST you may wish to pass the length
  433. Xof the array in front of each array.
  434. X(See the section on subroutines later on.)
  435. XSUBROUTINE may be a scalar variable, in which case the variable contains
  436. Xthe name of the subroutine to execute.
  437. XThe parentheses are required to avoid confusion with the \*(L"do EXPR\*(R"
  438. Xform.
  439. X.Sp
  440. XAs an alternate form, you may call a subroutine by prefixing the name with
  441. Xan ampersand: &foo(@args).
  442. XIf you aren't passing any arguments, you don't have to use parentheses.
  443. XIf you omit the parentheses, no @_ array is passed to the subroutine.
  444. XThe & form is also used to specify subroutines to the defined and undef
  445. Xoperators.
  446. X.Ip "do EXPR" 8 3
  447. XUses the value of EXPR as a filename and executes the contents of the file
  448. Xas a
  449. X.I perl
  450. Xscript.
  451. XIts primary use is to include subroutines from a
  452. X.I perl
  453. Xsubroutine library.
  454. X.nf
  455. X
  456. X    do \'stat.pl\';
  457. X
  458. Xis just like
  459. X
  460. X    eval \`cat stat.pl\`;
  461. X
  462. X.fi
  463. Xexcept that it's more efficient, more concise, keeps track of the current
  464. Xfilename for error messages, and searches all the
  465. X.B \-I
  466. Xlibraries if the file
  467. Xisn't in the current directory (see also the @INC array in Predefined Names).
  468. XIt's the same, however, in that it does reparse the file every time you
  469. Xcall it, so if you are going to use the file inside a loop you might prefer
  470. Xto use \-P and #include, at the expense of a little more startup time.
  471. X(The main problem with #include is that cpp doesn't grok # comments\*(--a
  472. Xworkaround is to use \*(L";#\*(R" for standalone comments.)
  473. XNote that the following are NOT equivalent:
  474. X.nf
  475. X
  476. X.ne 2
  477. X    do $foo;    # eval a file
  478. X    do $foo();    # call a subroutine
  479. X
  480. X.fi
  481. XNote that inclusion of library routines is better done with
  482. Xthe \*(L"require\*(R" operator.
  483. X.Ip "dump LABEL" 8 6
  484. XThis causes an immediate core dump.
  485. XPrimarily this is so that you can use the undump program to turn your
  486. Xcore dump into an executable binary after having initialized all your
  487. Xvariables at the beginning of the program.
  488. XWhen the new binary is executed it will begin by executing a "goto LABEL"
  489. X(with all the restrictions that goto suffers).
  490. XThink of it as a goto with an intervening core dump and reincarnation.
  491. XIf LABEL is omitted, restarts the program from the top.
  492. XWARNING: any files opened at the time of the dump will NOT be open any more
  493. Xwhen the program is reincarnated, with possible resulting confusion on the part
  494. Xof perl.
  495. XSee also \-u.
  496. X.Sp
  497. XExample:
  498. X.nf
  499. X
  500. X.ne 16
  501. X    #!/usr/bin/perl
  502. X    require 'getopt.pl';
  503. X    require 'stat.pl';
  504. X    %days = (
  505. X        'Sun',1,
  506. X        'Mon',2,
  507. X        'Tue',3,
  508. X        'Wed',4,
  509. X        'Thu',5,
  510. X        'Fri',6,
  511. X        'Sat',7);
  512. X
  513. X    dump QUICKSTART if $ARGV[0] eq '-d';
  514. X
  515. X    QUICKSTART:
  516. X    do Getopt('f');
  517. X
  518. X.fi
  519. X.Ip "each(ASSOC_ARRAY)" 8 6
  520. X.Ip "each ASSOC_ARRAY" 8
  521. XReturns a 2 element array consisting of the key and value for the next
  522. Xvalue of an associative array, so that you can iterate over it.
  523. XEntries are returned in an apparently random order.
  524. XWhen the array is entirely read, a null array is returned (which when
  525. Xassigned produces a FALSE (0) value).
  526. XThe next call to each() after that will start iterating again.
  527. XThe iterator can be reset only by reading all the elements from the array.
  528. XYou must not modify the array while iterating over it.
  529. XThere is a single iterator for each associative array, shared by all
  530. Xeach(), keys() and values() function calls in the program.
  531. XThe following prints out your environment like the printenv program, only
  532. Xin a different order:
  533. X.nf
  534. X
  535. X.ne 3
  536. X    while (($key,$value) = each %ENV) {
  537. X        print "$key=$value\en";
  538. X    }
  539. X
  540. X.fi
  541. XSee also keys() and values().
  542. X.Ip "eof(FILEHANDLE)" 8 8
  543. X.Ip "eof()" 8
  544. X.Ip "eof" 8
  545. XReturns 1 if the next read on FILEHANDLE will return end of file, or if
  546. XFILEHANDLE is not open.
  547. XFILEHANDLE may be an expression whose value gives the real filehandle name.
  548. X(Note that this function actually reads a character and then ungetc's it,
  549. Xso it is not very useful in an interactive context.)
  550. XAn eof without an argument returns the eof status for the last file read.
  551. XEmpty parentheses () may be used to indicate the pseudo file formed of the
  552. Xfiles listed on the command line, i.e. eof() is reasonable to use inside
  553. Xa while (<>) loop to detect the end of only the last file.
  554. XUse eof(ARGV) or eof without the parentheses to test EACH file in a while (<>) loop.
  555. XExamples:
  556. X.nf
  557. X
  558. X.ne 7
  559. X    # insert dashes just before last line of last file
  560. X    while (<>) {
  561. X        if (eof()) {
  562. X            print "\-\|\-\|\-\|\-\|\-\|\-\|\-\|\-\|\-\|\-\|\-\|\-\|\-\|\-\en";
  563. X        }
  564. X        print;
  565. X    }
  566. X
  567. X.ne 7
  568. X    # reset line numbering on each input file
  569. X    while (<>) {
  570. X        print "$.\et$_";
  571. X        if (eof) {    # Not eof().
  572. X            close(ARGV);
  573. X        }
  574. X    }
  575. X
  576. X.fi
  577. X.Ip "eval(EXPR)" 8 6
  578. X.Ip "eval EXPR" 8 6
  579. XEXPR is parsed and executed as if it were a little
  580. X.I perl
  581. Xprogram.
  582. XIt is executed in the context of the current
  583. X.I perl
  584. Xprogram, so that
  585. Xany variable settings, subroutine or format definitions remain afterwards.
  586. XThe value returned is the value of the last expression evaluated, just
  587. Xas with subroutines.
  588. XIf there is a syntax error or runtime error, or a die statement is
  589. Xexecuted, an undefined value is returned by
  590. Xeval, and $@ is set to the error message.
  591. XIf there was no error, $@ is guaranteed to be a null string.
  592. XIf EXPR is omitted, evaluates $_.
  593. XThe final semicolon, if any, may be omitted from the expression.
  594. X.Sp
  595. XNote that, since eval traps otherwise-fatal errors, it is useful for
  596. Xdetermining whether a particular feature
  597. X(such as dbmopen or symlink) is implemented.
  598. XIt is also Perl's exception trapping mechanism, where the die operator is
  599. Xused to raise exceptions.
  600. X.Ip "exec(LIST)" 8 8
  601. X.Ip "exec LIST" 8 6
  602. XIf there is more than one argument in LIST, or if LIST is an array with
  603. Xmore than one value,
  604. Xcalls execvp() with the arguments in LIST.
  605. XIf there is only one scalar argument, the argument is checked for shell metacharacters.
  606. XIf there are any, the entire argument is passed to \*(L"/bin/sh \-c\*(R" for parsing.
  607. XIf there are none, the argument is split into words and passed directly to
  608. Xexecvp(), which is more efficient.
  609. XNote: exec (and system) do not flush your output buffer, so you may need to
  610. Xset $| to avoid lost output.
  611. XExamples:
  612. X.nf
  613. X
  614. X    exec \'/bin/echo\', \'Your arguments are: \', @ARGV;
  615. X    exec "sort $outfile | uniq";
  616. X
  617. X.fi
  618. X.Sp
  619. XIf you don't really want to execute the first argument, but want to lie
  620. Xto the program you are executing about its own name, you can specify
  621. Xthe program you actually want to run by assigning that to a variable and
  622. Xputting the name of the variable in front of the LIST without a comma.
  623. X(This always forces interpretation of the LIST as a multi-valued list, even
  624. Xif there is only a single scalar in the list.)
  625. XExample:
  626. X.nf
  627. X
  628. X.ne 2
  629. X    $shell = '/bin/csh';
  630. X    exec $shell '-sh';        # pretend it's a login shell
  631. X
  632. X.fi
  633. X.Ip "exit(EXPR)" 8 6
  634. X.Ip "exit EXPR" 8
  635. XEvaluates EXPR and exits immediately with that value.
  636. XExample:
  637. X.nf
  638. X
  639. X.ne 2
  640. X    $ans = <STDIN>;
  641. X    exit 0 \|if \|$ans \|=~ \|/\|^[Xx]\|/\|;
  642. X
  643. X.fi
  644. XSee also
  645. X.IR die .
  646. XIf EXPR is omitted, exits with 0 status.
  647. X.Ip "exp(EXPR)" 8 3
  648. X.Ip "exp EXPR" 8
  649. XReturns
  650. X.I e
  651. Xto the power of EXPR.
  652. XIf EXPR is omitted, gives exp($_).
  653. X.Ip "fcntl(FILEHANDLE,FUNCTION,SCALAR)" 8 4
  654. XImplements the fcntl(2) function.
  655. XYou'll probably have to say
  656. X.nf
  657. X
  658. X    require "fcntl.ph";    # probably /usr/local/lib/perl/fcntl.ph
  659. X
  660. X.fi
  661. Xfirst to get the correct function definitions.
  662. XIf fcntl.ph doesn't exist or doesn't have the correct definitions
  663. Xyou'll have to roll
  664. Xyour own, based on your C header files such as <sys/fcntl.h>.
  665. X(There is a perl script called h2ph that comes with the perl kit
  666. Xwhich may help you in this.)
  667. XArgument processing and value return works just like ioctl below.
  668. XNote that fcntl will produce a fatal error if used on a machine that doesn't implement
  669. Xfcntl(2).
  670. X.Ip "fileno(FILEHANDLE)" 8 4
  671. X.Ip "fileno FILEHANDLE" 8 4
  672. XReturns the file descriptor for a filehandle.
  673. XUseful for constructing bitmaps for select().
  674. XIf FILEHANDLE is an expression, the value is taken as the name of
  675. Xthe filehandle.
  676. X.Ip "flock(FILEHANDLE,OPERATION)" 8 4
  677. XCalls flock(2) on FILEHANDLE.
  678. XSee manual page for flock(2) for definition of OPERATION.
  679. XReturns true for success, false on failure.
  680. XWill produce a fatal error if used on a machine that doesn't implement
  681. Xflock(2).
  682. XHere's a mailbox appender for BSD systems.
  683. X.nf
  684. X
  685. X.ne 20
  686. X    $LOCK_SH = 1;
  687. X    $LOCK_EX = 2;
  688. X    $LOCK_NB = 4;
  689. X    $LOCK_UN = 8;
  690. X
  691. X    sub lock {
  692. X        flock(MBOX,$LOCK_EX);
  693. X        # and, in case someone appended
  694. X        # while we were waiting...
  695. X        seek(MBOX, 0, 2);
  696. X    }
  697. X
  698. X    sub unlock {
  699. X        flock(MBOX,$LOCK_UN);
  700. X    }
  701. X
  702. X    open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
  703. X        || die "Can't open mailbox: $!";
  704. X
  705. X    do lock();
  706. X    print MBOX $msg,"\en\en";
  707. X    do unlock();
  708. X
  709. X.fi
  710. X.Ip "fork" 8 4
  711. XDoes a fork() call.
  712. XReturns the child pid to the parent process and 0 to the child process.
  713. XNote: unflushed buffers remain unflushed in both processes, which means
  714. Xyou may need to set $| to avoid duplicate output.
  715. X.Ip "getc(FILEHANDLE)" 8 4
  716. X.Ip "getc FILEHANDLE" 8
  717. X.Ip "getc" 8
  718. XReturns the next character from the input file attached to FILEHANDLE, or
  719. Xa null string at EOF.
  720. XIf FILEHANDLE is omitted, reads from STDIN.
  721. X.Ip "getlogin" 8 3
  722. XReturns the current login from /etc/utmp, if any.
  723. XIf null, use getpwuid.
  724. X
  725. X    $login = getlogin || (getpwuid($<))[0] || "Somebody";
  726. X
  727. X.Ip "getpeername(SOCKET)" 8 3
  728. XReturns the packed sockaddr address of other end of the SOCKET connection.
  729. X.nf
  730. X
  731. X.ne 4
  732. X    # An internet sockaddr
  733. X    $sockaddr = 'S n a4 x8';
  734. X    $hersockaddr = getpeername(S);
  735. X.ie t \{\
  736. X    ($family, $port, $heraddr) = unpack($sockaddr,$hersockaddr);
  737. X'br\}
  738. X.el \{\
  739. X    ($family, $port, $heraddr) =
  740. X            unpack($sockaddr,$hersockaddr);
  741. X'br\}
  742. X
  743. X.fi
  744. X.Ip "getpgrp(PID)" 8 4
  745. X.Ip "getpgrp PID" 8
  746. XReturns the current process group for the specified PID, 0 for the current
  747. Xprocess.
  748. XWill produce a fatal error if used on a machine that doesn't implement
  749. Xgetpgrp(2).
  750. XIf EXPR is omitted, returns process group of current process.
  751. X.Ip "getppid" 8 4
  752. XReturns the process id of the parent process.
  753. X.Ip "getpriority(WHICH,WHO)" 8 4
  754. XReturns the current priority for a process, a process group, or a user.
  755. X(See getpriority(2).)
  756. XWill produce a fatal error if used on a machine that doesn't implement
  757. Xgetpriority(2).
  758. X.Ip "getpwnam(NAME)" 8
  759. X.Ip "getgrnam(NAME)" 8
  760. X.Ip "gethostbyname(NAME)" 8
  761. X.Ip "getnetbyname(NAME)" 8
  762. X.Ip "getprotobyname(NAME)" 8
  763. X.Ip "getpwuid(UID)" 8
  764. X.Ip "getgrgid(GID)" 8
  765. X.Ip "getservbyname(NAME,PROTO)" 8
  766. X.Ip "gethostbyaddr(ADDR,ADDRTYPE)" 8
  767. X.Ip "getnetbyaddr(ADDR,ADDRTYPE)" 8
  768. X.Ip "getprotobynumber(NUMBER)" 8
  769. X.Ip "getservbyport(PORT,PROTO)" 8
  770. X.Ip "getpwent" 8
  771. X.Ip "getgrent" 8
  772. X.Ip "gethostent" 8
  773. X.Ip "getnetent" 8
  774. X.Ip "getprotoent" 8
  775. X.Ip "getservent" 8
  776. X.Ip "setpwent" 8
  777. X.Ip "setgrent" 8
  778. X.Ip "sethostent(STAYOPEN)" 8
  779. X.Ip "setnetent(STAYOPEN)" 8
  780. X.Ip "setprotoent(STAYOPEN)" 8
  781. X.Ip "setservent(STAYOPEN)" 8
  782. X.Ip "endpwent" 8
  783. X.Ip "endgrent" 8
  784. X.Ip "endhostent" 8
  785. X.Ip "endnetent" 8
  786. X.Ip "endprotoent" 8
  787. X.Ip "endservent" 8
  788. XThese routines perform the same functions as their counterparts in the
  789. Xsystem library.
  790. XThe return values from the various get routines are as follows:
  791. X.nf
  792. X
  793. X    ($name,$passwd,$uid,$gid,
  794. X       $quota,$comment,$gcos,$dir,$shell) = getpw.\|.\|.
  795. X    ($name,$passwd,$gid,$members) = getgr.\|.\|.
  796. X    ($name,$aliases,$addrtype,$length,@addrs) = gethost.\|.\|.
  797. X    ($name,$aliases,$addrtype,$net) = getnet.\|.\|.
  798. X    ($name,$aliases,$proto) = getproto.\|.\|.
  799. X    ($name,$aliases,$port,$proto) = getserv.\|.\|.
  800. X
  801. X.fi
  802. XThe $members value returned by getgr.\|.\|. is a space separated list
  803. Xof the login names of the members of the group.
  804. X.Sp
  805. XThe @addrs value returned by the gethost.\|.\|. functions is a list of the
  806. Xraw addresses returned by the corresponding system library call.
  807. XIn the Internet domain, each address is four bytes long and you can unpack
  808. Xit by saying something like:
  809. X.nf
  810. X
  811. X    ($a,$b,$c,$d) = unpack('C4',$addr[0]);
  812. X
  813. X.fi
  814. X.Ip "getsockname(SOCKET)" 8 3
  815. XReturns the packed sockaddr address of this end of the SOCKET connection.
  816. X.nf
  817. X
  818. X.ne 4
  819. X    # An internet sockaddr
  820. X    $sockaddr = 'S n a4 x8';
  821. X    $mysockaddr = getsockname(S);
  822. X.ie t \{\
  823. X    ($family, $port, $myaddr) = unpack($sockaddr,$mysockaddr);
  824. X'br\}
  825. X.el \{\
  826. X    ($family, $port, $myaddr) =
  827. X            unpack($sockaddr,$mysockaddr);
  828. X'br\}
  829. X
  830. X.fi
  831. X.Ip "getsockopt(SOCKET,LEVEL,OPTNAME)" 8 3
  832. XReturns the socket option requested, or undefined if there is an error.
  833. X.Ip "gmtime(EXPR)" 8 4
  834. X.Ip "gmtime EXPR" 8
  835. XConverts a time as returned by the time function to a 9-element array with
  836. Xthe time analyzed for the Greenwich timezone.
  837. XTypically used as follows:
  838. X.nf
  839. X
  840. X.ne 3
  841. X.ie t \{\
  842. X    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
  843. X'br\}
  844. X.el \{\
  845. X    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  846. X                        gmtime(time);
  847. X'br\}
  848. X
  849. X.fi
  850. XAll array elements are numeric, and come straight out of a struct tm.
  851. XIn particular this means that $mon has the range 0.\|.11 and $wday has the
  852. Xrange 0.\|.6.
  853. XIf EXPR is omitted, does gmtime(time).
  854. X.Ip "goto LABEL" 8 6
  855. XFinds the statement labeled with LABEL and resumes execution there.
  856. XCurrently you may only go to statements in the main body of the program
  857. Xthat are not nested inside a do {} construct.
  858. XThis statement is not implemented very efficiently, and is here only to make
  859. Xthe
  860. X.IR sed -to- perl
  861. Xtranslator easier.
  862. XI may change its semantics at any time, consistent with support for translated
  863. X.I sed
  864. Xscripts.
  865. XUse it at your own risk.
  866. XBetter yet, don't use it at all.
  867. X.Ip "grep(EXPR,LIST)" 8 4
  868. XEvaluates EXPR for each element of LIST (locally setting $_ to each element)
  869. Xand returns the array value consisting of those elements for which the
  870. Xexpression evaluated to true.
  871. XIn a scalar context, returns the number of times the expression was true.
  872. X.nf
  873. X
  874. X    @foo = grep(!/^#/, @bar);    # weed out comments
  875. X
  876. X.fi
  877. XNote that, since $_ is a reference into the array value, it can be
  878. Xused to modify the elements of the array.
  879. XWhile this is useful and supported, it can cause bizarre results if
  880. Xthe LIST is not a named array.
  881. X.Ip "hex(EXPR)" 8 4
  882. X.Ip "hex EXPR" 8
  883. XReturns the decimal value of EXPR interpreted as an hex string.
  884. X(To interpret strings that might start with 0 or 0x see oct().)
  885. XIf EXPR is omitted, uses $_.
  886. X.Ip "index(STR,SUBSTR,POSITION)" 8 4
  887. X.Ip "index(STR,SUBSTR)" 8 4
  888. XReturns the position of the first occurrence of SUBSTR in STR at or after
  889. XPOSITION.
  890. XIf POSITION is omitted, starts searching from the beginning of the string.
  891. XThe return value is based at 0, or whatever you've
  892. Xset the $[ variable to.
  893. XIf the substring is not found, returns one less than the base, ordinarily \-1.
  894. X.Ip "int(EXPR)" 8 4
  895. X.Ip "int EXPR" 8
  896. XReturns the integer portion of EXPR.
  897. XIf EXPR is omitted, uses $_.
  898. X.Ip "ioctl(FILEHANDLE,FUNCTION,SCALAR)" 8 4
  899. XImplements the ioctl(2) function.
  900. XYou'll probably have to say
  901. X.nf
  902. X
  903. X    require "ioctl.ph";    # probably /usr/local/lib/perl/ioctl.ph
  904. X
  905. X.fi
  906. Xfirst to get the correct function definitions.
  907. XIf ioctl.ph doesn't exist or doesn't have the correct definitions
  908. Xyou'll have to roll
  909. Xyour own, based on your C header files such as <sys/ioctl.h>.
  910. X(There is a perl script called h2ph that comes with the perl kit
  911. Xwhich may help you in this.)
  912. XSCALAR will be read and/or written depending on the FUNCTION\*(--a pointer
  913. Xto the string value of SCALAR will be passed as the third argument of
  914. Xthe actual ioctl call.
  915. X(If SCALAR has no string value but does have a numeric value, that value
  916. Xwill be passed rather than a pointer to the string value.
  917. XTo guarantee this to be true, add a 0 to the scalar before using it.)
  918. XThe pack() and unpack() functions are useful for manipulating the values
  919. Xof structures used by ioctl().
  920. XThe following example sets the erase character to DEL.
  921. X.nf
  922. X
  923. X.ne 9
  924. X    require 'ioctl.ph';
  925. X    $sgttyb_t = "ccccs";        # 4 chars and a short
  926. X    if (ioctl(STDIN,$TIOCGETP,$sgttyb)) {
  927. X        @ary = unpack($sgttyb_t,$sgttyb);
  928. X        $ary[2] = 127;
  929. X        $sgttyb = pack($sgttyb_t,@ary);
  930. X        ioctl(STDIN,$TIOCSETP,$sgttyb)
  931. X            || die "Can't ioctl: $!";
  932. X    }
  933. X
  934. X.fi
  935. XThe return value of ioctl (and fcntl) is as follows:
  936. X.nf
  937. X
  938. X.ne 4
  939. X    if OS returns:\h'|3i'perl returns:
  940. X      -1\h'|3i'  undefined value
  941. X      0\h'|3i'  string "0 but true"
  942. X      anything else\h'|3i'  that number
  943. X
  944. X.fi
  945. XThus perl returns true on success and false on failure, yet you can still
  946. Xeasily determine the actual value returned by the operating system:
  947. X.nf
  948. X
  949. X    ($retval = ioctl(...)) || ($retval = -1);
  950. X    printf "System returned %d\en", $retval;
  951. X.fi
  952. X.Ip "join(EXPR,LIST)" 8 8
  953. X.Ip "join(EXPR,ARRAY)" 8
  954. XJoins the separate strings of LIST or ARRAY into a single string with fields
  955. Xseparated by the value of EXPR, and returns the string.
  956. XExample:
  957. X.nf
  958. X    
  959. X.ie t \{\
  960. X    $_ = join(\|\':\', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
  961. X'br\}
  962. X.el \{\
  963. X    $_ = join(\|\':\',
  964. X        $login,$passwd,$uid,$gid,$gcos,$home,$shell);
  965. X'br\}
  966. X
  967. X.fi
  968. XSee
  969. X.IR split .
  970. X.Ip "keys(ASSOC_ARRAY)" 8 6
  971. X.Ip "keys ASSOC_ARRAY" 8
  972. XReturns a normal array consisting of all the keys of the named associative
  973. Xarray.
  974. XThe keys are returned in an apparently random order, but it is the same order
  975. Xas either the values() or each() function produces (given that the associative array
  976. Xhas not been modified).
  977. XHere is yet another way to print your environment:
  978. X.nf
  979. X
  980. X.ne 5
  981. X    @keys = keys %ENV;
  982. X    @values = values %ENV;
  983. X    while ($#keys >= 0) {
  984. X        print pop(@keys), \'=\', pop(@values), "\en";
  985. X    }
  986. X
  987. Xor how about sorted by key:
  988. X
  989. X.ne 3
  990. X    foreach $key (sort(keys %ENV)) {
  991. X        print $key, \'=\', $ENV{$key}, "\en";
  992. X    }
  993. X
  994. X.fi
  995. X.Ip "kill(LIST)" 8 8
  996. X.Ip "kill LIST" 8 2
  997. XSends a signal to a list of processes.
  998. XThe first element of the list must be the signal to send.
  999. XReturns the number of processes successfully signaled.
  1000. X.nf
  1001. X
  1002. X    $cnt = kill 1, $child1, $child2;
  1003. X    kill 9, @goners;
  1004. X
  1005. X.fi
  1006. XIf the signal is negative, kills process groups instead of processes.
  1007. X(On System V, a negative \fIprocess\fR number will also kill process groups,
  1008. Xbut that's not portable.)
  1009. XYou may use a signal name in quotes.
  1010. X.Ip "last LABEL" 8 8
  1011. X.Ip "last" 8
  1012. XThe
  1013. X.I last
  1014. Xcommand is like the
  1015. X.I break
  1016. Xstatement in C (as used in loops); it immediately exits the loop in question.
  1017. XIf the LABEL is omitted, the command refers to the innermost enclosing loop.
  1018. XThe
  1019. X.I continue
  1020. Xblock, if any, is not executed:
  1021. X.nf
  1022. X
  1023. X.ne 4
  1024. X    line: while (<STDIN>) {
  1025. X        last line if /\|^$/;    # exit when done with header
  1026. X        .\|.\|.
  1027. X    }
  1028. X
  1029. X.fi
  1030. X.Ip "length(EXPR)" 8 4
  1031. X.Ip "length EXPR" 8
  1032. XReturns the length in characters of the value of EXPR.
  1033. XIf EXPR is omitted, returns length of $_.
  1034. X.Ip "link(OLDFILE,NEWFILE)" 8 2
  1035. XCreates a new filename linked to the old filename.
  1036. XReturns 1 for success, 0 otherwise.
  1037. X.Ip "listen(SOCKET,QUEUESIZE)" 8 2
  1038. XDoes the same thing that the listen system call does.
  1039. XReturns true if it succeeded, false otherwise.
  1040. XSee example in section on Interprocess Communication.
  1041. X.Ip "local(LIST)" 8 4
  1042. XDeclares the listed variables to be local to the enclosing block,
  1043. Xsubroutine, eval or \*(L"do\*(R".
  1044. XAll the listed elements must be legal lvalues.
  1045. XThis operator works by saving the current values of those variables in LIST
  1046. Xon a hidden stack and restoring them upon exiting the block, subroutine or eval.
  1047. XThis means that called subroutines can also reference the local variable,
  1048. Xbut not the global one.
  1049. XThe LIST may be assigned to if desired, which allows you to initialize
  1050. Xyour local variables.
  1051. X(If no initializer is given for a particular variable, it is created with
  1052. Xan undefined value.)
  1053. XCommonly this is used to name the parameters to a subroutine.
  1054. XExamples:
  1055. X.nf
  1056. X
  1057. X.ne 13
  1058. X    sub RANGEVAL {
  1059. X        local($min, $max, $thunk) = @_;
  1060. X        local($result) = \'\';
  1061. X        local($i);
  1062. X
  1063. X        # Presumably $thunk makes reference to $i
  1064. X
  1065. X        for ($i = $min; $i < $max; $i++) {
  1066. X            $result .= eval $thunk;
  1067. X        }
  1068. X
  1069. X        $result;
  1070. X    }
  1071. X
  1072. X.ne 6
  1073. X    if ($sw eq \'-v\') {
  1074. X        # init local array with global array
  1075. X        local(@ARGV) = @ARGV;
  1076. X        unshift(@ARGV,\'echo\');
  1077. X        system @ARGV;
  1078. X    }
  1079. X    # @ARGV restored
  1080. X
  1081. X.ne 6
  1082. X    # temporarily add to digits associative array
  1083. X    if ($base12) {
  1084. X        # (NOTE: not claiming this is efficient!)
  1085. X        local(%digits) = (%digits,'t',10,'e',11);
  1086. X        do parse_num();
  1087. X    }
  1088. X
  1089. X.fi
  1090. XNote that local() is a run-time command, and so gets executed every time
  1091. Xthrough a loop, using up more stack storage each time until it's all
  1092. Xreleased at once when the loop is exited.
  1093. X.Ip "localtime(EXPR)" 8 4
  1094. X.Ip "localtime EXPR" 8
  1095. XConverts a time as returned by the time function to a 9-element array with
  1096. Xthe time analyzed for the local timezone.
  1097. XTypically used as follows:
  1098. X.nf
  1099. X
  1100. X.ne 3
  1101. X.ie t \{\
  1102. X    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  1103. X'br\}
  1104. X.el \{\
  1105. X    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  1106. X                        localtime(time);
  1107. X'br\}
  1108. X
  1109. X.fi
  1110. XAll array elements are numeric, and come straight out of a struct tm.
  1111. XIn particular this means that $mon has the range 0.\|.11 and $wday has the
  1112. Xrange 0.\|.6.
  1113. XIf EXPR is omitted, does localtime(time).
  1114. X.Ip "log(EXPR)" 8 4
  1115. X.Ip "log EXPR" 8
  1116. XReturns logarithm (base
  1117. X.IR e )
  1118. Xof EXPR.
  1119. XIf EXPR is omitted, returns log of $_.
  1120. X.Ip "lstat(FILEHANDLE)" 8 6
  1121. X.Ip "lstat FILEHANDLE" 8
  1122. X.Ip "lstat(EXPR)" 8
  1123. X.Ip "lstat SCALARVARIABLE" 8
  1124. XDoes the same thing as the stat() function, but stats a symbolic link
  1125. Xinstead of the file the symbolic link points to.
  1126. XIf symbolic links are unimplemented on your system, a normal stat is done.
  1127. X.Ip "m/PATTERN/io" 8 4
  1128. X.Ip "/PATTERN/io" 8
  1129. XSearches a string for a pattern match, and returns true (1) or false (\'\').
  1130. XIf no string is specified via the =~ or !~ operator,
  1131. Xthe $_ string is searched.
  1132. X(The string specified with =~ need not be an lvalue\*(--it may be the result of an expression evaluation, but remember the =~ binds rather tightly.)
  1133. XSee also the section on regular expressions.
  1134. X.Sp
  1135. XIf / is the delimiter then the initial \*(L'm\*(R' is optional.
  1136. XWith the \*(L'm\*(R' you can use any pair of non-alphanumeric characters
  1137. Xas delimiters.
  1138. XThis is particularly useful for matching Unix path names that contain \*(L'/\*(R'.
  1139. XIf the final delimiter is followed by the optional letter \*(L'i\*(R', the matching is
  1140. Xdone in a case-insensitive manner.
  1141. XPATTERN may contain references to scalar variables, which will be interpolated
  1142. X(and the pattern recompiled) every time the pattern search is evaluated.
  1143. X(Note that $) and $| may not be interpolated because they look like end-of-string tests.)
  1144. XIf you want such a pattern to be compiled only once, add an \*(L"o\*(R" after
  1145. Xthe trailing delimiter.
  1146. XThis avoids expensive run-time recompilations, and
  1147. Xis useful when the value you are interpolating won't change over the
  1148. Xlife of the script.
  1149. XIf the PATTERN evaluates to a null string, the most recent successful
  1150. Xregular expression is used instead.
  1151. X.Sp
  1152. XIf used in a context that requires an array value, a pattern match returns an
  1153. Xarray consisting of the subexpressions matched by the parentheses in the
  1154. Xpattern,
  1155. Xi.e. ($1, $2, $3.\|.\|.).
  1156. XIt does NOT actually set $1, $2, etc. in this case, nor does it set $+, $`, $&
  1157. Xor $'.
  1158. XIf the match fails, a null array is returned.
  1159. XIf the match succeeds, but there were no parentheses, an array value of (1)
  1160. Xis returned.
  1161. X.Sp
  1162. XExamples:
  1163. X.nf
  1164. X
  1165. X.ne 4
  1166. X    open(tty, \'/dev/tty\');
  1167. X    <tty> \|=~ \|/\|^y\|/i \|&& \|do foo(\|);    # do foo if desired
  1168. X
  1169. X    if (/Version: \|*\|([0\-9.]*\|)\|/\|) { $version = $1; }
  1170. X
  1171. X    next if m#^/usr/spool/uucp#;
  1172. X
  1173. X.ne 5
  1174. X    # poor man's grep
  1175. X    $arg = shift;
  1176. X    while (<>) {
  1177. X        print if /$arg/o;    # compile only once
  1178. X    }
  1179. X
  1180. X    if (($F1, $F2, $Etc) = ($foo =~ /^(\eS+)\es+(\eS+)\es*(.*)/))
  1181. X
  1182. X.fi
  1183. XThis last example splits $foo into the first two words and the remainder
  1184. Xof the line, and assigns those three fields to $F1, $F2 and $Etc.
  1185. XThe conditional is true if any variables were assigned, i.e. if the pattern
  1186. Xmatched.
  1187. X.Ip "mkdir(FILENAME,MODE)" 8 3
  1188. XCreates the directory specified by FILENAME, with permissions specified by
  1189. XMODE (as modified by umask).
  1190. XIf it succeeds it returns 1, otherwise it returns 0 and sets $! (errno).
  1191. X.Ip "msgctl(ID,CMD,ARG)" 8 4
  1192. XCalls the System V IPC function msgctl.  If CMD is &IPC_STAT, then ARG
  1193. Xmust be a variable which will hold the returned msqid_ds structure.
  1194. XReturns like ioctl: the undefined value for error, "0 but true" for
  1195. Xzero, or the actual return value otherwise.
  1196. X.Ip "msgget(KEY,FLAGS)" 8 4
  1197. XCalls the System V IPC function msgget.  Returns the message queue id,
  1198. Xor the undefined value if there is an error.
  1199. X.Ip "msgsnd(ID,MSG,FLAGS)" 8 4
  1200. XCalls the System V IPC function msgsnd to send the message MSG to the
  1201. Xmessage queue ID.  MSG must begin with the long integer message type,
  1202. Xwhich may be created with pack("L", $type).  Returns true if
  1203. Xsuccessful, or false if there is an error.
  1204. X.Ip "msgrcv(ID,VAR,SIZE,TYPE,FLAGS)" 8 4
  1205. XCalls the System V IPC function msgrcv to receive a message from
  1206. Xmessage queue ID into variable VAR with a maximum message size of
  1207. XSIZE.  Note that if a message is received, the message type will be
  1208. Xthe first thing in VAR, and the maximum length of VAR is SIZE plus the
  1209. Xsize of the message type.  Returns true if successful, or false if
  1210. Xthere is an error.
  1211. X''' Beginning of part 3
  1212. X''' $RCSfile: perl.man,v $$Revision: 4.0.1.1 $$Date: 91/04/11 17:50:44 $
  1213. X'''
  1214. X''' $Log:    perl.man,v $
  1215. X''' Revision 4.0.1.1  91/04/11  17:50:44  lwall
  1216. X''' patch1: fixed some typos
  1217. X''' 
  1218. X''' Revision 4.0  91/03/20  01:38:08  lwall
  1219. X''' 4.0 baseline.
  1220. X''' 
  1221. X''' Revision 3.0.1.12  91/01/11  18:18:15  lwall
  1222. X''' patch42: added binary and hex pack/unpack options
  1223. X''' 
  1224. X''' Revision 3.0.1.11  90/11/10  01:48:21  lwall
  1225. X''' patch38: random cleanup
  1226. X''' patch38: documented tr///cds
  1227. X''' 
  1228. X''' Revision 3.0.1.10  90/10/20  02:15:17  lwall
  1229. X''' patch37: patch37: fixed various typos in man page
  1230. X''' 
  1231. X''' Revision 3.0.1.9  90/10/16  10:02:43  lwall
  1232. X''' patch29: you can now read into the middle string
  1233. X''' patch29: index and substr now have optional 3rd args
  1234. X''' patch29: added scalar reverse
  1235. X''' patch29: added scalar
  1236. X''' patch29: added SysV IPC
  1237. X''' patch29: added waitpid
  1238. X''' patch29: added sysread and syswrite
  1239. X''' 
  1240. X''' Revision 3.0.1.8  90/08/09  04:39:04  lwall
  1241. X''' patch19: added require operator
  1242. X''' patch19: added truncate operator
  1243. X''' patch19: unpack can do checksumming
  1244. X''' 
  1245. X''' Revision 3.0.1.7  90/08/03  11:15:42  lwall
  1246. X''' patch19: Intermediate diffs for Randal
  1247. X''' 
  1248. X''' Revision 3.0.1.6  90/03/27  16:17:56  lwall
  1249. X''' patch16: MSDOS support
  1250. X''' 
  1251. X''' Revision 3.0.1.5  90/03/12  16:52:21  lwall
  1252. X''' patch13: documented that print $filehandle &foo is ambiguous
  1253. X''' patch13: added splice operator: @oldelems = splice(@array,$offset,$len,LIST)
  1254. X''' 
  1255. X''' Revision 3.0.1.4  90/02/28  18:00:09  lwall
  1256. X''' patch9: added pipe function
  1257. X''' patch9: documented how to handle arbitrary weird characters in filenames
  1258. X''' patch9: documented the unflushed buffers problem on piped opens
  1259. X''' patch9: documented how to force top of page
  1260. X''' 
  1261. X''' Revision 3.0.1.3  89/12/21  20:10:12  lwall
  1262. X''' patch7: documented that s`pat`repl` does command substitution on replacement
  1263. X''' patch7: documented that $timeleft from select() is likely not implemented
  1264. X''' 
  1265. X''' Revision 3.0.1.2  89/11/17  15:31:05  lwall
  1266. X''' patch5: fixed some manual typos and indent problems
  1267. X''' patch5: added warning about print making an array context
  1268. X''' 
  1269. X''' Revision 3.0.1.1  89/11/11  04:45:06  lwall
  1270. X''' patch2: made some line breaks depend on troff vs. nroff
  1271. X''' 
  1272. X''' Revision 3.0  89/10/18  15:21:46  lwall
  1273. X''' 3.0 baseline
  1274. X''' 
  1275. X.Ip "next LABEL" 8 8
  1276. X.Ip "next" 8
  1277. XThe
  1278. X.I next
  1279. Xcommand is like the
  1280. X.I continue
  1281. Xstatement in C; it starts the next iteration of the loop:
  1282. X.nf
  1283. X
  1284. X.ne 4
  1285. X    line: while (<STDIN>) {
  1286. X        next line if /\|^#/;    # discard comments
  1287. X        .\|.\|.
  1288. X    }
  1289. X
  1290. X.fi
  1291. XNote that if there were a
  1292. X.I continue
  1293. Xblock on the above, it would get executed even on discarded lines.
  1294. XIf the LABEL is omitted, the command refers to the innermost enclosing loop.
  1295. X.Ip "oct(EXPR)" 8 4
  1296. X.Ip "oct EXPR" 8
  1297. XReturns the decimal value of EXPR interpreted as an octal string.
  1298. X(If EXPR happens to start off with 0x, interprets it as a hex string instead.)
  1299. XThe following will handle decimal, octal and hex in the standard notation:
  1300. X.nf
  1301. X
  1302. X    $val = oct($val) if $val =~ /^0/;
  1303. X
  1304. X.fi
  1305. XIf EXPR is omitted, uses $_.
  1306. X.Ip "open(FILEHANDLE,EXPR)" 8 8
  1307. X.Ip "open(FILEHANDLE)" 8
  1308. X.Ip "open FILEHANDLE" 8
  1309. XOpens the file whose filename is given by EXPR, and associates it with
  1310. XFILEHANDLE.
  1311. XIf FILEHANDLE is an expression, its value is used as the name of the
  1312. Xreal filehandle wanted.
  1313. XIf EXPR is omitted, the scalar variable of the same name as the FILEHANDLE
  1314. Xcontains the filename.
  1315. XIf the filename begins with \*(L"<\*(R" or nothing, the file is opened for
  1316. Xinput.
  1317. XIf the filename begins with \*(L">\*(R", the file is opened for output.
  1318. XIf the filename begins with \*(L">>\*(R", the file is opened for appending.
  1319. X(You can put a \'+\' in front of the \'>\' or \'<\' to indicate that you
  1320. Xwant both read and write access to the file.)
  1321. XIf the filename begins with \*(L"|\*(R", the filename is interpreted
  1322. Xas a command to which output is to be piped, and if the filename ends
  1323. Xwith a \*(L"|\*(R", the filename is interpreted as command which pipes
  1324. Xinput to us.
  1325. X(You may not have a command that pipes both in and out.)
  1326. XOpening \'\-\' opens
  1327. X.I STDIN
  1328. Xand opening \'>\-\' opens
  1329. X.IR STDOUT .
  1330. XOpen returns non-zero upon success, the undefined value otherwise.
  1331. XIf the open involved a pipe, the return value happens to be the pid
  1332. Xof the subprocess.
  1333. XExamples:
  1334. X.nf
  1335. X    
  1336. X.ne 3
  1337. X    $article = 100;
  1338. X    open article || die "Can't find article $article: $!\en";
  1339. X    while (<article>) {\|.\|.\|.
  1340. X
  1341. X.ie t \{\
  1342. X    open(LOG, \'>>/usr/spool/news/twitlog\'\|);    # (log is reserved)
  1343. X'br\}
  1344. X.el \{\
  1345. X    open(LOG, \'>>/usr/spool/news/twitlog\'\|);
  1346. X                    # (log is reserved)
  1347. X'br\}
  1348. X
  1349. X.ie t \{\
  1350. X    open(article, "caesar <$article |"\|);        # decrypt article
  1351. X'br\}
  1352. X.el \{\
  1353. X    open(article, "caesar <$article |"\|);
  1354. X                    # decrypt article
  1355. X'br\}
  1356. X
  1357. X.ie t \{\
  1358. X    open(extract, "|sort >/tmp/Tmp$$"\|);        # $$ is our process#
  1359. X'br\}
  1360. X.el \{\
  1361. X    open(extract, "|sort >/tmp/Tmp$$"\|);
  1362. X                    # $$ is our process#
  1363. X'br\}
  1364. X
  1365. X.ne 7
  1366. X    # process argument list of files along with any includes
  1367. X
  1368. X    foreach $file (@ARGV) {
  1369. X        do process($file, \'fh00\');    # no pun intended
  1370. X    }
  1371. X
  1372. X    sub process {
  1373. X        local($filename, $input) = @_;
  1374. X        $input++;        # this is a string increment
  1375. X        unless (open($input, $filename)) {
  1376. X            print STDERR "Can't open $filename: $!\en";
  1377. X            return;
  1378. X        }
  1379. X.ie t \{\
  1380. X        while (<$input>) {        # note the use of indirection
  1381. X'br\}
  1382. X.el \{\
  1383. X        while (<$input>) {        # note use of indirection
  1384. X'br\}
  1385. X            if (/^#include "(.*)"/) {
  1386. X                do process($1, $input);
  1387. X                next;
  1388. X            }
  1389. X            .\|.\|.        # whatever
  1390. X        }
  1391. X    }
  1392. X
  1393. X.fi
  1394. XYou may also, in the Bourne shell tradition, specify an EXPR beginning
  1395. Xwith \*(L">&\*(R", in which case the rest of the string
  1396. Xis interpreted as the name of a filehandle
  1397. X(or file descriptor, if numeric) which is to be duped and opened.
  1398. XYou may use & after >, >>, <, +>, +>> and +<.
  1399. XThe mode you specify should match the mode of the original filehandle.
  1400. XHere is a script that saves, redirects, and restores
  1401. X.I STDOUT
  1402. Xand
  1403. X.IR STDERR :
  1404. X.nf
  1405. X
  1406. X.ne 21
  1407. X    #!/usr/bin/perl
  1408. X    open(SAVEOUT, ">&STDOUT");
  1409. X    open(SAVEERR, ">&STDERR");
  1410. X
  1411. X    open(STDOUT, ">foo.out") || die "Can't redirect stdout";
  1412. X    open(STDERR, ">&STDOUT") || die "Can't dup stdout";
  1413. X
  1414. X    select(STDERR); $| = 1;        # make unbuffered
  1415. X    select(STDOUT); $| = 1;        # make unbuffered
  1416. X
  1417. X    print STDOUT "stdout 1\en";    # this works for
  1418. X    print STDERR "stderr 1\en";     # subprocesses too
  1419. X
  1420. X    close(STDOUT);
  1421. X    close(STDERR);
  1422. X
  1423. X    open(STDOUT, ">&SAVEOUT");
  1424. X    open(STDERR, ">&SAVEERR");
  1425. X
  1426. X    print STDOUT "stdout 2\en";
  1427. X    print STDERR "stderr 2\en";
  1428. X
  1429. X.fi
  1430. XIf you open a pipe on the command \*(L"\-\*(R", i.e. either \*(L"|\-\*(R" or \*(L"\-|\*(R",
  1431. Xthen there is an implicit fork done, and the return value of open
  1432. Xis the pid of the child within the parent process, and 0 within the child
  1433. Xprocess.
  1434. X(Use defined($pid) to determine if the open was successful.)
  1435. XThe filehandle behaves normally for the parent, but i/o to that
  1436. Xfilehandle is piped from/to the
  1437. X.IR STDOUT / STDIN
  1438. Xof the child process.
  1439. XIn the child process the filehandle isn't opened\*(--i/o happens from/to
  1440. Xthe new
  1441. X.I STDOUT
  1442. Xor
  1443. X.IR STDIN .
  1444. XTypically this is used like the normal piped open when you want to exercise
  1445. Xmore control over just how the pipe command gets executed, such as when
  1446. Xyou are running setuid, and don't want to have to scan shell commands
  1447. Xfor metacharacters.
  1448. XThe following pairs are more or less equivalent:
  1449. X.nf
  1450. X
  1451. X.ne 5
  1452. X    open(FOO, "|tr \'[a\-z]\' \'[A\-Z]\'");
  1453. X    open(FOO, "|\-") || exec \'tr\', \'[a\-z]\', \'[A\-Z]\';
  1454. X
  1455. X    open(FOO, "cat \-n '$file'|");
  1456. X    open(FOO, "\-|") || exec \'cat\', \'\-n\', $file;
  1457. X
  1458. X.fi
  1459. XExplicitly closing any piped filehandle causes the parent process to wait for the
  1460. Xchild to finish, and returns the status value in $?.
  1461. XNote: on any operation which may do a fork,
  1462. Xunflushed buffers remain unflushed in both
  1463. Xprocesses, which means you may need to set $| to
  1464. Xavoid duplicate output.
  1465. X.Sp
  1466. XThe filename that is passed to open will have leading and trailing
  1467. Xwhitespace deleted.
  1468. XIn order to open a file with arbitrary weird characters in it, it's necessary
  1469. Xto protect any leading and trailing whitespace thusly:
  1470. X.nf
  1471. X
  1472. X.ne 2
  1473. X        $file =~ s#^(\es)#./$1#;
  1474. X        open(FOO, "< $file\e0");
  1475. X
  1476. X.fi
  1477. X.Ip "opendir(DIRHANDLE,EXPR)" 8 3
  1478. XOpens a directory named EXPR for processing by readdir(), telldir(), seekdir(),
  1479. Xrewinddir() and closedir().
  1480. XReturns true if successful.
  1481. XDIRHANDLEs have their own namespace separate from FILEHANDLEs.
  1482. X.Ip "ord(EXPR)" 8 4
  1483. X.Ip "ord EXPR" 8
  1484. XReturns the numeric ascii value of the first character of EXPR.
  1485. XIf EXPR is omitted, uses $_.
  1486. X''' Comments on f & d by gnb@melba.bby.oz.au    22/11/89
  1487. X.Ip "pack(TEMPLATE,LIST)" 8 4
  1488. XTakes an array or list of values and packs it into a binary structure,
  1489. Xreturning the string containing the structure.
  1490. XThe TEMPLATE is a sequence of characters that give the order and type
  1491. Xof values, as follows:
  1492. X.nf
  1493. X
  1494. X    A    An ascii string, will be space padded.
  1495. X    a    An ascii string, will be null padded.
  1496. X    c    A signed char value.
  1497. X    C    An unsigned char value.
  1498. X    s    A signed short value.
  1499. X    S    An unsigned short value.
  1500. X    i    A signed integer value.
  1501. X    I    An unsigned integer value.
  1502. X    l    A signed long value.
  1503. X    L    An unsigned long value.
  1504. X    n    A short in \*(L"network\*(R" order.
  1505. X    N    A long in \*(L"network\*(R" order.
  1506. X    f    A single-precision float in the native format.
  1507. X    d    A double-precision float in the native format.
  1508. X    p    A pointer to a string.
  1509. X    x    A null byte.
  1510. X    X    Back up a byte.
  1511. X    @    Null fill to absolute position.
  1512. X    u    A uuencoded string.
  1513. X    b    A bit string (ascending bit order, like vec()).
  1514. X    B    A bit string (descending bit order).
  1515. X    h    A hex string (low nybble first).
  1516. X    H    A hex string (high nybble first).
  1517. X
  1518. X.fi
  1519. XEach letter may optionally be followed by a number which gives a repeat
  1520. Xcount.
  1521. XWith all types except "a", "A", "b", "B", "h" and "H",
  1522. Xthe pack function will gobble up that many values
  1523. Xfrom the LIST.
  1524. XA * for the repeat count means to use however many items are left.
  1525. XThe "a" and "A" types gobble just one value, but pack it as a string of length
  1526. Xcount,
  1527. !STUFFY!FUNK!
  1528. echo " "
  1529. echo "End of kit 5 (of 36)"
  1530. cat /dev/null >kit5isdone
  1531. run=''
  1532. config=''
  1533. for iskit in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36; do
  1534.     if test -f kit${iskit}isdone; then
  1535.     run="$run $iskit"
  1536.     else
  1537.     todo="$todo $iskit"
  1538.     fi
  1539. done
  1540. case $todo in
  1541.     '')
  1542.     echo "You have run all your kits.  Please read README and then type Configure."
  1543.     for combo in *:AA; do
  1544.         if test -f "$combo"; then
  1545.         realfile=`basename $combo :AA`
  1546.         cat $realfile:[A-Z][A-Z] >$realfile
  1547.         rm -rf $realfile:[A-Z][A-Z]
  1548.         fi
  1549.     done
  1550.     rm -rf kit*isdone
  1551.     chmod 755 Configure
  1552.     ;;
  1553.     *)  echo "You have run$run."
  1554.     echo "You still need to run$todo."
  1555.     ;;
  1556. esac
  1557. : Someone might mail this, so...
  1558. exit
  1559.  
  1560. exit 0 # Just in case...
  1561. -- 
  1562. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1563. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1564. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1565. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1566.