-
-
Save edlitmus/d33f6ce8d8c9a435fdd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package JS::Perl::Tidy; | |
use Perl::Tidy qw(); | |
use strict; | |
use warnings; | |
sub perltidy { | |
return Perl::Tidy::perltidy( prefilter => \&prefilter, postfilter => \&postfilter, @_ ); | |
} | |
sub prefilter { | |
$_ = $_[0]; | |
# Turn method into sub | |
s/^method (.*)/sub $1 \#__METHOD/gm; | |
# Hide 'has' lines from perltidy | |
s/^(has .*)/\# __NO_TIDY $1/gm; | |
return $_; | |
} | |
sub postfilter { | |
$_ = $_[0]; | |
# Remove hidden lines | |
s/^\# __NO_TIDY //gm; | |
# Turn sub back into method | |
s/^sub (.*?)\s* \#__METHOD/method $1/gm; | |
# Add empty parens | |
s/^method(\s*\w+\s*)\{/method$1 \(\) \{/gm; | |
# One arg, no spaces inside paren | |
s/^method (\w+) \(\s*([\$\@\%]\w+)\s*\)/method $1 \($2\)/gm; | |
# Space between method name and paren | |
s/^method (\w+)\(/method $1 \(/gm; | |
return $_; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment