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