我可以使用此处文档附加到字符串吗?

发布于 2024-10-28 00:25:28 字数 81 浏览 2 评论 0 原文

我有一个字符串,我想向其附加格式化的 SQL 块。有没有办法使用此处文档进行追加?或者我是否需要使用此处文档创建一个新字符串并将其附加到现有字符串?

I have a string that I want to append a block of formatted SQL to. Is there a way to append using a here document? Or do I need to create a new string using the here document and append that to the existing string?

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

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

发布评论

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

评论(3

多像笑话 2024-11-04 00:25:28

你可能会说,

    $str = <<EOH;
$str
more stuff here
EOH

但是 Perl 优化了它,

    $str .= <<EOH;
more stuff here
EOH

你不需要像 ; 那样设置一个单独的变量

    $str1 = <<EOH;
more stuff here
EOH
    $str .= $str1;

。即使在较旧的 Perls 中,前两个也应该可以正常工作(而对于非常旧的 Perl 5,在某些情况下不起作用,特别是 print < 会丢失此处文档的内容)。

You could probably say

    $str = <<EOH;
$str
more stuff here
EOH

but Perl optimizes that to

    $str .= <<EOH;
more stuff here
EOH

You should not need to set up a separate variable as in

    $str1 = <<EOH;
more stuff here
EOH
    $str .= $str1;

; even in older Perls the previous two should work fine (whereas with very old Perl 5 there were some cases that didn't work, notably print <<EOH; would lose the contents of the here document).

相思碎 2024-11-04 00:25:28

这里的文档非常灵活,可以在大多数使用普通字符串文字的情况下使用。它们可以用作函数调用或复合表达式的一部分。

my $inner_str = 'middle text';

my $full_str = <<FIRST_BLOCK . "$inner_str\n" . <<SECOND_BLOCK;
This text is at the start of the string.
And so is this.
FIRST_BLOCK
This text ends the first assignment to the string.
SECOND_BLOCK

Perl 用于解析heredoc 的规则允许一些非常奇怪的行为,当找到heredoc 时,将继续解析当前行,直到该行结束。然后perl将停止解析当前表达式并开始读取heredoc直到找到结束标记。一旦它找到在该行上开始的所有此处文档的结束标记,它将继续解析,就好像表达式中没有中断一样。

$full_str .= <<THIRD_BLOCK . "$inner_str
This text is "appended" to the string.
THIRD_BLOCK
" . <<LAST_BLOCK;
This text ends the string.
LAST_BLOCK

print $full_str;

请注意,该字符串在此处文档 THIRD_BLOCK 正文之前开始,并在此处文档 THIRD_BLOCK 之后结束,但不包含其内容。

虽然您可以非常灵活地使用heredoc语法,但我建议您每个表达式仅使用一个heredoc,并保持表达式简单。如果您需要在一个表达式中使用多个heredoc,或者需要在复杂表达式中使用一个heredoc,那么首先将heredoc分配给一个变量,然后在表达式中使用该变量。

Here docs are very flexible, and can be used in most situations you would use a normal string literal. They can be used as a part of a function call or compound expression.

my $inner_str = 'middle text';

my $full_str = <<FIRST_BLOCK . "$inner_str\n" . <<SECOND_BLOCK;
This text is at the start of the string.
And so is this.
FIRST_BLOCK
This text ends the first assignment to the string.
SECOND_BLOCK

The rules Perl use to parse the heredoc allow for some very strange behavior, when a heredoc is found the current line will continue to be parsed until that line ends. Then perl will stop parsing the current expression and start reading the heredoc until if finds the end token. Once it finds end tokens for all heredocs that were started on that line it will resume parsing as if there was not an interruption in the expression.

$full_str .= <<THIRD_BLOCK . "$inner_str
This text is "appended" to the string.
THIRD_BLOCK
" . <<LAST_BLOCK;
This text ends the string.
LAST_BLOCK

print $full_str;

Note that the string starts before the body of the heredoc THIRD_BLOCK, and ends after the heredoc THIRD_BLOCK, but doesn't include its contents.

While you can be very flexible with the heredoc syntax I recommend you only use one heredoc per expression, keep the expression simple. If you need multiple heredocs in one expression or need one in a complex expression then assign the heredoc to a variable first and use that variable in the expression.

忆梦 2024-11-04 00:25:28

是的,你可以。

$sql = "select something";
print <<END
Line 1
Line 2
Line 3
END
. $sql;

输出:

Line 1
Line 2
Line 3
select something

Yes, you can.

$sql = "select something";
print <<END
Line 1
Line 2
Line 3
END
. $sql;

outputs:

Line 1
Line 2
Line 3
select something
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文