This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add perldelta entry for commit 4f46e52b00
[perl5.git] / pod / perlintro.pod
index db30810..afce360 100644 (file)
@@ -14,30 +14,49 @@ write your own simple scripts.
 This introductory document does not aim to be complete.  It does not
 even aim to be entirely accurate.  In some cases perfection has been
 sacrificed in the goal of getting the general idea across.  You are
-B<strongly> advised to follow this introduction with more information
+I<strongly> advised to follow this introduction with more information
 from the full Perl manual, the table of contents to which can be found
 in L<perltoc>.
 
-Throughout this document you'll see references to other parts of the 
+Throughout this document you'll see references to other parts of the
 Perl documentation.  You can read that documentation using the C<perldoc>
-command or using whatever method you're using to read this document.
+command or whatever method you're using to read this document.
+
+Throughout Perl's documentation, you'll find numerous examples intended
+to help explain the discussed features.  Please keep in mind that many
+of them are code fragments rather than complete programs.
+
+These examples often reflect the style and preference of the author of
+that piece of the documentation, and may be briefer than a corresponding
+line of code in a real program.  Except where otherwise noted, you
+should assume that C<use strict> and C<use warnings> statements
+appear earlier in the "program", and that any variables used have
+already been declared, even if those declarations have been omitted
+to make the example easier to read.
+
+Do note that the examples have been written by many different authors over
+a period of several decades.  Styles and techniques will therefore differ,
+although some effort has been made to not vary styles too widely in the
+same sections.  Do not consider one style to be better than others - "There's
+More Than One Way To Do It" is one of Perl's mottos.  After all, in your
+journey as a programmer, you are likely to encounter different styles.
 
 =head2 What is Perl?
 
-Perl is a general-purpose programming language originally developed for 
-text manipulation and now used for a wide range of tasks including 
-system administration, web development, network programming, GUI 
+Perl is a general-purpose programming language originally developed for
+text manipulation and now used for a wide range of tasks including
+system administration, web development, network programming, GUI
 development, and more.
 
-The language is intended to be practical (easy to use, efficient, 
-complete) rather than beautiful (tiny, elegant, minimal).  Its major 
-features are that it's easy to use, supports both procedural and OO
-programming, has powerful built-in support for text processing, and 
-has one of the world's most impressive collections of third-party 
-modules.
+The language is intended to be practical (easy to use, efficient,
+complete) rather than beautiful (tiny, elegant, minimal).  Its major
+features are that it's easy to use, supports both procedural and
+object-oriented (OO) programming, has powerful built-in support for text
+processing, and has one of the world's most impressive collections of
+third-party modules.
 
-Different definitions of Perl are given in L<perl>, L<perlfaq1> and 
-no doubt other places.  From this we can determine that Perl is different 
+Different definitions of Perl are given in L<perl>, L<perlfaq1> and
+no doubt other places.  From this we can determine that Perl is different
 things to different people, but that lots of people think it's at least
 worth writing about.
 
@@ -54,15 +73,35 @@ Alternatively, put this as the first line of your script:
 ... and run the script as C</path/to/script.pl>.  Of course, it'll need
 to be executable first, so C<chmod 755 script.pl> (under Unix).
 
