如何在 Perl 中引用长字符串? [英] How can I quote a long string in Perl?

查看:46
本文介绍了如何在 Perl 中引用长字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用简单的引号,但有时我会得到很长的行,我无法打破并且还需要使用转义字符,所以我得到了这样的东西:

I usually use simple quotes, but sometime I get very long lines which I can't break and also need to use escape characters, so I get something like this:

my $str = "select query_accession,query_tag,hit_accession,hit_tag,significance from summaryTables where query_id = \'$query_id\';"

我知道在 Perl 中有多种其他方式来表示字符串.你会推荐什么?

I know there are various other ways for representing strings in Perl. What would you recommend?

更新感谢大家提供与 SQL 相关的建议.我学到了一些有价值的东西,但是,我的问题仍然存在(作为一般问题,不管 SQL 是什么):是否有一些运算符允许引用而不捕获换行符?

UPDATE Thank you all guys for the SQL-related suggestions. I learn some valuable stuff, but, my question remains (as a general one, regardless of SQL): is there some operator that allows quoting without catching line breaks?

我现在做的事情是这样的:

what I do now is something like:

my $str = "123 123 456 sdndfnd sdfdmd " .
 "dfsdjkfs 343489 dfjsdj 3 34kdfsk kd " .
 "fd kd9534 rfg 546 5";

太丑了.

推荐答案

没有.Perl 5 的所有字符串创建方法都知道并包含换行符.您可以像在问题中一样使用连接运算符,也可以抽象出解决问题所需的代码:

No. All of Perl 5's string creation methods know about and include newlines. You can either use the concatenation operator as you did in your question, or abstract away the code needed to fix the problem:

#!/usr/bin/perl

use strict;
use warnings;

sub single_line {
    my @strings = @_;
    for (@strings) {
        s/\s+/ /g;
        s/^\s+|\s+$//g;
    }
    return wantarray ? @strings : $strings[0];
}


my $sql = single_line "
    select query_accession,query_tag,hit_accession,hit_tag,significance
    from summaryTables
    where query_id = ?;
";

print "[$sql]\n";

这篇关于如何在 Perl 中引用长字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