Special variables of Perl [Repost]

xiaoxiao2021-03-06  90

Let your Perl code look more like Perl code, not like C or Basic code, the best way is to understand the built-in variables of Perl. Perl can control many aspects of the program run through these built-in variables. In this article, we have experienced the excellent performance of many built-in variables on the input and output control of the file. The number of rows I decided to write this article is that when I find that many people don't know "$." The existence of built-in variables, this is indeed surprising. I can still see many people writing code: code MY $ line_no = 0; while () { $ line_no; unless "error in line $ line_no / n" NEXT;} # process the record in some way} For some reason, many people seem to completely ignore the existence of "$.". And the role of this variable is to track the current record number. So the above code can also be written: code while () {unless "error in line $. / N"; Next;} # Process the record in some way} translator Note: It is popular that this built-in variable is very similar to the record pointers in the database, and its value is the current line number in your current read file. Although using this built-in variable can't make you smoking how much words, it is important that we can save some unnecessary variable declarations. Another way to use this built-in variable is to use with the continuous operator (..). When used in the list context, (..) is the list build operator. It creates all elements from given between the start and end elements. For example: code my @numbers = (1 .. 1000); @Numbers will contain all integers between 1 and 1000. But when you use this operator in an expression context (for example, as a statement), its role is completely different. The first operand (".." expression on the left side) will be evaluated, if the result is obtained, the operation will not do and return to the false value. If the result is true, the operation returns true value and continues to return the value below until the second operand (".." The expression on the right side of the operator is returned. We give an example explanation. Suppose you have a file, you just want to handle a few parts of this file. These parts are "!! start !!" for the beginning, "!! End !!" is end. Use a continuous operator you can write this code: code while () {if (/ !! start !! / .. / !! end !! /) {# Process line}} Each cycle, continuous The operator will check the current line. If the current line does not match "/ !!", the operator returns the false value and continues to loop. When looping to the first row, the continuous operator returns a true value and executes the code in the IF statement block. In the loop behind the While statement, the continuous operator will Check the "/ !! end !! /" again, but it will return true values ​​until you find the match line. This is also said "!! Start !!" and "!! End !!" tag All rows between room will be processed. After the match line is found / !! End !! /, the continuous operator returns a false and starts to match the first rule expression again. These and "$." What is the relationship? If the operand of the continuous operator has a constant, they will be converted to integer and match the "$.".

So the top 10 lines of a file we can write code: Code while () {print if 1 .. 10;} About "$." The last thing to explain is that there is only one "$" ."variable. If you read data from multiple file handles, then "$." Variable saves the current record number in the recently read file handle. If you want more complex to solve this problem, you can use similar to IO :: File objects. These objects have an input_line_number method. Recording separators "$ /" and "$ /" are input and output record separators, respectively. When you are reading or writing data, what they primarily controlled to define a "record". Let me explain it in more detail. When you learn Perl for the first time, I will know when I entered the operator. Maybe you will be told "" is a line of data from a file, and each line of reading includes a new line character. ("/ N"). In fact, these things you know are not entirely true, that is just a very special situation. In fact, the file input operator ("<>") is read after reading the data, and the file input separator specified in "$" is included. Let's look at an example: Suppose you have a text file, the content is some interesting citation or some lyrics or something else. Such as the following: Code this is the definition of my life %% We Arefar Too Young and Clever %% Stab A Sorry Heart with your favorite finger There are three paragraphs that are separated by a line "%%". So how should we read a group from this file? (Translator Note: This citation can also be a few lines, such as the first paragraphs and second paragraphs in the examples, and the third paragraph is 2 lines) one of the solution is that once from the file Read a row, then check if the read line is "%%". So we need to declare a variable to save data read in, when encounter "%%", re-combined the previously read data as a complete citation. Oh, you still need to remember to deal with the last citation because it doesn't last "%%". Such methods are too complex, a simple method is to change the contents of the "$ /" variable. The default value of this variable is a new row character ("/ n"), which is why "<>" operator is reading a line when reading the file content. But we can modify this variable content for our favorite value. For example: code $ / = "%% / n"; while () {chomp; print;} Now we call "<>" each time, Perl will read data from the file handle until "%% / N ". (Not a diploma once). Therefore, when you use the Chomp function to remove the slot segments read by the data, the separator specified in the "$ /" variable will be deleted. Data after the CHOMP function processed in the previous example will delete %% / N ". Change the special variable of Perl before we continue, I need to remind you that when you modify these special variables, you will Get a warning. The problem is that most of these variables are forced in the master. That is to say, when you change the value of these variables, the places used in the program (including those modules you include) will give Warning. For example, if you write a module, and you change the value of the "$ /" variable in the module, you must modify other modules to adapt the program when others apply your module to your own program. Execution. So modify the value of a special variable to get the difficulty of finding BUGS. Therefore, we should avoid it as much as possible. The first avoidable method is to take this special Variable weight returns the original value.

转载请注明原文地址:https://www.9cbs.com/read-95671.html

New Post(0)