The binding operators (=`~`

and !`~`

) are used as
search, modify, and translation operations and work on the `$_` variable
by default. What if the string to be searched is in some other variable? That's where
the binding operators come into play. They let you bind the regular expression operators
to a variable other than `$_`. There are two forms of the binding operator:
the regular `=``~`

and its complement `!``~`

.
The following small program
shows the syntax of the `=``~`

operator (`bind1.pl`):

$scalar = "The root has many leaves"; $match = $scalar =~ m/root/; $substitution = $scalar =~ s/root/tree/; $translate = $scalar =~ tr/h/H/; print("\$match = $match\n"); print("\$substitution = $substitution\n"); print("\$translate = $translate\n"); print("\$scalar = $scalar\n");This program displays the following:

$match = 1 $substitution = 1 $translate = 2 $scalar = The tree has many leaves

This example uses all three of the regular expression operators with the regular
binding operator. Each of the regular expression operators was bound to the `$scalar`
variable instead of `$_`. This example also shows the return values of the
regular expression operators. If you don't need the return values, you could do
this (`bind2.pl`):

$scalar = "The root has many leaves"; print("String has root.\n") if $scalar =~ m/root/; $scalar =~ s/root/tree/; $scalar =~ tr/h/H/; print("\$scalar = $scalar\n");

This program displays the following:

String has root. $scalar = THe tree Has many leaves

The left operand of the binding operator is the string to be searched, modified,
or transformed; the right operand is the regular expression operator to be evaluated.
The complementary binding operator is valid only when used with the matching regular
expression operator. If you use it with the substitution or translation operator,
you get the following message if you're using the `-w` command-line option
to run Perl:

`
Useless use of not in void context at bind.pl line 4.`

You can see that the `!``~`

is the opposite of `=``~`

by replacing the `=``~`

in the previous example. This is (`
bind3.pl`):

$scalar = "The root has many leaves"; print("String has root.\n") if $scalar !~ m/root/; $scalar =~ s/root/tree/; $scalar =~ tr/h/H/; print("\$scalar = $scalar\n");

This program displays the following:

$scalar = THe tree Has many leaves

The first print line does not get executed because the complementary binding operator returns false.