Here is a code snippet I use about 100 times a day:
my $filename = 'pathToFile.txt'; # Use the 3 arg form of open. # Use 'my' to scope the file handle. # die if we cannot open the file, report error in '$!'. open my $input_fh, '<', $filename or die "Could not open $filename: $!"; # Loop through each line in the file. # Use 'chomp' to remove the trailing newline. ## Label your loops. If you nest loops like I do, ## your 'next' commands can reference your label to make ## sure you short circuit the correct loop. LINE: while (my $line = <$input_fh>) { chomp $line; # Do stuff here with $line... # And later maybe you test something and need to skip # to the next line: if ($something_happens) { next LINE; } }
In reply to Re: Read from a file one line at a time?
by Anonymous Monk
in thread Read from a file one line at a time?
by john.perry.14
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |