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

wanted
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";
  1. $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;
  2. $line ← ok, $lines ← 1

    pass 1 of 4
    9while ($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$matches
    1okok0 10 1
    2skip1 2
    3okok2 31 2
    4done3 4
  3. $matches ← 1

    pass 1 of 2
    11$lines = $lines + 1;12if ($lineok eq $wantedok) {13    $matches→ 1 = $matches + 1;14}
  4. $matches ← 2

    pass 2 of 2
    11$lines = $lines + 1;12if ($lineok eq $wantedok) {13    $matches→ 2 = $matches + 1;14}
  5. print "wanted=$wanted ";

    17print "wanted=$wantedok\n";18print "lines=$lines4\n";19print "matches=$matches2\n";
    outputwanted=ok
    lines=4
    matches=2
  1. $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;
  2. $line ← ok, $lines ← 1

    pass 1 of 4
    9while ($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$matches
    1ok0 1
    2skipskip1 20 1
    3ok2 3
    4done3 4
  3. $matches ← 1

    11$lines = $lines + 1;12if ($lineskip eq $wantedskip) {13    $matches→ 1 = $matches + 1;14}
  4. print "wanted=$wanted ";

    17print "wanted=$wantedskip\n";18print "lines=$lines4\n";19print "matches=$matches1\n";
    outputwanted=skip
    lines=4
    matches=1
  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;
  2. $line ← ok, $lines ← 1

    pass 1 of 4
    9while ($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$matches
    1ok0 1
    2skip1 2
    3ok2 3
    4donedone3 40 1
  3. $matches ← 1

    11$lines = $lines + 1;12if ($linedone eq $wanteddone) {13    $matches→ 1 = $matches + 1;14}
  4. print "wanted=$wanted ";

    17print "wanted=$wanteddone\n";18print "lines=$lines4\n";19print "matches=$matches1\n";
    outputwanted=done
    lines=4
    matches=1