Perltidy 自动格式化 hashref 作为参数

标签 perl autoformatting perl-tidy

我有以下代码片段:

my $obj = $class->new({ 
    schema    => $schema,
    reminder  => $reminder,
    action    => $action,
    dt        => $dt,     
});

我的问题是, perltidy 试图将其格式化为某些内容,如下所示:
my $obj = $class->new(
    {   schema   => $schema,
        reminder => $reminder,
        action   => $action,
        dt       => $dt,
    }
);

我不喜欢花括号的位置。我可以以某种方式配置 perltidy 以像第一个示例一样对其进行格式化吗? (跳过块的格式不是一个选项。我想将每个更长的 hashref 格式化为该格式,因此它更紧凑和可读)

到目前为止,我的 perltidyrc:
-l=79  # Max line width is 78 cols
-i=4   # Indent level is 4 cols
-ci=4  # Continuation indent is 4 cols
-st    # Output to STDOUT
-se    # Errors to STDERR
-vt=2  # Maximal vertical tightness
-cti=0 # No extra indentation for closing brackets
-pt=1  # Medium parenthesis tightness
-bt=1  # Medium brace tightness
-sbt=1 # Medium square bracket tightness
-bbt=1 # Medium block brace tightness
-nsfs  # No space before semicolons
-nolq  # Don't outdent long quoted strings

如果我删除“{}”并将参数作为列表传递,顺便说一句,它会做正确的事情。但我必须通过一个 hashref。

或者你能推荐一种合理的方式来格式化这样的代码吗?

最佳答案

以下似乎解决了上述问题并对我有用:

# perltidy configuration file created Thu Sep 24 15:54:07 2015
# using: -

# I/O control
--standard-error-output                 # -se
--nostandard-output                     # -nst

# Basic formatting options
--indent-columns=4                      # -i=4  [=default]
--maximum-line-length=140               # -l=140

# Code indentation control
--closing-brace-indentation=0           # -cbi=0  [=default]
--closing-paren-indentation=0           # -cpi=0  [=default]
--closing-square-bracket-indentation=0  # -csbi=0  [=default]
--continuation-indentation=4            # -ci=4
--nooutdent-labels                      # -nola
--nooutdent-long-quotes                 # -nolq

# Whitespace control
--block-brace-tightness=1               # -bbt=1
--brace-tightness=1                     # -bt=1  [=default]
--paren-tightness=2                     # -pt=2
--nospace-for-semicolon                 # -nsfs
--square-bracket-tightness=1            # -sbt=1  [=default]
--square-bracket-vertical-tightness=0   # -sbvt=0  [=default]

# Comment controls
--ignore-side-comment-lengths           # -iscl
--minimum-space-to-comment=2            # -msc=2
--static-side-comment-prefix="#"        # -sscp="#"
--static-side-comments                  # -ssc

# Linebreak controls
--brace-vertical-tightness=0            # -bvt=0  [=default]
--paren-vertical-tightness=0            # -pvt=0  [=default]
--stack-closing-hash-brace              # -schb
--stack-closing-paren                   # -scp
--stack-closing-square-bracket          # -scsb
--stack-opening-hash-brace              # -sohb
--stack-opening-paren                   # -sop
--stack-opening-square-bracket          # -sosb
--want-break-before="% + - * / x != == >= <= =~ < > | & **= += *= &= <<= &&= -= /= |= + >>= ||= .= %= ^= x="  # -wbb="% + - * / x != == >= <= =~ < > | & **= += *= &= <<= &&= -= /= |= + >>= ||= .= %= ^= x="

# Blank line control
--noblanks-before-comments              # -nbbc

关于Perltidy 自动格式化 hashref 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920028/

相关文章:

regex - 如何根据真实数据自动创建模式?

windows - 调用 ssh2 命令的 Perl 脚本无法在 Windows 任务计划程序中取回输出

c# - Visual Studio 2010 自动格式化

php - 粘贴时如何禁用 Eclipse PDT 自动格式化代码

perltidy 格式复杂的 if

perl - 说服 Perltidy 不理会空格

regex - 以模式排列给定的线

perl - 这个 zip 命令是如何在 perl 中使用的?

python - 我如何使用python(pywin32)更改word中表格的大小