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. |