1 Perl Automatic Detection
1.1 Command Line Detection Method
You can add the-W parameter to the command line parameters of the first line of the program, as follows:
#! /Usr/local/bin/perl-W
1.2 internal Detection Method of Perl Program
Add the following lines to the program:
Use strict;
Or
Use diagnostics;
2 variables
2.1 scalar
General Usage: $ scalar.
Array 2.2
General Usage: @ array. Array subscript starts from 0.
The element types in the array can be different.
Use of elements in the array: $ array [1].
Array reference (which can be seen as a pointer in C): $ arrayref =/@ array;
Use: $ arrayref [1] Or @ $ arrayref.
2.3 hash
General Usage: % hash.
Use of elements in a hash table: $ hash {index }.
Hash reference (which can be seen as a pointer in C): $ hashref =/% hash;
Use: $ hashref {index} or % $ hashref.
Output Method of the content in the hash table:
Use Data: dumper;
Print dumper (/% hash );
2.4 determine the type of the Variable
You can use the function ref. For example:
$ Arrayref = // @ array;
Print REF ($ arrayref );
2.5 scope of Variables
Variables in Perl do not need to be declared. variables can be introduced at Will anytime and anywhere. However, if we do not add any restrictions before the variables, the variables introduced wherever they are will be global variables.
To maintain program consistency and integrity, Perl provides the following two methods to control the scope of variables.
2.5.1 my scope
My scope is mainly used to declare local variables.
The variables declared by my will act on the declared levels and subroutines defined at the same level.
2.5.2 local scope
Local scope is mainly used for dynamic effects on global variables. It only affects declared levels and lower levels, indicating that the value Modification of the global variable at the local scope declaration level is only valid at this level and lower levels, the value of the global variable on the external layer does not have any impact.
2.6 internal variables
2.6.1
Internal file handle that receives user input from the keyboard.
2.6.2 stdout and stderr
Internal file handle. stdout is the standard output and stderr is the error output.
2.6.3
Command line parameter file handle.
2.6.4 @ argv
Save the command line parameters in the array.
2.6.5 @ INC
Save the directory order of the Perl Search Library in the array.
2.6.6% Inc
Use a hash structure to give the source (directory) of the use library ).
2.6.7% SIG
Set the signal processing program in the Perl program. For example:
$ Sig {int} = 'ignore ';
Or
$ Sig {int} =/& hit_control_c;
2.6.8 $ _
Act as the default variable. $ _ Becomes the variable when a variable is obviously needed but not specified in the statement.
When a function has no parameters and we know it must have one.
Print;
Die if (-F );
See the code like a regular expression, but it does not contain = ~ .
If (M "Whatever) {& do_something ;}
Used in the foreach loop.
Foreach (@ ARGs) {$ _...}
2.6.9 $"
When you decide to convert an array to a string, Perl automatically adds information between each element. For example:
$ "=" | ";
Print "@ arrayname ";
2.6.10 $,
When you decide to convert an array to a string output, Perl automatically adds information between each element, but it only applies to the situations listed below. For example:
$, = "| ";
Print @ arrayname;
2.6.11 $/
Determines the information added at the end of each row in print output. The default value is null.
2.6.12 $/
Determines the delimiter of the Data row when reading information from the file handle. The default value is '/N '.
2.6.13 $1, $2, $3 ,...
Save the matching part specified by parentheses in the regular expression.
2.6.14 $ ', $ &, $'
When a regular expression matches, it matches the previous text, the actual text, and the matched text.
Situation when multiple matches need to be confirmed
2.6.15 $0
Program name.
2.6.16 $
The ID of the current process.
2.6.17 $?
Displays the final error status of any system call or command in the anti-mark.
3. Control Structure
Note: In all control structures, even if there is only one statement, {} must be added {}.
3.1 Branch Structure
3.1.1 If-else-elsif
Syntax:
If (condition1)
{
Doifsomething ();
}
Elsif (condition2)
{
Doelsifsomething ();
}
Else
{
Doelsesomething ();
}
3.1.2 Unless
Syntax:
Unless (condition)
{
Dounlesssomething ();
}
3.2 Loop Structure
3.2.1 while
Syntax:
While (condition)
{
...
}
Continue
{
# Statements executed after each loop ends.
}
3.2.2
Syntax:
For (pre_clause; end_condition; clause after each loop)
{
...
}
The while statement is:
Pre_clause;
While (end_condition)
{
...
}
Continue
{
Clause after each loop;
}
3.2.3 foreach
Syntax:
Foreach variable (array)
{
...
}
3.2.4
Syntax:
Until (condition)
{
...
}
The while statement is equivalent:
While (! Condition ){... }
3.2.5 do... While
Syntax:
Do {clause;} while (condition );
The while statement is equivalent:
{Clause ;}
While (condition) {clause ;}
3.2.5.1 do... Until
Syntax:
Do {clause;} until (condition );
The while statement is equivalent:
{Clause ;}
While (! Condition) {clause ;}
3.3 control in Loop Structure
3.3.1 next
Terminate the current execution of the loop at the tag specified by next and start the next loop. The statement of the continue block cannot be skipped.
Syntax:
Next [tag];
3.3.2 last
Run the code block following the control structure of the tag specified by last.
Syntax:
Last [tag];
3.3.3 redo
Similar to next, redo does not change the loop conditions of for/foreach.
4 Functions
4.1 function call and Return Value
Multiple values can be returned as follows:
Sub function {
...
If (condition1 ){
Return ($ value1 );
}
...
Return (/@ array1, $ value2 );
}
Function call method:
($ Return1, $ return2) = & function ();
Or
($ Return1, $ return2) = & function;
Or
($ Return1, $ return2) = function ();
Or
($ Return1, $ return2) = function;
4.2 function parameter transfer
Parameters in the function are transmitted in arrays.
The function is defined as follows:
Sub function {
My ($ parameter1, $ parameter2, @ parameter3) = @_;
...
}
The function is called as follows:
& Function ($ value1, $ value2, @ value3 );
When using function parameters, we need to pay attention to the following issues:
L use of array parameters
The following function definitions can achieve the expected results:
Sub function {
My (@ array1, @ array2) = @_;
...
}
At last, we will find that @ array2 is empty, and the incoming two arrays are merged into an array @ array1.
L transfer by reference
Try to use the reference method for transmission. The above functions can be implemented as follows:
Sub function {
My ($ parameter1, $ parameter2) = @_;
My (@ array1, @ array2 );
@ Array1 = @ $ parameter1;
@ Array2 = @ $ parameter2;
...
}
Function call method:
& Function (// @ array1, // @ array2 );
4.3 dynamic function call
Use eval in Perl to call functions dynamically. For example:
If (condition1 ){
$ Function = "function1 $ parameter1 ";
} Else {
$ Function = "otherwise ";
}
Eval $ function;
...
Sub function1 {
My ($ parameter1) = @_;
...
}
Sub otherwise {
...
}
When using Eval, we should pay attention to the following issues:
L The programs executed in eval fully comply with the Perl syntax;
L The main program will not be terminated when an error occurs in the Program executed by eval;
L eval can use all the variables in the main program.
5. Statement simplification
We can use the if statement:
If (condition1 ){
If (condition2 ){
Clause;
}
}
Simplified:
Condition1 & condition2 & clause;
We can use the unless statement:
Unless (condition1 ){
Unless (condition2 ){
Clause;
}
}
Simplified:
Condition1 | condition2 | clause;
6. Regular Expression
We can use regular expressions to complete pattern matching and replacement in strings. They can only be used for scalar.
6.1 delimiters
A regular expression can use/and "as the delimiter. The specific delimiters can be determined based on the matching mode. If ", we can use/as the Separator in the matching mode. If"/"appears in the matching mode, we can use" as the separator.
6.2 wildcard characters
6.2.1 Declaration
/D Non-numeric
/D Number
/W non-word
/W words
/S non-space
/S space
'.' Any character other than line breaks
6.2.2 negative declaration (only one position, not matching specific characters)
^ Starts with a string
$ End of string
/B word boundary
/B Non-word boundary
6.3 Multiple matching Operators
6.3.1 greedy
* Match 0 times, once or multiple times
+ Match once or multiple times
? Match 0 times or 1 time
{X} matches exactly X times
{X,} matches X times or more times
{X, y} matches X to Y.
6.3.2 non-greedy
*? Matches 0 times, once or multiple times, but the minimum possible number of matches
+? Matches once or multiple times, but the minimum number of possible matches
?? Match 0 times or 1 time
{X }? Match exactly X times
{X ,}? Matches X or more times, but the minimum number of possible matches
{X, y }? Matches X to Y times, but the minimum number of possible matches
6.4 special usage
6.5 reverse reference
Perl supports reverse referencing to retrieve content of interest from matching strings. For multiple reverse references, the system stores them in the system variables $1, $2,… from left to right ,... . For example:
$ String = "ABCD my content efgh next content ijkl ";
$ String = ~ /(My C. * ent). * (next C. * ent )/
The matching content in the first bracket is my content, which is saved in the system variable $1; the Matching content in the second bracket is next content, which is saved in the system variable $2.
L nested reverse reference
For nested reverse references, the order in which the system stores the content may be related to the specific perl interpreter. The conclusion I have tested is as follows, for the same level, it is left to right.
6.6 Modifier
X is a readable regular expression that allows the expression to be written into multiple rows.
I-case-insensitive Regular Expression
S treats the matched string as a single string
M treats the matched string as multiple strings.
O only compiles regular expressions once
G is only useful for the replacement method. It replaces all strings that can be matched in the matched string.
6.7 example