home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / tutorial / slides / slides.ms < prev    next >
Encoding:
Text File  |  1990-02-18  |  23.8 KB  |  947 lines

  1. .\" Copyright 1990  Tom Christiansen
  2. .\" You are free to use these as you wish providing
  3. .\" you give me credit for having originally written them.
  4. .\"
  5. .\" Start Quote macro -- place text in teletype font and indent
  6. .de QS
  7. .br
  8. .in +3n
  9. .ft TA
  10. .nf
  11. ..
  12. .\" End Quote macro -- restore normal situation
  13. .de QE
  14. .in -3n
  15. .fi
  16. .ft R
  17. ..
  18. .\" quotes i might want to use
  19. .ds qq \&"\"
  20. .ds lq \&"\"
  21. .ds rq \&"\"
  22. .if t \
  23. .       ds lq ``
  24. .if t \
  25. .       ds rq ''
  26. .\" Begin Page macro -- go to next page, center emboldened args
  27. .de BP 
  28. .bp
  29. .if n \
  30.         ---------------------------------------
  31. .c "\\fB\\s20\\$1\\$2\\$3\\$4\\$5\\s0\\fR"
  32. .sp 
  33. ..
  34. .\" some sizes 
  35. .nr VS 21
  36. .vs 21
  37. .nr PS 16
  38. .ps 16
  39. .ta 8 16 24 32 40 48 56 64 72
  40. .\" ===========================================================================
  41. .ce 7
  42. .au "  \fIperl\fR tutorial -- \\\\n(%  "
  43. .sp 1
  44. \s36\fBPERL
  45. .sp 
  46. \s18\fIa language by Larry Wall\fP
  47. .sp 3
  48. .br
  49. \s24Practical Extraction and Report Language
  50. .sp 1
  51. \fIor\fB
  52. .sp 1
  53. .br
  54. Pathologically Eclectic Rubbish Lister
  55. .sp 3
  56. \s18\fITom Christiansen
  57. \s-1CONVEX\s+1 Computer Corporation\fP
  58. .\" ===========================================================================
  59. .nr VS 21
  60. .vs 21
  61. .nr PS 16
  62. .ps 16
  63. .BP "Overview"
  64. .2
  65. What is \fIPerl\fP:
  66. features,
  67. where to get it,
  68. preview
  69. .sp
  70. .2
  71. Data Types:
  72. scalars and
  73. arrays
  74. .sp
  75. .2
  76. Operators
  77. .sp
  78. .2 
  79. Flow Control
  80. .sp
  81. .2
  82. Regular Expressions
  83. .sp
  84. .2
  85. I/O:
  86. regular I/O,
  87. system functions,
  88. directory access,
  89. formatted I/O
  90. .sp
  91. .2
  92. Functions and Subroutines: built-in array and string functions
  93. .sp
  94. .2 
  95. Esoterica:
  96. suid scripts,
  97. debugging,
  98. packages,
  99. command line options
  100. .sp
  101. .2
  102. Examples
  103. .\" ===========================================================================
  104. .BP "What is Perl?"
  105. .2
  106. An interpreted language that looks a lot like C with built-in
  107. \fIsed\fP, \fIawk\fP, and \fIsh\fP, as well as bits of \fIcsh\fP, Pascal, \s-1FORTRAN\s+1, \s-1BASIC-PLUS\s0
  108. thrown in.
  109. .sp
  110. .2
  111. Highly optimized for manipulating printable text, but also able to handle binary data.
  112. .sp
  113. .2
  114. Especially suitable for system management tasks due to interfaces to 
  115. most common system calls.
  116. .sp
  117. .2
  118. Rich enough for most general programming tasks.
  119. .sp
  120. .2
  121. \fI\*(lqA shell for C programmers.\*(rq\fR [Larry Wall]
  122. .\" ===========================================================================
  123. .BP "Features"
  124. .2
  125. Easy to learn because much derives from existing tools.
  126. .sp
  127. .2
  128. More rapid program development
  129. because it's an interpreter
  130. .sp
  131. .2
  132. Faster execution than shell script equivalents.
  133. .sp
  134. .2
  135. More powerful than \fIsed\fP, \fIawk\fP, or \fIsh\fP; \fIa2p\fP and \fIs2p\fP
  136. translators supplied for your old scripts.
  137. .sp
  138. .2
  139. Portable across many different architectures.
  140. .sp
  141. .2
  142. Absence of arbitrary limits like string length.
  143. .sp
  144. .2
  145. Fits nicely into \s-1UNIX\s0 tool and filter philosophy.
  146. .sp
  147. .2
  148. It's free!
  149. .\" ===========================================================================
  150. .BP "Where to get it"
  151. .2 
  152. Any \fIcomp.sources.unix\fP archive
  153. .sp
  154. .2
  155. Famous archive servers
  156. .3
  157. uunet.uu.net    192.48.96.2
  158. .3
  159. tut.cis.ohio-state.edu    128.146.8.60
  160. .sp
  161. .2
  162. Its author, Larry Wall <lwall@jpl-devvax.jpl.nasa.gov>
  163. .3
  164. jpl-devvax.jpl.nasa.gov    128.149.1.143
  165. .sp
  166. .2
  167. Perl reference guide (in postscript form) also available from Ohio State, along
  168. with some sample scripts and archives of the \fIperl-users\fP mailing list.
  169. .sp 
  170. .2
  171. \s-1USENET\s0 newsgroup \fIcomp.lang.perl\fP good source for questions,
  172. comments, examples.
  173. .\" ===========================================================================
  174. .BP "Preview"
  175. .2 
  176. It's not for nothing that \fIperl\fP is sometimes called the \*(lqpathologically
  177. eclectic rubbish lister.\*(rq  Before you drown in a deluge of features,
  178. here's a simple example to whet your
  179. appetites that demonstrates the principal features of the language, all 
  180. of which have been present since version 1.
  181. .QS
  182.  
  183. while (<>) {
  184.     next if /^#/;
  185.     ($x, $y, $z) = /(\eS+)\es+(\ed\ed\ed)\es+(foo|bar)/;
  186.     $x =~ tr/a-z/A-Z/;
  187.     $seen{$x}++;
  188.     $z =~ s/foo/fear/ && $scared++;
  189.     printf "%s %08x %-10s\en", $z, $y, $x 
  190.     if $seen{$x} > $y;
  191. .QE
  192. .\" ===========================================================================
  193. .BP "Data Types"
  194. .2
  195. Basic data types are scalars, 
  196. indexed arrays of scalars, 
  197. and associative arrays of scalars.
  198. .sp
  199. .2
  200. Scalars themselves are either string, numeric, or boolean, depending on context.
  201. Values of 0 (zero) and '' (null string) are false; all else is true.
  202. .sp
  203. .2
  204. Type of variable determined by leading special character.
  205. .3 "$    scalar"
  206. .3 "@    indexed array (lists)"
  207. .3 "%    associative array"
  208. .3 "&    function"
  209. .sp
  210. .2
  211. All data
  212. types have their own separate namespaces, as
  213. do labels, functions, and file and directory handles.
  214. .\" ===========================================================================
  215. .BP "Data Types (scalars)"
  216. .2
  217. Use a $ to indicate a scalar value
  218. .sp
  219. .QS
  220. $foo = 3.14159;
  221. .sp
  222. $foo = 'red';
  223. .sp
  224. $foo = "was $foo before";    # interpolate variable
  225. .sp
  226. $host = \s+2`\s0hostname\s+2`\s0;    # note backticks
  227. .sp
  228. ($foo, $bar, $glarch) = ('red', 'blue', 'green');
  229. .sp
  230. ($foo, $bar) = ($bar, $foo); # exchange
  231. .QE
  232. .\" ===========================================================================
  233. .BP "Special Scalar Variables"
  234. .2
  235. Special scalars are named with punctuation (except \fB$0\fP).  Examples are
  236. .sp
  237. .3
  238. \fB$0\fP    name of the currently executing script
  239. .3
  240. \fB$_\fP    default for pattern operators and implicit I/O
  241. .3
  242. \fB$$\fP    the current pid
  243. .3
  244. \fB$!\fP    the current system error message from \fIerrno\fP
  245. .3
  246. \fB$?\fP    status of last `backtick`, pipe, or system
  247. .3
  248. \fB$|\fP    whether output is buffered
  249. .3
  250. \fB$.\fP    the current line number of last input
  251. .3
  252. \fB$[\fP    array base, 0 by default; \fIawk\fP uses 1
  253. .3
  254. \fB$<\fP    the real uid of the process
  255. .3
  256. \fB$(\fP    the real gid of the process
  257. .3
  258. \fB$>\fP    the effective uid of the process
  259. .3
  260. \fB$)\fP    the effective gid of the process
  261. .\" ===========================================================================
  262. .BP "Data types (arrays)"
  263. .2
  264. Indexed arrays (lists); $ for one scalar element, @ for all
  265. .QS
  266. $foo[$i+2] = 3;    # set one element to 3
  267. @foo = ( 1, 3, 5 );# init whole array
  268. @foo = ( ) ;    # initialize empty array
  269. @foo = @bar;    # copy whole @array
  270. @foo = @bar[$i..$i+5];  # copy slice of @array
  271. .QE
  272. .sp
  273. .2
  274. $#ARRAY is index of highest subscript, 
  275. so the script's name is \fB$0\fP and its arguments run 
  276. from $ARGV[0] through $ARGV[$#ARGV], inclusive.
  277. .sp
  278. .2
  279. Associative (hashed) arrays; $ for one scalar element, % for all
  280. .QS
  281. $frogs{'green'} += 23;    # 23 more green frogs
  282. $location{$x, $y, $z} = 'troll'; # multi-dim array
  283. %foo = %bar;        # copy whole %array
  284. @frogs{'green', 'blue', 'yellow'} = (3, 6, 9);
  285. .QE
  286. .\" ===========================================================================
  287. .BP "Special Array Variables"
  288. .2
  289. \fB@ARGV\fP    command line arguments
  290. .sp
  291. .2
  292. \fB@INC\fP    search path for files called with \fBdo\fP
  293. .sp
  294. .2
  295. \fB@_\fP    default for \fBsplit\fP and subroutine parameters
  296. .sp
  297. .2
  298. \fB%ENV\fP    the current enviroment; e.g. $ENV{'HOME'}
  299. .sp
  300. .2
  301. \fB%SIG\fP    used to set signal handlers
  302. .QS
  303.  
  304. sub trapped { 
  305.     print STDERR "Interrupted\e007\en";
  306.     exit 1;
  307. }
  308. $SIG{'INT'} = 'trapped';
  309. .QE
  310. .\" ===========================================================================
  311. .BP "Operators"
  312. \fIPerl\fP uses all of C's operators except for type casting and `\fB\s+1&\fP\s0' and `\fB\s+1*\fP\s0' as address operators, plus these
  313. .sp
  314. .2
  315. exponentiation:  \fB**\fP, \fB**=\fP
  316. .sp
  317. .2
  318. range operator: \fB\s+1..\fP\s0
  319. .QS
  320. $inheader = 1 if /^From / .. /^$/;
  321. if (1..10) { do foo(); }
  322. for $i (60..75) { do foo($i); }
  323. @new = @old[30..50];
  324.  
  325. .QE
  326. .2
  327. string concatenation: \fB\s+1.\fP\s0, \fB\s+1.=\fP\s0
  328. .QS
  329.  
  330. $x = $y \s+1.\s0 &frob(@list) \s+1.\s0 $z;
  331. $x \s+1.\s0= "\en";
  332. .QE
  333. .\" ===========================================================================
  334. .BP "Operators (continued)"
  335. .2
  336. string repetition: \fB\s+1x\fP\s0, \fB\s+1x=\fP\s0
  337. .QS
  338.  
  339. $bar = '-' x 72; # row of 72 dashes
  340.  
  341. .QE
  342. .2
  343. string tests: \fBeq, ne, lt, gt, le, ge\fP
  344. .QS
  345.  
  346. if ($x eq 'foo') { }
  347. if ($x ge 'red' ) { }
  348.  
  349. .QE
  350. .2
  351. file test operators like augmented \fI/bin/test\fP tests 
  352. work on strings or filehandles
  353. .QS
  354.  
  355. if (-e $file)  { } # file exists
  356. if (-z $file) { } # zero length 
  357. if (-O LOG) { }    # LOG owned by real uid
  358. die "$file not a text file" unless -T $file;
  359. .QE
  360. .\" ===========================================================================
  361. .BP "Flow Control"
  362. .2
  363. Unlike C, blocks always require enclosing braces \s+2{}\s0
  364. .sp
  365. .2
  366. \fBunless\fP and \fBuntil\fP are just \fBif\fP and \fBwhile\fP negated
  367. .sp
  368. .3
  369. if (EXPR) BLOCK else BLOCK
  370. .3
  371. if (EXPR) BLOCK elsif (EXPR) BLOCK else BLOCK
  372. .3
  373. while (EXPR) BLOCK
  374. .3 
  375. do BLOCK while EXPR
  376. .3
  377. for (EXPR; EXPR; EXPR) BLOCK
  378. .3
  379. foreach $VAR (LIST) BLOCK
  380. .sp
  381. .2
  382. For readability, \fBif\fP, \fBunless\fP, \fBwhile\fP, and \fBuntil\fP may be used as trailing statement modifiers as in \s-1BASIC-PLUS\s0
  383. .QS
  384.  
  385. return -1 unless $x > 0;
  386. .QE
  387. .\" ===========================================================================
  388. .BP "Flow Control (continued)"
  389. .2
  390. Use \fBnext\fP and \fBlast\fP rather than C's \fBcontinue\fP and \fBbreak\fP
  391. .sp
  392. .2
  393. \fBredo\fP restarts the current iteration, ignoring the loop test
  394. .sp
  395. .2
  396. Blocks (and \fBnext\fP, \fBlast\fP, and \fBredo\fP) take optional labels for 
  397. clearer loop control, avoiding the use of
  398. \fBgoto\fP to exit nested loops.
  399. .sp
  400. .2
  401. No \fBswitch\fP statement, but it's easy to roll your own
  402. .sp 
  403. .2
  404. \fBdo\fP takes 3 forms
  405. .3
  406. execute a block
  407. .br
  408. \f(TAdo { $x += $a[$i++] } until $i > $j;\fP
  409. .3
  410. execute a subroutine
  411. .br
  412. \f(TAdo foo($x, $y);\fP
  413. .3
  414. execute a file in current context
  415. .br
  416. \f(TAdo 'subroutines.pl';\fP
  417. .\" ===========================================================================
  418. .BP "Regular Expressions"
  419. .2
  420. Understands \fIegrep\fP regexps, plus
  421. .sp
  422. .nf
  423. .3
  424. \fB\ew\fP, \fB\eW\fP    alphanumerics plus \s+2_\s0 (and negation)
  425. .3
  426. \fB\ed\fP, \fB\eD\fP    digits (and negation)
  427. .3
  428. \fB\es\fP, \fB\eS\fP    white space (and negation)
  429. .3
  430. \fB\eb\fP, \fB\eB\fP    word boundaries (and negation)
  431. .fi
  432. .sp
  433. .2
  434. C-style escapes recognized, like \fB\et\fP, \fB\en\fP, \fB\e034\fP
  435. .sp
  436. .2
  437. Don't escape these characters for their special meaning:\ \ \fB(\ )\ |\ {\ }\ +\fP
  438. .sp
  439. .2
  440. Character classes may contain metas, e.g. \fB[\ew.$]\fP
  441. .sp
  442. .2
  443. Special variables: \fB$&\fP means all text matched, \fB$`\fP is text before match, \fB$'\fP is text after match.
  444. .\" ===========================================================================
  445. .BP "Regular Expressions (continued)"
  446. .2
  447. Use \fB\e1\fP .. \fB\e9\fP within rexprs; \fB$1\fP .. \fB$9\fP outside
  448. .QS
  449.  
  450. if (/^this (red|blue|green) (bat|ball) is \e1/) 
  451.     { ($color, $object) = ($1, $2); } 
  452. ($color, $object) = 
  453.     /^this (red|blue|green) (bat|ball) is \e1/; 
  454.  
  455. .QE
  456. .2
  457. Substitute and translation operators are like \fIsed\fP's \fBs\fP and \fBy\fP.
  458. .\"    Substitutes recognize \fB/g\fP and \fB/i\fP switches.
  459. .QS
  460. s/alpha/beta/;
  461. s/(.)\e1/$1/g;
  462. y/A-Z/a-z/;
  463.  
  464. .QE
  465. .2
  466. Use \fB=~\fP and \fB!~\fP to match against variables
  467. .QS
  468.  
  469. if ($foo !~ /^\ew+$/) { exit 1; } 
  470. $foo =~ s/\ebtexas\eb/TX/i;   
  471. .QE
  472. .\" ===========================================================================
  473. .BP "I/O"
  474. .2
  475. Filehandles have their own distinct namespaces, but are typically 
  476. all upper case for clarity.  Pre-defined filehandles are 
  477. STDIN, STDOUT, STDERR.
  478. .sp
  479. .2
  480. Mentioning a filehandle in angle brackets reads next line in scalar context, 
  481. all lines in an array context; newlines are left intact.
  482. .QS
  483.  
  484. $line = <TEMP>;
  485. @lines = <TEMP>;
  486.  
  487. .QE
  488. .2
  489. \fB<>\fR means all files supplied on command line (or STDIN if none).  
  490. When used this way,
  491. $ARGV is the current filename.
  492. .sp
  493. .2
  494. When used in a \fBwhile\fP construct, input lines are 
  495. automatically assigned to the \fB$_\fP variable.
  496. .BP "I/O (continued)"
  497. .2
  498. Usually iterate over file a line at a time, assigning to \fB$_\fP
  499. each time and using that as the default operand.
  500. .QS
  501.  
  502. while ( <> ) {
  503.     next if /^#/;    # skip comments
  504.     s/left/right/g;    # global substitute
  505.     print;        # print $_
  506.  
  507. .QE
  508. .2
  509. If not using the pseudo-file \fB<>\fP, open a filehandle:
  510. .QS
  511.  
  512. open (PWD,      "/etc/passwd");
  513. open (TMP,      ">/tmp/foobar.$$");
  514. open (LOG,      ">>logfile");
  515. open (TOPIPE,   "| lpr"); 
  516. open (FROMPIPE, "/usr/etc/netstat -a |");
  517. .QE
  518. .\"    .2
  519. .\"    Filehandles may be used indirectly
  520. .\"    .QS
  521. .\"    $outfile = 'TEMP';
  522. .\"    $line = <$outfile>;
  523. .\"    .QE
  524. .\" ===========================================================================
  525. .BP "I/O (continued)"
  526. .2
  527. May also use \fBgetc\fP for character I/O and \fBread\fP for raw I/O
  528. .sp
  529. .2
  530. Access to \fBeof\fP, \fBseek\fP, \fBclose\fP, \fBflock\fP, \fBioctl\fP, \fBfcntl\fP, and \fBselect\fP calls for use with filehandles.
  531. .sp 
  532. .2
  533. Access to \fBmkdir\fP, \fPrmdir\fP, \fBchmod\fP, \fBchown\fP, \fBlink\fP, 
  534. \fBsymlink\fP (if supported),
  535. \fBstat\fP, \fBrename\fP, \fBunlink\fP calls for use with filenames.
  536. .sp 
  537. .2
  538. Pass \fBprintf\fP a filehandle as its first argument unless printing to STDOUT
  539. .QS
  540.  
  541. \f(TAprintf LOG "%-8s %s: weird bits: %08x\en", 
  542.     $program, &ctime, $bits;\fP
  543.  
  544. .QE
  545. .2 "Associative arrays may be bound to \fBdbm\fP files with \fBdbmopen()\fP" 
  546. .\" ===========================================================================
  547. .BP "System Functions"
  548. A plethora of functions from the C library are provided as built-ins, including
  549. most system calls.  These include 
  550. .sp
  551. .2
  552. \fBchdir\fP,
  553. \fBchroot\fP,
  554. \fBexec\fP,
  555. \fBexit\fP,
  556. \fBfork\fP,
  557. \fBgetlogin\fP,
  558. \fBgetpgrp\fP,
  559. \fBgetppid\fP,
  560. \fBkill\fP,
  561. \fBsetpgrp\fP,
  562. \fBsetpriority\fP,
  563. \fBsleep\fP,
  564. \fBsyscall\fP,
  565. \fBsystem\fP,
  566. \fBtimes\fP,
  567. \fBumask\fP,
  568. \fBwait\fP.
  569. .sp
  570. .2
  571. If your system has Berkeley-style networking, 
  572. \fBbind\fP,
  573. \fBconnect\fP,
  574. \fBsend\fP,
  575. \fBgetsockname\fP,
  576. \fBgetsockopt\fP,
  577. \fBgetpeername\fP,
  578. \fBrecv\fP,
  579. \fBlisten\fP,
  580. \fBsocket\fP,
  581. \fBsocketpair\fP.
  582. .sp
  583. .2
  584. \fBgetpw*\fP,
  585. \fBgetgr*\fP,
  586. \fBgethost*\fP,
  587. \fBgetnet*\fP,
  588. \fBgetserv*\fP, and
  589. \fBgetproto*\fP.
  590. .sp
  591. .2
  592. \fBpack\fP and \fBunpack\fP can be used 
  593. for manipulating binary data.
  594. .QE
  595. .\" ===========================================================================
  596. .BP "Directory Access"
  597. .1
  598. Three methods of accessing directories are provided.  
  599. .sp
  600. .2 
  601. You may open a pipe from \fI/bin/ls\fP like this:
  602. .QS
  603. open(FILES,"/bin/ls *.c |");
  604. while ($file = <FILES>) { chop($file); ... }
  605.  
  606. .QE
  607. .2
  608. The directory-reading routines are provided as built-ins and operate
  609. on directory handles.  Supported routines are 
  610. \fBopendir\fP,
  611. \fBreaddir\fP,
  612. \fBclosedir\fP,
  613. \fBseekdir\fP,
  614. \fBtelldir\fP, and
  615. \fBrewinddir\fP.
  616. .sp
  617. .2
  618. The easiest way is to use \fIperl\fP's file globbing notation.  A string enclosed in 
  619. angle brackets containing shell meta-characters evaluates to a list
  620. of matching filenames.
  621. .QS
  622.  
  623. foreach $x ( <*.[ch]> ) { rename($x, "$x.old"); }
  624. chmod 0644, <*.c>;
  625. .QE
  626. .\" ===========================================================================
  627. .BP "Subroutines"
  628. .2
  629. Subroutines called either with `\fBdo\fP' operator or with `\fB&\fP'.
  630. Any of the three principal data types may be passed as parameters
  631. or used as a return value.
  632. .QS
  633. .sp
  634. do foo(1.43);
  635. .sp
  636. do foo(@list)
  637. .sp
  638. $x = &foo('red', 3, @others);
  639. .sp
  640. @list = &foo(@olist);
  641. .sp
  642. %foo = &foo($foo, @foo);
  643. .QE
  644. .\" ===========================================================================
  645. .BP "Subroutines (continued)"
  646. .2
  647. Parameters are received by the subroutine in the special array \fB@_\fP.
  648. If desired, these can be copied to local variables.  This is especially 
  649. useful for recursive subroutines.
  650. .QS
  651.  
  652. $result = &simple($alpha, $beta, @tutti);
  653. sub simple {
  654.     local($x, $y, @rest) = @_;
  655.     local($sum, %seen);
  656.     return $sum;
  657.  
  658. .QE
  659. .2
  660. Subroutines may also be called indirectly
  661. .QS
  662.  
  663. $foo = 'some_routine';
  664. do $foo(@list)
  665. ($x, $y, $z) = do $foo(%maps);
  666. .QE
  667. .\" ===========================================================================
  668. .BP "Formatted I/O"
  669. .2 
  670. Besides \fBprintf\fP, formatted I/O can be done with \fBformat\fP and 
  671. \fBwrite\fP statements.  
  672. .sp
  673. .2
  674. Automatic pagination and printing of headers.
  675. .sp
  676. .2
  677. Picture description facilitates lining up multi-line output
  678. .sp
  679. .2
  680. Fields in picture may be left or right-justified or centered
  681. .sp
  682. .2
  683. Multi-line text-block filling is provided, something like having a \fB%s\fP format string with a built-in pipe to \fBfmt\fP)
  684. .sp
  685. .2
  686. These special scalar variables are useful:
  687. .3
  688. \fB$%\fP for current page number, 
  689. .3
  690. \fB$=\fP for current page length (default 60)
  691. .3
  692. \fB$-\fP for lines left on page
  693. .\" ===========================================================================
  694. .BP "Formatted I/O (example)"
  695. .nf
  696. .nr VS 12
  697. .nr PS 10
  698. .vs 12
  699. .ps 10
  700. \f(TA# a report from a bug report form; taken from perl man page
  701. format top =
  702. \&                        Bug Reports
  703. @<<<<<<<<<<<<<<<<<<<<<<<     @|||         @>>>>>>>>>>>>>>>>>>>>>>>
  704. $system,                      $%,         $date
  705. ------------------------------------------------------------------
  706. \&.
  707.  
  708.  
  709. format STDOUT =
  710. Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  711. \&         $subject
  712. Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  713. \&       $index,                       $description
  714. Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  715. \&          $priority,        $date,   $description
  716. From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  717. \&      $from,                         $description
  718. Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  719. \&             $programmer,            $description
  720. \&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  721. \&                                     $description
  722. \&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  723. \&                                     $description
  724. \&~                                    ^<<<<<<<<<<<<<<<<<<<<<<<...
  725. \&                                     $description
  726. \&.\s0\fP
  727. .nr VS 21
  728. .nr PS 16
  729. .vs 21
  730. .ps 16
  731. .fi
  732. .\" ===========================================================================
  733. .BP "Built-in Array Functions"
  734. .2
  735. Indexed arrays function as lists; you can add items to or remove them from 
  736. either end using these functions:
  737. .3 
  738. \fBpop\fP    remove last value from end of array
  739. .3 
  740. \fBpush\fP    add values to end of array
  741. .3 
  742. \fBshift\fP    remove first value from front of array
  743. .3 
  744. \fBunshift\fP    add values to front of array
  745. .sp
  746. .1 "For example"
  747. .QS
  748.  
  749. push(@list, $bar);
  750. push(@list, @rest);
  751. $tos = pop(@list);
  752. while ( $arg = shift(@ARGV) )  { }
  753. unshift( @ARGV, 'zeroth arg', 'first arg');
  754. .QE
  755. .\" ===========================================================================
  756. .BP "Built-in Array Functions (split and join)"
  757. .2 
  758. \fBsplit\fP breaks up a string into an array of new strings.  You can
  759. \fBsplit\fP on arbitrary regular expressions, limit the number of fields you 
  760. \fBsplit\fP into, and save the delimiters if you want.  
  761. .QS
  762.  
  763. @list = split(/[, \et]+/, $expr);
  764. while (<PASSWD>) { 
  765.     ($login, $passwd, $uid, $gid, $gcos, 
  766.     $home, $shell) = split(/:/); 
  767. }    
  768.  
  769. .QE
  770. .2
  771. The inverse of \fBsplit\fP is \fBjoin\fP.
  772. .QS
  773.  
  774. $line = join(':', $login, $passwd, $uid,
  775.           $gid, $gcos, $home, $shell);
  776. .QE
  777. .\" ===========================================================================
  778. .BP "Built-in Array Functions (sort, grep, reverse)"
  779. .2
  780. \fBreverse\fP inverts a list.
  781. .QS
  782.  
  783. foreach $tick (reverse 0..10) { }
  784. .QE
  785. .sp
  786. .2
  787. \fBsort\fP returns a new array with the elements ordered according
  788. to their \s-1ASCII\s0 values.  Use your own routine for different collating.
  789. .QS
  790.  
  791. print sort @list;
  792. sub numerically { $a - $b; }
  793. print sort numerically @list;
  794.  
  795. .QE
  796. .2
  797. \fBgrep\fP returns a new list consisting of all the elements for which 
  798. a given expression is true.  For example, this
  799. will delete all lines with leading pound signs:
  800. .QS
  801.  
  802. @lines = grep(!/^#/, @lines);
  803. .QE
  804. .\" ===========================================================================
  805. .BP "Built-in Array Functions (%arrays)"
  806. For manipulating associative arrays, the \fBkeys\fP and \fBvalues\fP
  807. functions return indexed arrays of the indices and data values respectively.
  808. \fPeach\fP is used to iterate through an associative array to retrieve
  809. one \fB($key,$value)\fP pair at a time.
  810. .QS
  811.  
  812. while (($key,$value) = each %array) {
  813.     printf "%s is %s\en", $key, $value;
  814.  
  815. foreach $key (keys %array) {
  816.     printf "%s is %s\en", $key, $array{$key};
  817.  
  818. print reverse sort values %array;
  819. .QE
  820. .\" ===========================================================================
  821. .BP "String functions"
  822. .2 
  823. Besides the powerful regular expression features, several well-known 
  824. C string manipulation functions are provided, including \fBcrypt\fP, 
  825. \fBindex\fP, \fBrindex\fP, \fBlength\fP, \fBsubstr\fP, and \fBsprintf\fP.
  826. .sp
  827. .2
  828. The \fBchop\fP function efficiently removes the last character from a string.
  829. It's usually used to delete the trailing newline on input lines.  
  830. Like many \fIperl\fP operators, it works on \fB$_\fP
  831. if no operand is given.
  832. .QS
  833.  
  834. chop($line);
  835. chop ($host = `hostname`);
  836. while (<STDIN>) { 
  837.     chop; ...
  838. }
  839. .QE
  840. .\" ===========================================================================
  841. .BP "String functions (continued)"
  842. .2
  843. The \fBeval\fP operator lets you execute dynamically generated code.  For
  844. example, to process any command line arguments of the form \fBvariable=value\fP, 
  845. place this at the top of your script:
  846. .QS
  847.  
  848. eval '$'.$1."'$2';" 
  849.     while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
  850.  
  851. .QE
  852. The \fBeval\fP operator is also useful for run-time testing of system-dependent features which would
  853. otherwise trigger fatal errors.  For example, not all systems support the 
  854. \fBsymlink\fP or \fBdbmopen\fP; you could test for their existence by executing
  855. the statements within an \fBeval\fP and testing the special variable \fB$@\fP, 
  856. which contains the text of the run-time error message if anything went wrong.
  857. .\" ===========================================================================
  858. .BP "Suid Scripts"
  859. .2
  860. \fIPerl\fP programs can be made to run setuid,
  861. and can actually be more secure than the corresponding \fIC\fP program.
  862. .sp
  863. .2
  864. Because interpreters have no guarantee that the filename they get as the first
  865. argument is the same file that was \fBexec\fP'ed, \fIperl\fP
  866. won't let your run a setuid script on a system where setuid scripts
  867. are not disabled.
  868. .sp
  869. .2
  870. Using a dataflow tracing mechanism
  871. triggered by setuid execution, perl can tell what data is safe to use and
  872. what data comes from an external source and thus is \*(lqtainted.\*(rq
  873. .sp
  874. .2
  875. Tainted data may not be used directly or
  876. indirectly in any 
  877. command that modifies files, directories or processes
  878. or else a fatal run-time error will result.
  879. .\" ===========================================================================
  880. .BP "Debugging and Packages"
  881. .2 
  882. When invoked with the \fB-d\fP switch, \fIperl\fP runs your program
  883. under a symbolic debugger (written in \fIperl\fP) somewhat similar to \fIsdb\fP in syntax.
  884. Amongst other things,
  885. breakpoints may be set, variables examined or changed, and
  886. call tracebacks printed out.  Because it uses \fBeval\fP on your code,
  887. you can execute any arbitrary perl code you want
  888. from the debugger.
  889. .sp 
  890. .2 
  891. Using \fBpackage\fPs you can write modules with separate namespaces to 
  892. avoid naming conflicts in library routines.  The debugger uses this 
  893. to keep its variables separate from yours.  Variable are accessed by
  894. the \fBpackage'name\fP notation, as in this line from the debugger:
  895. .sp
  896. .QS
  897. $DB'stop[$DB'line] =~ s/;9$//;
  898. .QE
  899. .\" ===========================================================================
  900. .BP "Command Line Options"
  901. The following are the more important command line switches recognized by \fIperl\fP:
  902. .sp
  903. .2
  904. \fB\-v\fP    print out version string
  905. .2
  906. \fB\-w\fP    issue warnings about error-prone constructs
  907. .2
  908. \fB\-d\fP    run script under the debugger
  909. .2
  910. \fB\-e\fP    like \fIsed\fP: used to enter single command lines
  911. .2
  912. \fB\-n\fP    loop around input like \fIsed -n\fP 
  913. .2
  914. \fB\-p\fP    as with \fB-n\fP but print out each line 
  915. .2
  916. \fB\-i\fP    edit files in place
  917. .2 
  918. \fB\-a\fP    turn on autosplit mode (like \fIawk\fP) into \fB@F\fP array
  919. .2
  920. \fB\-P\fP    call C pre-processor on script
  921. .\" ===========================================================================
  922. .BP "Examples: Command Line"
  923. .QS
  924. # output current version
  925. perl -v
  926.  
  927. # simplest perl program
  928. perl -e 'print "hello, world.\en";'
  929.  
  930. # useful at end of "find foo -print"
  931. perl -n -e 'chop;unlink;'
  932.  
  933. # add first and last columns (filter)
  934. perl -a -n -e 'print $F[0] + $F[$#F], "\en";'
  935.  
  936. # in-place edit of *.c files changing all foo to bar
  937. perl -p -i -e 's/\ebfoo\eb/bar/g;' *.c
  938.  
  939. # run a script under the debugger
  940. perl -d myscript
  941. .QE
  942.