问题
I wish to escape special characters susch as quotes and spaces in a Perl string.
I'd like to avoid using regex and installing extra modules.
回答1:
Using quotemeta may help you.
my $escaped = quotemeta $string_with_quotes_and_spaces;
which will escape with a backslash anything that isn't alphanumeric or an underscore.
回答2:
If you are receiving the string then Borodin's solution with quotemeta will work.
If you are declaring the string, you can use the qq switch to declare your own string delimiter. For example to use # instead of ' or ":
my $string = qq#Didn't know I could do this!"how" amazing#;
This will escape the usual string delimiters.
来源:https://stackoverflow.com/questions/11666858/perl-string-escape