Files and Safe IO
Readline Loop
A while loop can process one logical line at a time from input text.
Readline Loop
readline_loop.pl
use strict;
use warnings;
my $wanted = ;
my $content = "ok\nskip\nok\ndone\n";
my $matches = 0;
my $lines = 0;
while ($content =~ /([^\n]+)\n/g) {
my $line = $1;
$lines = $lines + 1;
if ($line eq $wanted) {
$matches = $matches + 1;
}
}
print "wanted=$wanted\n";
print "lines=$lines\n";
print "matches=$matches\n";
use strict;
use warnings;
my $wanted = ;
my $content = "ok\nskip\nok\ndone\n";
my $matches = 0;
my $lines = 0;
while ($content =~ /([^\n]+)\n/g) {
my $line = $1;
$lines = $lines + 1;
if ($line eq $wanted) {
$matches = $matches + 1;
}
}
print "wanted=$wanted\n";
print "lines=$lines\n";
print "matches=$matches\n";
use strict;
use warnings;
my $wanted = ;
my $content = "ok\nskip\nok\ndone\n";
my $matches = 0;
my $lines = 0;
while ($content =~ /([^\n]+)\n/g) {
my $line = $1;
$lines = $lines + 1;
if ($line eq $wanted) {
$matches = $matches + 1;
}
}
print "wanted=$wanted\n";
print "lines=$lines\n";
print "matches=$matches\n";
readline
A readline-style loop handles one line before moving to the next.