Handy Perl variables: @- and @+

Regular expression matching is one of the key features that makes Perl a widely used language in bioinformatics. For example, we can match a peptide to a longer protein sequence like this:

my $seq = "MVQRWLYSTNAKDIAVLYFMLAIFSGMAGTAMSLIIRLELAAPG";
my $pep = "MAGTAM";

while($seq =~/$pep/g) {
    print "Found a match\n";
                      }

Perl also uses a bunch of “special variables”, that are automatically assigned values when you perform certain operations. Two of these, @- and @+, contain indices representing the start and end of a regular expression match. Very handy for when you want to know the matching positions in the longer sequence. Note that you need to add one to the start position since indices start at 0, whereas sequences start at 1:

while($seq =~/$pep/g) {
    print "Found a match from ".($-[0]+1)." to ".($+[0])."\n";
                      }

See Perl regular expressions tutorial for more.