Your regular expression syntax is correct. Your Perl expression in the if condition is wrong.
if (
$line =~ /^[AGCT]/ # this tests $line
&& /[AGCT]/ # this defaults to $_
) {
You need to use $line =~ // explicitly both times.
In addition, your second pattern does not do what you want. You are missing a *$, and it would make sense to include the beginning character, too. It should read
/^[AGCT][AGCT]*$/
As you can see, essentially you can just have one pattern and be done with it.
if ( $line =~ /^[AGCT]+$/ ) { ... }
You can shorten it even further by doing the opposite pattern and saying it shouldn't match.
if ( $line !~ /[^AGCT]/ ) { ... }
This is a bit confusing because of the double negation ( !~ and [^] though.
In any case, you should chomp your input first. I would write your program like this:
use strict;
use warnings;
# the \n removes the line number from die
open my $fh, '<', $fasta or die "Couldn't read file $fasta";
local $/ = "\n>";
while (my $line = <>) {
chomp $line;
die if $line =~ /[^AGCT]/;
}
That program will die if any of the lines contains something that is not A, G, C or T. I do believe that's what you wanted to do.
FASTA, but then you do nothing with it. Your while loop reads fromARGVinstead.