1PERLINTRO(1)           Perl Programmers Reference Guide           PERLINTRO(1)
2
3
4

NAME

6       perlintro -- a brief introduction and overview of Perl
7

DESCRIPTION

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

AUTHOR

614       Kirrily "Skud" Robert <skud@cpan.org>
615
616
617
618perl v5.8.8                       2006-01-07                      PERLINTRO(1)
Impressum