在 Perl 中打印字符串
有没有一种简单的方法(也许使用子例程)在 Perl 中打印字符串而不转义每个特殊字符?
这就是我想做的:
print DELIMITER <I don't care what is here> DELIMITER
所以显然,如果我可以将字符串而不是特殊字符作为分隔符,那就太好了。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
perldoc perlop,在“引用和类引用运算符”下,包含所有内容你需要。
perldoc perlop, under "Quote and Quote-like Operators", contains everything you need.
如果您的意思是带有“特殊字符”的引号和撇号
if you mean quotes and apostrophes with 'special characters'
您可以使用 __DATA__ 指令,它将把以下所有行视为可以从 DATA 句柄访问的文件:
或者您可以使用heredoc:
You can use the
__DATA__
directive which will treat all of the following lines as a file that can be accessed from theDATA
handle:or you can use a heredoc:
打印并没有对转义符做特殊的事情,双引号字符串正在做这件事。您可能想尝试单引号字符串:
在单引号字符串中,唯一必须转义的字符是单引号和紧接在字符串末尾之前出现的反斜杠(即
'foo\\'
)。需要注意的是,插值不适用于单引号字符串,因此
不会打印
$foo
的内容。The printing is not doing special things to the escapes, double quoted strings are doing it. You may want to try single quoted strings:
In a single quoted string the only characters that must be escaped are single quotes and a backslash that occurs immediately before the end of the string (i.e.
'foo\\'
).It is important to note that interpolation does not work with single quoted strings, so
Will not print the contents of
$foo
.您几乎可以通过
q
或qq
使用任何您想要的字符。例如:您不能使用
qq DELIMITER foo DELIMITER
。但是,您可以使用heredocs来达到类似的效果:或者
您的源代码将非常难看。
You can pretty much use any character you want with
q
orqq
. For example:You cannot use
qq DELIMITER foo DELIMITER
. However, you could use heredocs for a similar effect:or
but your source code would be really ugly.
如果你想按字面打印字符串,并且你有 Perl 5.10 或更高版本,那么
将用换行符打印字符串。重要的是使用单引号
' '
而不是双引号" ”
If you want to print a string literally and you have Perl 5.10 or later then
will print the string with a newline.. The importaning thing is to use single quotes
' '
rather than double ones" "