/^\Q$host\E$/;
^ ^ ^^
| | ||
| | |+ - End of line
| | + - End quote metacharacters
| + - Begin quote metacharacters (e.g., [.*\+], etc.)
+ - Match beginning at the start of the line
If you remove the trailing $, it'll match a host name beginning with "db". Try the following with and without the trailing $ in the regex:
use warnings;
use strict;
my $host = 'db';
while (<DATA>) {
print if /^\Q$host\E$/i;
}
__DATA__
db
db4323
dbwindows822.tp.com
db726.tp.com
Output with $:
db
This is because if forces an exact match, like $host eq 'db'.
Output without $:
db
db4323
dbwindows822.tp.com
db726.tp.com
This is becuase it's only matching the first two characters of $_ (the default scalar) against the value of $host.
Note that there's the i modifier at the end of the regex. This makes the match case-insensitive, in case you have a host like DB4323. |