Changes

Jump to navigation Jump to search
1,368 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 151: Line 153:  
You can do various useful things to lists:
 
You can do various useful things to lists:
   −
         my @sorted = sort @animals;
+
         my @sorted = [http://perldoc.perl.org/functions/sort.html sort] @animals;
         my @backwards = reverse @numbers;
+
         my @backwards = [http://perldoc.perl.org/functions/reverse.html 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 [http://perldoc.perl.org/perlvar.html perlvar].
 
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].
Line 172: Line 174:     
         $fruit_color{"apple"}; # gives "red"
 
         $fruit_color{"apple"}; # gives "red"
You can get at lists of keys and values with '''keys()''' and '''values()'''.
+
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()]'''.
    
         my @fruits = keys %fruit_colors;
 
         my @fruits = keys %fruit_colors;
Line 237: 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] {
 
         ...
 
         ...
 
         }
 
         }
Line 247: Line 249:  
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 ) {
 
         ...
 
         ...
 
         }
 
         }
Line 265: Line 267:  
==== while ====
 
==== while ====
   −
         while ( condition ) {
+
         [http://perldoc.perl.org/functions/while.html while] ( condition ) {
 
         ...
 
         ...
 
         }
 
         }
Line 271: Line 273:  
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 ) {
 
         ...
 
         ...
 
         }
 
         }
Line 283: Line 285:  
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 299: Line 302:  
         }
 
         }
   −
    The '''foreach''' keyword is actually a synonym for the for keyword. See [http://perldoc.perl.org/perlsyn.html#Foreach-Loops 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 [http://perldoc.perl.org/perlsyn.html perlsyn].
Line 305: Line 308:  
=== 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 403: Line 406:  
         # in $a
 
         # in $a
   −
The '''s///''' substitution operator is documented in [http://perldoc.perl.org/perlop.html 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 ====
   Line 520: 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