如何使用Method :: Signatures使Perltidy工作?

时间:2022-11-29 23:21:04

I'm using Eclipse combined with EPIC to write my Perl code. I configured EPIC to use Perltidy with "-pbp" (perl best practices style) to format my code.

我正在使用Eclipse与EPIC结合来编写我的Perl代码。我将EPIC配置为使用Perltidy和“-pbp”(perl最佳实践样式)来格式化我的代码。

This doesn't work well when using Method::Signatures' named parameters. E.g., func (:$arg1, : $arg2) is formatted as func (: $arg1, : $arg2) which yields an error.

使用Method :: Signatures的命名参数时,这不起作用。例如,func(:$ arg1,:$ arg2)被格式化为func(:$ arg1,:$ arg2),这会产生错误。

Also, func keyword is not recognized like sub so indentation is wrong.

此外,func关键字不被识别为sub,因此缩进是错误的。

Related to this previous unanswered question and this cross post.

与此前未回答的问题和此交叉帖相关。

4 个解决方案

#1


4  

You can modify the perlcritic script with a pre and post filter. The changelog provides the following example

您可以使用前置和后置过滤器修改perlcritic脚本。更改日志提供以下示例

Perl::Tidy::perltidy(
  prefilter => sub { $_ = $_[0]; s/^method (.*)/sub $1 \#__METHOD/gm; return $_ },
  postfilter => sub { $_ = $_[0]; s/^sub (.*?)\s* \#__METHOD/method $1/gm; return $_ }
);

Perlcritic will now treat method as a sub for purposes of formatting. We can do the same with func. I modified my /usr/local/bin/perlcritic to work with func as follows:

Perlcritic现在将方法视为子格式以进行格式化。我们可以用func做同样的事情。我修改了我的/ usr / local / bin / perlcritic以使用func,如下所示:

#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
package main;

use Perl::Tidy;

my $arg_string = undef;

# give Macs a chance to provide command line parameters
if ($^O =~ /Mac/) {
    $arg_string =
      MacPerl::Ask( 'Please enter @ARGV (-h for help)',
        defined $ARGV[0] ? "\"$ARGV[0]\"" : "" );
}

Perl::Tidy::perltidy(
    argv => $arg_string,
    prefilter => sub { $_ = $_[0]; s/\W\Kfunc\((.*)/sub($1 \#__FUNC/gm; return $_ },
    postfilter => sub { $_ = $_[0]; s/\W\Ksub\((.*?)\s* \#__FUNC/func($1/gm; return $_ }
);

#2


3  

Perl::Tidy/perltidy does not make use of PPI, it predates PPI by about 9 years ( http://sourceforge.net/projects/perltidy/ says Registered: 2000-12-23 )

Perl :: Tidy / perltidy没有使用PPI,它比PPI早了大约9年(http://sourceforge.net/projects/perltidy/说注册:2000-12-23)

#3


2  

You can't, unless you make PPI, which is what Perltidy uses for most of its work, aware of the various signature modules such as MooseX::Method::Signatures, Method::Signatures::Simple, or Method::Signatures.

你不能,除非你制作PPI,这是Perltidy在其大部分工作中使用的,知道各种签名模块,如MooseX :: Method :: Signatures,Method :: Signatures :: Simple或Method :: Signatures 。

A reasonable workaround might be to not run Perltidy on all of your code, but only on the chunks of it you've just written and want formatted in some way. That way you can easily skip running it on any method signatures and have it process only the method bodies instead.

一个合理的解决方法可能是不要在你的所有代码上运行Perltidy,而只是在你刚编写的那些块上运行并希望以某种方式进行格式化。这样,您可以轻松地在任何方法签名上跳过它,并让它只处理方法体。

#4


1  

In the meantime a new module exists on CPAN which solves this problems. It is called Perl::Tidy::Sweetened and offers the script perltidier.

与此同时,CPAN上存在一个新模块,可以解决这个问题。它被称为Perl :: Tidy :: Sweetened并提供脚本perltidier。

It also uses the prefilter and postfilter hooks of Perl::Tidy, but you do not need to take care about that on your own.

它还使用了Perl :: Tidy的预过滤器和后过滤器钩子,但您不需要自己照顾它。

#1


4  

You can modify the perlcritic script with a pre and post filter. The changelog provides the following example

您可以使用前置和后置过滤器修改perlcritic脚本。更改日志提供以下示例

Perl::Tidy::perltidy(
  prefilter => sub { $_ = $_[0]; s/^method (.*)/sub $1 \#__METHOD/gm; return $_ },
  postfilter => sub { $_ = $_[0]; s/^sub (.*?)\s* \#__METHOD/method $1/gm; return $_ }
);

Perlcritic will now treat method as a sub for purposes of formatting. We can do the same with func. I modified my /usr/local/bin/perlcritic to work with func as follows:

Perlcritic现在将方法视为子格式以进行格式化。我们可以用func做同样的事情。我修改了我的/ usr / local / bin / perlcritic以使用func,如下所示:

#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
package main;

use Perl::Tidy;

my $arg_string = undef;

# give Macs a chance to provide command line parameters
if ($^O =~ /Mac/) {
    $arg_string =
      MacPerl::Ask( 'Please enter @ARGV (-h for help)',
        defined $ARGV[0] ? "\"$ARGV[0]\"" : "" );
}

Perl::Tidy::perltidy(
    argv => $arg_string,
    prefilter => sub { $_ = $_[0]; s/\W\Kfunc\((.*)/sub($1 \#__FUNC/gm; return $_ },
    postfilter => sub { $_ = $_[0]; s/\W\Ksub\((.*?)\s* \#__FUNC/func($1/gm; return $_ }
);

#2


3  

Perl::Tidy/perltidy does not make use of PPI, it predates PPI by about 9 years ( http://sourceforge.net/projects/perltidy/ says Registered: 2000-12-23 )

Perl :: Tidy / perltidy没有使用PPI,它比PPI早了大约9年(http://sourceforge.net/projects/perltidy/说注册:2000-12-23)

#3


2  

You can't, unless you make PPI, which is what Perltidy uses for most of its work, aware of the various signature modules such as MooseX::Method::Signatures, Method::Signatures::Simple, or Method::Signatures.

你不能,除非你制作PPI,这是Perltidy在其大部分工作中使用的,知道各种签名模块,如MooseX :: Method :: Signatures,Method :: Signatures :: Simple或Method :: Signatures 。

A reasonable workaround might be to not run Perltidy on all of your code, but only on the chunks of it you've just written and want formatted in some way. That way you can easily skip running it on any method signatures and have it process only the method bodies instead.

一个合理的解决方法可能是不要在你的所有代码上运行Perltidy,而只是在你刚编写的那些块上运行并希望以某种方式进行格式化。这样,您可以轻松地在任何方法签名上跳过它,并让它只处理方法体。

#4


1  

In the meantime a new module exists on CPAN which solves this problems. It is called Perl::Tidy::Sweetened and offers the script perltidier.

与此同时,CPAN上存在一个新模块,可以解决这个问题。它被称为Perl :: Tidy :: Sweetened并提供脚本perltidier。

It also uses the prefilter and postfilter hooks of Perl::Tidy, but you do not need to take care about that on your own.

它还使用了Perl :: Tidy的预过滤器和后过滤器钩子,但您不需要自己照顾它。