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.

In reply to Re^3: Matching a string in a parenthesized block (regex help) by jwkrahn
in thread Matching a string in a parenthesized block (regex help) by maxamillionk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.