Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make perltidy work with Method::Signatures?

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.

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.

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

Related to this previous unanswered question and this cross post.

like image 504
David B Avatar asked Oct 09 '10 06:10

David B


3 Answers

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

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:

#!/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 $_ }
);
like image 118
JRideout Avatar answered Oct 18 '22 10:10

JRideout


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 )

like image 24
slick Avatar answered Oct 18 '22 08:10

slick


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.

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.

like image 32
rafl Avatar answered Oct 18 '22 09:10

rafl