Reference: http://shouce.jb51.net/perl5/
Web site environment configuration: http://www.jb51.net/article/74005.htm
The basic syntax of Perl http://www.jb51.net/shouce/Perl.htm
Preface: What is Perl, and what is it used for? Perl's original designer's intention was to handle characters, and 80% of the strength was to handle characters, of course, many others. Many web pages are now Perl, often requiring CGI environments, such as $char =~/language/, which means finding strings containing the words "language." It can also do UNIX and Linux system management, file content processing (based on tools such as awk and SED), and many other things you want to do.
I. Perl environment configuration
1. Get Perl
Perl is usually located in/usr/local/bin/perl or/usr/bin/perl. You can get it free on the Internet with anonymous FTP, such as ftp://prep.ai.mit.edu/pub/gnu/perl-5.004.tar.gz
2. The installation process is:
(1) Decompression:
$gunzip perl-5.004.tar.gz
$tar XVF-<perl-5.004.tar.gz
(2) Compiling:
$make Makefile
(3) Placement:
Copies the compiled executable file to the executable file, usually in the same directory as:
$copy <compiled excutable file>/usr/local/bin/perl
3. Run
Edit your Perl program with a text editor, plus executable properties: $chmod +x <program> can perform:$./<program>. If the system prompts: "/usr/local/bin/perl not Found", you are not installed successfully, please reinstall.
Note: The first line of your program must be #!/usr/local/bin/perl (where Perl resides).
4 Note:
The method of the annotation is to use the character # at the beginning of the statement, such as:
# This is a comment
Note: It is good programming practice to use annotations frequently to make your program easy to read.
Two, constants, variables and other issues
1. Single and double quotes
The string within double quotes supports simple variable substitution such as: $text = "This text contains the number $number.";
Escape characters are supported in strings within double quotes
The single quote string has two differences from the double quotation mark string, one is without the variable substitution function, and the other is the backslash does not support the escape character.
2. Repetition and connection
Repeat: print "T" x 5 (output 5 T, note: where x is the English letter lowercase x)
Coupling: $a. = "BC" (equivalent to the company equals)
3. Simple variables, arrays, lists
Simple variable: With $ declaration, such as: $a = "Hello";
Array: with @ declaration, such as: @arr = (' A ', ' B ', ' C ');
List: A list is a sequence of values enclosed in parentheses that can be any value or NULL, such as: (1, 5.3, "Hello", 2), empty list: ()
Third, file operation
1. Open file: such as open (MYFILE, "File1") | | Die ("Could not open file"); MyFile is the file handle for the declaration, FILE1 is the filename/file path, and the entire line of code means: If the open fails, the output is "could not open file";
Closes the file: When the file operation completes, uses close (MYFILE); Closes the file.
2. Read the document
Statement $line = <MYFILE>; reads a row of data from a file into a simple variable $line and moves the file pointer back one line. <STDIN> for standard input files, usually keyboard input, do not need to be opened.
The statement @array = <MYFILE>; reads the entire contents of the file into an array @array, and each line of the file (including a carriage return) is an element of @array.
#!/usr/bin/perl
Open (MYFILE, ' 1.txt ');
@arr = <MYFILE>;
Print @arr;
3. Write a document
The form is:
Open (outfile, ">outfile");
Print outfile ("Here's an output line.\n");
Note: STDOUT, stderr are standard output and standard error files, usually screen, and do not need to be opened.
4. Determine file status
1. File test operator
The syntax is:-op expr, such as:
if (-E "/path/file1") {
Print STDERR ("File file1 exists.\n");
}
File test operator
Operator |
Describe |
-B |
is a block device |
-C |
is a character device |
-D |
is a directory |
-E |
Is there |
-F |
is a normal file |
-G |
Whether the Setgid bit is set |
-K |
Whether the sticky bit is set |
-L |
is a symbolic link |
-O |
Whether to own the file |
-P |
Whether it is a pipe |
-R |
is readable |
-S |
is not empty |
-T |
Indicates whether the terminal |
-U |
Whether the setuid bit is set |
-W |
Whether to write |
-X |
Whether to perform |
-Z |
is an empty file |
-A |
How long is the last visit? |
-B |
is a binary file |
-C |
How long is the inode from which the file was last accessed |
-M |
How long does it last modified? |
-O |
is owned only for "true users" |
-R |
is only "true user" readable |
-S |
is a socket |
-T |
is a text file |
-W |
is only "real user" writable |
-X |
is only "real user" executable |
Note: "Real user" refers to the UserID specified at logon, which, in contrast to the current process user ID, commands suid can change the valid user ID. |
Cases:
Unless (open (INFILE, "INFILE")) {
Die ("Input file infile cannot be opened.\n");
}
if (-E "outfile") {
Die ("Output file outfile already exists.\n");
}
Unless (open (outfile, ">outfile")) {
Die ("Output file outfile cannot be opened.\n");
}
Equivalent to
Open (INFILE, "INFILE") &&! (-E "outfile") &&
Open (outfile, ">outfile") | | Die ("Cannot open files\n");
iv. pattern Matching:
1. Concept: pattern refers to the character of a particular sequence that is sought in a string, which is included by a backslash:/def/, Mode def. Its usage, such as combining function split, splits a string into multiple words in a pattern: @array = Split (//, $line);
2. Matching operator =~,!~
=~ Verify that the match was successful: $result = $var =~/abc/; If the pattern is found in the string, it returns a value other than 0, true, or 0, or false, if it does not match.!~ is the opposite.
V. Control structure
(1), Conditional judgment: if () ElseIf () else ();
(2), Circulation:
1, while loop
2, until cycle
3, for Loop
4. A foreach loop for each element of a list (array)
Open (MYFILE, ' 1.txt ');
@arr = <MYFILE>;
foreach $str (@arr) {
print $str;
}
5. Do Loop
6. Circular control: The exit loop is last, the same as the break in C; the next loop is the same as the Continue function in C; a Perl-specific command is redo, which means repeating the loop, which is the same as the loop variable, returning to the starting point of the loop, but note that The redo command does not work in the Do loop.
7, the traditional goto statement: Goto label;
(3), single-line conditions
The syntax is statement keyword CONDEXPR. Where keyword can be if, unless, while, or until, such as:
Print ("This is zero.\n") if ($var = = 0);
Print ("This is zero.\n") unless ($var!= 0);
Print (' not Zero yet.\n ') while ($var-> 0);
Print ("Not Zero yet.\n") until ($var-= = 0);
Although the conditional judgment is written in the back, it is executed first.
Vi. Sub-procedure
(1), definition
A subroutine is a separate piece of code that performs a special task, which makes it easy to reduce duplicate code and make the program readable. In Perl, subroutines can appear anywhere in the program. The method is defined as:
Sub subroutine{
statements;
}
(2), call
The calling method is as follows: Using & Calling
&subname;
...
Sub subname{
...
}
vii. File system : Closely related to UNIX, reference: http://shouce.jb51.net/perl5/perl11.htm
Other:
Pipe: You can use the result of the previous execution as a subsequent argument, such as
env | grep EDITOR
You can delete the value of the editor in the environment variable.