my $found = 0; while ( $found < 2 and my $line = ) { if ( $line =~ m/customer2/ ) { # ... } }