perltidy格式化多行

时间:2013-07-18 21:13:59

标签: perl code-formatting perl-tidy

我正试图让perltidy格式化if语句,如下所示:

if ($self->image eq $_->[1]
        and $self->extension eq $_->[2]
        and $self->location  eq $_->[3]
        and $self->modified  eq $_->[4]
        and $self->accessed  eq $_->[5]) {

但无论我尝试什么,它都坚持像这样格式化:

if (    $self->image eq $_->[1]
    and $self->extension eq $_->[2]
    and $self->location  eq $_->[3]
    and $self->modified  eq $_->[4]
    and $self->accessed  eq $_->[5]) {

另外,有没有办法获得这个块的最后一行:

$dbh->do("INSERT INTO image VALUES(NULL, "
    . $dbh->quote($self->image) . ", "
    . $dbh->quote($self->extension) . ", "
    . $dbh->quote($self->location) . ","
    . $dbh->quote($self->modified) . ","
    . $dbh->quote($self->accessed)
    . ")");

像其他行一样跳到上一行:

$dbh->do("INSERT INTO image VALUES(NULL, "
    . $dbh->quote($self->image) . ", "
    . $dbh->quote($self->extension) . ", "
    . $dbh->quote($self->location) . ","
    . $dbh->quote($self->modified) . ","
    . $dbh->quote($self->accessed) . ")");

以下是我目前正在做的事情:

perltidy -ce -et=4 -l=100 -pt=2 -msc=1 -bar -ci=0 reporter.pm

感谢。

1 个答案:

答案 0 :(得分:1)

我在第一个问题上没什么可提供的,但是在第二个问题上,您是否考虑过重构它以使用占位符?它可能会更好地格式化,自动为您做引用并为您(以及您的模块的用户)提供一个健康的屏障来防止SQL注入问题。

my $sth = $dbh->prepare('INSERT INTO image VALUES(NULL, ?, ?, ?, ?, ?)');
$sth->execute(
    $self->image,    $self->extension, $self->location,
    $self->modified, $self->accessed
);

<小时/> 我还发现格式跳过:-fs来保护特定的代码段不被perltidy。我在这里举了一个例子,但该网站似乎在它上面做了一个简单的工作......