What are the new features of Perl 5.6 better than C / C / Java?
Teodor ZlatanovProgrammer, Northern Light, Inc.January 2001
The author focuses on the uniqueness of PERL and C or Java. You will definitely look at the features of these in other languages: the fault tolerance of operators, a variety of tasks, punctuation, regular expressions, and variable mechanisms. All of this gives your fingers more flexible magic. In some aspects, Perl can indeed give a lot of help to C and Java programmers, but unfortunately it is far from well-known. Therefore, hurry up the opportunity to improve your Perl level!
Perl sometimes even makes experienced programmers feel a headache, because they find that they will write a two-can-beaten statement. But this fuzzy in the structure and characteristic system exhibits power from the Perl language from the other hand. After all, the original design for the Perl language is to achieve the same purpose in a variety of ways.
Here we will explore those most confusing features in Perl 5.6 and compare them with their corresponding characteristics in C / C / Java. It is mainly laminated around the principles in "Natural Language Principles in Perl (LARRY WALL, see the data part of this article) because they are places that Perl can be distinguished from C, C and Java language. In addition, the structure of the Perl syntax can be found in the "Perldoc Perlsyn" reference document, and another Recommended Perl guide reading is Programming Perl.
Perl Interpreter Beginner will now find that there is no compiler in Perl. In fact, most perl scripts are running directly by the Perl interpreter, such as "perl" under the UNIX system, "Perl.exe" under DOS / Windows is Perl interpreter. These interpreters are not required in Macos. You can try to see how to run the Perl script. First start the corresponding Perl interpreter on your operating system, or run directly in the MacOS system. In most systems, the file tail flag (Control-D in the UNIX system) is used to indicate the end of the user input. So in the UNIX system, the following script will get the "5 6" calculation results:
Start with the simplest Perl program
> Perl
(Perl Is Waiting for User Input Here, Because No Script Name IS GIVEN)
PRINT 5 6
You Press Control-D Here
11
You can see that the Perl interpreter runs this only one scriptor and enters the calculation result 11.
The Perl interpreter has many options. For example, the -e option indicates that the input of the command line is executed as a script file, so the above script example can also be implemented: Enter Perl-E'Print 5 6 'in the command line (note that single quotes will be mated stand up). The -i option is similar to passing through a filter, allowing different locations in the file to be edited. -n and -p options allow programmers to turn output. -W Options are similar to the "-wall" compilation option in C / C , which can give a warning message in the program, but different from -wall is that the -w function is also activated at the time of the program.
Speed and Benchmark people often take PERL and C / C and complain that the Perl runs in less than enough. Some of this is indeed true, but it is not always. I suggest you try to use the Benchmark module before you think that C or C is faster. In addition, Perl can be easily connected to the C / C code or library, and some Perl built-in functions are not slower than C code, such as sort or printing. Here again, you can use the Benchmark module before firmly believe that C / C is faster. Remember, premature optimization is often the root of the wrong. If you write a prototype in Perl, it is no problem with other languages. The prototype means easy development.
Compared with Java, Perl also works well. Perl is not as good at threads like Java, but its TK GUI interface toolbox is better than Java's Swing GUI library. And the Java code can always be connected to the Perl program and vice versa. Therefore, sometimes you can use some extent to make the program work well in both.
Exceptions, compilation, and document Perl throw an exception via modules in the CPAN or its built-in function EVAL (). It seems that the exception is handled by the TRY / CATCH code block in C or Java, the EVAL function can handle an exception of a code segment or a string operation.
In fact, the Perl program needs to be compiled before running, but the compilation method of C / C / Java is different. In design and effect, it is similar to the Java byte interpretation process. For more detail of compilation, you can refer to "Perldoc Perlrun" and "Perldoc Perlcc" documentation.
You can use the POD format to embed the document into the Perl program. This document is embedded than javadoc format (only for API documents), but is more than C / C / Java comments.
Even if compared with C, C or Java, the Perl program is not a structured language. For example, the Begin statement will be executed first, but it can be described in the program. Definitions, variables, and functionals can appear in any position of the program, and the powerful functionality provided by Perl can best satisfy this randomness.
Due to this loose structure, embedded annotations, as well as a confusing statement caused by facilitating convenience, it is more like writing an English letter.
Perl's fault tolerance ability Perl is more blurred than C / C / Java. For example, you can use a comma to divide the statement or function parameters:
Sneez between statements or between function parameters
Print 'Hello', '', 'there', "/ n"; #print "Hello there / n"
Foreach (1..10)
{
MY $ I;
$ I = $ _ * 2, Print "$ I / N"; #print Evens from 2 to 20
}
Perl can do the best possible to eliminate ambiguity that may be caused by these statements. Of course, there are still ambiguuits that cannot be resolved (at this point, Perl is like English).
Another way to be ambiguous in Perl is that variables are often hidden use. For example, the "Print" statement defaults to print the value of the _ variable. In other confusing statement operations, $ _ variables are also their default values, which causes a confusion. For example: implicit use variables
$ _ = "hello";
S / Hello / Hi /; # $ _ is "hi" now
Print; #prints "hi"
Here you can see that using the default variable allows programming to be easily and concise. That is to say, Perl is similar to English, simplifies expressions through some kind of blur.
All languages have a variety of mandes in all languages have their own methods when resolving the problem. In C, the FOR () loop is the best way to repeat within a certain range; in Java, the call to the static function is directly passed instead of an instance.
But for the same thing, PERL has at least two solutions. The TMTOWTDI principle is the motto of Perl, and the differences in various processing are deeply encouraged in Perl programming.
Let's take a look at an example of a print array element. All expression methods have achieved the same purpose.
Print array element
Print foreach @Array;
Foreach (@Array) {print};
Map {print} @Array;
PRINT @Array;
It is to understand that the only way to understand these codes is to master all Perl grammar. Don't worry about which method is correct, because there is always a variety of correct ways to achieve the same goal. Consider these different ways, you can experience this motto of Perl.
In addition, although there are many ways to implement a task, this does not mean that all methods are correct. Usually, some error codes are more likely to be written. In order to ensure the correctness of the code, it is best to use those Perl built-in functions, while less using the functions you have written, and pay attention to the proof and record these less obvious ways.
Regular expressions If there is no initialization, the regular expression is likely to cause a mess. Most people believe that the regular expression is invented by Kalahari Bushmen, which penetrates all aspects of the University's computer scientific programming.
The regular expression of Perl is inherited from the shell script and the awk / grep tool. But its ability is far from the original model.
The basic regular expression is very easy to write, but it is difficult to read. For example, "CON / W " and "Contra", "Contrary" match, but do not match "Pro" or "Con". However, in Perl 5.6.0, regular expression is cured. Unicode character set, any code operation in the mode, Flag Toggles, conditional expressions, and other features are added to the regular expression library.
One of the best suggestions for beginners is: First study the most basic regular expressions (see the information section, or "Perldoc Perlre" reference manual), further learn from those complex advanced features. Since the regular expression must be fully written, there is no way to add a comment in the middle, which allows them to be part of the most difficult to read in all Perl code. Therefore, it is recommended to write a forming code.
Regular expressions in C / C / Java belong to external functions, but Perl is currently the best regular expression search and restructuring tool. In very few cases, it may be slower than pure C processes, but for those purely regular expressions, Perl is still your preferred tool. Different variables in scales, arrays, and hash rows and C, C , Java are different, and the type of variable of Perl is determined by its name and automatically initializes the corresponding type. This makes Perl beginners feel uncomfortable, but it is very intuitive and easy to understand.
The author recommends using "use strict". Through it to ensure that the variables are declared before use, thereby avoiding program errors caused by typing errors.
If you don't do this, it is possible to encounter the following problem:
A common typing error
$ I = 5;
Print $ j; #print $ I
In this example, the programmer is the value of the variable I, but the result is knocked into J. Perl does not feel that this code has any problem, it will continue to perform a printed statement, and the value of $ j is nothing. Sometimes, Perl's automatic generation object is indeed useful, but in my experience, it is best to use "Use Strict" to turn off this automatic function, thereby avoiding the above problems.
The Perl variable can be a scales, an arrays or hash ricks (Hashes, also known as associated arrays). (In fact, there are multiple data types in Perl, but the programmer does not face them directly.) Alternatively, they can be referenced, usually there is also a scalar type. The scalar name begins with "$", and the array name is starting with "@", and the hash begins with "%".
The scalar is the simplest data type in Perl. Each scalar has a unique value, or a string or reference. When necessary, strings and numbers can be transformed with each other. This often makes the beginner feel happy. Look at this example:
Scalar
$ I = "hi there";
Print 1 $ i; # prints 1
The value of the scalar $ i is a string "hi there", which corresponds to 0. Therefore, 1 "hi there" value is 1, and the program operation results are 1.
However, this does not mean that the Perl interpreter considers its string type and digital type on a scalar. In fact, it is just a scalar containing a certain value in memory. If this scalar value is converted into a numerical form in the statement of the numerical operation; if you are in a string operation statement (for example, print), it is performed as a string. But no matter what form, the scalar variable is substantially one value.
The value of the undefined scalar is "undef". If you can compare the other values with NULL in the C / C / Java program, but you can't take anything to compare with "undef". Use this defined () function:
Use of the 'defined ()' FUNCTION
$ I = "hi there";
Print $ i if defined $ i; #prints "hi there"
Undef $ i; # set $ I to be Undefprint $ i if defined $ i; # prints nothing
The array is essentially a set of scales. If necessary, the array size can be automatically changed, a bit like the Vector class in Java. There is no thing with the Perl array type type in C and C , but they also have libraries that provide similar functions (such as STL). An interesting feature of an array is that the number of scales of arrays is equal to its element:
Number of elements in arrays
@A = ("hi there", "nowhere");
Print scalar @A; # prints 2
Push @a, "hello"; # add "hello" at the end
Print scalar @A; # prints 3
The hash is similar to the array, but the scalar inside is not sorted in position, but is indexed by another scalar (must be a unique value). For example, a list of names with Social Security Number is a hash. After inserting a key value into the hash, the hashing will automatically expand. Hash is very similar to the HashMap and HashTable classes in Java.
The reference type is actually scalar, and they are similar to pointers in the C language, which can point to anything. This allows Perl to generate a hash array, array hash, hash, or array (multidimensional array). There are a variety of ways to get the content points to the reference, or use the referenced name directly, or use the "->" operator. Quoting is a very wide range of problems that can be referred to in the "Perldoc Perlref" reference documentation.
C and C have only some fixed types of scales. When a programmer is to use an array or a hash, an external library such as a hook (HOOP) or STL is omitted.
Java has class libraries equivalent to the decisions or hash functions in Perl, but they are not so direct in the Java language. For example, it is about three times the time required to do all elements on the hash.
Java code for all elements in the hash
Import java.util.enumeration;
Import java.util.hashtable;
Hashtable hi = new hashtable ();
// Fill in Hi's Values
// We can use an itrator, Still a lot of type
ENUMERATION ENUM = Hi.Elements ();
ENUM.haASMoreElements ();)
{
Object o = enum.nexTelement ();
// Do Something with O O
}
Perl code for all elements in the hash
# Note That this even incompudes the definition and inTIALIZATION OF
# The Hash, And Still Is More Compact Than The Java Code!
% HASH = {A => "Hi", B => "Hello"};
Foreach (Values% HASH)
{
# do something with $ _
}
Perl's shortcomings Perl lacks many of C, C and Java, but it is a completely different language after all. There are even many characteristics in these languages to be contradictory. For example, Java only supports single inheritance, and C can have multiple parent classes. In this conflict, it is certainly impossible to inherit all languages, and Perl has its own method of handling problems.
Since the Perl program can be connected to the library of C (in fact, this is one of the reasons why Perl application), this makes it hard to do any C or C .
Compared with C and C , Perl sometimes lacks running speed. This is indeed a problem, but through a good programming algorithm and the use of the Perl built-in function, it is possible to overcome this shortcomings.
Perl can't use the C and C libraries. The constants and function functions in these libraries and function functions must be converted to adaptation to Perl by different modules and bindings. This will result in a decrease in the development and program operation. However, due to a large number of modules in the CPAN library, this problem is not so difficult to solve.
In terms of training programming skills, Perl is not as deep as C and C . It is a young language, although very popular, but has not been universally accepted. However, PERLs are installed on most UNIX systems, and other operating systems also support Perl.
Perl supports single inheritance or multiple inheritance, packages, and polymorphism, but this is only achieved by external modules or programmers. That is, the Perl language itself does not have a strict object-oriented programming rule and requires the programmer to implement an object-oriented. This is also good, it is going to depends on the programmer or the project itself.
Perl's thread and unified character encoding (Unicode) support far behind Java, also slightly in C / C . Java supports threads and Unicode from the start of the design, while C / C has more time to adjust this aspects of correct support than Perl. In Perl, the support for threads and Unicode is still in the start phase, but this will be changed after the stable version after 5.6.0 is released.
The advantage of Perl For C / C / Java programmers, Perl's advantage is invaluable in some aspects. For example, the implementation of regular expressions in Perl is light, but it is very troublesome in C, C or Java. Included functions declares, unstrue grammar, as well as the program structure like the daily document makes Perl more attractive.
Perl is not suitable for everyone. It requires readers to adapt, but accept all of its shortcomings and advantages. We don't feel that Perl Cool talents use it, but because it is indeed a very good tool. If you use other languages when you solve a problem, you should give up Perl. A good programmer's hand always has several useful tools.
Perl has some small shortcomings, but those who don't know how tired will ignore these shortcomings. If you need threads and Unicode support, or strictly object-oriented programming, then you have to choose other more appropriate languages based on these needs.
Perl is a universal flexible language that binds many different models like glue. It enables algorithms for any process or function. Typically, Perl will greatly reduce the development time because it requires only a small amount of code for some common tasks (such as all elements in the hash table). Most importantly, Perl programming is always equivalent to an interesting learning process.