in reply to $_ and filehandles
Please get out of the habit of using expressions such as "...doesn't work." We need specifics. Doesn't work can mean a lot of things. I maintain that it does work according to defined behavior. It simply doesn't do what you expect, being unfamiliar with defined behavior.
To see an explanation of what behavior is defined for the <diamond> operator, see perlop: IO Operators. There's good info there dealing with how the diamond operator and $_ interact.
As for your question regarding the difference between:
while (<FILE>) {$somevar .= $_} works but while ($somevar .= <FILE>) {} doesn't. (It puts the program in a scary + infinite loop)
The first "works" because it's the right way to do things. The second puts the program into an infinate loop (Thank-you for describing what "doesn't work" means in this case) because 'while()' tests for truth. Once $somevar receives some value from <FILE>, and all future values from <FILE> are simply appended to $somevar, $somevar will always contain a value. Unless your file consists of nothing but zeros, that value will be true, so the loop will always evaluate truth, and thus will never exit. Note that...
while( 1 ) { # do something }
... will never exit either.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. |