B.12
Binding Operators
Binding operators
are used for pattern matching,
substitution, and transliteration on strings. They are used in
conjunction with regular expressions that specify the patterns.
Here's an example:
'ACGTACGTACGTACGT' =~ /CTA/
The pattern is the string CTA, enclosed by forward
slashes //. The string binding operator is
=~; it tells the program which string
to search, returning true if the pattern appears
in the string.
Another string binding operator is !~,
which returns true if the pattern isn't in
the string:
'ACGTACGTACGTACGT' !~ /CTA/
This is equivalent to:
not 'ACGTACGTACGTACGT' =~ /CTA/
You can substitute one pattern for
another using the string binding operator. In the next example,
s/thine/nine/ is the substitution command, which
substitutes the first occurrence of thine with the
string nine:
$poor_richard = 'A stitch in time saves thine.';
$poor_richard =~ s/thine/nine/;
print $poor_richard;
This produces the output:
A stitch in time saves nine.
Finally, the
transliteration
(or translate) operator tr substitutes characters
in a string. It has several uses, but the two uses I've covered
are first, to change
bases to their complements A T, C
A:
$DNA = 'ACGTTTAA';
$DNA =~ tr/ACGT/TGCA/;
This produces the value:
TGCAAATT
Second, the tr operator counts the number of a
particular character in a string, as in this example which counts the
number of Gs in a string of DNA sequence data:
$DNA = 'ACGTTTAA';
$count = ($DNA =~ tr/A//);
print $count;
This produces the value 3. This shows that a pattern match can return
a count of the number of translations made in a string, which is then
assigned to the variable $count.