Perl 中单引号和双引号有什么区别?

发布于 2024-08-17 04:54:35 字数 990 浏览 3 评论 0原文

我刚刚开始学习 Perl。我查看了 开始 perl 页面并开始工作。

它说:

单引号和双引号的区别在于,单引号表示其内容应按字面意思理解,而双引号表示其内容应按字面解释

当我运行这个程序时:

#!/usr/local/bin/perl
print "This string \n shows up on two lines.";
print 'This string \n shows up on only one.';

它输出:

This string
 shows up on two lines.
This string
 shows up on only one.

我有什么地方错了吗? perl 版本如下:

perl -v

This is perl, v5.8.5 built for aix

Copyright 1987-2004, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.

I am just begining to learn Perl. I looked at the beginning perl page and started working.

It says:

The difference between single quotes and double quotes is that single quotes mean that their contents should be taken literally, while double quotes mean that their contents should be interpreted

When I run this program:

#!/usr/local/bin/perl
print "This string \n shows up on two lines.";
print 'This string \n shows up on only one.';

It outputs:

This string
 shows up on two lines.
This string
 shows up on only one.

Am I wrong somewhere?
the version of perl below:

perl -v

This is perl, v5.8.5 built for aix

Copyright 1987-2004, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深海里的那抹蓝 2024-08-24 04:54:35

我倾向于说你的 shell/终端出了问题,无论你输出到什么,都将 \n 解释为换行符,问题不在于 Perl。

确认:这不应该发生(TM) - 在第一种情况下,我希望看到插入新行,但使用单引号它应该按字面输出字符 \n不是新行。

I am inclined to say something is up with your shell/terminal, and whatever you are outputting to is interpreting the \n as a newline and that the problem is not with Perl.

To confirm: This Shouldn't Happen(TM) - in the first case I would expect to see a new line inserted, but with single quotes it ought to output literally the characters \n and not a new line.

柠栀 2024-08-24 04:54:35

在 Perl 中,单引号字符串不会像 \n\t 那样扩展反斜杠转义符。您看到它们扩展的原因可能是由于您正在使用的 shell 的性质,该 shell 出于某种原因正在修改您的输出。

In Perl, single-quoted strings do not expand backslash-escapes like \n or \t. The reason you're seeing them expanded is probably due to the nature of the shell that you're using, which is munging your output for some reason.

不打扰别人 2024-08-24 04:54:35

您需要了解的有关引用和类似引用运算符的所有内容都在 中佩洛普。

为了回答您的具体问题,双引号可以将某些文字字符序列转换为其他字符。在您的示例中,双引号将字符序列 \n 转换为表示换行符的单个字符。在单引号字符串中,相同的文字序列只是文字 \n 字符。

Everything you need to know about quoting and quote-like operators is in perlop.

To answer your specific question, double-quotes can turn certain sequences of literal characters into other characters. In your example, the double quotes turn the sequence of characters \ and n into the single character that represents a newline. In a single quoted string, that same literal sequence is just the literal \ and n characters.

一百个冬季 2024-08-24 04:54:35

通过“解释”,它们意味着不会打印变量名称等,而是打印它们的值。 \n 是一个转义序列,所以我认为它不会被解释。

By "interpreted", they mean that variable names and such will not be printed, but their values instead. \n is an escape sequence, so I'd think it would not be interpreted.

半山落雨半山空 2024-08-24 04:54:35

除了您的 O'Reilly 链接之外,权威性不亚于 Larry Wall 所著的《Programming Perl》一书的参考文献指出,反斜杠插值不会出现在单引号字符串中。

... much like Unix shell quotes: double quoted string literals are subject to   
backslash and variable interpolation; single quoted strings are not   
(except for \' and \\, so that you may ...)  

Programing Perl, 2nd ed, 1996 page 16

所以看看你的 Perl 能做什么会很有趣
print '双反斜杠 n: \\n';

如上所述,请向我们展示“perl -v”的输出。

我相信我混淆了论坛编辑器软件,因为最后一个 Perl 'print' 应该缩进。

In addition to your O'Reilly link, a reference no less authoritative than the 'Programming Perl' book by Larry Wall, states that backslash interpolation does not occur in single quoted strings.

... much like Unix shell quotes: double quoted string literals are subject to   
backslash and variable interpolation; single quoted strings are not   
(except for \' and \\, so that you may ...)  

Programing Perl, 2nd ed, 1996 page 16

So it would be interesting to see what your Perl does with
print 'Double backslash n: \\n';

As above, please show us the output from 'perl -v'.

And I believe I have confused the forum editor software, because that last Perl 'print' should have indented.

征棹 2024-08-24 04:54:35

如果使用双引号,\n 将会被解释为换行符。

但如果您使用单引号,它不会将 \n 解释为换行符。

对我来说它工作正常。

文件内容

print "This string \n shows up on two lines.";
print 'This string \n shows up on only one.'

If you use the double quote it will be interpreted the \n as a newline.

But if you use the single quote it will not interpreted the \n as a newline.

For me it is working correctly.

file content

print "This string \n shows up on two lines.";
print 'This string \n shows up on only one.'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文