+(This start line assumes you have the B<env> program.  You can also put
+directly the path to your perl executable, like in C<#!/usr/bin/perl>).
+
 For more information, including instructions for other platforms such as
-Windows and MacOS, read L<perlrun>.
+Windows and Mac OS, read L<perlrun>.
+
+=head2 Safety net
+
+Perl by default is very forgiving.  In order to make it more robust
+it is recommended to start every program with the following lines:
+
+    #!/usr/bin/perl
+    use strict;
+    use warnings;
+
+The two additional lines request from perl to catch various common
+problems in your code.  They check different things so you need both.  A
+potential problem caught by C<use strict;> will cause your code to stop
+immediately when it is encountered, while C<use warnings;> will merely
+give a warning (like the command-line switch B<-w>) and let your code run.
+To read more about them check their respective manual pages at L<strict>
+and L<warnings>.
 
 =head2 Basic syntax overview
 
 A Perl script or program consists of one or more statements.  These
 statements are simply written in the script in a straightforward
-fashion.  There is no need to have a main() function or anything of that
-kind.
+fashion.  There is no need to have a C<main()> function or anything of
+that kind.
 
 Perl statements end in a semi-colon:
 
@@ -74,7 +113,7 @@ Comments start with a hash symbol and run to the end of the line
 
 Whitespace is irrelevant:
 
-    print 
+    print
         "Hello, world"
         ;
 
@@ -100,7 +139,7 @@ Numbers don't need quotes around them:
     print 42;
 
 You can use parentheses for functions' arguments or omit them
-according to your personal taste.  They are only required 
+according to your personal taste.  They are only required
 occasionally to clarify issues of precedence.
 
     print("Hello, world\n");
@@ -121,9 +160,11 @@ A scalar represents a single value:
     my $animal = "camel";
     my $answer = 42;
 
-Scalar values can be strings, integers or floating point numbers, and Perl 
-will automatically convert between them as required.  There is no need 
-to pre-declare your variable types.
+Scalar values can be strings, integers or floating point numbers, and Perl
+will automatically convert between them as required.  There is no need
+to pre-declare your variable types, but you have to declare them using
+the C<my> keyword the first time you use them.  (This is one of the
+requirements of C<use strict;>.)
 
 Scalar values can be used in various ways:
 
@@ -136,7 +177,7 @@ punctuation or line noise.  These special variables are used for all
 kinds of purposes, and are documented in L<perlvar>.  The only one you
 need to know about for now is C<$_> which is the "default variable".
 It's used as the default argument to a number of functions in Perl, and
-it's set implicitly by certain looping constructs.  
+it's set implicitly by certain looping constructs.
 
     print;          # prints contents of $_ by default
 
@@ -153,23 +194,23 @@ Arrays are zero-indexed.  Here's how you get at elements in an array:
     print $animals[0];              # prints "camel"
     print $animals[1];              # prints "llama"
 
-The special variable C<$#array> tells you the index of the last element 
+The special variable C<$#array> tells you the index of the last element
 of an array:
 
     print $mixed[$#mixed];       # last element, prints 1.23
 
-You might be tempted to use C<$#array + 1> to tell you how many items there 
+You might be tempted to use C<$#array + 1> to tell you how many items there
 are in an array.  Don't bother.  As it happens, using C<@array> where Perl
 expects to find a scalar value ("in scalar context") will give you the number
 of elements in the array:
 
     if (@animals < 5) { ... }
 
-The elements we're getting from the array start with a C<$> because 
-we're getting just a single value out of the array -- you ask for a scalar, 
+The elements we're getting from the array start with a C<$> because
+we're getting just a single value out of the array; you ask for a scalar,
 you get a scalar.
 
-To get multiple values from a array:
+To get multiple values from an array:
 
     @animals[0,1];                  # gives ("camel", "llama");
     @animals[0..2];                 # gives ("camel", "llama", "owl");
@@ -213,7 +254,7 @@ C<values()>.
 Hashes have no particular internal order, though you can sort the keys
 and loop through them.
 
-Just like special scalars and arrays, there are also special hashes.  
+Just like special scalars and arrays, there are also special hashes.
 The most well known of these is C<%ENV> which contains environment
 variables.  Read all about it (and other special variables) in
 L<perlvar>.
@@ -226,13 +267,13 @@ More complex data types can be constructed using references, which allow
 you to build lists and hashes within lists and hashes.
 
 A reference is a scalar value and can refer to any other Perl data
-type. So by storing a reference as the value of an array or hash
-element, you can easily create lists and hashes within lists and    
-hashes. The following example shows a 2 level hash of hash
+type.  So by storing a reference as the value of an array or hash
+element, you can easily create lists and hashes within lists and
+hashes.  The following example shows a 2 level hash of hash
 structure using anonymous hash references.
 
     my $variables = {
-        scalar  =>  { 
+        scalar  =>  {
                      description => "single item",
                      sigil => '$',
                     },
@@ -267,30 +308,30 @@ scoped variables instead.  The variables are scoped to the block
 (i.e. a bunch of statements surrounded by curly-braces) in which they
 are defined.
 
-    my $a = "foo";
+    my $x = "foo";
+    my $some_condition = 1;
     if ($some_condition) {
-        my $b = "bar";
-        print $a;           # prints "foo"
-        print $b;           # prints "bar"
+        my $y = "bar";
+        print $x;           # prints "foo"
+        print $y;           # prints "bar"
     }
-    print $a;               # prints "foo"
-    print $b;               # prints nothing; $b has fallen out of scope
+    print $x;               # prints "foo"
+    print $y;               # prints nothing; $y has fallen out of scope
 
 Using C<my> in combination with a C<use strict;> at the top of
-your Perl scripts means that the interpreter will pick up certain common 
+your Perl scripts means that the interpreter will pick up certain common
 programming errors.  For instance, in the example above, the final
-C<print $b> would cause a compile-time error and prevent you from
+C<print $y> would cause a compile-time error and prevent you from
 running the program.  Using C<strict> is highly recommended.
 
 =head2 Conditional and looping constructs
 
-Perl has most of the usual conditional and looping constructs except for
-case/switch (but you can find a Switch module on CPAN, if you really
-want one -- see the section on modules, below, for more information 
-about modules and CPAN).
+Perl has most of the usual conditional and looping constructs.  As of Perl
+5.10, it even has a case/switch statement (spelled C<given>/C<when>).  See
+L<perlsyn/"Switch Statements"> for more details.
 
 The conditions can be any Perl expression.  See the list of operators in
-the next section for information on comparison and boolean logic operators, 
+the next section for information on comparison and boolean logic operators,
 which are commonly used in conditional statements.
 
 =over 4
@@ -311,7 +352,7 @@ There's also a negated version of it:
         ...
     }
 
-This is provided as a more readable version of C<if (! condition)>.
+This is provided as a more readable version of C<if (!I<condition>)>.
 
 Note that the braces are required in Perl, even if you've only got one
 line in the block.  However, there is a clever way of making your one-line
@@ -346,12 +387,12 @@ You can also use C<while> in a post-condition:
 
 Exactly like C:
 
-    for ($i=0; $i <= $max; $i++) {
+    for ($i = 0; $i <= $max; $i++) {
         ...
     }
 
 The C style for loop is rarely needed in Perl since Perl provides
-the the more friendly list scanning C<foreach> loop.
+the more friendly list scanning C<foreach> loop.
 
 =item foreach
 
@@ -359,11 +400,16 @@ the the more friendly list scanning C<foreach> loop.
         print "This element is $_\n";
     }
 
+    print $list[$_] foreach 0 .. $max;
+
     # you don't have to use the default $_ either...
     foreach my $key (keys %hash) {
         print "The value of $key is $hash{$key}\n";
     }
 
+The C<foreach> keyword is actually a synonym for the C<for>
+keyword.  See C<L<perlsyn/"Foreach Loops">>.
+
 =back
 
 For more detail on looping constructs (and some that weren't mentioned in
@@ -373,8 +419,8 @@ this overview) see L<perlsyn>.
 
 Perl comes with a wide selection of builtin functions.  Some of the ones
 we've already seen include C<print>, C<sort> and C<reverse>.  A list of
-them is given at the start of L<perlfunc> and you can easily read 
-about any given function by using C<perldoc -f functionname>.
+them is given at the start of L<perlfunc> and you can easily read
+about any given function by using C<perldoc -f I<functionname>>.
 
 Perl operators are documented in full in L<perlop>, but here are a few
 of the most common ones:
@@ -406,8 +452,8 @@ of the most common ones:
     le  less than or equal
     ge  greater than or equal
 
-(Why do we have separate numeric and string comparisons?  Because we don't 
-have special variable types, and Perl needs to know whether to sort 
+(Why do we have separate numeric and string comparisons?  Because we don't
+have special variable types, and Perl needs to know whether to sort
 numerically (where 99 is less than 100) or alphabetically (where 100 comes
 before 99).
 
@@ -417,10 +463,10 @@ before 99).
     ||  or
     !   not
 
-(C<and>, C<or> and C<not> aren't just in the above table as descriptions 
-of the operators -- they're also supported as operators in their own
-right.  They're more readable than the C-style operators, but have 
-different precedence to C<&&> and friends.  Check L<perlop> for more 
+(C<and>, C<or> and C<not> aren't just in the above table as descriptions
+of the operators.  They're also supported as operators in their own
+right.  They're more readable than the C-style operators, but have
+different precedence to C<&&> and friends.  Check L<perlop> for more
 detail.)
 
 =item Miscellaneous
@@ -441,28 +487,28 @@ Many operators can be combined with a C<=> as follows:
 =head2 Files and I/O
 
 You can open a file for input or output using the C<open()> function.
-It's documented in extravagant detail in L<perlfunc> and L<perlopentut>, 
+It's documented in extravagant detail in L<perlfunc> and L<perlopentut>,
 but in short:
 
-    open(INFILE,  "input.txt")   or die "Can't open input.txt: $!";
-    open(OUTFILE, ">output.txt") or die "Can't open output.txt: $!";
-    open(LOGFILE, ">>my.log")    or die "Can't open logfile: $!";
+    open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";
+    open(my $out, ">",  "output.txt") or die "Can't open output.txt: $!";
+    open(my $log, ">>", "my.log")     or die "Can't open my.log: $!";
 
 You can read from an open filehandle using the C<< <> >> operator.  In
 scalar context it reads a single line from the filehandle, and in list
 context it reads the whole file in, assigning each line to an element of
 the list:
 
-    my $line  = <INFILE>;
-    my @lines = <INFILE>;
+    my $line  = <$in>;
+    my @lines = <$in>;
 
-Reading in the whole file at one time is called slurping. It can
-be useful but it may be a memory hog. Most text file processing
+Reading in the whole file at one time is called slurping.  It can
+be useful but it may be a memory hog.  Most text file processing
 can be done a line at a time with Perl's looping constructs.
 
 The C<< <> >> operator is most often seen in a C<while> loop:
 
-    while (<INFILE>) {     # assigns each line in turn to $_ 
+    while (<$in>) {     # assigns each line in turn to $_
         print "Just read in this line: $_";
     }
 
@@ -471,13 +517,13 @@ However, C<print()> can also take an optional first argument specifying
 which filehandle to print to:
 
     print STDERR "This is your final warning.\n";
-    print OUTFILE $record;
-    print LOGFILE $logmessage;
+    print $out $record;
+    print $log $logmessage;
 
 When you're done with your filehandles, you should C<close()> them
 (though to be honest, Perl will clean up after you if you forget):
 
-    close INFILE;
+    close $in or die "$in: $!";
 
 =head2 Regular expressions
 
@@ -512,7 +558,7 @@ expressions.  These are documented at great length in L<perlre>, but for
 the meantime, here's a quick cheat sheet:
 
     .                   a single character
-    \s                  a whitespace character (space, tab, newline)
+    \s                  a whitespace character (space, tab, newline, ...)
     \S                  non-whitespace character
     \d                  a digit (0-9)
     \D                  a non-digit
@@ -525,9 +571,9 @@ the meantime, here's a quick cheat sheet:
     ^                   start of string
     $                   end of string
 
-Quantifiers can be used to specify how many of the previous thing you 
-want to match on, where "thing" means either a literal character, one 
-of the metacharacters listed above, or a group of characters or 
+Quantifiers can be used to specify how many of the previous thing you
+want to match on, where "thing" means either a literal character, one
+of the metacharacters listed above, or a group of characters or
 metacharacters in parentheses.
 
     *                   zero or more of the previous thing
@@ -541,9 +587,9 @@ Some brief examples:
 
     /^\d+/              string starts with one or more digits
     /^$/                nothing in the string (start and end are adjacent)
-    /(\d\s){3}/         a three digits, each followed by a whitespace 
+    /(\d\s){3}/         a three digits, each followed by a whitespace
                         character (eg "3 4 5 ")
-    /(a.)+/             matches a string in which every odd-numbered letter 
+    /(a.)+/             matches a string in which every odd-numbered letter
                         is a (eg "abacadaf")
 
     # This loop reads from STDIN, and prints non-blank lines:
@@ -554,13 +600,13 @@ Some brief examples:
 
 =item Parentheses for capturing
 
-As well as grouping, parentheses serve a second purpose.  They can be 
+As well as grouping, parentheses serve a second purpose.  They can be
 used to capture the results of parts of the regexp match for later use.
 The results end up in C<$1>, C<$2> and so on.
 
     # a cheap and nasty way to break an email address up into parts
 
-    if ($email =~ /([^@]+@(.+)/) {
+    if ($email =~ /([^@]+)@(.+)/) {
         print "Username is $1\n";
         print "Hostname is $2\n";
     }
@@ -577,16 +623,21 @@ L<perlretut>, and L<perlre>.
 
 Writing subroutines is easy:
 
-    sub log {
+    sub logger {
         my $logmessage = shift;
-        print LOGFILE $logmessage;
+        open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
+        print $logfile $logmessage;
     }
 
+Now we can use the subroutine just as any other built-in function:
+
+    logger("We have a logger subroutine!");
+
 What's that C<shift>?  Well, the arguments to a subroutine are available
 to us as a special array called C<@_> (see L<perlvar> for more on that).
 The default argument to the C<shift> function just happens to be C<@_>.
 So C<my $logmessage = shift;> shifts the first item off the list of
-arguments and assigns it to C<$logmessage>. 
+arguments and assigns it to C<$logmessage>.
 
 We can manipulate C<@_> in other ways too:
 
@@ -601,14 +652,18 @@ Subroutines can also return values:
         return $result;
     }
 
+Then use it like:
+
+    $sq = square(8);
+
 For more information on writing subroutines, see L<perlsub>.
 
 =head2 OO Perl
 
 OO Perl is relatively simple and is implemented using references which
 know what sort of object they are based on Perl's concept of packages.
-However, OO Perl is largely beyond the scope of this document.  
-Read L<perlboot>, L<perltoot>, L<perltooc> and L<perlobj>.
+However, OO Perl is largely beyond the scope of this document.
+Read L<perlootut> and L<perlobj>.
 
 As a beginning Perl programmer, your most common use of OO Perl will be
 in using third-party modules, which are documented below.
@@ -616,7 +671,7 @@ in using third-party modules, which are documented below.
 =head2 Using Perl modules
 
 Perl modules provide a range of features to help you avoid reinventing
-the wheel, and can be downloaded from CPAN (http://www.cpan.org).  A
+the wheel, and can be downloaded from CPAN ( http://www.cpan.org/ ).  A
 number of popular modules are included with the Perl distribution
 itself.
 
@@ -625,11 +680,11 @@ to database integration to graphics.  A categorized list of modules is
 also available from CPAN.
 
 To learn how to install modules you download from CPAN, read
-L<perlmodinstall>
+L<perlmodinstall>.
 
-To learn how to use a particular module, use C<perldoc Module::Name>.
-Typically you will want to C<use Module::Name>, which will then give you
-access to exported functions or an OO interface to the module.
+To learn how to use a particular module, use C<perldoc I<Module::Name>>.
+Typically you will want to C<use I<Module::Name>>, which will then give
+you access to exported functions or an OO interface to the module.
 
 L<perlfaq> contains questions and answers related to many common
 tasks, and often provides suggestions for good CPAN modules to use.