1. Simple Variables
==============
1. Integer
$ X = 123456;
Perl stores integer data in floating-point registers. In fact, integer variables are treated as floating-point numbers in Perl, but they are only a special column of floating-point data.
The octal data starts with 0.
$ Var1 = 047 (decimal 39)
Hexadecimal data headers with 0x
$ Var2 = 0x1f (decimal 31)
2. Floating Point Type
Floating-point registers are usually unable to accurately store floating-point numbers, which may produce errors. When using floating point numbers, note that the index range is usually-309 to + 308.
E.g.
11.4,-0.3,. 3, 3., 64.1e + 02, 5.4e04
$ Value = 9.01e + 21 + 0.01-9.01e + 21; (value is 0)
$ Value = 9.01e + 21-9.01e + 21 + 0.01; (value: 0.01)
3. String
The string in pelr does not end with '/0' in C. Note that.
Strings in Perl support single quotes and double quotes, but they are different:
Double quotation marks support replacement of simple variables:
$ Number = 11;
$ Text = "This text contains the number $ number .";
Double quotation marks support escape characters:
/A bell
/B backspace
/CN the CTRL + N character
/E escape
/E end the effect of/L,/U or/Q
/F form feed
/L forces the next letter into lowercase
/L all following letters are lowercase
/N newline
/R carriage return
/Q do not look for specifial Pattern Characters
/T Tab
/U force next letter into uppercase
/U all following letters are uppercase
/V vertical Tab
To enclose a string with double quotation marks or backslash, you must add a backslash as an escape character:
$ Res = "A quote/" and a backslash //";
There are two differences between single quotation marks and double quotation marks: 1. There is no variable replacement function, and 2. The backslash does not support escape characters.
All variables in Perl are UNDEF by default.
Ii. Operators
=====
1. Arithmetic Operators
A) The power base cannot be negative: (-5) ** 2
B) The result of Power Multiplication cannot exceed the computer's representation range: 10 ** 99999
C) if the remainder operand is not an integer, It is rounded to an integer before calculation. The right side of the operator cannot be 0.
D) single-object negative values can be used as variables:-$ Y (equivalent to $ y *-1)
2. Integer comparison Operators
<Less
> Greater
= Equal
<= Less than or equal
> = Greater than or equal
! = Not equal
<=> Comparison, return 1, 0 or-1 (0 values are equal, 1 the first value is large,-1 The second value is large)
E.g.
$ Value1 = 20;
$ Value2 = 30;
Print $ value1 <=> $ value2;
The result is-1.
3. String comparison Operators
Lt is less
GT>
Equal to EQ
Le is less than or equal
GE is greater than or equal
Ne is not equal
CMP comparison, returns 1, 0, or-1
4. logical operators
| (OR) logical or
& (And) logic and
! (Not) logical non-
XOR logic exclusive or
5. bitwise operators
& Bit and
| Bit or
~ Bitwise
^ Bitwise OR
<Left shift
> Right shift
PS: If $ is used as a negative integer, Perl converts it to an unsigned number.
6. Value assignment operator
=
+ =
-=
* =
/=
% =
** =
& =
| =
^ =
7. Auto-increment and auto-Increment
8. String concatenation and repeated Operators
Join:. (. =)
Repeat: x
E.g.
$ Newstring = "potato". "head"; (equal to "Potatohead ")
$ Newstring = "T" x 5 (equal to "ttttt ")
9. Comma Operator
The expression before the comma is calculated first.
$ Var1 + = 1, $ var2 = $ var1;
Equal:
$ Var1 + = 1;
$ Var2 = $ var1;
$ Var = 26;
$ Result = (++ $ var, $ var + 5); (result is 32)
10. Conditional Operators
$ Result = $ Var = 0? 14: 7
11. operator precedence
Ii. List and array Variables
======================
1. List
The list is a sequence of values contained in parentheses. It can be any value or be empty.
E.g.
(1, 4.3, "hello", 55)
() Empty list
PS: the list (4.3) containing a value is different from the value itself (4.3), but it can be converted
(17, $ var, "a string ")
(17,256 <2)
2. Array (@)
The list is stored in an array variable. Unlike a simple variable, the array variable starts "@".
@ Array = (1, 2, 3 );
When an array variable is created, the initialization value is an empty list (). Since Perl uses @ and $ to distinguish array variables from simple variables, you can use the same name to represent array variables and simple variables.
E.g.
$ Var = 1;
@ Var = (11, 23, $ var );
Array Storage
Values in the array can be accessed by subscript. The first element subscript is 0. If you try to access an element that is not in the array, the result is null. If you assign a value to an element that exceeds the array size, the array will automatically grow, and the increasing element will fill in the null value.
E.g.
@ Array = (1, 2, 4, 5 );
$ Scalar = $ array [0];
$ Array [3] = 4; # array is (1, 2, 4, 4)
$ Scalar = $ array [4]; # $ scalar is null
$ Array [6] = 17; # @ array is (1, 2, 4, 4, "", "", 17)
Copy Array
@ Result = @ original
Assign values to the list using Arrays
@ List1 = (2, 4, 5 );
@ List2 = (1, @ list1, 65 );
Assign values to simple variables
@ Array = (2, 3, 4 );
($ Var1, $ var2, $ var3) = @ array;
Square brackets and variable replacement in the string
"$ Var [0]" 1st element values in the array
"$ Var/[0]" equals "$ Var". "[0]"
"$ {Var} [0]" equals "$ Var". "[0]"
"$/{Var}" equals $ {var}
List range
(1 .. 10) = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
(3 .. 3) = (3)
(5.3. 2.1) = (3.1, 4.1, 5.1)
(1.6) = ()
("AAA"... "aad") = ("AAA", "AAB", "AAC", "aad ")
($ Var1.. $ var2 + 4)
Array output
Print ("@ array/N ")
Array Length
If the array variable appears where the expected simple variable appears, Perl parses it to its length.
@ Array = (1, 2, 4)
$ Scalar = @ array; # $ scalar = 3
($ Scalar) = @ array; # $ scalar = 1
The above expression actually regards $ scalar as an expression for retrieving list elements. Therefore, it obtains the value of the first element of the List array.
E.g. traverse the list with array Length
$ COUNT = 1;
While ($ count <= @ array ){
Print ("element $ count: @ array [$ count-1]/n ");
$ Count ++;
}
Child Array
@ Array = (1, 2, 3, 4, 5 );
@ Subarray = @ array [0, 1]; # @ subarray = (1, 2)
@ Subarray2 = @ array [1 .. 3]; # @ subarray = (2, 3, 4)
@ Array [0, 1] = ("string", 24) # @ array = ("string", 24, 3, 4, 5)
@ Array [1, 2] = @ array [2, 1] exchange two elements in the form of child Arrays
Array Functions
Sort-sort by character
@ Array = ("this", "is", "a", "test ");
@ Array2 = sort (@ array); # @ array2 = ("a", "is", "test", "this ")
Reverse-reverse Array
@ Array2 = reverse (@ array );
Chop-array tail
When the STDIN input string is removed, the last character-line break is used. If an array is used, each element of the array is processed at the end.
@ List = ("rabbit", "12345", "quartz ");
Chop (@ list); # @ list = ("rabbi", "1234", "quart ")
Join/split-join/split
The first parameter of join is the delimiter used for the connection, and the rest are the character arrays to be connected.
$ String = join ("", "this", "is", "a", "string"); # "this is a string"
$ String = "words: and: clons"
@ Array = split (/: //, $ string); # @ array = ("words", "and", "colons ");
Iii. file read/write
==============
1. Open and Close files
Open (filevar, filename)
Filevar: file handle
Filename: File Name
There are three access modes in Perl: read, write, and Add. The write mode overwrites the original data, while the Add mode adds new content to the end of the file. However, you cannot read, write, or add files.
Open (myfile, "myfile") read
Open (myfile, "> myfile") Write
Open (myfile, "> myfile") add
File opening process:
If (open (myfile, "myfile )){
# Here's what to do if the file opened succesfully
}
Unless (open (myfile, "myfile ")){
Die ("can not open input file! /N ");
}
Open (myfile, "myfile") | die ("cocould not open file .");
Close (myfile) is required to close the file after use.
Read files
$ Line = <myfile> reads a row of data from the file to the variable $ line, and moves the file pointer to a row. For <stdin> files that do not need to be opened, you can operate
@ Array = <myfile> reads all the file content into the array @ array. each row of the file (including the carriage return) is an element of @ array.
Write files
Open (OUTFILE, "> outfile ")
Print OUTFILE ("Here is an output line./n ")
PS: STDOUT, STDERR is a standard output and standard error file, usually a terminal, so you do not need to open the file
Process File status
-Op expr
E.g.
If (-e "path/file1 "){
Print STDERR ("File exists./n ");
}
-Whether B is a block Device
-C is a character device
-D is a directory
-E exists?
-F is a common file
-Whether the setgid bit is set for g
-K whether sticky Bit is set
-L indicates whether a symbolic connection is used.
-O whether the file exists
-P is a pipe
-R readable?
-S is not empty
-Whether the setuid bit is set for u
-T indicates the terminal
-W is writable?
-X executable?
-Whether z is a null File
-How long is A from the last access?
-Whether B is a binary file
-C: How long is inode from the last accessed file?
-M: How long is the last modification time?
-O: only real users
-R: only real user readable?
-S is SOCKET
-T indicates whether the file is a text file.
-W: Can only real users write data?
-X: Is it accessible only to real users?
Command Line Parameters
@ Argv is an array of command line parameters in Perl, but @ argv [0] does not represent the file name, but is the first parameter.
$ Var = $ argv [0]; # The first parameter
$ Number = @ argv; # number of parameters
In Perl, the <> operator is actually an implicit reference to the array @ argv:
1. When the Perl parser sees <> for the first time, open the file named $ argv [0 ].
2. Execute the shift (@ argv) action. Move the element of array @ argv forward, and the number of elements is reduced by 1.
3. <> the operator reads all rows in the file opened in step 1.
4. After reading, the parser returns to the first step to repeat
E.g.
@ Argv = ("myfile1", "myfile2"); # It is actually assigned by the command line parameter
While ($ line = <> ){
Print ($ line );
}
Extract the contents of myfile1 and myfile2.
Open MPs queue
Open (message, "| cat> hello ")
Equivalent
Open (mypipe, "> hello ")
Iv. Pattern Matching
==============
V. Control Structure
==============
1) condition judgment
If (<expression> ){
<Statement_block_1>
} Elseif (<expression> ){
<Statement_block) 2>
}
...
Else {
<Statement_block_n>
}
2) loop
A) while loop
While (<expression> ){
<Statement_block>
}
B) Until Loop
Until (<expression> ){
<Statement_block>
}
C) For Loop
For ($ COUNT = 1; $ count <= 5; $ count ++ ){
# Statements inside the loop go here
}
D) foreach Loop
Foreach localvar (listexpr ){
Statement_block;
}
E.g.
Foreach $ word (@ words ){
If ($ word EQ ""){
Print ("found the word 'The'/N ");
}
}
E) Do Loop
Do {
Statement_block
} While/until (condexpr)
F) cyclic control
Exit loop last (break of C)
Execute the next loop next (C's continue)
Redo repeats this loop and the loop variable remains unchanged, but redo does not work in the do loop.
G) GOTO
Goto label;
3) Single Row Condition Statement
Statement keyword condexpr
Keyword: if, unless, while,
E.g.
Pirnt ("this is zero./N") if ($ Var = 0 );
Print ("this is zero./N") Unless ($ var! = 0 );
PS: although the condition judgment is followed, it is executed first.
Vi. subprograms
==========
1. Definition
Sub subroutine {
Statements;
}
2. Call (&)
& Subname;
...
Sub subname {
...
}
3. You can omit the & Symbol after defining it.
Sub subname {
...
}
Subname;
4. Forward reference, now define the word program name, and then define the subroutine body
Sub subname;
...
Subname;
...
Sub subname {
...
}
5. Use do to call
Do my_sub (1, 2, 3); # equivalent & my_sub (1, 2, 3 );
6. Return Value
By default, the last statement of the subroutine is used as the return value.
7. Local Variables
The variables defined by my only exist in this subroutine.
The local variable does not exist in the main program, but exists in the subprogram and the subprogram called by the Spring Festival.
E.g.
My ($ scalar) = 43;
Local (@ array) = (1, 2, 3 );
8. parameter transfer
& Sub1 ($ number1, & number2, & Number3 );
...
Sub sub1 {
My ($ number1, $ number2, $ Number3) = @_;
...
}
9. Transfer An Array
& Addlist (& mylist );
& Addlist ("14", "6", "11 ");
Sub addlist {
My (@ list) = @_;
...
}
When the parameter is an array, the subroutine can only assign it to an array variable.
10. Pass array parameters through aliases (pass references)
@ Myarray = (1, 2, 4, 5 );
& My_sub (* myarray );
Sub my_sub {
My (* subarray) = @_;
}
After defining an array alias, if the same simple variable exists, operations on this simple variable will also affect the alias array.
11. pre-defined subprograms
Perl5 pre-defines three subprograms:
The in subroutine is called when the program is started.
The end subroutine is called at the end of the program.
Autoload is called when a subroutine cannot be found
E.g.
Begin {
Print ("Hi! Welcome./N ");
}
Autoload {
Print ("hhh/N ");
}
If multiple predefined subprograms are defined, begin is executed in order, and end is executed in reverse order.
VII. associated array
8. format the output
IX. File System
10. Reference (pointer)
==================
Perl5 has two types of reference: Hard reference and symbolic reference. Symbol reference contains the name of a variable. It is useful for creating a variable name at runtime and locating it. Basically, symbolic references are like file names or soft connections in UNIX systems. Hard reference is like a hard connection in a file system.
When the number of hard reference trace reference Counters is 0, Perl Automatically releases the referenced data. If the data is an object, the analysis structure is released to the memory pool. Unreference must be displayed for non-simple variables and told how to do so.
1) Use references
Any simple variables can save Hard references. Because arrays and Hasse tables contain multiple simple variables, you can create a complex data structure composed of multiple combinations, such as arrays, array of the Hasse table.
Address:
If the value of $ pointer is an array pointer, @ $ pointer can access the elements in the array to retrieve the address value in $ pointer and use it as an array. Similar to % $ pointer, it indicates a reference to the first element in the Hasse table.
Create reference
The backslash (/) is similar to the address operator passed in C Language (&). Perl uses the '/' symbol to create a new reference:
$ Variavle = 22;
$ Pointer =/$ variable;
$ Ice = "Jello ";
$ Iceptr =/$ ice;
Reference $ pointer to the value position of $ variavle and reference $ iceptr to "Jello". Even if $ varivle is destroyed, you can still use $ pointer to access this value. This is a hard reference, therefore, $ pointer and $ variavle must be destroyed at the same time to free up space.
In this example, the reference variable $ pointer saves the address of $ variavle, instead of the value itself. To obtain the value, use two $ symbols.
E.g.
$ Value = 10;
$ Pointer =/$ value;
Printf "$ Pointer ";
2) references and Arrays
$ Pointer = // @ argv
$ I = 0;
Foreach (@ $ pointer ){
Print "$ I: $ pointer [$ I ++]/n ";
}
The reference to access the Hasse table is in the form of $ pointer {$ index} ($ index is the key value of the Hasse table). You can also use the => symbol to construct the Hasse table:
% Weekday = {
'01 '=> 'mon ',
'02 '=> 'tue ',
...
};
When using a Hasse table and an array, using $ is equivalent to using->:
$ Name [0] = "Kamran ";
Equivalent
$ Name-> [0] = "Kamran ";
$ Lastnames {"Kamran"} = "Husain ";
Equivalent
$ Lastnames-> {"Kamran"} = "Husain ";
3) subprogram reference
$ Pointer_to_sub = sub {... declaration of sub ...};
Call function pointer
& $ Pointer_to_sub (parameters );
4) file handle reference
/* Filehandle
E.g.
Splitout (/* stdin );
Splitout (/* lphandle );
11. Object-oriented
12. packages and modules