将一行 Perl 代码分成两行的正确方法是什么?

发布于 2024-09-28 02:36:08 字数 394 浏览 0 评论 0原文

$ 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 个字符。如何将一行代码分成两行而不产生任何副作用?

如上所示,简单的 \ 不起作用。

执行此操作的正确方法是什么?

$ 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
$

To make my code easily readable, I want to restrict the number of columns to 80 characters. How can I break a line of code into two without any side effects?

As shown above, a simple or \ does not work.

What is the correct way to do this?

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

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

发布评论

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

评论(4

邮友 2024-10-05 02:36:08

在 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

In Perl, a carriage return will serve in any place where a regular space does. Backslashes are not used like in some languages; just add a CR.

You can break strings up over multiple lines with concatenation or list operations:

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";

You can read about here documents in in perldoc perlop.

情定在深秋 2024-10-05 02:36:08

Perl 最佳实践中的另一件事:

打破长行:在运算符之前打破长表达式。< /强>
喜欢

push @steps, $step[-1]
                  + $radial_velocity * $elapsed_time
                  + $orbital_velocity * ($phrase + $phrase_shift)
                  - $test
                  ; #like that

One more thing from Perl Best Practices:

Breaking Long lines : Break long expressions before an operator.
like

push @steps, $step[-1]
                  + $radial_velocity * $elapsed_time
                  + $orbital_velocity * ($phrase + $phrase_shift)
                  - $test
                  ; #like that
帅冕 2024-10-05 02:36:08

这是因为您位于字符串内部。您可以使用 . 拆分字符串并连接,如下所示:

print "3\n";
print "hello, ".
"world\n";

This is because you are inside a string. You can split the strings and concatenate using . as:

print "3\n";
print "hello, ".
"world\n";
绝不服输 2024-10-05 02:36:08

使用字符串连接运算符 .

$ perl
print "hello, " .
"world\n";ctrl-d
hello, world
$

Use ., the string concatenation operator:

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