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