First taste of Perl

Source: Internet
Author: User



This article will describe the following aspects:



1. What is Perl



2. What is perl for?



3. Construction of Perl environment under Windows



4. Perl Version Hello World



5. Perl Grammar Synopsis



6. Some reference materials








What is Perl


Perl is a scripting language designed and implemented by Larry Wall (Larry Wall), and Larry Wall's original purpose in designing the language was to make it easier for UNIX to work on report processing. The first edition of Perl was released for 1987 years (the first version of Python was released for 1991 years, the first version of Ruby was released in 1995, and the three programming languages were often compared).



Perl is from the word pearl (the Meaning of pearls), perhaps in the designer Larry Wall eyes, perl as precious as pearls



Perl's mascot for Camels is said to be because OReilly published the best-selling programming Perl's cover is a camel, the Perl community adopted the Camel as the mascot of the Perl language.





What is Perl for?


Referring to Wikipedia, Perl has a wide range of uses, in addition to CGI, Perl is also used in graphic programming, system programming, network programming, finance, biology and other fields. Due to its flexibility, Perl is called the Swiss Army Knife in the scripting language. There are plenty of good Perl libraries on the CPAN. Perl is built-in on both Linux and Mac OS. It's useful to summarize a sentence--perl, and when you need a scripting language, consider Perl. We can understand these.



This article for the first taste of Perl, positioning to understand the outline of Perl, bloggers are currently working in the real work of Perl, just as a knowledge of the reserve, later in the use of Perl in-depth study.





Perl Environment setup under Windows


1, visit http://www.perl.org/Download the implementation version of Perl Windows, there are two versions (Strawberry perl and ActiveState Perl) These two versions are no big difference, Bloggers chose the latter ActiveState Perl 5.20



2, install ActiveState Perl 5.20, all the way by default installation can



3. Verify that Perl is installed successfully, enter perl–v at the command prompt






If the displayed information indicates that Perl has been installed successfully





Perl version Hello World


1, open a text editor (such as notepad++), enter the following content


# perl-w Print " Hello World ";


Run the result at the fame prompt (the code is saved in 1.pl)






The # above is an annotation symbol, PERL–W is an option that requires Perl to explicitly warmming information





Perl Syntax Synopsis


1, variable definition, in Perl there are several ways to define variables, I picked the most common two kinds of instructions:



singular variable : Starts with $ and represents only one variable object



Complex variable : begins with @, represents an array



Example 1: singular variables


 
 
# perl-w
$pi = 3.141592;
print $pi;


Example 2: Complex variables


 
# perl-w
@digital = (a..z);
Print @digital; #Print all lowercase letters from a to z


2, each statement with a semicolon ";" End






3. Simple mathematical operation


# perl-w
$a = 2;
$b = 3;
Print $a + $b; # addition
Print "\n";
Print $a - $b; # subtraction
Print "\n";
Print $a % $b; #modulo operation
Print "\n";
Print $a ** $b; # power operation 

4. Comparison operator and if branch structure


Comparison Digital String Meaning
Equals == eq Judging equality or not
Range != Ne Judging unequal or not
Less than < Lt Judged less than or not
Greater than > Gt Judgment greater than or not
Less than or equal to <= Le Judgment is not greater than or not
Greater than or equal to >= Ge Judge not less than or not
Comparison <=> Cmp Equal to 0, the former large is 1, otherwise-1


Perhaps readers see this will be strange, why there are two sets of comparison operators, in fact, I am also very strange, but the Perl design is the design of this, I do not care. These two sets of comparators require that the parameters to be compared are first converted to the appropriate parameter type after comparison. Light say no practice fake Bashi See code:


# perl-w
$a = "a";
$b = "b";
If($a == $b){
     Print "two variables equal";
} else {
     Print "The two variables are not equal";
}
# will print two variables equal because $a and $b are both strings converted to numbers will fail, return the same default value, so equal 




# perl-w
$a = "a";
$b = "b";
If($a eq $b){
     Print "two variables equal";
} else {
     Print "The two variables are not equal";
}



If branch includes if, If–else, if– several elsif-else 3 kinds






5. Circulation



Perl has 4 loops of while, Do-while, for, Foeach


# perl-w
# print 1-100 Divisible by 3
$var = 1;
While($var <= 100){
     If($var % 3 == 0){
         Print $var;
         Print " ";      }
     $var += 1;
} 





Do-while slightly


# perl-w
# 打印1-100 Divisible by 3
For($var = 1;$var <=100;$var++){
     If($var % 3 == 0) {
         Print $var;
         Print " ";
     }
} 


The format of foreach is:



foreach $i (@some_list) {



statement_1;



...



Statement_n;



}


# perl-w
# 打印1-100 Divisible by 3
# (1..100) returns an array
Foreach $var ((1..100)){
     If($var % 3 == 0){
         Print $var;
         Print " ";
     }
} 


There are two loop control keywords--last and next in for and foreach. Last equivalent to Break,next for continue, C-based people can read what meaning.






6. Functions



The definition of a function is declared with a key sub



Sub methodname{



statement_1;



...



Statement_n;



}



Parameter list exists in @_



As function with return value, end with Return statement interface



Look at the code


# perl-w
Sub add{
     Foreach $para (@_) {
         Print $para;
         Print " ";
     }
     Print "\n";
     Return $_[0] + $_[1]; # with return value
}
Print add(3,4);



Some references


1. Small Camel Book






2, the Big Camel Book






3. Perl official website http://www.perl.org/






End of this article



First taste of Perl


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.