If I assume that your posted code ends with two closing curly brackets, I see a clear issue. The while(<$fh>){ structure splits your file based upon the record separator, which is new line by default. Since you haven't set it ($/), that means your code is first getting fed ">CIMG_12545 | Coccidioides immitis RS hypothetical protein (translation) (95 aa)" on the first loop iteration and then "MSSQPTTLQSQTGIQRHGEISSQAQKPTTALEEDDEFEDFPVEDWPQEDAEALGPAGTNN" on the second. Therefore, you can't match across the new lines. If you want to look for patterns across new lines, you either need to slurp your file ($/) or modify your while loop to read in more than one line in an iteration (plus many other possible variations on the theme). So something like this might work:

use strict; use warnings; local $/; my ($name, $aa); while (my $seq = <DATA>) { if ($seq =~ />(\w+).+aa\)\n(\w+)/m) { $name = $1; $aa = $'; } } print "$name $aa\n"; __DATA__ >CIMG_12545 | Coccidioides immitis RS hypothetical protein (translatio +n) (95 aa) MSSQPTTLQSQTGIQRHGEISSQAQKPTTALEEDDEFEDFPVEDWPQEDAEALGPAGTNN DHLWEESWDDDDSNEEFSRQLKEELKKVEAMKQQ*

A side note, while (my $seq = <$IN2>) { is a bad structure to get into the habit of using, since it will exit your loop if the line evaluates to false - i.e. if you have a blank line in your file. Better would be while (defined (my $seq = <$IN2>)) {. See almut below.

Update: I should mention that for what you've written the m modifier on your regular expression has no impact. As it states in Modifiers, m modifies the matching behavior of ^ and $ so they match new lines. You don't have either metacharacter in your expression, so the modifier is unnecessary.


In reply to Re: Pattern Match Issue by kennethk
in thread Pattern Match Issue by perl_n00b

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.