Dist::Zilla::Plugin::PerlTidy - PerlTidy in Dist::Zilla


Dist-Zilla-Plugin-PerlTidy documentation Contained in the Dist-Zilla-Plugin-PerlTidy distribution.

Index


Code Index:

NAME

Top

Dist::Zilla::Plugin::PerlTidy - PerlTidy in Dist::Zilla

VERSION

Top

version 0.09

METHODS

Top

munge_file

Implements the required munge_file method for the Dist::Zilla::Role::FileMunger role, munging each Perl file it finds. Files whose names do not end in .pm, .pl, or .t, or whose contents do not begin with #!perl are left alone.

SYNOPSIS

    # dist.ini
    [PerlTidy]

    # or
    [PerlTidy]
    perltidyrc = xt/.perltidyrc

DEFAULTS

If you do not specify a specific perltidyrc in dist.ini it will try to use the same defaults as Perl::Tidy.

SEE ALSO

Perl::Tidy

AUTHORS

Top

  Fayland Lam <fayland@gmail.com>
  Mark Gardner <mjgardner@cpan.org>

COPYRIGHT AND LICENSE

Top


Dist-Zilla-Plugin-PerlTidy documentation Contained in the Dist-Zilla-Plugin-PerlTidy distribution.

package Dist::Zilla::Plugin::PerlTidy;

BEGIN {
    $Dist::Zilla::Plugin::PerlTidy::VERSION = '0.09';
}

# ABSTRACT: PerlTidy in Dist::Zilla

use Moose;
with 'Dist::Zilla::Role::FileMunger';

has 'perltidyrc' => ( is => 'ro' );

sub munge_file {
    my ( $self, $file ) = @_;

    return $self->_munge_perl($file) if $file->name    =~ /\.(?:pm|pl|t)$/i;
    return $self->_munge_perl($file) if $file->content =~ /^#!perl(?:$|\s)/;
    return;
}

sub _munge_perl {
    my ( $self, $file ) = @_;

    my $source = $file->content;

    my $perltidyrc;
    if ( defined $self->perltidyrc ) {
        if ( -r $self->perltidyrc ) {
            $perltidyrc = $self->perltidyrc;
        } else {
            $self->log_fatal(
                [ "specified perltidyrc is not readable: %s", $perltidyrc ] );
        }
    }

    # make Perl::Tidy happy
    local @ARGV = ();

    my $destination;
    require Perl::Tidy;
    Perl::Tidy::perltidy(
        source      => \$source,
        destination => \$destination,
        ( $perltidyrc ? ( perltidyrc => $perltidyrc ) : () ),
    );

    $file->content($destination);
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;

__END__