my $first_pattern = 'DESCRIPTION:***Private-from-iPhone***'; my $found = 0; while (<>) { chomp; $found = 1 if m/\Q$first_pattern/; if ($found && m/SUMMARY:/) { ... } }