简体   繁体   English

Perl中单引号和双引号之间的区别是什么?

[英]What's the difference between single and double quotes in Perl?

I am just begining to learn Perl. 我刚开始学习Perl。 I looked at the beginning perl page and started working. 我查看了开始的perl页面并开始工作。

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的版本如下:

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 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. 我倾向于说你的shell /终端有什么问题,无论你输出什么,都将\\ n解释为换行符,而问题不在于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. 要确认:这不应该发生(TM) - 在第一种情况下我希望看到插入一个新行,但是使用单引号它应该输出字符\\ n不是新行。

In Perl, single-quoted strings do not expand backslash-escapes like \\n or \\t . 在Perl中,单引号字符串不会像\\n\\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. 你看到它们扩展的原因可能是由于你正在使用的shell的性质,这是出于某种原因使你的输出变得混乱。

Everything you need to know about quoting and quote-like operators is in perlop . 您需要了解的有关引用和类似报价的运算符的所有内容都在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. 在您的示例中,双引号将字符序列\\n转换为表示换行符的单个字符。 In a single quoted string, that same literal sequence is just the literal \\ and n characters. 在单引号字符串中,相同的文字序列只是文字\\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. \\ n是一个转义序列,所以我认为它不会被解释。

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. 除了你的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

So it would be interesting to see what your Perl does with 所以看看你的Perl做什么会很有趣
print 'Double backslash n: \\\\n'; print'双反斜杠n:\\\\ n';

As above, please show us the output from 'perl -v'. 如上所示,请向我们展示'perl -v'的输出。

And I believe I have confused the forum editor software, because that last Perl 'print' should have indented. 我相信我已经混淆了论坛编辑器软件,因为最后一个Perl'print'应该缩进。

If you use the double quote it will be interpreted the \\n as a newline. 如果使用双引号,它将被解释为\\ n作为换行符。

But if you use the single quote it will not interpreted the \\n as a newline. 但是如果使用单引号,则不会将\\ n解释为换行符。

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.'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM