How can I prevent Perl from interpreting double-backslash as single-backslash character?

后端 未结 3 1950
深忆病人
深忆病人 2021-01-14 09:32

How can I print a string (single-quoted) containing double-backslash \\\\ characters as is without making Perl somehow interpolating it to single-slash \\

3条回答
  •  轮回少年
    2021-01-14 10:00

    If you are mildly crazy, and like the idea of using experimental software that mucks about with perl's internals to improve the aesthetics of your code, you can use the Syntax::Keyword::RawQuote module, on CPAN since this morning.

    use syntax 'raw_quote';
    my $string1 = r'a\\\b';
    print $string1; # prints 'a\\\b'
    

    Thanks to @melpomene for the inspiration.

提交回复
热议问题