5

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

6 に答える 6

15

私はあなたのシェル/ターミナルに何か問題があり、出力先が何であれ \n を改行として解釈しており、問題は Perl にはないと言いがちです。

確認するには: This Shouldn't Happen(TM) - 最初のケースでは、改行が挿入されることを期待しますが、一重引用符を使用すると、改行ではなく文字\nを文字通り出力する必要があります。

于 2010-01-08T14:37:10.107 に答える
4

\nPerl では、単一引用符で囲まれた文字列は、またはのようなバックスラッシュ エスケープを展開しません\t。それらが展開されている理由は、おそらく、使用しているシェルの性質によるもので、何らかの理由で出力を変更しています。

于 2010-01-08T15:18:00.697 に答える
3

引用符と引用符のような演算子について知っておく必要があることはすべてperlopにあります。

特定の質問に答えるために、二重引用符はリテラル文字の特定のシーケンスを他の文字に変えることができます。あなたの例では、二重引用符は文字のシーケンスを改行を表す単一の文字に変えます\n一重引用符で囲まれた文字列では、同じリテラルシーケンスはリテラル\n文字だけです。

于 2010-01-08T16:38:38.787 に答える
1

O'Reillyリンクに加えて、LarryWallによる「ProgrammingPerl」の本と同じくらい権威のある参考文献は、バックスラッシュ補間は一重引用符で囲まれた文字列では発生しないと述べています。

... 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'Double backslash n:\\n'で何をするかを見るのは興味深いでしょう。

上記のように、「perl-v」からの出力を表示してください。

そして、最後のPerlの「印刷」がインデントされているはずなので、フォーラムエディタソフトウェアを混乱させたと思います。

于 2010-01-08T16:41:26.613 に答える
1

「解釈された」とは、変数名などは出力されず、代わりにその値が出力されることを意味します。\n はエスケープ シーケンスなので、解釈されないと思います。

于 2010-01-08T14:31:21.243 に答える
1

二重引用符を使用すると、\n が改行として解釈されます。

ただし、一重引用符を使用すると、\n が改行として解釈されません。

私にとっては正しく機能しています。

ファイルの内容

print "This string \n shows up on two lines.";
print 'This string \n shows up on only one.'
于 2010-03-02T11:18:04.530 に答える