From: Dave Rolsky Date: Wed, 15 Sep 2010 02:04:56 +0000 (-0500) Subject: Use Perl::Tidy to tidy dump of generate source code if we can. X-Git-Tag: 1.09~49 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d2d9edc060207e798deae747ae9c57abca6fc2b0;p=gitmo%2FClass-MOP.git Use Perl::Tidy to tidy dump of generate source code if we can. Also check source dumping env var each time we generate a sub. This makes it possible to turn it on selectively, at the cost of a small speed hit (which I really doubt matters next to the cost of eval'ing a block of code!) --- diff --git a/lib/Class/MOP/Method/Generated.pm b/lib/Class/MOP/Method/Generated.pm index 903cc5a..9aebcbc 100644 --- a/lib/Class/MOP/Method/Generated.pm +++ b/lib/Class/MOP/Method/Generated.pm @@ -12,8 +12,6 @@ our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Method'; -use constant _PRINT_SOURCE => $ENV{MOP_PRINT_SOURCE} ? 1 : 0; - ## accessors sub new { @@ -29,8 +27,7 @@ sub _initialize_body { } sub _eval_closure { - # my ($self, $captures, $sub_body) = @_; - my $__captures = $_[1]; + my ($self, $__captures, $sub_body) = @_; my $code; @@ -49,8 +46,10 @@ sub _eval_closure { . $_ . q['}};]; } keys %$__captures ), - $_[2]; - print STDERR "\n", $_[0]->name, ":\n", $source, "\n" if _PRINT_SOURCE; + $sub_body; + + $self->_dump_source($source) if $ENV{MOP_PRINT_SOURCE}; + $code = eval $source; $@; }; @@ -58,6 +57,36 @@ sub _eval_closure { return ( $code, $e ); } +sub _dump_source { + my ( $self, $source ) = @_; + + my $output; + if ( eval { require Perl::Tidy } ) { + require File::Spec; + + my $rc_file = File::Spec->catfile( + $INC{'Class/MOP/Method/Generated.pm'}, + ('..') x 5, + 'perltidyrc' + ); + warn $rc_file; + + my %p = ( + source => \$source, + destination => \$output, + ); + $p{perltidyrc} = $rc_file + if -f $rc_file; + + Perl::Tidy::perltidy(%p); + } + else { + $output = $source; + } + + print STDERR "\n", $self->name, ":\n", $output, "\n"; +} + sub _add_line_directive { my ( $self, %args ) = @_; diff --git a/perltidyrc b/perltidyrc new file mode 100644 index 0000000..3b82ac9 --- /dev/null +++ b/perltidyrc @@ -0,0 +1,20 @@ +-l=78 +-i=4 +-ci=4 +-se +-b +-bar +-boc +-vt=0 +-vtc=0 +-cti=0 +-pt=1 +-bt=1 +-sbt=1 +-bbt=1 +-nolq +-npro +-nsfs +--opening-hash-brace-right +--no-outdent-long-comments +-wbb="% + - * / x != == >= <= =~ !~ < > | & >= < = **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="