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