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