The rule with regexes is that non-alphanumeric characters (\W) lose their special meaning when escaped with a \, (\* is a litteral '*', \( is a litteral parenthesis etc...), while alphanumeric characters gain a special meaning when escaped. So \1, \2 etc... are reference to captured groups, \w means alphanumeric characters. This means that \x where x is a letter should always be considered to have a special meaning, even if this is not the case for the current version of perl, because such a meaning may be added in future versions. And yes, even interpolated variables are read using the regex syntax, and not taken litteraly.
\g is the start of a regex special meaning, referenced in perlreref:
\g1 or \g{1}, \g2 ... Matches the text from the Nth groupSince you don't have a number after you \g, this is, indeed, an incomplete pattern.
The solution is to remove its special meaning to \ (the escaping character), by adding another \ in front of it. The easiest way to do that is to use quotemeta which will leave all alphanumeric characters untouched, and escape all others (those that may have a special meaning).
my $esc_bad_gitdir = quotemeta $bad_gitdit;
Or using the \Q shortcut:
qr/Cannot find directory\(ies\): \Q$bad_gitdir/
Edit: there were typos everywhere...
In reply to Re: Unterminated \g... pattern in regex behaving badly on Windows
by Eily
in thread Unterminated \g... pattern in regex behaving badly on Windows
by jkeenan1
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |