for (;;) { last if not defined (my $line1 = ); last if not defined (my $line2 = ); next if not $line1 =~ /red line/; print($line1, $line2); }