While learning Perl, Python, and shell scripts can easily confuse their syntax, this article is primarily a personal summary that facilitates the use of queries.
Perl basic syntax, installation, help documentation
File Header format:
#!/usr/bin/perl
use strict;
use warnings;
Run the Perl script:
#call perl program
Perl test.pl
#Executable script
Chmod 755 test.pl
./test.pl
Basic reading and writing:
#keyboard input
$a = <STDIN>;
Chomp($a);
Print $a;
#File input
Open(IN, "<test.txt") or die "Cannot open file:$!\n";
Open(OUT, ">test.txt") or die "Cannot open file:$!\n";
While(<IN>){
Chomp;
Print OUT "1. $_";
Last;
};
Close(IN);
Close(OUT);
Basic Control Flow:
If( ){
}elsif( ){
}else{
}
Print "Hello!" if (1 == 1); #shorthand
While( ){
$count++;
}
For($i; $i <= 10; $i++){
}
Foreach (@array){
Print $_;
}
Last; #break
Next; #continue
Data structure: Scalar quantity ($), list (), array (@), hash (%), file handle (<>)
Syntax structure: The statement ends with a semicolon (;), the code block is delimited with curly braces {}, and the dynamic language does not have to indicate the variable type
Single double quotes: single quote q (), all output as-is, double quotation mark QQ (), allow inserting variable
Syntax Conventions: There are many places in Perl where parentheses () are available, especially built-in functions such as print, split, and so on; You can use the increment decrement operator
Install: Windows installed active version, (perl-v) check, Linux pack,
$ tar -zxvf stable.tar.gz -C dir
$ sh Configure # Configure
$ make # installation
$ make test #test whether make succeeds
$ make install
Help document: Perldoc command
Perldoc # will prompt usage
Perldoc perl # will show detailed usage
Perldoc perlfunc #Three modules
Perldoc perlop
Perldoc perlfaq
It is recommended to use a browser to view local HTML documents:
file:///D:/Program%20Files/Perl64/html/index. html
Keywords: special variables
$_: The default parameter, which is the current default action parameter, is important in reading files <FILE>, iterating for (@array), split//, Print
$!: standard error output, usually only with or Die statement
$: A variable within the first parenthesis in the regular expression pattern, such as (. *), (string), and so on
An array of arguments passed by the @_:sub function,
Keyword: string operator
Connection string: The collocated operator (.)
Escaped: backslash \
Distinguish between scalar and string: {} Displays the specified variable name, such as ${name}space
repeating operator (x), such as "-" x 10
Keyword: array, hash
List: Lists are constants, created by parentheses (), separated by commas
# Range operator
@a = (1..10)
@b = (1..10, 21..30)
@list=(aa..zz);
Create an array: Create an array with a list ()
Referencing arrays: Referencing arrays with square brackets [] subscript
Create hash: Creates a hash with a list (), in order to look good, the key value pairs are organized in the form of = =, or you can create individual
Reference hash: Reference hash with curly braces {}
Keyword: array manipulation
Keywords: (numeric/String) comparison
Numeric comparisons: (= =, >, <, >=, <=,! =)
String comparisons: (eq, GT, lt, ge, le, ne)
Keywords: print usage
Print: Can be directly connected to the variable $var, @array,%hash, the output is no space carriage return, all elements are linked together, an array of a lump, hash a lump, where the hash order is chaotic.
Print: can be double quotation marks, there are spaces between the array, can be distinguished, the hash is invalid, no content, it shows the%hash.
Print: When output to a file, the file handle is in front, the middle is a space (not a comma), followed by the output
Keywords: q (), QQ (), QW (), qx{}
Q (): Enclose the content in single quotation marks
QQ (): Enclose the content in double quotation marks
QW (): A sequence of spaces separated by single quotation marks to assemble an array, shielding the scalar inside
# Both are equivalent, it is best not to have embedded variables
@a = qw(abc def ghi)
@b = (‘abc‘,‘def‘, ‘ghi‘)
qx{}: Capture command output
$directory = `dir`;
$directory_2 = qx{dir};
Keywords: regular expressions, patterns
Keywords: functions
Standard function Format:
Keywords: common built-in functions
int (5.20);
Length ("Nose");
LC ("ME TOO");
UC ("HAL 9000");
COS (50);
RAND (5)
Keyword: stack operation function
Push ()
Pop ()
Shift (): Remove the last element, common language handler function parameters
Unshift ()
Keywords: command line
Keyword: context
Keyword: chomp
Chomp is an operator that can use parentheses or spaces; it changes the parameter variable, and its return value is 1 or 0
$a = "lizhiixn\n";
Print $a;
Print "next\n";
Chomp $a;
Print $a;
Print "next";
$b = chomp($a); #very rarely used
Perl Grammar Quick Check