Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perlintro.html
blob606729a4ba3d09ded936c5fe42e2225b9b153977
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>perlintro -- a brief introduction and overview of Perl</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;perlintro -- a brief introduction and overview of Perl</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#description">DESCRIPTION</a></li>
24 <ul>
26 <li><a href="#what_is_perl">What is Perl?</a></li>
27 <li><a href="#running_perl_programs">Running Perl programs</a></li>
28 <li><a href="#basic_syntax_overview">Basic syntax overview</a></li>
29 <li><a href="#perl_variable_types">Perl variable types</a></li>
30 <li><a href="#variable_scoping">Variable scoping</a></li>
31 <li><a href="#conditional_and_looping_constructs">Conditional and looping constructs</a></li>
32 <li><a href="#builtin_operators_and_functions">Builtin operators and functions</a></li>
33 <li><a href="#files_and_i_o">Files and I/O</a></li>
34 <li><a href="#regular_expressions">Regular expressions</a></li>
35 <li><a href="#writing_subroutines">Writing subroutines</a></li>
36 <li><a href="#oo_perl">OO Perl</a></li>
37 <li><a href="#using_perl_modules">Using Perl modules</a></li>
38 </ul>
40 <li><a href="#author">AUTHOR</a></li>
41 </ul>
42 <!-- INDEX END -->
44 <hr />
45 <p>
46 </p>
47 <h1><a name="name">NAME</a></h1>
48 <p>perlintro -- a brief introduction and overview of Perl</p>
49 <p>
50 </p>
51 <hr />
52 <h1><a name="description">DESCRIPTION</a></h1>
53 <p>This document is intended to give you a quick overview of the Perl
54 programming language, along with pointers to further documentation. It
55 is intended as a ``bootstrap'' guide for those who are new to the
56 language, and provides just enough information for you to be able to
57 read other peoples' Perl and understand roughly what it's doing, or
58 write your own simple scripts.</p>
59 <p>This introductory document does not aim to be complete. It does not
60 even aim to be entirely accurate. In some cases perfection has been
61 sacrificed in the goal of getting the general idea across. You are
62 <em>strongly</em> advised to follow this introduction with more information
63 from the full Perl manual, the table of contents to which can be found
64 in <a href="file://C|\msysgit\mingw\html/pod/perltoc.html">the perltoc manpage</a>.</p>
65 <p>Throughout this document you'll see references to other parts of the
66 Perl documentation. You can read that documentation using the <code>perldoc</code>
67 command or whatever method you're using to read this document.</p>
68 <p>
69 </p>
70 <h2><a name="what_is_perl">What is Perl?</a></h2>
71 <p>Perl is a general-purpose programming language originally developed for
72 text manipulation and now used for a wide range of tasks including
73 system administration, web development, network programming, GUI
74 development, and more.</p>
75 <p>The language is intended to be practical (easy to use, efficient,
76 complete) rather than beautiful (tiny, elegant, minimal). Its major
77 features are that it's easy to use, supports both procedural and
78 object-oriented (OO) programming, has powerful built-in support for text
79 processing, and has one of the world's most impressive collections of
80 third-party modules.</p>
81 <p>Different definitions of Perl are given in <a href="file://C|\msysgit\mingw\html/pod/perl.html">the perl manpage</a>, <a href="file://C|\msysgit\mingw\html/pod/perlfaq1.html">the perlfaq1 manpage</a> and
82 no doubt other places. From this we can determine that Perl is different
83 things to different people, but that lots of people think it's at least
84 worth writing about.</p>
85 <p>
86 </p>
87 <h2><a name="running_perl_programs">Running Perl programs</a></h2>
88 <p>To run a Perl program from the Unix command line:</p>
89 <pre>
90 perl progname.pl</pre>
91 <p>Alternatively, put this as the first line of your script:</p>
92 <pre>
93 #!/usr/bin/env perl</pre>
94 <p>... and run the script as <code>/path/to/script.pl</code>. Of course, it'll need
95 to be executable first, so <code>chmod 755 script.pl</code> (under Unix).</p>
96 <p>For more information, including instructions for other platforms such as
97 Windows and Mac OS, read <a href="file://C|\msysgit\mingw\html/pod/perlrun.html">the perlrun manpage</a>.</p>
98 <p>
99 </p>
100 <h2><a name="basic_syntax_overview">Basic syntax overview</a></h2>
101 <p>A Perl script or program consists of one or more statements. These
102 statements are simply written in the script in a straightforward
103 fashion. There is no need to have a <code>main()</code> function or anything of
104 that kind.</p>
105 <p>Perl statements end in a semi-colon:</p>
106 <pre>
107 print &quot;Hello, world&quot;;</pre>
108 <p>Comments start with a hash symbol and run to the end of the line</p>
109 <pre>
110 # This is a comment</pre>
111 <p>Whitespace is irrelevant:</p>
112 <pre>
113 print
114 &quot;Hello, world&quot;
115 ;</pre>
116 <p>... except inside quoted strings:</p>
117 <pre>
118 # this would print with a linebreak in the middle
119 print &quot;Hello
120 world&quot;;</pre>
121 <p>Double quotes or single quotes may be used around literal strings:</p>
122 <pre>
123 print &quot;Hello, world&quot;;
124 print 'Hello, world';</pre>
125 <p>However, only double quotes ``interpolate'' variables and special
126 characters such as newlines (<code>\n</code>):</p>
127 <pre>
128 print &quot;Hello, $name\n&quot;; # works fine
129 print 'Hello, $name\n'; # prints $name\n literally</pre>
130 <p>Numbers don't need quotes around them:</p>
131 <pre>
132 print 42;</pre>
133 <p>You can use parentheses for functions' arguments or omit them
134 according to your personal taste. They are only required
135 occasionally to clarify issues of precedence.</p>
136 <pre>
137 print(&quot;Hello, world\n&quot;);
138 print &quot;Hello, world\n&quot;;</pre>
139 <p>More detailed information about Perl syntax can be found in <a href="file://C|\msysgit\mingw\html/pod/perlsyn.html">the perlsyn manpage</a>.</p>
141 </p>
142 <h2><a name="perl_variable_types">Perl variable types</a></h2>
143 <p>Perl has three main variable types: scalars, arrays, and hashes.</p>
144 <dl>
145 <dt><strong><a name="item_scalars">Scalars</a></strong>
147 <dd>
148 <p>A scalar represents a single value:</p>
149 </dd>
150 <dd>
151 <pre>
152 my $animal = &quot;camel&quot;;
153 my $answer = 42;</pre>
154 </dd>
155 <dd>
156 <p>Scalar values can be strings, integers or floating point numbers, and Perl
157 will automatically convert between them as required. There is no need
158 to pre-declare your variable types.</p>
159 </dd>
160 <dd>
161 <p>Scalar values can be used in various ways:</p>
162 </dd>
163 <dd>
164 <pre>
165 print $animal;
166 print &quot;The animal is $animal\n&quot;;
167 print &quot;The square of $answer is &quot;, $answer * $answer, &quot;\n&quot;;</pre>
168 </dd>
169 <dd>
170 <p>There are a number of ``magic'' scalars with names that look like
171 punctuation or line noise. These special variables are used for all
172 kinds of purposes, and are documented in <a href="file://C|\msysgit\mingw\html/pod/perlvar.html">the perlvar manpage</a>. The only one you
173 need to know about for now is <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$_</code></a> which is the ``default variable''.
174 It's used as the default argument to a number of functions in Perl, and
175 it's set implicitly by certain looping constructs.</p>
176 </dd>
177 <dd>
178 <pre>
179 print; # prints contents of $_ by default</pre>
180 </dd>
181 </li>
182 <dt><strong><a name="item_arrays">Arrays</a></strong>
184 <dd>
185 <p>An array represents a list of values:</p>
186 </dd>
187 <dd>
188 <pre>
189 my @animals = (&quot;camel&quot;, &quot;llama&quot;, &quot;owl&quot;);
190 my @numbers = (23, 42, 69);
191 my @mixed = (&quot;camel&quot;, 42, 1.23);</pre>
192 </dd>
193 <dd>
194 <p>Arrays are zero-indexed. Here's how you get at elements in an array:</p>
195 </dd>
196 <dd>
197 <pre>
198 print $animals[0]; # prints &quot;camel&quot;
199 print $animals[1]; # prints &quot;llama&quot;</pre>
200 </dd>
201 <dd>
202 <p>The special variable <code>$#array</code> tells you the index of the last element
203 of an array:</p>
204 </dd>
205 <dd>
206 <pre>
207 print $mixed[$#mixed]; # last element, prints 1.23</pre>
208 </dd>
209 <dd>
210 <p>You might be tempted to use <code>$#array + 1</code> to tell you how many items there
211 are in an array. Don't bother. As it happens, using <code>@array</code> where Perl
212 expects to find a scalar value (``in scalar context'') will give you the number
213 of elements in the array:</p>
214 </dd>
215 <dd>
216 <pre>
217 if (@animals &lt; 5) { ... }</pre>
218 </dd>
219 <dd>
220 <p>The elements we're getting from the array start with a <code>$</code> because
221 we're getting just a single value out of the array -- you ask for a scalar,
222 you get a scalar.</p>
223 </dd>
224 <dd>
225 <p>To get multiple values from an array:</p>
226 </dd>
227 <dd>
228 <pre>
229 @animals[0,1]; # gives (&quot;camel&quot;, &quot;llama&quot;);
230 @animals[0..2]; # gives (&quot;camel&quot;, &quot;llama&quot;, &quot;owl&quot;);
231 @animals[1..$#animals]; # gives all except the first element</pre>
232 </dd>
233 <dd>
234 <p>This is called an ``array slice''.</p>
235 </dd>
236 <dd>
237 <p>You can do various useful things to lists:</p>
238 </dd>
239 <dd>
240 <pre>
241 my @sorted = sort @animals;
242 my @backwards = reverse @numbers;</pre>
243 </dd>
244 <dd>
245 <p>There are a couple of special arrays too, such as <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__argv"><code>@ARGV</code></a> (the command
246 line arguments to your script) and <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>@_</code></a> (the arguments passed to a
247 subroutine). These are documented in <a href="file://C|\msysgit\mingw\html/pod/perlvar.html">the perlvar manpage</a>.</p>
248 </dd>
249 </li>
250 <dt><strong><a name="item_hashes">Hashes</a></strong>
252 <dd>
253 <p>A hash represents a set of key/value pairs:</p>
254 </dd>
255 <dd>
256 <pre>
257 my %fruit_color = (&quot;apple&quot;, &quot;red&quot;, &quot;banana&quot;, &quot;yellow&quot;);</pre>
258 </dd>
259 <dd>
260 <p>You can use whitespace and the <code>=&gt;</code> operator to lay them out more
261 nicely:</p>
262 </dd>
263 <dd>
264 <pre>
265 my %fruit_color = (
266 apple =&gt; &quot;red&quot;,
267 banana =&gt; &quot;yellow&quot;,
268 );</pre>
269 </dd>
270 <dd>
271 <p>To get at hash elements:</p>
272 </dd>
273 <dd>
274 <pre>
275 $fruit_color{&quot;apple&quot;}; # gives &quot;red&quot;</pre>
276 </dd>
277 <dd>
278 <p>You can get at lists of keys and values with <code>keys()</code> and
279 <code>values()</code>.</p>
280 </dd>
281 <dd>
282 <pre>
283 my @fruits = keys %fruit_colors;
284 my @colors = values %fruit_colors;</pre>
285 </dd>
286 <dd>
287 <p>Hashes have no particular internal order, though you can sort the keys
288 and loop through them.</p>
289 </dd>
290 <dd>
291 <p>Just like special scalars and arrays, there are also special hashes.
292 The most well known of these is <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__env"><code>%ENV</code></a> which contains environment
293 variables. Read all about it (and other special variables) in
294 <a href="file://C|\msysgit\mingw\html/pod/perlvar.html">the perlvar manpage</a>.</p>
295 </dd>
296 </li>
297 </dl>
298 <p>Scalars, arrays and hashes are documented more fully in <a href="file://C|\msysgit\mingw\html/pod/perldata.html">the perldata manpage</a>.</p>
299 <p>More complex data types can be constructed using references, which allow
300 you to build lists and hashes within lists and hashes.</p>
301 <p>A reference is a scalar value and can refer to any other Perl data
302 type. So by storing a reference as the value of an array or hash
303 element, you can easily create lists and hashes within lists and
304 hashes. The following example shows a 2 level hash of hash
305 structure using anonymous hash references.</p>
306 <pre>
307 my $variables = {
308 scalar =&gt; {
309 description =&gt; &quot;single item&quot;,
310 sigil =&gt; '$',
312 array =&gt; {
313 description =&gt; &quot;ordered list of items&quot;,
314 sigil =&gt; '@',
316 hash =&gt; {
317 description =&gt; &quot;key/value pairs&quot;,
318 sigil =&gt; '%',
320 };</pre>
321 <pre>
322 print &quot;Scalars begin with a $variables-&gt;{'scalar'}-&gt;{'sigil'}\n&quot;;</pre>
323 <p>Exhaustive information on the topic of references can be found in
324 <a href="file://C|\msysgit\mingw\html/pod/perlreftut.html">the perlreftut manpage</a>, <a href="file://C|\msysgit\mingw\html/pod/perllol.html">the perllol manpage</a>, <a href="file://C|\msysgit\mingw\html/pod/perlref.html">the perlref manpage</a> and <a href="file://C|\msysgit\mingw\html/pod/perldsc.html">the perldsc manpage</a>.</p>
326 </p>
327 <h2><a name="variable_scoping">Variable scoping</a></h2>
328 <p>Throughout the previous section all the examples have used the syntax:</p>
329 <pre>
330 my $var = &quot;value&quot;;</pre>
331 <p>The <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my</code></a> is actually not required; you could just use:</p>
332 <pre>
333 $var = &quot;value&quot;;</pre>
334 <p>However, the above usage will create global variables throughout your
335 program, which is bad programming practice. <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my</code></a> creates lexically
336 scoped variables instead. The variables are scoped to the block
337 (i.e. a bunch of statements surrounded by curly-braces) in which they
338 are defined.</p>
339 <pre>
340 my $a = &quot;foo&quot;;
341 if ($some_condition) {
342 my $b = &quot;bar&quot;;
343 print $a; # prints &quot;foo&quot;
344 print $b; # prints &quot;bar&quot;
346 print $a; # prints &quot;foo&quot;
347 print $b; # prints nothing; $b has fallen out of scope</pre>
348 <p>Using <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my</code></a> in combination with a <code>use strict;</code> at the top of
349 your Perl scripts means that the interpreter will pick up certain common
350 programming errors. For instance, in the example above, the final
351 <code>print $b</code> would cause a compile-time error and prevent you from
352 running the program. Using <code>strict</code> is highly recommended.</p>
354 </p>
355 <h2><a name="conditional_and_looping_constructs">Conditional and looping constructs</a></h2>
356 <p>Perl has most of the usual conditional and looping constructs except for
357 case/switch (but if you really want it, there is a Switch module in Perl
358 5.8 and newer, and on CPAN. See the section on modules, below, for more
359 information about modules and CPAN).</p>
360 <p>The conditions can be any Perl expression. See the list of operators in
361 the next section for information on comparison and boolean logic operators,
362 which are commonly used in conditional statements.</p>
363 <dl>
364 <dt><strong><a name="item_if">if</a></strong>
366 <dd>
367 <pre>
368 if ( condition ) {
370 } elsif ( other condition ) {
372 } else {
374 }</pre>
375 </dd>
376 <dd>
377 <p>There's also a negated version of it:</p>
378 </dd>
379 <dd>
380 <pre>
381 unless ( condition ) {
383 }</pre>
384 </dd>
385 <dd>
386 <p>This is provided as a more readable version of <a href="#item_if"><code>if (!condition)</code></a>.</p>
387 </dd>
388 <dd>
389 <p>Note that the braces are required in Perl, even if you've only got one
390 line in the block. However, there is a clever way of making your one-line
391 conditional blocks more English like:</p>
392 </dd>
393 <dd>
394 <pre>
395 # the traditional way
396 if ($zippy) {
397 print &quot;Yow!&quot;;
398 }</pre>
399 </dd>
400 <dd>
401 <pre>
402 # the Perlish post-condition way
403 print &quot;Yow!&quot; if $zippy;
404 print &quot;We have no bananas&quot; unless $bananas;</pre>
405 </dd>
406 <dt><strong><a name="item_while">while</a></strong>
408 <dd>
409 <pre>
410 while ( condition ) {
412 }</pre>
413 </dd>
414 <dd>
415 <p>There's also a negated version, for the same reason we have <code>unless</code>:</p>
416 </dd>
417 <dd>
418 <pre>
419 until ( condition ) {
421 }</pre>
422 </dd>
423 <dd>
424 <p>You can also use <a href="#item_while"><code>while</code></a> in a post-condition:</p>
425 </dd>
426 <dd>
427 <pre>
428 print &quot;LA LA LA\n&quot; while 1; # loops forever</pre>
429 </dd>
430 <dt><strong><a name="item_for">for</a></strong>
432 <dd>
433 <p>Exactly like C:</p>
434 </dd>
435 <dd>
436 <pre>
437 for ($i=0; $i &lt;= $max; $i++) {
439 }</pre>
440 </dd>
441 <dd>
442 <p>The C style for loop is rarely needed in Perl since Perl provides
443 the more friendly list scanning <a href="#item_foreach"><code>foreach</code></a> loop.</p>
444 </dd>
445 </li>
446 <dt><strong><a name="item_foreach">foreach</a></strong>
448 <dd>
449 <pre>
450 foreach (@array) {
451 print &quot;This element is $_\n&quot;;
452 }</pre>
453 </dd>
454 <dd>
455 <pre>
456 # you don't have to use the default $_ either...
457 foreach my $key (keys %hash) {
458 print &quot;The value of $key is $hash{$key}\n&quot;;
459 }</pre>
460 </dd>
461 </dl>
462 <p>For more detail on looping constructs (and some that weren't mentioned in
463 this overview) see <a href="file://C|\msysgit\mingw\html/pod/perlsyn.html">the perlsyn manpage</a>.</p>
465 </p>
466 <h2><a name="builtin_operators_and_functions">Builtin operators and functions</a></h2>
467 <p>Perl comes with a wide selection of builtin functions. Some of the ones
468 we've already seen include <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_print"><code>print</code></a>, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_sort"><code>sort</code></a> and <code>reverse</code>. A list of
469 them is given at the start of <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html">the perlfunc manpage</a> and you can easily read
470 about any given function by using <code>perldoc -f functionname</code>.</p>
471 <p>Perl operators are documented in full in <a href="file://C|\msysgit\mingw\html/pod/perlop.html">the perlop manpage</a>, but here are a few
472 of the most common ones:</p>
473 <dl>
474 <dt><strong><a name="item_arithmetic">Arithmetic</a></strong>
476 <dd>
477 <pre>
478 + addition
479 - subtraction
480 * multiplication
481 / division</pre>
482 </dd>
483 <dt><strong><a name="item_numeric_comparison">Numeric comparison</a></strong>
485 <dd>
486 <pre>
487 == equality
488 != inequality
489 &lt; less than
490 &gt; greater than
491 &lt;= less than or equal
492 &gt;= greater than or equal</pre>
493 </dd>
494 <dt><strong><a name="item_string_comparison">String comparison</a></strong>
496 <dd>
497 <pre>
498 eq equality
499 ne inequality
500 lt less than
501 gt greater than
502 le less than or equal
503 ge greater than or equal</pre>
504 </dd>
505 <dd>
506 <p>(Why do we have separate numeric and string comparisons? Because we don't
507 have special variable types, and Perl needs to know whether to sort
508 numerically (where 99 is less than 100) or alphabetically (where 100 comes
509 before 99).</p>
510 </dd>
511 <dt><strong><a name="item_boolean_logic">Boolean logic</a></strong>
513 <dd>
514 <pre>
515 &amp;&amp; and
516 || or
517 ! not</pre>
518 </dd>
519 <dd>
520 <p>(<code>and</code>, <code>or</code> and <code>not</code> aren't just in the above table as descriptions
521 of the operators -- they're also supported as operators in their own
522 right. They're more readable than the C-style operators, but have
523 different precedence to <code>&amp;&amp;</code> and friends. Check <a href="file://C|\msysgit\mingw\html/pod/perlop.html">the perlop manpage</a> for more
524 detail.)</p>
525 </dd>
526 <dt><strong><a name="item_miscellaneous">Miscellaneous</a></strong>
528 <dd>
529 <pre>
530 = assignment
531 . string concatenation
532 x string multiplication
533 .. range operator (creates a list of numbers)</pre>
534 </dd>
535 </dl>
536 <p>Many operators can be combined with a <code>=</code> as follows:</p>
537 <pre>
538 $a += 1; # same as $a = $a + 1
539 $a -= 1; # same as $a = $a - 1
540 $a .= &quot;\n&quot;; # same as $a = $a . &quot;\n&quot;;</pre>
542 </p>
543 <h2><a name="files_and_i_o">Files and I/O</a></h2>
544 <p>You can open a file for input or output using the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open()</code></a> function.
545 It's documented in extravagant detail in <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html">the perlfunc manpage</a> and <a href="file://C|\msysgit\mingw\html/pod/perlopentut.html">the perlopentut manpage</a>,
546 but in short:</p>
547 <pre>
548 open(INFILE, &quot;input.txt&quot;) or die &quot;Can't open input.txt: $!&quot;;
549 open(OUTFILE, &quot;&gt;output.txt&quot;) or die &quot;Can't open output.txt: $!&quot;;
550 open(LOGFILE, &quot;&gt;&gt;my.log&quot;) or die &quot;Can't open logfile: $!&quot;;</pre>
551 <p>You can read from an open filehandle using the <code>&lt;&gt;</code> operator. In
552 scalar context it reads a single line from the filehandle, and in list
553 context it reads the whole file in, assigning each line to an element of
554 the list:</p>
555 <pre>
556 my $line = &lt;INFILE&gt;;
557 my @lines = &lt;INFILE&gt;;</pre>
558 <p>Reading in the whole file at one time is called slurping. It can
559 be useful but it may be a memory hog. Most text file processing
560 can be done a line at a time with Perl's looping constructs.</p>
561 <p>The <code>&lt;&gt;</code> operator is most often seen in a <a href="#item_while"><code>while</code></a> loop:</p>
562 <pre>
563 while (&lt;INFILE&gt;) { # assigns each line in turn to $_
564 print &quot;Just read in this line: $_&quot;;
565 }</pre>
566 <p>We've already seen how to print to standard output using <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_print"><code>print()</code></a>.
567 However, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_print"><code>print()</code></a> can also take an optional first argument specifying
568 which filehandle to print to:</p>
569 <pre>
570 print STDERR &quot;This is your final warning.\n&quot;;
571 print OUTFILE $record;
572 print LOGFILE $logmessage;</pre>
573 <p>When you're done with your filehandles, you should <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_close"><code>close()</code></a> them
574 (though to be honest, Perl will clean up after you if you forget):</p>
575 <pre>
576 close INFILE;</pre>
578 </p>
579 <h2><a name="regular_expressions">Regular expressions</a></h2>
580 <p>Perl's regular expression support is both broad and deep, and is the
581 subject of lengthy documentation in <a href="file://C|\msysgit\mingw\html/pod/perlrequick.html">the perlrequick manpage</a>, <a href="file://C|\msysgit\mingw\html/pod/perlretut.html">the perlretut manpage</a>, and
582 elsewhere. However, in short:</p>
583 <dl>
584 <dt><strong><a name="item_simple_matching">Simple matching</a></strong>
586 <dd>
587 <pre>
588 if (/foo/) { ... } # true if $_ contains &quot;foo&quot;
589 if ($a =~ /foo/) { ... } # true if $a contains &quot;foo&quot;</pre>
590 </dd>
591 <dd>
592 <p>The <code>//</code> matching operator is documented in <a href="file://C|\msysgit\mingw\html/pod/perlop.html">the perlop manpage</a>. It operates on
593 <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>$_</code></a> by default, or can be bound to another variable using the <code>=~</code>
594 binding operator (also documented in <a href="file://C|\msysgit\mingw\html/pod/perlop.html">the perlop manpage</a>).</p>
595 </dd>
596 <dt><strong><a name="item_simple_substitution">Simple substitution</a></strong>
598 <dd>
599 <pre>
600 s/foo/bar/; # replaces foo with bar in $_
601 $a =~ s/foo/bar/; # replaces foo with bar in $a
602 $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a</pre>
603 </dd>
604 <dd>
605 <p>The <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_s_"><code>s///</code></a> substitution operator is documented in <a href="file://C|\msysgit\mingw\html/pod/perlop.html">the perlop manpage</a>.</p>
606 </dd>
607 <dt><strong><a name="item_more_complex_regular_expressions">More complex regular expressions</a></strong>
609 <dd>
610 <p>You don't just have to match on fixed strings. In fact, you can match
611 on just about anything you could dream of by using more complex regular
612 expressions. These are documented at great length in <a href="file://C|\msysgit\mingw\html/pod/perlre.html">the perlre manpage</a>, but for
613 the meantime, here's a quick cheat sheet:</p>
614 </dd>
615 <dd>
616 <pre>
617 . a single character
618 \s a whitespace character (space, tab, newline)
619 \S non-whitespace character
620 \d a digit (0-9)
621 \D a non-digit
622 \w a word character (a-z, A-Z, 0-9, _)
623 \W a non-word character
624 [aeiou] matches a single character in the given set
625 [^aeiou] matches a single character outside the given set
626 (foo|bar|baz) matches any of the alternatives specified</pre>
627 </dd>
628 <dd>
629 <pre>
630 ^ start of string
631 $ end of string</pre>
632 </dd>
633 <dd>
634 <p>Quantifiers can be used to specify how many of the previous thing you
635 want to match on, where ``thing'' means either a literal character, one
636 of the metacharacters listed above, or a group of characters or
637 metacharacters in parentheses.</p>
638 </dd>
639 <dd>
640 <pre>
641 * zero or more of the previous thing
642 + one or more of the previous thing
643 ? zero or one of the previous thing
644 {3} matches exactly 3 of the previous thing
645 {3,6} matches between 3 and 6 of the previous thing
646 {3,} matches 3 or more of the previous thing</pre>
647 </dd>
648 <dd>
649 <p>Some brief examples:</p>
650 </dd>
651 <dd>
652 <pre>
653 /^\d+/ string starts with one or more digits
654 /^$/ nothing in the string (start and end are adjacent)
655 /(\d\s){3}/ a three digits, each followed by a whitespace
656 character (eg &quot;3 4 5 &quot;)
657 /(a.)+/ matches a string in which every odd-numbered letter
658 is a (eg &quot;abacadaf&quot;)</pre>
659 </dd>
660 <dd>
661 <pre>
662 # This loop reads from STDIN, and prints non-blank lines:
663 while (&lt;&gt;) {
664 next if /^$/;
665 print;
666 }</pre>
667 </dd>
668 </li>
669 <dt><strong><a name="item_parentheses_for_capturing">Parentheses for capturing</a></strong>
671 <dd>
672 <p>As well as grouping, parentheses serve a second purpose. They can be
673 used to capture the results of parts of the regexp match for later use.
674 The results end up in <code>$1</code>, <code>$2</code> and so on.</p>
675 </dd>
676 <dd>
677 <pre>
678 # a cheap and nasty way to break an email address up into parts</pre>
679 </dd>
680 <dd>
681 <pre>
682 if ($email =~ /([^@]+)@(.+)/) {
683 print &quot;Username is $1\n&quot;;
684 print &quot;Hostname is $2\n&quot;;
685 }</pre>
686 </dd>
687 </li>
688 <dt><strong><a name="item_other_regexp_features">Other regexp features</a></strong>
690 <dd>
691 <p>Perl regexps also support backreferences, lookaheads, and all kinds of
692 other complex details. Read all about them in <a href="file://C|\msysgit\mingw\html/pod/perlrequick.html">the perlrequick manpage</a>,
693 <a href="file://C|\msysgit\mingw\html/pod/perlretut.html">the perlretut manpage</a>, and <a href="file://C|\msysgit\mingw\html/pod/perlre.html">the perlre manpage</a>.</p>
694 </dd>
695 </li>
696 </dl>
698 </p>
699 <h2><a name="writing_subroutines">Writing subroutines</a></h2>
700 <p>Writing subroutines is easy:</p>
701 <pre>
702 sub log {
703 my $logmessage = shift;
704 print LOGFILE $logmessage;
705 }</pre>
706 <p>What's that <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_shift"><code>shift</code></a>? Well, the arguments to a subroutine are available
707 to us as a special array called <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>@_</code></a> (see <a href="file://C|\msysgit\mingw\html/pod/perlvar.html">the perlvar manpage</a> for more on that).
708 The default argument to the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_shift"><code>shift</code></a> function just happens to be <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>@_</code></a>.
709 So <code>my $logmessage = shift;</code> shifts the first item off the list of
710 arguments and assigns it to <code>$logmessage</code>.</p>
711 <p>We can manipulate <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___"><code>@_</code></a> in other ways too:</p>
712 <pre>
713 my ($logmessage, $priority) = @_; # common
714 my $logmessage = $_[0]; # uncommon, and ugly</pre>
715 <p>Subroutines can also return values:</p>
716 <pre>
717 sub square {
718 my $num = shift;
719 my $result = $num * $num;
720 return $result;
721 }</pre>
722 <p>For more information on writing subroutines, see <a href="file://C|\msysgit\mingw\html/pod/perlsub.html">the perlsub manpage</a>.</p>
724 </p>
725 <h2><a name="oo_perl">OO Perl</a></h2>
726 <p>OO Perl is relatively simple and is implemented using references which
727 know what sort of object they are based on Perl's concept of packages.
728 However, OO Perl is largely beyond the scope of this document.
729 Read <a href="file://C|\msysgit\mingw\html/pod/perlboot.html">the perlboot manpage</a>, <a href="file://C|\msysgit\mingw\html/pod/perltoot.html">the perltoot manpage</a>, <a href="file://C|\msysgit\mingw\html/pod/perltooc.html">the perltooc manpage</a> and <a href="file://C|\msysgit\mingw\html/pod/perlobj.html">the perlobj manpage</a>.</p>
730 <p>As a beginning Perl programmer, your most common use of OO Perl will be
731 in using third-party modules, which are documented below.</p>
733 </p>
734 <h2><a name="using_perl_modules">Using Perl modules</a></h2>
735 <p>Perl modules provide a range of features to help you avoid reinventing
736 the wheel, and can be downloaded from CPAN ( <a href="http://www.cpan.org/">http://www.cpan.org/</a> ). A
737 number of popular modules are included with the Perl distribution
738 itself.</p>
739 <p>Categories of modules range from text manipulation to network protocols
740 to database integration to graphics. A categorized list of modules is
741 also available from CPAN.</p>
742 <p>To learn how to install modules you download from CPAN, read
743 <a href="file://C|\msysgit\mingw\html/pod/perlmodinstall.html">the perlmodinstall manpage</a></p>
744 <p>To learn how to use a particular module, use <code>perldoc Module::Name</code>.
745 Typically you will want to <code>use Module::Name</code>, which will then give
746 you access to exported functions or an OO interface to the module.</p>
747 <p><a href="file://C|\msysgit\mingw\html/pod/perlfaq.html">the perlfaq manpage</a> contains questions and answers related to many common
748 tasks, and often provides suggestions for good CPAN modules to use.</p>
749 <p><a href="file://C|\msysgit\mingw\html/pod/perlmod.html">the perlmod manpage</a> describes Perl modules in general. <a href="file://C|\msysgit\mingw\html/pod/perlmodlib.html">the perlmodlib manpage</a> lists the
750 modules which came with your Perl installation.</p>
751 <p>If you feel the urge to write Perl modules, <a href="file://C|\msysgit\mingw\html/pod/perlnewmod.html">the perlnewmod manpage</a> will give you
752 good advice.</p>
754 </p>
755 <hr />
756 <h1><a name="author">AUTHOR</a></h1>
757 <p>Kirrily ``Skud'' Robert &lt;<a href="mailto:skud@cpan.org">skud@cpan.org</a>&gt;</p>
758 <table border="0" width="100%" cellspacing="0" cellpadding="3">
759 <tr><td class="block" style="background-color: #cccccc" valign="middle">
760 <big><strong><span class="block">&nbsp;perlintro -- a brief introduction and overview of Perl</span></strong></big>
761 </td></tr>
762 </table>
764 </body>
766 </html>