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