$ cat temp.pl
use strict;
use warnings;
print "1\n";
print "hello, world\n";
print "2\n";
print "hello,
world\n";
print "3\n";
print "hello, \
world\n";
$ perl temp.pl
1
hello, world
2
hello,
world
3
hello,
world
$
为了使我的代码易于阅读,我想将列数限制为80个字符。如何将一行代码分成两部分而没有任何副作用?
如上所示,简单的↵或\无效。
什么是正确的方法?
最佳答案
在Perl中,回车将在常规空间所在的任何地方使用。反斜杠不像某些语言那样使用。只需添加一个CR。
您可以使用串联或列表操作将字符串分成多行:
print "this is ",
"one line when printed, ",
"because print takes multiple ",
"arguments and prints them all!\n";
print "however, you can also " .
"concatenate strings together " .
"and print them all as one string.\n";
print <<DOC;
But if you have a lot of text to print,
you can use a "here document" and create
a literal string that runs until the
delimiter that was declared with <<.
DOC
print "..and now we're back to regular code.\n";
您可以在perldoc perlop中阅读有关此处的文档。
关于perl - 将Perl代码分成两行的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3984769/