while (<FILE>) {
# do something
}
This code has become so familiar that many do not even think about it, but how does it actually work? In this article I will describe one feature, which is very useful to remember.while
is executed while its condition is true. However, in our case, the condition is a string that gives a boolean truth only if it is not:undef
;"0"
."\n"
, and therefore will be regarded in the boolean context as true. The same applies to the case when the file contains a line with only one character "0"
. But there is a case when this rule does not work: if the last line of the file contains only the character "0"
and does not end with a new line character. As a result, when reading this last line, we get the string "0"
, which for while
should be considered false, and, therefore, this line will not be processed. Right? It would seem that everything is correct, and now we must urgently grab either a favorite text editor, or validol, or both at the same time, depending on the degree of importance of the project where this code was used.while
operator. I quote perlop ( loosely translated):The following lines are equivalent:Thus, inside aAnd this line of code works in a similar way, but without using $ _:while ( defined ($_ = <STDIN>)) { print ; }
while ($_ = <STDIN>) { print ; }
while (<STDIN>) { print ; }
for (;<STDIN>;) { print ; }
print while defined ($_ = <STDIN>);
print while ($_ = <STDIN>);
print while <STDIN>;In all of these constructs, the assigned value (regardless of whether the assignment was explicit or not) checks whether the value is determined. This avoids the problem when the string value is false in a boolean context, for example, "" or "0" without the terminating newline character. If you intentionally want to use these values to complete a loop, you need to check them explicitly:while ( my $line = <STDIN>) { print $line }
In other expressions with a boolean context, a warning will be displayed using thewhile (($_ = <STDIN>) ne '0') { ... }
while (<STDIN>) { last unless $_; ... }<filehandle>
if there is no explicit call to thedefined
function or a comparison operation, provided that theuse warnings
pragma isuse warnings
or the-w
command line parameter (or$^W
variable) is used.
while
reading from a file automatically turns into a check on the defined
. However, you should replace while
with if
or even just complicate the loop condition by adding a sub expression through and
or or
like -w
.Source: https://habr.com/ru/post/84762/
All Articles