[Perl learning] learning notes (continuously updated)

Source: Internet
Author: User
Tags perl interpreter

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

1. Differences and relationships between Shell and Perl
Perl is full of 10 thousand gold oil. It has implemented the functions of various system commands from its own interior, which is much more efficient. Moreover, it has specialized debugging tools that support object-oriented design, both small scripts and large applications are competent.
Shell scripts are only applicable to simple system management. In my opinion, shell is like a binder. It sticks various Linux system commands together to form scripts. So you need to learn awk, sed, and so on, because shell mostly calls external programs to execute operations, the efficiency is low, and it is inconvenient to debug shell scripts. Therefore, it will be very tired to write more than 500 lines of scripts using shell.
Contact: they are all an explanatory programming language.
2. Like C, Perl ends with a semicolon.
3. In Perl, you do not need to declare the data type before using the variable, that is, the variable has no type. You can assign a string to a variable. After that, you can assign a number to the same variable. The type of a variable is determined by the specific processing of the variable. Usually
Is to add a type modifier before the variable name to indicate the type of the variable.
4. Data Structure
Perl has three data structures: single value, array, and associated array (that is, "hash table ").
1) single value (scalar) Variable
The single-value variable name usually starts with the $ symbol
E.g. $ mystr = "Hello world."; $ mynum = 321;
There are three methods to represent strings. Note that they are the same as the rules of shell scripts.
Double quotation marks ("): In a program, Perl replaces the variable in the string.
Single quotes ('): Perl programs do not have any special processing.
'): Perl runs the string as a shell command (as an independent process) and uses the result as the result of the string.
2) List (array) variables
The list size is dynamically scalable. The list consists of elements separated by commas (,) contained in parentheses. Each element can be referenced by its position in the list. The index at the first position is 0. You can also use square brackets ([]) to reference a part of the list.
E.g. The following defines a list of four strings, and assigns the element (Mon) whose index is 0 to the single-value variable Monday.
$ Monday = ("mon.", "Tus.", "Fri.", "Sun.") [0];
And ("Mon. "," Tus. "," Fri. "," Sun. ") [1, 3]; the result of this sentence is also a list --" mon "," fri"
The declaration list usually starts with the @ symbol and uses the prefix $ or @ to tell the perl interpreter the type of a variable. E.g. @ REV = reverse (@ num1234); $ num1 = $ num1234 [0];
Perl provides functions to process the list:
Sort (list) sorts the list and returns a list.
Reverse (list) returns a list in reverse order of the list.
Join (expr, list) returns the list of combinations of expr and list
Split (pattern, expr) returns the Child list of expr separated by Pattern
Push (@ array, List) to add the list to the end of @ Array
Pop (@ array) removes an element from the end of @ Array
Unshift (@ array, list) adds list before @ Array
Shift (@ array) and return the first element of @ Array
Scalar (@ array) returns the number of elements in the array.
3) Associate array Variables
Perl provides an associated array based on key => value. In this type of array, the key is used to index the value of the element ). The key here is a string. The variable names of the associated array usually start with the % symbol, for example, % myvar,
In the joined array, braces ({}) are used to represent the key to index the element.
% Myhash = ("first" => "first element", "second" => "second element ",
"Third" => "third element ");
$ Secondelement = $ myhash {"second "};
In Perl, the associated array is saved through a hash table, and the key is hashed.
To locate the position of the element. Therefore, the associated array is also called a hash table.
Perl provides functions to deal with correlated Arrays:
Keys (% array) returns a list Of all keys associated with array % Array
Values (% array) returns a list Of all values of the associated array % array.
Each time each (% array) is called, a list consisting of a pair of keys and values is returned.
Delete ($ Array {key}) deletes elements associated with keys from array % Array
5. Enter
When you want to read from a file, you can use the file name as the parameter for script execution after the Script Name. When interpreting the running script, the perl interpreter opens the file, reads each row of the file, and assigns the value to the variable $ _.
You may have written several file names as command line parameters, for example:
Lines Foo bar zap
This means that the script lines uses the file Foo, bar, and zap as a single merged file.
Processing.
6. Output
Print is very similar to printf in shell.
7. condition test and True Value
Perl provides two sets of conditional testing methods, one for numbers and one for strings:
Compare test string numbers
Equal eq =
Not waiting for ne! =
Greater than GT>
Greater than or equal to ge> =
Less than lt <
Less than or equal to Le <=
Returns-, 1
CMP <=>
You can use & (and) and | (OR) to combine test operations.
True Value Issues are flexible in Perl.
The following values are processed as true values:
1;
("A", "B"); # list of elements
""; # Space character (note this)
"Hello"; # A non-null string
"00" # This is actually a non-null string
The following values are processed as false:
0;
(); # Empty list
""; # Empty string
"0"; # This is also false! (Note this)
8. Control Structure
Perl has rich control structures. You can use the if statement to implement the branch structure.
Ring Structure. In addition to IF and while, Perl also provides a foreach statement, which greatly facilitates
Processing. Note that the braces of a program block are required, which is slightly different from the C language.
If statement format:
If (expression)
{
Block
}
If (expression)
{
Block1
}
Else
{
Block2
}
The while statement format is as follows:
While (expression)
{
Block
}
The last statement tells the perl interpreter that the statement is the last statement executed in the loop body. Execute this sentence
The loop body will exit. This statement is similar to the break function in C.
The next statement tells the perl interpreter to execute a new loop. This statement and the C Language
Continue is similar.
In addition to the for statement similar to the C language, in Perl, there is also a foreach Language
Sentence. This statement greatly facilitates operations on arrays.
Foreach Single-value variable (list)
{
Block
}
In each loop, the scalar value is the next value in the list, and then runs the statements Statement.
9. Process
Perl also provides processes to facilitate code reuse.
The PERL language defines the process in the following format:
Sub name
{
Statements;
}
All processes PASS Parameters through the list (array @ _), and the return value of the process can be a single value or a list.
Sub Error
{
($ Message) = @_;
Print ("<B> error: <B> ",
$ Message,
"/N ");
Exit (0 );
}
10. File test operators
Below is a list of common file test operators:
-R readable
-X executable
-E exists
-D is the Directory
-T is tty
-T is a text file.
11. File Management
The PERL language also provides powerful support for file I/O operations. The redirection method is similar to the <,>,> and | operators.
Open (FD, filename );
Close (FD );
It is similar to a macro constant in C language and exists as a handle.
"File" open a file and use it as the data input. You can also write it as "<file"
"> File" open a file as the output. If the file does not exist, create
"> File" opens the file in append mode, and the data is added to the end of the file.
"+> File" open the file in read/write mode
"| Cmd" open a pipeline to cmd (PIPE)
"Cmd |" open a cmd-input pipeline.
# Read a row from the file pointed to by filehandle
$ Line = <filehandle>;
# Read the entire file from the file pointed to by filehandle
@ Lines = <filehandle>;
In Perl, print is used to write files. The first optional parameter of print is filehandle. For more information, see
Note that the first parameter is not followed by a comma (,):
Print (filehandle "your", "list", "here ");

 

12. Perl Philosophy
Perl happens to be designed by a linguistics (you can say so), so it is designed as a programming language that can be used as a natural language.
Perl has been designed to be evolutionary and has actually evolved. Camels (the Perl logo) are a committee-designed horse, but are placed in the desert. In the desert, camels are very adaptable to the desert life. The horse has evolved into a self-sufficiency camel, which is also one of the reasons for choosing camels as the mascot of Perl, and has nothing to do with linguistics. I think it is related to biochemistry.
Environment is very important in Perl. Perl will determine what you want based on the environment, instead of explicitly telling it like other programming languages.
The simplest application philosophy of arrays and hashes: When you want to search for things by number, you need to use arrays. if you want to search for things by name, you should use a hash. these two concepts are complementary.
Hash uses the interesting character % to indicate the name of the hash (if you carefully observe %, you will find that the key words and values on both sides are skewed .)
13. variables that have not been assigned a value are used.
This uninitialized variable will automatically exist as needed. follow the minimum unexpected principle. The variable is initialized as null, "" or 0. variables are automatically interpreted as strings, numbers, or "true" and "false" (usually called boolean values) based on different locations ).
14. scalar Environment
Different operators in Perl require a single value of a specific type as a parameter. this operator provides a scalar environment for these parameters. sometimes it is more explicit. For example, the operator will provide these parameters with a digital environment, a string environment, or a Boolean environment. perl automatically converts data to the correct format according to the environment.
For example:
$ Camels = '000000 ';
Print $ camels + 1, "/N ";
124
15. List operations
List assignment: ($ potato, $ lift, $ tennis, $ pipe) = @ home;
They occur logically in parallel, so you can exchange two variables as follows: ($ Alpha, $ Omega) = ($ Omega, $ alpha );

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.