|
|
| Just another Perl shrine | |
| PerlMonks |
Answer: How do I localize $_ in while loops? |
| ( #276542=categorized answer: print w/ replies, xml ) | Need Help?? |
|
Q&A > input and output > How do I localize $_ in while loops? contributed by ajdelore
As discussed here, $_ is automatically localized in a foreach loop. So, for example:
However, a while loop does not do this automatically:
In order to do this, you need to explicitly localize $_ within the while loop:
Another way to do it:
Note that you must have the local declaration enclosed in a block with the while statement, as above. The following example will not work:
See perlop for more information.
|
|
||||||||||||||||||||