Perl Overview
Perl was first developed by Larry Wall and is a "glue" language, for example, converting the output of the previous program into the input of the next program. The first version of Perl was released in December 18, 1987 and has been steadily upgraded until Perl 5. Perl 5 is a major upgrade that includes some improvements to the old Syntax of Perl 4. The Perl version of this book is 5.10.1. Perl is still being upgraded. the next major version is Perl 6, and Perl 6 is a fully rewritten Perl implementation. Unlike the previous version, the Community contributes code completely.
Perl aims to "more than one approach" (tmtowtdi, there's more than one way to do it .). It has the following advantages:
Easy to learn
Good code readability
Portable)
Good at Text Processing
Perl is a high-level language that does not need to consider many details like C/C ++.
Chapter 2: perlhello, world!
The first PERL Program (helloworld. pl ):
#!/usr/bin/perluse warnings;print "Hello, world!\n";
Perl#The content from the end to the end of the line is considered as a comment. In Linux/Unix ,#!ProgramIndicatesProgramThis text file is processed, so the first line is written.#! /Usr/bin/perlIndicates that the file is composed/Usr/bin/perlIf the file has the + x permission, execute./helloworld. pl is equivalent to executing Perl helloworld. pl.
Enable warning information
Declare at the beginning of source code
Use warnings;
Or you can call the Perl run. pl source code with the-W parameter to enable the warning information.
Debugging
Run. pl with Perl-D for debugging.
Chapter 2: Scalar
Scalar in Perl refers to a single simple data type, such as integer, floating point, and string. Literal is a constant. For example, 5. A variable is an identifier that can be re-assigned. All variables start with $, for example, $ number.
Number
Perl can be segmented by underscores (_). For example, 10,000,000 can be written as 10_000_000.
Write the decimal number directly. The value 0 starts with octal, and the value 0 starts with binary, and the value 0x starts with hexadecimal.
String
Single quotation marks '/double quotation marks' can represent strings, the difference is that strings in single quotation marks do not handle escape characters ('except, \' indicates '), suchPrint '\ tThis is a single-quoted string. \ n ';Output\ TThis is a single-quoted string. \ nDouble quotation marks are used to process escape characters in strings.
You can also use Q // and QQ // to represent a string (two or two strings). Q // is equivalent to single quotation marks and QQ // is equivalent to double quotation marks. In addition to/, you can use any non-character or non-numeric characters, such as | ,#. in addition, you can use four symbol pairs: {}, [], (), and <>.
You can use here-document to include a large amount of text in your code. The local document format is:
<Label
Document Content
Label
WhereLabelIt can be EOF or EOT. The content of the document is processed by string content written in double quotation marks. There are usually strings with too many rows that can be declared in the local document. For example:
print <<EOF;This is a here-document. It starts on the line after the two arrows,and it ends when the text following the arrows is found at the beginningof a line, like this:EOF
Numeric/String Conversion
"0.25" * 4The value is 1.
Hex ()The function converts a numeric string written in hexadecimal notation to the number type.
Oct ()Parse the numeric string written in octal
Operator
+,-, *,/, ++, --, (,), &, |, ^ ,~ , = ,! =, &, | ,! , =, Same as C/C ++, Java
$ A = $ A <some operator> $ B;Can be abbreviated$ A <some operator >=$ B;For example, $ A = $ A + $ B can write $ A + = $ B in the same way as C/C ++ and Java
** Is a power operation operator, 2 ** 4 = 16
In Perl, the following values are boolean values false: 0 (number 0), "0" (string "0"), "" (Null String), undefined, and empty table (empty list)
<=> OPERATOR: A <=> B compares the sizes of A and B. A is 1, equal to 0, and B is-1.
String OPERATOR:. Is the string merging operator, "ba". "Nana"-> "banana"
You can use X to perform string multiplication "ba". ("Na" X 2)-> "banana"
Ord () can get an ASCII value of a character, such as ord ("F")-> 102
Variable
The variable scope is the current code block and all the child blocks in the code block.
Generally, variables can be declared and assigned values at will. But there is a problem. If you declare$ Foo = 4;And later it is written$ Ofo = 7;It will make the code work incorrectly (and cannot be found at once)
You can declare use strict; Make Perl strictly check the usage of variables. Using undefined/initialized variables will cause compilation errors. The my keyword must be used to declare variables in strict mode. For exampleMy $ record ;.Use strict is recommended for easy maintenance of code specifications.
Special variables: There are some special variables. For example, $ _ represents the default parameters of many function input and output. For special variables, see perldoc perlvar.
Variables can be used in double quotation marks, and the variables will be replaced with their values. For example
my $name = "fred";my $salutation = "Dear $name,";print $salutation, "\n";
Output
Dear Fred,
Standard Input <stdin>
My $ yen = <stdin>;Read once from stdin, read once every assignment
Chomp () modifies the specified string of the input parameter. If the last character of the string is a line break, remove it. If not, do not modify it.
The Chop () function removes the last character from the input string.
Chomp () and chop () directly modify the input, no return value
Two Functions
Exit () is used to exit. Exit () is equivalent to exit (0) and Exits normally. Exit (1) indicates exit with an error
Die () is used to report an error and exit. Die accepts a string parameter, prints it to stderr, and then exits (1) two functions.
Chapter 1 Process Control
Similar to C/C ++ and Java:
If (){...} Else (){...}
For (Init;Cond;OP){...}
While (Cond){...}
Do {...} While (Cond)
Infinite Loop while (1 ){...}
Logical operator
String comparison operators: GT (>), LT (<), Ge (> =), Le (<=), eq (=), NE (! =) Note that the brackets are only comments and cannot be replaced by common ones.
Defined ($ var) operator. If $ VaR is defined, it is true; otherwise, it is false.
Use and instead of &, or |, not to replace !. Note the operator priority of and/or/Not and &/| /! Different
If (){...} Elseif (){...} Elseif (){...} Else (){...} Can be used for multi-branch Process Control
Unless (Cond) Is equivalent to If (!Cond)
You can set
if ($number == 0) {die "can't divide by 0";}
Writing
die "can't divide by 0" if $number == 0;
Until (Cond){...} It is equivalent to while (!Cond){...}
Foreach my $ VaRListYou can use the variable $ VaR to traverse the list.ListFor example, foreach my $ number (1 .. 10 ){...}
Do {...} Until (Cond) Is equivalent to do {...} While (!Cond)
In Perl, last equals to break in C/C ++ and Java, and next equals to continue.
Redo statements can be restarted and executed from the beginning of the loop. Note that the redo restart loop does not perform condition checks for and while/until, but directly executes the loop from the beginning (in my personal understanding, this is to remove the condition check on the basis of next)
Loop mark. By default, last, next, and redo are only valid for the loop of the layer. To jump to the outer layer, you need to use the tag:
OUTER: while ($i <= 5) {my $j = 1;while ($j <= 5) {last OUTER if $j == 3;print "$i ** $j = ", $i ** $j, "\n";$j++;}$i++;}