Dear Monks,
I'd like to append a single character to a string only if it's not there already. Now, if the char is 'c' (for example) I can do something like:
$char = 'c';
$str = 'bla';
$str =~ s/$char?$/$char/;
print "$str\n"
and it works like a charm, printing "blac" whether $str is "bla" or "blac" already.
However, if I want to append a backslash instead of a simple character:
$char = '\\';
$str = 'bla';
$str =~ s/$char?$/$char/;
print "$str\n"
it does not work as I'd expect. I even tried '\\\\' but this way I get two backslashes (*as I'd expect*)
What am I doing wrong? I just ran out of ideas... Any help would be greatly appreciated.
Thanks!