Document directory
- 1. The variable type is determined by the symbol above it
- 2. There is no need to declare a variable in advance
- 3. No type conversion
- 4. No character type
- 5./Not division
- 6. Let's talk about arrays.
- 8. No struct or union
- 9. No suspended else
- 10. Unusual do
- 11. No Memory leakage
- 12. Function Parameters
- 13. function prototype
- 14. No Main Function
- 15. Different left values
- 16. Implicit variables/parameters
- References
Many people do not learn Perl as the first programming language. Before learning Perl, they often have mastered one or two other languages. Although controversial, I personally think Perl is not suitable for entry-level languages. This article is intended for programmers who are familiar with C and want to master Perl. It introduces some techniques and how to avoid mistakes that C programmers often make and take you through the dangerous swamp. Before reading this article, check the perltrap manual page, which contains a lot of useful information and will not be repeated here. Welcome to the magic world of Perl!
If you like C, you will also fall in love with Perl.
Dennis ritche, a C language designer, said: "The C language is strange and has many defects, but it has achieved great success. This is probably because the degree of abstraction of C happens to meet both the requirements of programmers and is easy to implement. Those who love C are happy to write some strange C Programs and show their talents. In this regard, Perl can be said to have been "never missed ". Perl is ugly and abstract. It can be used to write chaotic code, but it is flexible and practical. It is closer to natural languages and can also be used to write poems. This is interesting in itself. In the underlying field that C is good at, Perl can only look beyond its reach. After all, it is not designed to deal with hardware. But in the text processing field, C has to bow to the title, and Perl is very powerful in this regard. It is said that Perl has also been favored by many scientists and has helped the human genome program to a great extent. Thank you, Larry Wall!
Tips for C programmers
Perl integrates the features of multiple programming languages, and the C language is also included. Perl and C have the following similarities:
- 1. Semicolons are required for each simple statement. line feed does not end the statement.
- 2. array subscript also starts from 0, and string functions like substr in Perl also start from 0.
- 3. the comma operator works the same way.
- 4. & | the operator works the same way.
However, Perl and C are two completely different programming languages, and there are many notable points from C to Perl. We will discuss it in detail below.
1. The variable type is determined by the symbol above it
This is not to say that Perl uses Hungary notation, but is a feature of Perl. In Perl, $ indicates that the variable is a scalar, @ indicates that the variable is an array, and % indicates that the variable is a hash. For example, @ foo is an array, while $ Foo [0] is the first element in the array @ Foo, and @ Foo [0] is an array fragment. Of course, it is also an array, however, this segment only has one element $ Foo [0]. If you assign an array variable to a scalar, such as $ bar = @ Foo;, you will get the number of elements in the array.
2. There is no need to declare a variable in advance
Every time you introduce a variable in C, you must declare its type before. There is no need in Perl. You can introduce new variables at any time. However, the problem arises, so be careful. If you accidentally typed a letter, Perl treats it as your new variable and automatically initializes it. Sometimes, no error is prompted, this is obviously not in line with your initial goal! Therefore, add use strict; before each Perl program to ensure that Perl can perform more rigorous checks on the code, just as if you used lint to check the C program.
3. No type conversion
The scalar type in Perl can be an integer, a string, or a floating point number. You can safely convert an integer to a corresponding string. The Perl interpreter can understand what you mean, so don't worry. However, this does not mean that you can rest assured at any time. When you convert a string to an integer, you really have to work hard. We will discuss this issue below.
4. No character type
Perl does not have the char type.
$ CH = 'C ';
The above statement actually assigns a string value to the scalar $ ch, because the single quotes in Perl can also enclose the string (compare the difference between single quotes and double quotes for exercises ). That makes converting a string to an integer or floating point a little effort. We can process characters like this:
@array = split(//, $string); # each element a single character
@array = unpack("C*", $string); # each element a code point (number)
You can also use regular expressions. Perl also has functions similar to atoi (), called POSIX: strtodd. It should be included before use in the POSIX module.
5./Not division
Since Perl does not distinguish between integers and floating-point numbers, it is not what you want to use the/operator to represent division by C. In reality,/is a floating point division in Perl. The following program is dangerous:
while($a/=2)
{
push @tmp, $a % 2;
}
It accurately removes $ A from Perl and cannot represent it! If you want to represent the division, put the entire expression into the int function.
6. Let's talk about arrays.
Note: In Perl, only hash is initialized using {}, and the normal array is initialized using! An error is reported when {} is used to assign a value to an ordinary array interpreter. In addition, arrays in Perl can be scaled at will, and there is no array out-of-bounds problem. Unlike C, Perl allows anonymous arrays, hashes, and subfunctions. For example, you can use an anonymous array to exchange values of two variables:
($ Var1, $ var2) = ($ var2, $ var1 );
Perl arrays are separated from the underlying features and are more flexible and convenient.
7. No Switch
This really surprised C programmers that Perl has no switch. Indeed, Perl does not need a switch, because the switch can use if/elsif/else (Note: It is elsif rather than else if) or? . The switch in Perl can be written as follows:
SWITCH: {
if ($value == 1) { print "One" };
if ($value == 2) { print "Two" };
if ($value == 3) { print "Three" };
if ($value > 3) { print "Unknown" };
}
#Or like this:
SWITCH: {
$value == 1 and print "One", last;
$value == 2 and print "Two", last;
$value == 3 and print "Three", last;
print "Unknown"; #default
}
You can also use Goto. After all, tmtowtdi (there's more than one
Way to do it .).
- // There is a problem here, because 7 is implemented in 5.8 and is very powerful. It is implemented using modules.
8. No struct or union
If you decide to use Perl programming, you can bypass struct. Union is more underlying than Perl. If you want to use struct to implement a data structure, such as a single-chain table, you can select hash and reference in Perl. In fact, hash can implement a lot of data structures. For more details, see mastering algorithms with Perl. If you want to use struct to implement class, you can use objects in Perl. Finally, if you say, "I can't complete this program without struct", why don't you use Perl instead of C?
9. No suspended else
The condition and loop block in Perl must be included in {}, so there is no suspended else problem. Remember: The block itself is equivalent to a loop that only executes once, so the last block also works. The exception is that when the condition is determined to appear at the end of a statement, no curly brackets are required before it. For example:
if $test print "yes"; #This one is WRONG!
{print "yse"} if $test; #WRONG again!
print "yes" if $test; #This one is right.
10. Unusual do
Do is given three different meanings in Perl. When it is followed by a block, it will execute all the statements in the subsequent block and return the value of the last expression. If it is used with while or, perl decides to execute the statements in the block based on the test conditions. However, the statements in the block are not calculated in the cycle. Therefore, using last/next/Redo to control blocks is useless. When it is followed by a file name, it is used to include the file named here. When it is followed by a subfunction, it is a call to the post-face function, but this is not recommended.
11. No Memory leakage
You no longer have to worry about memory leakage caused by the free and malloc functions, because there is no such function in Perl and there is no pointer. You almost don't have to worry about memory allocation. References similar to pointers in Perl do not have the underlying features. In fact, it is rare to cause memory leakage in Perl. You no longer need to fear that the string space is insufficient and whether the string ends with '\ 0'. the string in Perl is as convenient as the string class in C ++, that is, the benefits of concatenating and comparing strings without the C ++ overload operator (Perl can also overload operators, which will not be discussed here ).
12. Function Parameters
Perl is designed to be a computer language that is very similar to natural languages, so it is no wonder that Perl can also be used to write poetry. Function parameters do not need to be enclosed in parentheses. Although adding parentheses does not affect your program, you must know that not adding parentheses makes your program easier to read and more elegant. Try to compare the following statements:
Open (youreyes, $ wide) or die ("$! ");
Open youreyes, $ wide or die $ !;
This is Perl. Make it easy. Furthermore, if you do not want to forward any parameters to the function, you do not need to include extra parentheses. However, if you want to process your own subfunctions, you must define or declare the function before using it.
In addition, Perl supports variable parameters, and Perl transfers function parameters by reference instead of passing values like C! In other words, if you modify the elements in @ _, the corresponding real parameters also change. Perl uses this method to easily return the required value.
13. function prototype
The function prototype in Perl is an automatic template in the calling environment, unlike in C. In addition, the function prototype only affects functions that are called without and. You must pay attention to whether the function prototype brings your sub-functions into a new environment. Therefore, "it is best to use the function prototype in the new function, but not in the old function ." If you are not careful, you may have a lot of trouble with the function prototype. However, if you are very careful, you can use the function prototype to complete the task well.
14. No Main Function
The C family language must have a main function, but Perl is not in it. Similar to basic, Perl does not have the main function and is interpreted and executed from top to bottom. Parameters on the command line interface in Perl are passed through the @ argv array, and there is no $ argc variable, because the @ argv is assigned to a scalar to obtain the number of parameters. However, $ argv [0] in Perl is equivalent to argv [1] In C, and the variable equivalent to argv [0] is $0. In C, environment variables are passed through the main parameter char ** ENV, while Perl is hashed by % Env.
15. Different left values
In Perl, all things that may be left values can be left values. For example, if? : If the two expressions of the operator are left values, the entire expression can also be left. The function can also be the left value. If the first parameter operation of the substr function is changeable, it can also be used as the left value. You can also define your sub-functions as left values. Yes, Perl allows you to do this. Like this:
my $val;
sub canuse : lvalue {
$val;
}
canuse() = 9;
It can safely assign the right value to $ var.
16. Implicit variables/parameters
One of the major features of Perl is that it has many predefined variables, which have their own special purposes, which are different from those of C. You must be familiar with them to control them. $ _ Is probably the most commonly used implicit variable. It is the default variable/parameter in input and mode matching. @ _ Is the list used to pass sub-function parameters. $! Stores the error message of the last System Call (equivalent to errno in c )...... There are many others. Implicit variables seem odd, but when you are familiar with them, it can save you a lot of time and increase the readability of the program.
Of course, Perl has far more magic and charm than that. Perl has its own unique style and emits its own light. You should carefully look for pearl in Perl! May you use Perl to create more miracles and more art!
References
1. Programming Perl, Third Edition
2. professional Perl programming
3. Perl debugged
4. The C programming language, Second Edition