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. As of
302 Perl 5.10, it even has a case/switch statement (spelled
303 "given"/"when"). See "Switch statements" in perlsyn for more details.
304
305 The conditions can be any Perl expression. See the list of operators
306 in the next section for information on comparison and boolean logic
307 operators, which are commonly used in conditional statements.
308
309 if
310 if ( condition ) {
311 ...
312 } elsif ( other condition ) {
313 ...
314 } else {
315 ...
316 }
317
318 There's also a negated version of it:
319
320 unless ( condition ) {
321 ...
322 }
323
324 This is provided as a more readable version of "if (!condition)".
325
326 Note that the braces are required in Perl, even if you've only got
327 one line in the block. However, there is a clever way of making
328 your one-line conditional blocks more English like:
329
330 # the traditional way
331 if ($zippy) {
332 print "Yow!";
333 }
334
335 # the Perlish post-condition way
336 print "Yow!" if $zippy;
337 print "We have no bananas" unless $bananas;
338
339 while
340 while ( condition ) {
341 ...
342 }
343
344 There's also a negated version, for the same reason we have
345 "unless":
346
347 until ( condition ) {
348 ...
349 }
350
351 You can also use "while" in a post-condition:
352
353 print "LA LA LA\n" while 1; # loops forever
354
355 for Exactly like C:
356
357 for ($i = 0; $i <= $max; $i++) {
358 ...
359 }
360
361 The C style for loop is rarely needed in Perl since Perl provides
362 the more friendly list scanning "foreach" loop.
363
364 foreach
365 foreach (@array) {
366 print "This element is $_\n";
367 }
368
369 print $list[$_] foreach 0 .. $max;
370
371 # you don't have to use the default $_ either...
372 foreach my $key (keys %hash) {
373 print "The value of $key is $hash{$key}\n";
374 }
375
376 For more detail on looping constructs (and some that weren't mentioned
377 in this overview) see perlsyn.
378
379 Builtin operators and functions
380 Perl comes with a wide selection of builtin functions. Some of the
381 ones we've already seen include "print", "sort" and "reverse". A list
382 of them is given at the start of perlfunc and you can easily read about
383 any given function by using "perldoc -f functionname".
384
385 Perl operators are documented in full in perlop, but here are a few of
386 the most common ones:
387
388 Arithmetic
389 + addition
390 - subtraction
391 * multiplication
392 / division
393
394 Numeric comparison
395 == equality
396 != inequality
397 < less than
398 > greater than
399 <= less than or equal
400 >= greater than or equal
401
402 String comparison
403 eq equality
404 ne inequality
405 lt less than
406 gt greater than
407 le less than or equal
408 ge greater than or equal
409
410 (Why do we have separate numeric and string comparisons? Because
411 we don't have special variable types, and Perl needs to know
412 whether to sort numerically (where 99 is less than 100) or
413 alphabetically (where 100 comes before 99).
414
415 Boolean logic
416 && and
417 || or
418 ! not
419
420 ("and", "or" and "not" aren't just in the above table as
421 descriptions of the operators. They're also supported as operators
422 in their own right. They're more readable than the C-style
423 operators, but have different precedence to "&&" and friends.
424 Check perlop for more detail.)
425
426 Miscellaneous
427 = assignment
428 . string concatenation
429 x string multiplication
430 .. range operator (creates a list of numbers)
431
432 Many operators can be combined with a "=" as follows:
433
434 $a += 1; # same as $a = $a + 1
435 $a -= 1; # same as $a = $a - 1
436 $a .= "\n"; # same as $a = $a . "\n";
437
438 Files and I/O
439 You can open a file for input or output using the "open()" function.
440 It's documented in extravagant detail in perlfunc and perlopentut, but
441 in short:
442
443 open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
444 open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
445 open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
446
447 You can read from an open filehandle using the "<>" operator. In
448 scalar context it reads a single line from the filehandle, and in list
449 context it reads the whole file in, assigning each line to an element
450 of the list:
451
452 my $line = <$in>;
453 my @lines = <$in>;
454
455 Reading in the whole file at one time is called slurping. It can be
456 useful but it may be a memory hog. Most text file processing can be
457 done a line at a time with Perl's looping constructs.
458
459 The "<>" operator is most often seen in a "while" loop:
460
461 while (<$in>) { # assigns each line in turn to $_
462 print "Just read in this line: $_";
463 }
464
465 We've already seen how to print to standard output using "print()".
466 However, "print()" can also take an optional first argument specifying
467 which filehandle to print to:
468
469 print STDERR "This is your final warning.\n";
470 print $out $record;
471 print $log $logmessage;
472
473 When you're done with your filehandles, you should "close()" them
474 (though to be honest, Perl will clean up after you if you forget):
475
476 close $in or die "$in: $!";
477
478 Regular expressions
479 Perl's regular expression support is both broad and deep, and is the
480 subject of lengthy documentation in perlrequick, perlretut, and
481 elsewhere. However, in short:
482
483 Simple matching
484 if (/foo/) { ... } # true if $_ contains "foo"
485 if ($a =~ /foo/) { ... } # true if $a contains "foo"
486
487 The "//" matching operator is documented in perlop. It operates on
488 $_ by default, or can be bound to another variable using the "=~"
489 binding operator (also documented in perlop).
490
491 Simple substitution
492 s/foo/bar/; # replaces foo with bar in $_
493 $a =~ s/foo/bar/; # replaces foo with bar in $a
494 $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a
495
496 The "s///" substitution operator is documented in perlop.
497
498 More complex regular expressions
499 You don't just have to match on fixed strings. In fact, you can
500 match on just about anything you could dream of by using more
501 complex regular expressions. These are documented at great length
502 in perlre, but for the meantime, here's a quick cheat sheet:
503
504 . a single character
505 \s a whitespace character (space, tab, newline, ...)
506 \S non-whitespace character
507 \d a digit (0-9)
508 \D a non-digit
509 \w a word character (a-z, A-Z, 0-9, _)
510 \W a non-word character
511 [aeiou] matches a single character in the given set
512 [^aeiou] matches a single character outside the given set
513 (foo|bar|baz) matches any of the alternatives specified
514
515 ^ start of string
516 $ end of string
517
518 Quantifiers can be used to specify how many of the previous thing
519 you want to match on, where "thing" means either a literal
520 character, one of the metacharacters listed above, or a group of
521 characters or metacharacters in parentheses.
522
523 * zero or more of the previous thing
524 + one or more of the previous thing
525 ? zero or one of the previous thing
526 {3} matches exactly 3 of the previous thing
527 {3,6} matches between 3 and 6 of the previous thing
528 {3,} matches 3 or more of the previous thing
529
530 Some brief examples:
531
532 /^\d+/ string starts with one or more digits
533 /^$/ nothing in the string (start and end are adjacent)
534 /(\d\s){3}/ a three digits, each followed by a whitespace
535 character (eg "3 4 5 ")
536 /(a.)+/ matches a string in which every odd-numbered letter
537 is a (eg "abacadaf")
538
539 # This loop reads from STDIN, and prints non-blank lines:
540 while (<>) {
541 next if /^$/;
542 print;
543 }
544
545 Parentheses for capturing
546 As well as grouping, parentheses serve a second purpose. They can
547 be used to capture the results of parts of the regexp match for
548 later use. The results end up in $1, $2 and so on.
549
550 # a cheap and nasty way to break an email address up into parts
551
552 if ($email =~ /([^@]+)@(.+)/) {
553 print "Username is $1\n";
554 print "Hostname is $2\n";
555 }
556
557 Other regexp features
558 Perl regexps also support backreferences, lookaheads, and all kinds
559 of other complex details. Read all about them in perlrequick,
560 perlretut, and perlre.
561
562 Writing subroutines
563 Writing subroutines is easy:
564
565 sub logger {
566 my $logmessage = shift;
567 open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
568 print $logfile $logmessage;
569 }
570
571 Now we can use the subroutine just as any other built-in function:
572
573 logger("We have a logger subroutine!");
574
575 What's that "shift"? Well, the arguments to a subroutine are available
576 to us as a special array called @_ (see perlvar for more on that). The
577 default argument to the "shift" function just happens to be @_. So "my
578 $logmessage = shift;" shifts the first item off the list of arguments
579 and assigns it to $logmessage.
580
581 We can manipulate @_ in other ways too:
582
583 my ($logmessage, $priority) = @_; # common
584 my $logmessage = $_[0]; # uncommon, and ugly
585
586 Subroutines can also return values:
587
588 sub square {
589 my $num = shift;
590 my $result = $num * $num;
591 return $result;
592 }
593
594 Then use it like:
595
596 $sq = square(8);
597
598 For more information on writing subroutines, see perlsub.
599
600 OO Perl
601 OO Perl is relatively simple and is implemented using references which
602 know what sort of object they are based on Perl's concept of packages.
603 However, OO Perl is largely beyond the scope of this document. Read
604 perlboot, perltoot, perltooc and perlobj.
605
606 As a beginning Perl programmer, your most common use of OO Perl will be
607 in using third-party modules, which are documented below.
608
609 Using Perl modules
610 Perl modules provide a range of features to help you avoid reinventing
611 the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ). A
612 number of popular modules are included with the Perl distribution
613 itself.
614
615 Categories of modules range from text manipulation to network protocols
616 to database integration to graphics. A categorized list of modules is
617 also available from CPAN.
618
619 To learn how to install modules you download from CPAN, read
620 perlmodinstall.
621
622 To learn how to use a particular module, use "perldoc Module::Name".
623 Typically you will want to "use Module::Name", which will then give you
624 access to exported functions or an OO interface to the module.
625
626 perlfaq contains questions and answers related to many common tasks,
627 and often provides suggestions for good CPAN modules to use.
628
629 perlmod describes Perl modules in general. perlmodlib lists the
630 modules which came with your Perl installation.
631
632 If you feel the urge to write Perl modules, perlnewmod will give you
633 good advice.
634
636 Kirrily "Skud" Robert <skud@cpan.org>
637
638
639
640perl v5.12.4 2011-06-07 PERLINTRO(1)