Changes

Jump to navigation Jump to search
2,783 bytes added ,  01:34, 30 December 2013
Line 1: Line 1:  
== NAME ==
 
== NAME ==
   −
perlintro -- a brief introduction and overview of Perl
+
perlintro -- a brief introduction and overview of Perl,if you are looking to found the complete edition of perl documentation, see http://perldoc.perl.org/index.html
    
you can find the source of this Tutorial http://perldoc.perl.org/perlintro.html
 
you can find the source of this Tutorial http://perldoc.perl.org/perlintro.html
 +
 
== DESCRIPTION ==
 
== DESCRIPTION ==
    
This document is intended to give you a quick overview of the Perl programming language, along with pointers to further documentation. It is intended as a "bootstrap" guide for those who are new to the language, and provides just enough information for you to be able to read other peoples' Perl and understand roughly what it's doing, or write your own simple scripts.
 
This document is intended to give you a quick overview of the Perl programming language, along with pointers to further documentation. It is intended as a "bootstrap" guide for those who are new to the language, and provides just enough information for you to be able to read other peoples' Perl and understand roughly what it's doing, or 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 strongly advised to follow this introduction with more information from the full Perl manual, the table of contents to which can be found in perltoc.
+
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 strongly advised to follow this introduction with more information from the full Perl manual, the table of contents to which can be found in [http://search.cpan.org/perldoc/perltoc perltoc].
   −
Throughout this document you'll see references to other parts of the Perl documentation. You can read that documentation using the perldoc command or whatever method you're using to read this document.
+
Throughout this document you'll see references to other parts of the Perl documentation. You can read that documentation using the '''perldoc''' 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.
 
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.
Line 35: Line 36:  
     #!/usr/bin/env perl
 
     #!/usr/bin/env perl
   −
... and run the script as /path/to/script.pl. Of course, it'll need to be executable first, so <code>chmod 755 script.pl</code> (under Unix).
+
... and run the script as /path/to/script.pl. Of course, it'll need to be executable first, so <code>'''chmod 755 script.pl'''</code> (under Unix).
   −
(This start line assumes you have the env program. You can also put directly the path to your perl executable, like in <code>#!/usr/bin/perl</code> ).
+
(This start line assumes you have the env program. You can also put directly the path to your perl executable, like in <code>'''#!/usr/bin/perl'''</code> ).
    
For more information, including instructions for other platforms such as Windows and Mac OS, read [http://perldoc.perl.org/perlrun.html perlrun].
 
For more information, including instructions for other platforms such as Windows and Mac OS, read [http://perldoc.perl.org/perlrun.html perlrun].
Line 49: Line 50:  
     use warnings;
 
     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 use strict; will cause your code to stop immediately when it is encountered, while use warnings; will merely give a warning (like the command-line switch -w) and let your code run. To read more about them check their respective manual pages at strict and 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 '''use strict;''' will cause your code to stop immediately when it is encountered, while '''use warnings;''' will merely give a warning (like the command-line switch -w) and let your code run. To read more about them check their respective manual pages at [http://perldoc.perl.org/strict.html strict] and [http://perldoc.perl.org/warnings.html warnings].
 +
 
 
=== Basic syntax overview ===
 
=== 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 <code>main()</code> function or anything of that kind.
+
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 <code>'''main()'''</code> function or anything of that kind.
    
Perl statements end in a semi-colon:
 
Perl statements end in a semi-colon:
   −
     print "Hello, world";
+
     [http://perldoc.perl.org/functions/print.html print] "Hello, world";
    
Comments start with a hash symbol and run to the end of the line
 
Comments start with a hash symbol and run to the end of the line
Line 106: Line 108:  
         my $answer = 42;
 
         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, but you have to declare them using the '''my''' keyword the first time you use them. (This is one of the requirements of '''use strict;''' .)
+
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 '''[http://perldoc.perl.org/functions/my.html my]''' keyword the first time you use them. (This is one of the requirements of '''[http://perldoc.perl.org/functions/use.html use] strict;''' .)
    
Scalar values can be used in various ways:
 
Scalar values can be used in various ways:
Line 114: Line 116:  
         print "The square of $answer is ", $answer * $answer, "\n";
 
         print "The square of $answer is ", $answer * $answer, "\n";
   −
There are a number of "magic" scalars with names that look like punctuation or line noise. These special variables are used for all kinds of purposes, and are documented in perlvar. The only one you need to know about for now is $_ 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.
+
There are a number of "magic" scalars with names that look like punctuation or line noise. These special variables are used for all kinds of purposes, and are documented in [http://perldoc.perl.org/perlvar.html perlvar]. The only one you need to know about for now is '''$_''' 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.
    
         print; # prints contents of $_ by default
 
         print; # prints contents of $_ by default
Line 120: Line 122:  
==== Arrays ====
 
==== Arrays ====
   −
    An array represents a list of values:
+
An array represents a list of values:
    
         my @animals = ("camel", "llama", "owl");
 
         my @animals = ("camel", "llama", "owl");
Line 126: Line 128:  
         my @mixed = ("camel", 42, 1.23);
 
         my @mixed = ("camel", 42, 1.23);
   −
    Arrays are zero-indexed. Here's how you get at elements in an array:
+
Arrays are zero-indexed. Here's how you get at elements in an array:
    
         print $animals[0]; # prints "camel"
 
         print $animals[0]; # prints "camel"
 
         print $animals[1]; # prints "llama"
 
         print $animals[1]; # prints "llama"
   −
    The special variable $#array tells you the index of the last element of an array:
+
The special variable '''$#array''' tells you the index of the last element of an array:
    
         print $mixed[$#mixed]; # last element, prints 1.23
 
         print $mixed[$#mixed]; # last element, prints 1.23
   −
    You might be tempted to use $#array + 1 to tell you how many items there are in an array. Don't bother. As it happens, using @array where Perl expects to find a scalar value ("in scalar context") will give you the number of elements in the array:
+
You might be tempted to use '''$#array + 1''' to tell you how many items there are in an array. Don't bother. As it happens, using '''@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) { ... }
 
         if (@animals < 5) { ... }
   −
    The elements we're getting from the array start with a $ because we're getting just a single value out of the array; you ask for a scalar, you get a scalar.
+
The elements we're getting from the array start with a '''$''' 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 an array:
+
To get multiple values from an array:
    
         @animals[0,1]; # gives ("camel", "llama");
 
         @animals[0,1]; # gives ("camel", "llama");
Line 147: Line 149:  
         @animals[1..$#animals]; # gives all except the first element
 
         @animals[1..$#animals]; # gives all except the first element
   −
    This is called an "array slice".
+
This is called an "array slice".
 +
 
 +
You can do various useful things to lists:
   −
    You can do various useful things to lists:
+
        my @sorted = [http://perldoc.perl.org/functions/sort.html sort] @animals;
 +
        my @backwards = [http://perldoc.perl.org/functions/reverse.html reverse] @numbers;
   −
        my @sorted = sort @animals;
+
There are a couple of special arrays too, such as '''@ARGV''' (the command line arguments to your script) and '''@_''' (the arguments passed to a subroutine). These are documented in [http://perldoc.perl.org/perlvar.html perlvar].
        my @backwards = reverse @numbers;
     −
    There are a couple of special arrays too, such as @ARGV (the command line arguments to your script) and @_ (the arguments passed to a subroutine). These are documented in perlvar.
   
==== Hashes ====
 
==== Hashes ====
   −
    A hash represents a set of key/value pairs:
+
A hash represents a set of key/value pairs:
    
         my %fruit_color = ("apple", "red", "banana", "yellow");
 
         my %fruit_color = ("apple", "red", "banana", "yellow");
   −
    You can use whitespace and the => operator to lay them out more nicely:
+
You can use whitespace and the '''=>''' operator to lay them out more nicely:
    
         my %fruit_color = (
 
         my %fruit_color = (
Line 168: Line 171:  
         );
 
         );
   −
    To get at hash elements:
+
To get at hash elements:
    
         $fruit_color{"apple"}; # gives "red"
 
         $fruit_color{"apple"}; # gives "red"
 
+
You can get at lists of keys and values with '''[http://perldoc.perl.org/functions/keys.html keys()]''' and '''[http://perldoc.perl.org/functions/values.html values()]'''.
    You can get at lists of keys and values with keys() and values().
      
         my @fruits = keys %fruit_colors;
 
         my @fruits = keys %fruit_colors;
 
         my @colors = values %fruit_colors;
 
         my @colors = values %fruit_colors;
   −
    Hashes have no particular internal order, though you can sort the keys and loop through them.
+
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. The most well known of these is %ENV which contains environment variables. Read all about it (and other special variables) in perlvar.
+
Just like special scalars and arrays, there are also special hashes. The most well known of these is '''%ENV''' which contains environment variables. Read all about it (and other special variables) in [http://perldoc.perl.org/perlvar.html perlvar].
   −
Scalars, arrays and hashes are documented more fully in perldata.
+
Scalars, arrays and hashes are documented more fully in [http://perldoc.perl.org/perldata.html perldata].
    
More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes.
 
More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes.
Line 203: Line 205:  
     print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n";
 
     print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n";
   −
Exhaustive information on the topic of references can be found in perlreftut, perllol, perlref and perldsc.
+
Exhaustive information on the topic of references can be found in [http://perldoc.perl.org/perlreftut.html perlreftut], [http://perldoc.perl.org/perllol.html perllol], [http://perldoc.perl.org/perlref.html perlref] and [http://perldoc.perl.org/perldsc.html perldsc].
 +
 
 
=== Variable scoping ===
 
=== Variable scoping ===
   Line 210: Line 213:  
     my $var = "value";
 
     my $var = "value";
   −
The my is actually not required; you could just use:
+
The '''my''' is actually not required; you could just use:
    
     $var = "value";
 
     $var = "value";
   −
However, the above usage will create global variables throughout your program, which is bad programming practice. my creates lexically 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.
+
However, the above usage will create global variables throughout your program, which is bad programming practice. '''my''' creates lexically 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 $x = "foo";
 
     my $x = "foo";
Line 226: Line 229:  
     print $y; # prints nothing; $y has fallen out of scope
 
     print $y; # prints nothing; $y has fallen out of scope
   −
Using my in combination with a use strict; at the top of your Perl scripts means that the interpreter will pick up certain common programming errors. For instance, in the example above, the final print $y would cause a compile-time error and prevent you from running the program. Using strict is highly recommended.
+
Using '''my''' in combination with a '''use strict;''' at the top of your Perl scripts means that the interpreter will pick up certain common programming errors. For instance, in the example above, the final '''print $y''' would cause a compile-time error and prevent you from running the program. Using '''strict''' is highly recommended.
 +
 
 
=== Conditional and looping constructs ===
 
=== Conditional and looping constructs ===
   −
Perl has most of the usual conditional and looping constructs. As of Perl 5.10, it even has a case/switch statement (spelled given /when ). See Switch Statements in perlsyn for more details.
+
Perl has most of the usual conditional and looping constructs. As of Perl 5.10, it even has a case/switch statement (spelled '''given''' /'''when''' ). See [http://perldoc.perl.org/perlsyn.html#Switch-Statements Switch Statements in perlsyn] 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, which are commonly used in conditional statements.
 
The conditions can be any Perl expression. See the list of operators in the next section for information on comparison and boolean logic operators, which are commonly used in conditional statements.
Line 235: Line 239:  
==== if ====
 
==== if ====
   −
         if ( condition ) {
+
         [http://perldoc.perl.org/functions/if.html if] ( condition ) {
 
         ...
 
         ...
         } elsif ( other condition ) {
+
         } [http://perldoc.perl.org/functions/elsif.html elsif] ( other condition ) {
 
         ...
 
         ...
         } else {
+
         } [http://perldoc.perl.org/functions/else.html else] {
 
         ...
 
         ...
 
         }
 
         }
   −
    There's also a negated version of it:
+
There's also a negated version of it:
   −
         unless ( condition ) {
+
         [http://perldoc.perl.org/functions/unless.html unless] ( condition ) {
 
         ...
 
         ...
 
         }
 
         }
   −
    This is provided as a more readable version of if (!condition).
+
This is provided as a more readable version of '''if (!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 conditional blocks more English like:
+
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 conditional blocks more English like:
    
         # the traditional way
 
         # the traditional way
Line 263: Line 267:  
==== while ====
 
==== while ====
   −
         while ( condition ) {
+
         [http://perldoc.perl.org/functions/while.html while] ( condition ) {
 
         ...
 
         ...
 
         }
 
         }
   −
    There's also a negated version, for the same reason we have unless :
+
There's also a negated version, for the same reason we have '''unless''' :
   −
         until ( condition ) {
+
         [http://perldoc.perl.org/functions/until.html until] ( condition ) {
 
         ...
 
         ...
 
         }
 
         }
   −
    You can also use while in a post-condition:
+
You can also use '''while''' in a post-condition:
    
         print "LA LA LA\n" while 1; # loops forever
 
         print "LA LA LA\n" while 1; # loops forever
Line 279: Line 283:  
==== for ====
 
==== for ====
   −
    Exactly like C:
+
Exactly like C:
   −
         for ($i = 0; $i <= $max; $i++) {
+
         [http://perldoc.perl.org/functions/for.html for] ($i = 0; $i <= $max; $i++) {
 
         ...
 
         ...
 
         }
 
         }
   −
    The C style for loop is rarely needed in Perl since Perl provides the more friendly list scanning foreach loop.
+
The C style for loop is rarely needed in Perl since Perl provides the more friendly list scanning '''foreach''' loop.
 +
 
 
==== foreach ====
 
==== foreach ====
   −
         foreach (@array) {
+
         [http://perldoc.perl.org/functions/foreach.html foreach] (@array) {
 
         print "This element is $_\n";
 
         print "This element is $_\n";
 
         }
 
         }
Line 297: Line 302:  
         }
 
         }
   −
    The foreach keyword is actually a synonym for the for keyword. See Foreach Loops in perlsyn.
+
The '''foreach''' keyword is actually a synonym for the for keyword. See [http://perldoc.perl.org/perlsyn.html#Foreach-Loops Foreach Loops in perlsyn].
 +
 
 +
For more detail on looping constructs (and some that weren't mentioned in this overview) see [http://perldoc.perl.org/perlsyn.html perlsyn].
   −
For more detail on looping constructs (and some that weren't mentioned in this overview) see perlsyn.
   
=== Builtin operators and functions ===
 
=== Builtin operators and functions ===
   −
Perl comes with a wide selection of builtin functions. Some of the ones we've already seen include print, sort and reverse. A list of them is given at the start of perlfunc and you can easily read about any given function by using perldoc -f functionname.
+
Perl comes with a wide selection of builtin functions. Some of the ones we've already seen include '''[http://perldoc.perl.org/functions/print.html print]''', '''[http://perldoc.perl.org/functions/sort.html sort]''' and '''[http://perldoc.perl.org/functions/reverse.html reverse]'''. A list of them is given at the start of [http://perldoc.perl.org/perlfunc.html perlfunc] and you can easily read about any given function by using '''perldoc -f functionname'''.
   −
Perl operators are documented in full in perlop, but here are a few of the most common ones:
+
Perl operators are documented in full in [http://perldoc.perl.org/perlop.html perlop], but here are a few of the most common ones:
    
==== Arithmetic ====
 
==== Arithmetic ====
Line 331: Line 337:  
         ge greater 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 numerically (where 99 is less than 100) or alphabetically (where 100 comes before 99).
+
(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).
    Boolean logic
+
==== Boolean logic ====
    
         && and
 
         && and
Line 338: Line 344:  
         ! not
 
         ! not
   −
    (and , or and 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 && and friends. Check perlop for more detail.)
+
('''and''' , '''or''' and '''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 '''&&''' and friends. Check [http://perldoc.perl.org/perlop.html perlop] for more detail.)
 
==== Miscellaneous ====
 
==== Miscellaneous ====
   Line 346: Line 352:  
         .. range operator (creates a list of numbers)
 
         .. range operator (creates a list of numbers)
   −
Many operators can be combined with a = as follows:
+
Many operators can be combined with a '''=''' as follows:
    
     $a += 1; # same as $a = $a + 1
 
     $a += 1; # same as $a = $a + 1
Line 354: Line 360:  
=== Files and I/O ===
 
=== Files and I/O ===
   −
You can open a file for input or output using the open() function. It's documented in extravagant detail in perlfunc and perlopentut, but in short:
+
You can open a file for input or output using the [http://perldoc.perl.org/functions/open.html open()] function. It's documented in extravagant detail in [http://perldoc.perl.org/perlfunc.html perlfunc] and [http://perldoc.perl.org/perlopentut.html perlopentut], but in short:
    
     open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
 
     open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
Line 360: Line 366:  
     open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
 
     open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
   −
You can read from an open filehandle using the <> 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:
+
You can read from an open filehandle using the '''<>''' 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 = <$in>;
 
     my $line = <$in>;
Line 367: Line 373:  
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.
 
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 <> operator is most often seen in a while loop:
+
The '''<>''' operator is most often seen in a '''while''' loop:
    
     while (<$in>) { # assigns each line in turn to $_
 
     while (<$in>) { # assigns each line in turn to $_
Line 373: Line 379:  
     }
 
     }
   −
We've already seen how to print to standard output using print(). However, print() can also take an optional first argument specifying which filehandle to print to:
+
We've already seen how to print to standard output using '''[http://perldoc.perl.org/functions/print.html print()]'''. However, '''[http://perldoc.perl.org/functions/print.html print()]''' can also take an optional first argument specifying which filehandle to print to:
    
     print STDERR "This is your final warning.\n";
 
     print STDERR "This is your final warning.\n";
Line 379: Line 385:  
     print $log $logmessage;
 
     print $log $logmessage;
   −
When you're done with your filehandles, you should close() them (though to be honest, Perl will clean up after you if you forget):
+
When you're done with your filehandles, you should '''[http://perldoc.perl.org/functions/close.html close()]''' them (though to be honest, Perl will clean up after you if you forget):
    
     close $in or die "$in: $!";
 
     close $in or die "$in: $!";
Line 385: Line 391:  
=== Regular expressions ===
 
=== Regular expressions ===
   −
Perl's regular expression support is both broad and deep, and is the subject of lengthy documentation in perlrequick, perlretut, and elsewhere. However, in short:
+
Perl's regular expression support is both broad and deep, and is the subject of lengthy documentation in [http://perldoc.perl.org/perlrequick.html perlrequick], [http://perldoc.perl.org/perlretut.html perlretut], and elsewhere. However, in short:
    
==== Simple matching ====
 
==== Simple matching ====
Line 392: Line 398:  
         if ($a =~ /foo/) { ... } # true if $a contains "foo"
 
         if ($a =~ /foo/) { ... } # true if $a contains "foo"
   −
    The // matching operator is documented in perlop. It operates on $_ by default, or can be bound to another variable using the =~ binding operator (also documented in perlop).
+
The '''//''' matching operator is documented in [http://perldoc.perl.org/perlop.html perlop]. It operates on '''$_''' by default, or can be bound to another variable using the '''=~''' binding operator (also documented in [http://perldoc.perl.org/perlop.html perlop]).
 
==== Simple substitution ====
 
==== Simple substitution ====
   Line 400: Line 406:  
         # in $a
 
         # in $a
   −
    The s/// substitution operator is documented in perlop.
+
The '''[http://perldoc.perl.org/functions/s.html s///]''' substitution operator is documented in [http://perldoc.perl.org/perlop.html perlop].
 +
 
 
==== More complex regular expressions ====
 
==== More complex regular expressions ====
   −
    You don't just have to match on fixed strings. In fact, you can match on just about anything you could dream of by using more complex regular expressions. These are documented at great length in perlre, but for the meantime, here's a quick cheat sheet:
+
You don't just have to match on fixed strings. In fact, you can match on just about anything you could dream of by using more complex regular expressions. These are documented at great length in [http://perldoc.perl.org/perlre.html perlre], but for the meantime, here's a quick cheat sheet:
    
         . a single character
 
         . a single character
Line 420: Line 427:  
         $ end 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 metacharacters in parentheses.
+
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
 
         * zero or more of the previous thing
Line 429: Line 436:  
         {3,} matches 3 or more of the previous thing
 
         {3,} matches 3 or more of the previous thing
   −
    Some brief examples:
+
Some brief examples:
    
         /^\d+/ string starts with one or more digits
 
         /^\d+/ string starts with one or more digits
Line 446: Line 453:  
==== Parentheses for capturing ====
 
==== Parentheses for capturing ====
   −
    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 $1 , $2 and so on.
+
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 '''$1''' , '''$2''' and so on.
    
         # a cheap and nasty way to break an email address up into parts
 
         # a cheap and nasty way to break an email address up into parts
Line 456: Line 463:  
==== Other regexp features ====
 
==== Other regexp features ====
   −
    Perl regexps also support backreferences, lookaheads, and all kinds of other complex details. Read all about them in perlrequick, perlretut, and perlre.
+
Perl regexps also support backreferences, lookaheads, and all kinds of other complex details. Read all about them in [http://perldoc.perl.org/perlrequick.html perlrequick], [http://perldoc.perl.org/perlretut.html perlretut], and [http://perldoc.perl.org/perlre.html perlre].
    
=== Writing subroutines ===
 
=== Writing subroutines ===
Line 472: Line 479:  
     logger("We have a logger subroutine!");
 
     logger("We have a logger subroutine!");
   −
What's that shift? Well, the arguments to a subroutine are available to us as a special array called @_ (see perlvar for more on that). The default argument to the shift function just happens to be @_ . So my $logmessage = shift; shifts the first item off the list of arguments and assigns it to $logmessage .
+
What's that '''[http://perldoc.perl.org/functions/shift.html shift]'''? Well, the arguments to a subroutine are available to us as a special array called '''@_''' (see [http://perldoc.perl.org/perlvar.html perlvar] for more on that). The default argument to the '''[http://perldoc.perl.org/functions/shift.html shift]''' function just happens to be '''@_''' . So '''my $logmessage = shift;''' shifts the first item off the list of arguments and assigns it to '''$logmessage''' .
    
We can manipulate @_ in other ways too:
 
We can manipulate @_ in other ways too:
Line 492: Line 499:     
For more information on writing subroutines, see [http://perldoc.perl.org/perlsub.html perlsub].
 
For more information on writing subroutines, see [http://perldoc.perl.org/perlsub.html perlsub].
 +
 
=== OO Perl ===
 
=== OO Perl ===
   Line 516: Line 524:     
Kirrily "Skud" Robert <skud@cpan.org>
 
Kirrily "Skud" Robert <skud@cpan.org>
 +
[[Category:Developer]]
 +
[[Category:SME Server Development Framework]]
 +
[[Category:Development Tools]]

Navigation menu