$ 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个字符。如何将一行代码分成两行而没有任何副作用?
如上所示,简单的↵或 \ 不起作用。
这样做的正确方法是什么?
答案 0 :(得分:42)
在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中阅读此处的文档。
答案 1 :(得分:11)
Perl Best Practices还有一件事:
断开长行:在运算符之前断开长表达式。 喜欢
push @steps, $step[-1]
+ $radial_velocity * $elapsed_time
+ $orbital_velocity * ($phrase + $phrase_shift)
- $test
; #like that
答案 2 :(得分:5)
这是因为你在一个字符串里面。您可以使用.
拆分字符串并连接为:
print "3\n";
print "hello, ".
"world\n";
答案 3 :(得分:1)
使用.
,字符串连接运算符:
$ perl
print "hello, " .
"world\n";ctrl-d
hello, world
$