在 Perl 中打印字符串

发布于 2024-10-01 00:44:37 字数 197 浏览 7 评论 0 原文

有没有一种简单的方法(也许使用子例程)在 Perl 中打印字符串而不转义每个特殊字符?

这就是我想做的:

print   DELIMITER <I don't care what is here> DELIMITER

所以显然,如果我可以将字符串而不是特殊字符作为分隔符,那就太好了。

Is there an easy way, using a subroutine maybe, to print a string in Perl without escaping every special character?

This is what I want to do:

print   DELIMITER <I don't care what is here> DELIMITER

So obviously it will great if I can put a string as a delimiter instead of special characters.

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

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

发布评论

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

评论(6

纵性 2024-10-08 00:44:37

perldoc perlop,在“引用和类引用运算符”下,包含所有内容你需要。

虽然我们通常将引号视为文字值,但在 Perl 中它们充当运算符,提供各种插值和模式匹配
能力。 Perl 为这些行为提供了惯用的引号字符,而且还提供了一种方法让您为任何以下行为选择引号字符:
他们。在下表中,“{}”代表您选择的任意一对分隔符。

习惯通用含义插值
    '' q{} 文字否
    "" qq{} 字面意思是
    `` qx{} 命令是*
            qw{} 单词表 否
    // m{} 模式匹配 yes*
            qr{} 模式 是*
             s{}{} 替换 是*
            tr{}{} 音译 no(但见下文)
    <


perldoc perlop, under "Quote and Quote-like Operators", contains everything you need.

While we usually think of quotes as literal values, in Perl they function as operators, providing various kinds of interpolating and pattern matching
capabilities. Perl provides customary quote characters for these behaviors, but also provides a way for you to choose your quote character for any of
them. In the following table, a "{}" represents any pair of delimiters you choose.

Customary  Generic        Meaning        Interpolates
    ''       q{}          Literal             no
    ""      qq{}          Literal             yes
    ``      qx{}          Command             yes*
            qw{}         Word list            no
    //       m{}       Pattern match          yes*
            qr{}          Pattern             yes*
             s{}{}      Substitution          yes*
            tr{}{}    Transliteration         no (but see below)
    <<EOF                 here-doc            yes*

    * unless the delimiter is ''.
歌枕肩 2024-10-08 00:44:37
$str = q(this is a "string");
print $str;

如果您的意思是带有“特殊字符”的引号和撇号

$str = q(this is a "string");
print $str;

if you mean quotes and apostrophes with 'special characters'

万水千山粽是情ミ 2024-10-08 00:44:37

您可以使用 __DATA__ 指令,它将把以下所有行视为可以从 DATA 句柄访问的文件:

while (<DATA>) {
   print # or do something else with the lines
}

__DATA__
#!/usr/bin/perl -w
use Some::Module;
....

或者您可以使用heredoc:

my $string = <<'END';  #single quotes prevent any interpolation
#!/usr/bin/perl -b
use Some::Module;
....
END

You can use the __DATA__ directive which will treat all of the following lines as a file that can be accessed from the DATA handle:

while (<DATA>) {
   print # or do something else with the lines
}

__DATA__
#!/usr/bin/perl -w
use Some::Module;
....

or you can use a heredoc:

my $string = <<'END';  #single quotes prevent any interpolation
#!/usr/bin/perl -b
use Some::Module;
....
END
GRAY°灰色天空 2024-10-08 00:44:37

打印并没有对转义符做特殊的事情,双引号字符串正在做这件事。您可能想尝试单引号字符串:

print 'this is \n', "\n";

在单引号字符串中,唯一必须转义的字符是单引号和紧接在字符串末尾之前出现的反斜杠(即 'foo\\' )。

需要注意的是,插值不适用于单引号字符串,因此

print 'foo is $foo', "\n";

不会打印 $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:

print 'this is \n', "\n";

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

print 'foo is $foo', "\n";

Will not print the contents of $foo.

梦醒时光 2024-10-08 00:44:37

您几乎可以通过 qqq 使用任何您想要的字符。例如:

#!/usr/bin/perl

use utf8;
use strict; use warnings;

print q∞This is a test∞;
print qq☼\nThis is another test\n☼;
print q»But, what is the point?»;
print qq\nYou are just making life hard on yourself!\n;
print qq¿That last one is tricky\n¿;

您不能使用 qq DELIMITER foo DELIMITER。但是,您可以使用heredocs来达到类似的效果:

print <<DELIMITER
...
DELIMETER
;

或者

print <<'DELIMETER'
...
DELIMETER
;

您的源代码将非常难看。

You can pretty much use any character you want with q or qq. For example:

#!/usr/bin/perl

use utf8;
use strict; use warnings;

print q∞This is a test∞;
print qq☼\nThis is another test\n☼;
print q»But, what is the point?»;
print qq\nYou are just making life hard on yourself!\n;
print qq¿That last one is tricky\n¿;

You cannot use qq DELIMITER foo DELIMITER. However, you could use heredocs for a similar effect:

print <<DELIMITER
...
DELIMETER
;

or

print <<'DELIMETER'
...
DELIMETER
;

but your source code would be really ugly.

断舍离 2024-10-08 00:44:37

如果你想按字面打印字符串,并且你有 Perl 5.10 或更高版本,那么

say 'This is a string with "quotes"' ;

将用换行符打印字符串。重要的是使用单引号 ' ' 而不是双引号 " ”

If you want to print a string literally and you have Perl 5.10 or later then

say 'This is a string with "quotes"' ;

will print the string with a newline.. The importaning thing is to use single quotes ' ' rather than double ones " "

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