use Modern::Perl '2014'; my $first = qr/STRING/; my $second = qr/MAGIC/; my $first_pos; my $second_pos; while () { $first_pos = $. if /$first/; $second_pos = $. if /$second/; } if ( $first_pos and $second_pos > $first_pos ) { say "Success! STRING at $first_pos followed by MAGIC at $second_pos"; } else { say "Failure! (STRING: $first_pos - MAGIC: $second_pos)"; } __DATA__ First line second line here is the STRING empty text the STRING again! followed by the MAGIC word more emptiness Oh no! the first STRING again sadness did we look in vain? Ah, MAGIC success