与“连接”不同\";并加入'\n';在Perl中

与“连接”不同\";并加入'\n';在Perl中,perl,Perl,我有一个所有图像名称的列表,现在我想在运行之前通过换行符加入它,所以我做了以下操作: print (join '\n', @all_img_name); 上述行将产生此类输出: meomeo\u 0\nmomeo\u 1\nmomeo\u 2\nmomeo\u 3\nmomeo\u 4\nmomeo\u 5\nmomeo\u 6\nmomeo\u 7\nmomeo\u 8\nmomeo\u 9\nmomeo\u 10\nmomeo\u 11\nmomeo\u 13\nmomeo\u 14\nm

我有一个所有图像名称的列表,现在我想在运行之前通过换行符加入它,所以我做了以下操作:

print (join '\n', @all_img_name);
上述行将产生此类输出:

meomeo\u 0\nmomeo\u 1\nmomeo\u 2\nmomeo\u 3\nmomeo\u 4\nmomeo\u 5\nmomeo\u 6\nmomeo\u 7\nmomeo\u 8\nmomeo\u 9\nmomeo\u 10\nmomeo\u 11\nmomeo\u 13\nmomeo\u 14\nmomeo\u 15

如果我这样做,我会把一切都做好:

print (join "\n", @all_img_name);

这两种方式有什么区别?

在单引号中,只有
\'
\
是特殊的


在双引号中,许多字符组合具有特殊含义。我们说双引号是“插值”。有关详细信息,请参阅中的“引号和类似引号的运算符”。

这是关于创建引号字符串的。它与
join()
无关

发件人:

字符串文字通常由单字符或双字符分隔 引用。它们的工作原理非常类似于标准Unix shell中的引号: 双引号字符串文字受反斜杠和变量限制 替代;单引号字符串不是(除了“\”和 "\"). 通常的C样式反斜杠规则适用于生成字符 例如换行符、制表符等,以及一些更奇特的形式。看见 perlop中的“引用和引用类似运算符”用于列表

按照阅读的建议,我们找到了这个列表,它告诉我们哪些引号式运算符插入变量和C样式的转义序列(如
\n
):

(我省略了注释-可以在上面的链接中找到。)

综上所述,您的问题的答案是
\n
(以及大多数其他C样式转义序列)在双引号字符串中展开,而不是在单引号字符串中展开


这是Perl与许多(可能是大多数)其他编程语言共享的功能。

请不要将代码作为图像发布。这与
join
无关。比较
print'\n'
print“\n”
Customary  Generic        Meaning          Interpolates
''         q{}            Literal          no
""         qq{}           Literal          yes
``         qx{}           Command          yes*
           qw{}           Word list        no
//         m{}            Pattern match    yes*
           qr{}           Pattern          yes*
           s{}{}          Substitution     yes*
           tr{}{}         Transliteration  no (but see below)          
           y{}{}          Transliteration  no (but see below)
           <<EOF          here-doc         yes*

* unless the delimiter is ''.
Sequence     Note  Description
\t                  tab               (HT, TAB)
\n                  newline           (NL)
\r                  return            (CR)
\f                  form feed         (FF)
\b                  backspace         (BS)
\a                  alarm (bell)      (BEL)
\e                  escape            (ESC)
\x{263A}     [1,8]  hex char          (example: SMILEY)
\x1b         [2,8]  restricted range hex char (example: ESC)
\N{name}     [3]    named Unicode character or character sequence
\N{U+263D}   [4,8]  Unicode character (example: FIRST QUARTER MOON)
\c[          [5]    control char      (example: chr(27))
\o{23072}    [6,8]  octal char        (example: SMILEY)
\033         [7,8]  restricted range octal char  (example: ESC)