This seems to work:
$ cat file.txt
ASDF_ONE {
magic
tmp
tmp
}
ASDF_TWO {
tmp
magic
tmp
}
string3 {
tmp
tmp
magic
}
#!/usr/bin/perl
use warnings;
use strict;
use feature 'state';
my $file_name = 'file.txt';
sub get_file_data {
state $data;
unless ( length $data ) {
open my $FH, '<', $file_name or die "Cannot open '$file_name'
+because: $!";
my $read = read $FH, $data, -s $FH;
$read == -s _ or die "Error reading '$file_name'";
}
return $data;
}
sub has_word {
my $query = shift;
my $file = get_file_data();
local $/ = "\n}\n";
open my $FH, '<', \$file;
while ( <$FH> ) {
if ( /^ASDF_\Q$query/ && /magic/ ) {
return 1;
}
}
return;
}
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";
}
And it produces this output:
$ perl 11129184.pl
ONE already has the word.
TWO already has the word.