use warnings; use strict; my $file = "/path/to/file.txt"; sub has_word { my $arg = $_[0]; local $/; open FILE, '<', $file; while ( ) { if ( /^ASDF_$arg/ && /magic/ ) { close FILE; return 1; } else { close FILE; return 0; } } } sub main { if (has_word("ONE")) { print "ONE already has the word.\n"; } else { print "ONE does not have the word.\n"; } if (has_word("TWO")) { print "TWO already has the word.\n"; } else { print "TWO does not have the word.\n"; } } main; #### ASDF_ONE { magic tmp tmp } ASDF_TWO { tmp magic tmp } string3 { tmp tmp magic } #### ONE already has the word. TWO does not have the word.