Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man1 / perlintro.1
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "PERLINTRO 1"
132.TH PERLINTRO 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134perlintro \-\- a brief introduction and overview of Perl
135.SH "DESCRIPTION"
136.IX Header "DESCRIPTION"
137This document is intended to give you a quick overview of the Perl
138programming language, along with pointers to further documentation. It
139is intended as a \*(L"bootstrap\*(R" guide for those who are new to the
140language, and provides just enough information for you to be able to
141read other peoples' Perl and understand roughly what it's doing, or
142write your own simple scripts.
143.PP
144This introductory document does not aim to be complete. It does not
145even aim to be entirely accurate. In some cases perfection has been
146sacrificed in the goal of getting the general idea across. You are
147\&\fIstrongly\fR advised to follow this introduction with more information
148from the full Perl manual, the table of contents to which can be found
149in perltoc.
150.PP
151Throughout this document you'll see references to other parts of the
152Perl documentation. You can read that documentation using the \f(CW\*(C`perldoc\*(C'\fR
153command or whatever method you're using to read this document.
154.Sh "What is Perl?"
155.IX Subsection "What is Perl?"
156Perl is a general-purpose programming language originally developed for
157text manipulation and now used for a wide range of tasks including
158system administration, web development, network programming, \s-1GUI\s0
159development, and more.
160.PP
161The language is intended to be practical (easy to use, efficient,
162complete) rather than beautiful (tiny, elegant, minimal). Its major
163features are that it's easy to use, supports both procedural and
164object-oriented (\s-1OO\s0) programming, has powerful built-in support for text
165processing, and has one of the world's most impressive collections of
166third-party modules.
167.PP
168Different definitions of Perl are given in perl, perlfaq1 and
169no doubt other places. From this we can determine that Perl is different
170things to different people, but that lots of people think it's at least
171worth writing about.
172.Sh "Running Perl programs"
173.IX Subsection "Running Perl programs"
174To run a Perl program from the Unix command line:
175.PP
176.Vb 1
177\& perl progname.pl
178.Ve
179.PP
180Alternatively, put this as the first line of your script:
181.PP
182.Vb 1
183\& #!/usr/bin/env perl
184.Ve
185.PP
186\&... and run the script as \f(CW\*(C`/path/to/script.pl\*(C'\fR. Of course, it'll need
187to be executable first, so \f(CW\*(C`chmod 755 script.pl\*(C'\fR (under Unix).
188.PP
189For more information, including instructions for other platforms such as
190Windows and Mac \s-1OS\s0, read perlrun.
191.Sh "Basic syntax overview"
192.IX Subsection "Basic syntax overview"
193A Perl script or program consists of one or more statements. These
194statements are simply written in the script in a straightforward
195fashion. There is no need to have a \f(CW\*(C`main()\*(C'\fR function or anything of
196that kind.
197.PP
198Perl statements end in a semi\-colon:
199.PP
200.Vb 1
201\& print "Hello, world";
202.Ve
203.PP
204Comments start with a hash symbol and run to the end of the line
205.PP
206.Vb 1
207\& # This is a comment
208.Ve
209.PP
210Whitespace is irrelevant:
211.PP
212.Vb 3
213\& print
214\& "Hello, world"
215\& ;
216.Ve
217.PP
218\&... except inside quoted strings:
219.PP
220.Vb 3
221\& # this would print with a linebreak in the middle
222\& print "Hello
223\& world";
224.Ve
225.PP
226Double quotes or single quotes may be used around literal strings:
227.PP
228.Vb 2
229\& print "Hello, world";
230\& print 'Hello, world';
231.Ve
232.PP
233However, only double quotes \*(L"interpolate\*(R" variables and special
234characters such as newlines (\f(CW\*(C`\en\*(C'\fR):
235.PP
236.Vb 2
237\& print "Hello, $name\en"; # works fine
238\& print 'Hello, $name\en'; # prints $name\en literally
239.Ve
240.PP
241Numbers don't need quotes around them:
242.PP
243.Vb 1
244\& print 42;
245.Ve
246.PP
247You can use parentheses for functions' arguments or omit them
248according to your personal taste. They are only required
249occasionally to clarify issues of precedence.
250.PP
251.Vb 2
252\& print("Hello, world\en");
253\& print "Hello, world\en";
254.Ve
255.PP
256More detailed information about Perl syntax can be found in perlsyn.
257.Sh "Perl variable types"
258.IX Subsection "Perl variable types"
259Perl has three main variable types: scalars, arrays, and hashes.
260.IP "Scalars" 4
261.IX Item "Scalars"
262A scalar represents a single value:
263.Sp
264.Vb 2
265\& my $animal = "camel";
266\& my $answer = 42;
267.Ve
268.Sp
269Scalar values can be strings, integers or floating point numbers, and Perl
270will automatically convert between them as required. There is no need
271to pre-declare your variable types.
272.Sp
273Scalar values can be used in various ways:
274.Sp
275.Vb 3
276\& print $animal;
277\& print "The animal is $animal\en";
278\& print "The square of $answer is ", $answer * $answer, "\en";
279.Ve
280.Sp
281There are a number of \*(L"magic\*(R" scalars with names that look like
282punctuation or line noise. These special variables are used for all
283kinds of purposes, and are documented in perlvar. The only one you
284need to know about for now is \f(CW$_\fR which is the \*(L"default variable\*(R".
285It's used as the default argument to a number of functions in Perl, and
286it's set implicitly by certain looping constructs.
287.Sp
288.Vb 1
289\& print; # prints contents of $_ by default
290.Ve
291.IP "Arrays" 4
292.IX Item "Arrays"
293An array represents a list of values:
294.Sp
295.Vb 3
296\& my @animals = ("camel", "llama", "owl");
297\& my @numbers = (23, 42, 69);
298\& my @mixed = ("camel", 42, 1.23);
299.Ve
300.Sp
301Arrays are zero\-indexed. Here's how you get at elements in an array:
302.Sp
303.Vb 2
304\& print $animals[0]; # prints "camel"
305\& print $animals[1]; # prints "llama"
306.Ve
307.Sp
308The special variable \f(CW$#array\fR tells you the index of the last element
309of an array:
310.Sp
311.Vb 1
312\& print $mixed[$#mixed]; # last element, prints 1.23
313.Ve
314.Sp
315You might be tempted to use \f(CW\*(C`$#array + 1\*(C'\fR to tell you how many items there
316are in an array. Don't bother. As it happens, using \f(CW@array\fR where Perl
317expects to find a scalar value (\*(L"in scalar context\*(R") will give you the number
318of elements in the array:
319.Sp
320.Vb 1
321\& if (@animals < 5) { ... }
322.Ve
323.Sp
324The elements we're getting from the array start with a \f(CW\*(C`$\*(C'\fR because
325we're getting just a single value out of the array \*(-- you ask for a scalar,
326you get a scalar.
327.Sp
328To get multiple values from an array:
329.Sp
330.Vb 3
331\& @animals[0,1]; # gives ("camel", "llama");
332\& @animals[0..2]; # gives ("camel", "llama", "owl");
333\& @animals[1..$#animals]; # gives all except the first element
334.Ve
335.Sp
336This is called an \*(L"array slice\*(R".
337.Sp
338You can do various useful things to lists:
339.Sp
340.Vb 2
341\& my @sorted = sort @animals;
342\& my @backwards = reverse @numbers;
343.Ve
344.Sp
345There are a couple of special arrays too, such as \f(CW@ARGV\fR (the command
346line arguments to your script) and \f(CW@_\fR (the arguments passed to a
347subroutine). These are documented in perlvar.
348.IP "Hashes" 4
349.IX Item "Hashes"
350A hash represents a set of key/value pairs:
351.Sp
352.Vb 1
353\& my %fruit_color = ("apple", "red", "banana", "yellow");
354.Ve
355.Sp
356You can use whitespace and the \f(CW\*(C`=>\*(C'\fR operator to lay them out more
357nicely:
358.Sp
359.Vb 4
360\& my %fruit_color = (
361\& apple => "red",
362\& banana => "yellow",
363\& );
364.Ve
365.Sp
366To get at hash elements:
367.Sp
368.Vb 1
369\& $fruit_color{"apple"}; # gives "red"
370.Ve
371.Sp
372You can get at lists of keys and values with \f(CW\*(C`keys()\*(C'\fR and
373\&\f(CW\*(C`values()\*(C'\fR.
374.Sp
375.Vb 2
376\& my @fruits = keys %fruit_colors;
377\& my @colors = values %fruit_colors;
378.Ve
379.Sp
380Hashes have no particular internal order, though you can sort the keys
381and loop through them.
382.Sp
383Just like special scalars and arrays, there are also special hashes.
384The most well known of these is \f(CW%ENV\fR which contains environment
385variables. Read all about it (and other special variables) in
386perlvar.
387.PP
388Scalars, arrays and hashes are documented more fully in perldata.
389.PP
390More complex data types can be constructed using references, which allow
391you to build lists and hashes within lists and hashes.
392.PP
393A reference is a scalar value and can refer to any other Perl data
394type. So by storing a reference as the value of an array or hash
395element, you can easily create lists and hashes within lists and
396hashes. The following example shows a 2 level hash of hash
397structure using anonymous hash references.
398.PP
399.Vb 14
400\& my $variables = {
401\& scalar => {
402\& description => "single item",
403\& sigil => '$',
404\& },
405\& array => {
406\& description => "ordered list of items",
407\& sigil => '@',
408\& },
409\& hash => {
410\& description => "key/value pairs",
411\& sigil => '%',
412\& },
413\& };
414.Ve
415.PP
416.Vb 1
417\& print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\en";
418.Ve
419.PP
420Exhaustive information on the topic of references can be found in
421perlreftut, perllol, perlref and perldsc.
422.Sh "Variable scoping"
423.IX Subsection "Variable scoping"
424Throughout the previous section all the examples have used the syntax:
425.PP
426.Vb 1
427\& my $var = "value";
428.Ve
429.PP
430The \f(CW\*(C`my\*(C'\fR is actually not required; you could just use:
431.PP
432.Vb 1
433\& $var = "value";
434.Ve
435.PP
436However, the above usage will create global variables throughout your
437program, which is bad programming practice. \f(CW\*(C`my\*(C'\fR creates lexically
438scoped variables instead. The variables are scoped to the block
439(i.e. a bunch of statements surrounded by curly\-braces) in which they
440are defined.
441.PP
442.Vb 8
443\& my $a = "foo";
444\& if ($some_condition) {
445\& my $b = "bar";
446\& print $a; # prints "foo"
447\& print $b; # prints "bar"
448\& }
449\& print $a; # prints "foo"
450\& print $b; # prints nothing; $b has fallen out of scope
451.Ve
452.PP
453Using \f(CW\*(C`my\*(C'\fR in combination with a \f(CW\*(C`use strict;\*(C'\fR at the top of
454your Perl scripts means that the interpreter will pick up certain common
455programming errors. For instance, in the example above, the final
456\&\f(CW\*(C`print $b\*(C'\fR would cause a compile-time error and prevent you from
457running the program. Using \f(CW\*(C`strict\*(C'\fR is highly recommended.
458.Sh "Conditional and looping constructs"
459.IX Subsection "Conditional and looping constructs"
460Perl has most of the usual conditional and looping constructs except for
461case/switch (but if you really want it, there is a Switch module in Perl
4625.8 and newer, and on \s-1CPAN\s0. See the section on modules, below, for more
463information about modules and \s-1CPAN\s0).
464.PP
465The conditions can be any Perl expression. See the list of operators in
466the next section for information on comparison and boolean logic operators,
467which are commonly used in conditional statements.
468.IP "if" 4
469.IX Item "if"
470.Vb 7
471\& if ( condition ) {
472\& ...
473\& } elsif ( other condition ) {
474\& ...
475\& } else {
476\& ...
477\& }
478.Ve
479.Sp
480There's also a negated version of it:
481.Sp
482.Vb 3
483\& unless ( condition ) {
484\& ...
485\& }
486.Ve
487.Sp
488This is provided as a more readable version of \f(CW\*(C`if (!\f(CIcondition\f(CW)\*(C'\fR.
489.Sp
490Note that the braces are required in Perl, even if you've only got one
491line in the block. However, there is a clever way of making your one-line
492conditional blocks more English like:
493.Sp
494.Vb 4
495\& # the traditional way
496\& if ($zippy) {
497\& print "Yow!";
498\& }
499.Ve
500.Sp
501.Vb 3
502\& # the Perlish post-condition way
503\& print "Yow!" if $zippy;
504\& print "We have no bananas" unless $bananas;
505.Ve
506.IP "while" 4
507.IX Item "while"
508.Vb 3
509\& while ( condition ) {
510\& ...
511\& }
512.Ve
513.Sp
514There's also a negated version, for the same reason we have \f(CW\*(C`unless\*(C'\fR:
515.Sp
516.Vb 3
517\& until ( condition ) {
518\& ...
519\& }
520.Ve
521.Sp
522You can also use \f(CW\*(C`while\*(C'\fR in a post\-condition:
523.Sp
524.Vb 1
525\& print "LA LA LA\en" while 1; # loops forever
526.Ve
527.IP "for" 4
528.IX Item "for"
529Exactly like C:
530.Sp
531.Vb 3
532\& for ($i=0; $i <= $max; $i++) {
533\& ...
534\& }
535.Ve
536.Sp
537The C style for loop is rarely needed in Perl since Perl provides
538the more friendly list scanning \f(CW\*(C`foreach\*(C'\fR loop.
539.IP "foreach" 4
540.IX Item "foreach"
541.Vb 3
542\& foreach (@array) {
543\& print "This element is $_\en";
544\& }
545.Ve
546.Sp
547.Vb 4
548\& # you don't have to use the default $_ either...
549\& foreach my $key (keys %hash) {
550\& print "The value of $key is $hash{$key}\en";
551\& }
552.Ve
553.PP
554For more detail on looping constructs (and some that weren't mentioned in
555this overview) see perlsyn.
556.Sh "Builtin operators and functions"
557.IX Subsection "Builtin operators and functions"
558Perl comes with a wide selection of builtin functions. Some of the ones
559we've already seen include \f(CW\*(C`print\*(C'\fR, \f(CW\*(C`sort\*(C'\fR and \f(CW\*(C`reverse\*(C'\fR. A list of
560them is given at the start of perlfunc and you can easily read
561about any given function by using \f(CW\*(C`perldoc \-f \f(CIfunctionname\f(CW\*(C'\fR.
562.PP
563Perl operators are documented in full in perlop, but here are a few
564of the most common ones:
565.IP "Arithmetic" 4
566.IX Item "Arithmetic"
567.Vb 4
568\& + addition
569\& - subtraction
570\& * multiplication
571\& / division
572.Ve
573.IP "Numeric comparison" 4
574.IX Item "Numeric comparison"
575.Vb 6
576\& == equality
577\& != inequality
578\& < less than
579\& > greater than
580\& <= less than or equal
581\& >= greater than or equal
582.Ve
583.IP "String comparison" 4
584.IX Item "String comparison"
585.Vb 6
586\& eq equality
587\& ne inequality
588\& lt less than
589\& gt greater than
590\& le less than or equal
591\& ge greater than or equal
592.Ve
593.Sp
594(Why do we have separate numeric and string comparisons? Because we don't
595have special variable types, and Perl needs to know whether to sort
596numerically (where 99 is less than 100) or alphabetically (where 100 comes
597before 99).
598.IP "Boolean logic" 4
599.IX Item "Boolean logic"
600.Vb 3
601\& && and
602\& || or
603\& ! not
604.Ve
605.Sp
606(\f(CW\*(C`and\*(C'\fR, \f(CW\*(C`or\*(C'\fR and \f(CW\*(C`not\*(C'\fR aren't just in the above table as descriptions
607of the operators \*(-- they're also supported as operators in their own
608right. They're more readable than the C\-style operators, but have
609different precedence to \f(CW\*(C`&&\*(C'\fR and friends. Check perlop for more
610detail.)
611.IP "Miscellaneous" 4
612.IX Item "Miscellaneous"
613.Vb 4
614\& = assignment
615\& . string concatenation
616\& x string multiplication
617\& .. range operator (creates a list of numbers)
618.Ve
619.PP
620Many operators can be combined with a \f(CW\*(C`=\*(C'\fR as follows:
621.PP
622.Vb 3
623\& $a += 1; # same as $a = $a + 1
624\& $a -= 1; # same as $a = $a - 1
625\& $a .= "\en"; # same as $a = $a . "\en";
626.Ve
627.Sh "Files and I/O"
628.IX Subsection "Files and I/O"
629You can open a file for input or output using the \f(CW\*(C`open()\*(C'\fR function.
630It's documented in extravagant detail in perlfunc and perlopentut,
631but in short:
632.PP
633.Vb 3
634\& open(INFILE, "input.txt") or die "Can't open input.txt: $!";
635\& open(OUTFILE, ">output.txt") or die "Can't open output.txt: $!";
636\& open(LOGFILE, ">>my.log") or die "Can't open logfile: $!";
637.Ve
638.PP
639You can read from an open filehandle using the \f(CW\*(C`<>\*(C'\fR operator. In
640scalar context it reads a single line from the filehandle, and in list
641context it reads the whole file in, assigning each line to an element of
642the list:
643.PP
644.Vb 2
645\& my $line = <INFILE>;
646\& my @lines = <INFILE>;
647.Ve
648.PP
649Reading in the whole file at one time is called slurping. It can
650be useful but it may be a memory hog. Most text file processing
651can be done a line at a time with Perl's looping constructs.
652.PP
653The \f(CW\*(C`<>\*(C'\fR operator is most often seen in a \f(CW\*(C`while\*(C'\fR loop:
654.PP
655.Vb 3
656\& while (<INFILE>) { # assigns each line in turn to $_
657\& print "Just read in this line: $_";
658\& }
659.Ve
660.PP
661We've already seen how to print to standard output using \f(CW\*(C`print()\*(C'\fR.
662However, \f(CW\*(C`print()\*(C'\fR can also take an optional first argument specifying
663which filehandle to print to:
664.PP
665.Vb 3
666\& print STDERR "This is your final warning.\en";
667\& print OUTFILE $record;
668\& print LOGFILE $logmessage;
669.Ve
670.PP
671When you're done with your filehandles, you should \f(CW\*(C`close()\*(C'\fR them
672(though to be honest, Perl will clean up after you if you forget):
673.PP
674.Vb 1
675\& close INFILE;
676.Ve
677.Sh "Regular expressions"
678.IX Subsection "Regular expressions"
679Perl's regular expression support is both broad and deep, and is the
680subject of lengthy documentation in perlrequick, perlretut, and
681elsewhere. However, in short:
682.IP "Simple matching" 4
683.IX Item "Simple matching"
684.Vb 2
685\& if (/foo/) { ... } # true if $_ contains "foo"
686\& if ($a =~ /foo/) { ... } # true if $a contains "foo"
687.Ve
688.Sp
689The \f(CW\*(C`//\*(C'\fR matching operator is documented in perlop. It operates on
690\&\f(CW$_\fR by default, or can be bound to another variable using the \f(CW\*(C`=~\*(C'\fR
691binding operator (also documented in perlop).
692.IP "Simple substitution" 4
693.IX Item "Simple substitution"
694.Vb 3
695\& s/foo/bar/; # replaces foo with bar in $_
696\& $a =~ s/foo/bar/; # replaces foo with bar in $a
697\& $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a
698.Ve
699.Sp
700The \f(CW\*(C`s///\*(C'\fR substitution operator is documented in perlop.
701.IP "More complex regular expressions" 4
702.IX Item "More complex regular expressions"
703You don't just have to match on fixed strings. In fact, you can match
704on just about anything you could dream of by using more complex regular
705expressions. These are documented at great length in perlre, but for
706the meantime, here's a quick cheat sheet:
707.Sp
708.Vb 10
709\& . a single character
710\& \es a whitespace character (space, tab, newline)
711\& \eS non-whitespace character
712\& \ed a digit (0-9)
713\& \eD a non-digit
714\& \ew a word character (a-z, A-Z, 0-9, _)
715\& \eW a non-word character
716\& [aeiou] matches a single character in the given set
717\& [^aeiou] matches a single character outside the given set
718\& (foo|bar|baz) matches any of the alternatives specified
719.Ve
720.Sp
721.Vb 2
722\& ^ start of string
723\& $ end of string
724.Ve
725.Sp
726Quantifiers can be used to specify how many of the previous thing you
727want to match on, where \*(L"thing\*(R" means either a literal character, one
728of the metacharacters listed above, or a group of characters or
729metacharacters in parentheses.
730.Sp
731.Vb 6
732\& * zero or more of the previous thing
733\& + one or more of the previous thing
734\& ? zero or one of the previous thing
735\& {3} matches exactly 3 of the previous thing
736\& {3,6} matches between 3 and 6 of the previous thing
737\& {3,} matches 3 or more of the previous thing
738.Ve
739.Sp
740Some brief examples:
741.Sp
742.Vb 6
743\& /^\ed+/ string starts with one or more digits
744\& /^$/ nothing in the string (start and end are adjacent)
745\& /(\ed\es){3}/ a three digits, each followed by a whitespace
746\& character (eg "3 4 5 ")
747\& /(a.)+/ matches a string in which every odd-numbered letter
748\& is a (eg "abacadaf")
749.Ve
750.Sp
751.Vb 5
752\& # This loop reads from STDIN, and prints non-blank lines:
753\& while (<>) {
754\& next if /^$/;
755\& print;
756\& }
757.Ve
758.IP "Parentheses for capturing" 4
759.IX Item "Parentheses for capturing"
760As well as grouping, parentheses serve a second purpose. They can be
761used to capture the results of parts of the regexp match for later use.
762The results end up in \f(CW$1\fR, \f(CW$2\fR and so on.
763.Sp
764.Vb 1
765\& # a cheap and nasty way to break an email address up into parts
766.Ve
767.Sp
768.Vb 4
769\& if ($email =~ /([^@])+@(.+)/) {
770\& print "Username is $1\en";
771\& print "Hostname is $2\en";
772\& }
773.Ve
774.IP "Other regexp features" 4
775.IX Item "Other regexp features"
776Perl regexps also support backreferences, lookaheads, and all kinds of
777other complex details. Read all about them in perlrequick,
778perlretut, and perlre.
779.Sh "Writing subroutines"
780.IX Subsection "Writing subroutines"
781Writing subroutines is easy:
782.PP
783.Vb 4
784\& sub log {
785\& my $logmessage = shift;
786\& print LOGFILE $logmessage;
787\& }
788.Ve
789.PP
790What's that \f(CW\*(C`shift\*(C'\fR? Well, the arguments to a subroutine are available
791to us as a special array called \f(CW@_\fR (see perlvar for more on that).
792The default argument to the \f(CW\*(C`shift\*(C'\fR function just happens to be \f(CW@_\fR.
793So \f(CW\*(C`my $logmessage = shift;\*(C'\fR shifts the first item off the list of
794arguments and assigns it to \f(CW$logmessage\fR.
795.PP
796We can manipulate \f(CW@_\fR in other ways too:
797.PP
798.Vb 2
799\& my ($logmessage, $priority) = @_; # common
800\& my $logmessage = $_[0]; # uncommon, and ugly
801.Ve
802.PP
803Subroutines can also return values:
804.PP
805.Vb 5
806\& sub square {
807\& my $num = shift;
808\& my $result = $num * $num;
809\& return $result;
810\& }
811.Ve
812.PP
813For more information on writing subroutines, see perlsub.
814.Sh "\s-1OO\s0 Perl"
815.IX Subsection "OO Perl"
816\&\s-1OO\s0 Perl is relatively simple and is implemented using references which
817know what sort of object they are based on Perl's concept of packages.
818However, \s-1OO\s0 Perl is largely beyond the scope of this document.
819Read perlboot, perltoot, perltooc and perlobj.
820.PP
821As a beginning Perl programmer, your most common use of \s-1OO\s0 Perl will be
822in using third-party modules, which are documented below.
823.Sh "Using Perl modules"
824.IX Subsection "Using Perl modules"
825Perl modules provide a range of features to help you avoid reinventing
826the wheel, and can be downloaded from \s-1CPAN\s0 ( http://www.cpan.org/ ). A
827number of popular modules are included with the Perl distribution
828itself.
829.PP
830Categories of modules range from text manipulation to network protocols
831to database integration to graphics. A categorized list of modules is
832also available from \s-1CPAN\s0.
833.PP
834To learn how to install modules you download from \s-1CPAN\s0, read
835perlmodinstall
836.PP
837To learn how to use a particular module, use \f(CW\*(C`perldoc \f(CIModule::Name\f(CW\*(C'\fR.
838Typically you will want to \f(CW\*(C`use \f(CIModule::Name\f(CW\*(C'\fR, which will then give
839you access to exported functions or an \s-1OO\s0 interface to the module.
840.PP
841perlfaq contains questions and answers related to many common
842tasks, and often provides suggestions for good \s-1CPAN\s0 modules to use.
843.PP
844perlmod describes Perl modules in general. perlmodlib lists the
845modules which came with your Perl installation.
846.PP
847If you feel the urge to write Perl modules, perlnewmod will give you
848good advice.
849.SH "AUTHOR"
850.IX Header "AUTHOR"
851Kirrily \*(L"Skud\*(R" Robert <skud@cpan.org>