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

后端 未结 3 1943
深忆病人
深忆病人 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:03

    The only quoting construct in Perl that doesn't interpret backslashes at all is the single-quoted here document:

    my $string1 = <<'EOF';
    a\\\b
    EOF
    print $string1; # Prints a\\\b, with newline
    

    Because here-docs are line-based, it's unavoidable that you will get a newline at the end of your string, but you can remove it with chomp.

    Other techniques are simply to live with it and backslash your strings correctly (for small amounts of data), or to put them in a __DATA__ section or an external file (for large amounts of data).

提交回复
热议问题