my $text; { open(my $test_fh, '<', 'testfile) or die "Unable to open testfile: $!\n"; local $/; # Read to end of file. $text = <$test_fh>; } if ($text =~ /\w+\n\n\w+/) { print "reading file test:\n$text\nmatches.\n"; } #### ... while ($text =~ /\w+\n\n\w+/g) { print "reading file test:\n$text\nmatches.\n"; }