Files and Safe IO
Readline Loop
A while loop can process one logical line at a time from input text.
readline
A readline-style loop handles one line before moving to the next.
Readline Loop
readline_loop.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $wanted = "ok";
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 = "skip";
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 = "done";
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";
$wanted ← ok, $content ← ok skip ok done , $matches ← 0, $lines ← 0
4my $wanted→ ok = "ok"; #@wanted="skip", "done"5my $content→ ok skip ok done = "ok\nskip\nok\ndone\n";6my $matches→ 0 = 0;7my $lines→ 0 = 0;$line ← ok, $lines ← 1
pass 1 of 49while ($contentok skip ok done =~ /([^\n]+)\n/g) {10 my $line→ ok = $1;11 $lines→ 1 = $lines + 1;12 if ($line eq $wanted) {All 4 passes — pass 1 is the card above pass $wanted$line$lines$matches1 ok ok 0 → 1 0 → 1 2 — skip 1 → 2 — 3 ok ok 2 → 3 1 → 2 4 — done 3 → 4 — $matches ← 1
pass 1 of 211$lines = $lines + 1;12if ($lineok eq $wantedok) {13 $matches→ 1 = $matches + 1;14}$matches ← 2
pass 2 of 211$lines = $lines + 1;12if ($lineok eq $wantedok) {13 $matches→ 2 = $matches + 1;14}print "wanted=$wanted ";
17print "wanted=$wantedok\n";18print "lines=$lines4\n";19print "matches=$matches2\n";outputwanted=ok lines=4 matches=2
$wanted ← skip, $content ← ok skip ok done , $matches ← 0, $lines ← 0
4my $wanted→ skip = "skip";5my $content→ ok skip ok done = "ok\nskip\nok\ndone\n";6my $matches→ 0 = 0;7my $lines→ 0 = 0;$line ← ok, $lines ← 1
pass 1 of 49while ($contentok skip ok done =~ /([^\n]+)\n/g) {10 my $line→ ok = $1;11 $lines→ 1 = $lines + 1;12 if ($line eq $wanted) {All 4 passes — pass 1 is the card above pass $wanted$line$lines$matches1 — ok 0 → 1 — 2 skip skip 1 → 2 0 → 1 3 — ok 2 → 3 — 4 — done 3 → 4 — $matches ← 1
11$lines = $lines + 1;12if ($lineskip eq $wantedskip) {13 $matches→ 1 = $matches + 1;14}print "wanted=$wanted ";
17print "wanted=$wantedskip\n";18print "lines=$lines4\n";19print "matches=$matches1\n";outputwanted=skip lines=4 matches=1
$wanted ← done, $content ← ok skip ok done , $matches ← 0, $lines ← 0
4my $wanted→ done = "done";5my $content→ ok skip ok done = "ok\nskip\nok\ndone\n";6my $matches→ 0 = 0;7my $lines→ 0 = 0;$line ← ok, $lines ← 1
pass 1 of 49while ($contentok skip ok done =~ /([^\n]+)\n/g) {10 my $line→ ok = $1;11 $lines→ 1 = $lines + 1;12 if ($line eq $wanted) {All 4 passes — pass 1 is the card above pass $wanted$line$lines$matches1 — ok 0 → 1 — 2 — skip 1 → 2 — 3 — ok 2 → 3 — 4 done done 3 → 4 0 → 1 $matches ← 1
11$lines = $lines + 1;12if ($linedone eq $wanteddone) {13 $matches→ 1 = $matches + 1;14}print "wanted=$wanted ";
17print "wanted=$wanteddone\n";18print "lines=$lines4\n";19print "matches=$matches1\n";outputwanted=done lines=4 matches=1