my $matches = 0; while (...) { ++$matches; ... } if (! $matches) { ... "sorry no matches found" }