File Coverage

blib/lib/Code/TidyAll/Plugin/PerlTidy.pm
Criterion Covered Total %
statement 27 27 100.0
branch 7 10 70.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 42 45 93.3


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   686 use warnings;
  1         3  
  1         35  
4 1     1   4  
  1         2  
  1         43  
5             use Capture::Tiny qw(capture_merged);
6 1     1   5 use Perl::Tidy;
  1         2  
  1         73  
7 1     1   6  
  1         2  
  1         128  
8             use Moo;
9 1     1   7  
  1         3  
  1         9  
10             extends 'Code::TidyAll::Plugin';
11              
12             our $VERSION = '0.81';
13              
14             my ( $self, $source ) = @_;
15              
16 6     6 1 21 # This bit of insanity is needed because if some other code calls
17             # Getopt::Long::Configure() to change some options, then everything can go
18             # to hell. Internally perltidy() tries to use Getopt::Long without
19             # resetting the configuration defaults, leading to very confusing
20             # errors. See https://rt.cpan.org/Ticket/Display.html?id=118558
21             Getopt::Long::ConfigDefaults();
22              
23 6         41 # perltidy reports errors in two different ways.
24             # Argument/profile errors are output and an error_flag is returned.
25             # Syntax errors are sent to errorfile or stderr, depending on the
26             # the setting of -se/-nse (aka --standard-error-output). These flags
27             # might be hidden in other bundles, e.g. -pbp. Be defensive and
28             # check both.
29             my ( $output, $error_flag, $errorfile, $stderr, $destination );
30              
31 6         129 # Add --encode-output-strings (-eos) for PT releases in 2022 and later to
32             # tell perltidy that we want encoded character strings returned. See
33             # https://github.com/houseabsolute/perl-code-tidyall/issues/84
34             # https://github.com/perltidy/perltidy/issues/83
35             my $argv = $self->argv;
36             $argv .= ' --encode-output-strings' if $Perl::Tidy::VERSION > 20220101;
37 6         22  
38 6 50       35 $output = capture_merged {
39             $error_flag = Perl::Tidy::perltidy(
40             argv => $self->argv,
41 6     6   8134 source => \$source,
42             destination => \$destination,
43             stderr => \$stderr,
44             errorfile => \$errorfile
45             );
46             };
47             die $stderr if $stderr;
48 6         184 die $errorfile if $errorfile;
49 6 100       493742 die $output if $error_flag;
50 5 100       35 print STDERR $output if defined($output);
51 4 50       19 return $destination;
52 4 50       25 }
53 4         38  
54             1;
55              
56             # ABSTRACT: Use perltidy with tidyall
57              
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             Code::TidyAll::Plugin::PerlTidy - Use perltidy with tidyall
66              
67             =head1 VERSION
68              
69             version 0.81
70              
71             =head1 SYNOPSIS
72              
73             # In configuration:
74              
75             ; Configure in-line
76             ;
77             [PerlTidy]
78             select = lib/**/*.pm
79             argv = --noll
80              
81             ; or refer to a .perltidyrc in the same directory
82             ;
83             [PerlTidy]
84             select = lib/**/*.pm
85             argv = --profile=$ROOT/.perltidyrc
86              
87             =head1 DESCRIPTION
88              
89             Runs L<perltidy>, a Perl tidier.
90              
91             =head1 INSTALLATION
92              
93             Install perltidy from CPAN.
94              
95             cpanm perltidy
96              
97             =head1 CONFIGURATION
98              
99             This plugin accepts the following configuration options:
100              
101             =head2 argv
102              
103             Arguments to pass to C<perltidy>.
104              
105             If you are using C<Perl::Tidy> version 20220101 or newer, than the
106             C<--encode-output-strings> flag will be appended to whatever you supply. In
107             this case, you should ensure that you are I<not> passing a
108             C<--character-encoding> (C<-enc>) or C<-utf8> flag to perltidy as well.
109              
110             =head1 SUPPORT
111              
112             Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
113              
114             =head1 SOURCE
115              
116             The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.
117              
118             =head1 AUTHORS
119              
120             =over 4
121              
122             =item *
123              
124             Jonathan Swartz <swartz@pobox.com>
125              
126             =item *
127              
128             Dave Rolsky <autarch@urth.org>
129              
130             =back
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2011 - 2022 by Jonathan Swartz.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             The full text of the license can be found in the
140             F<LICENSE> file included with this distribution.
141              
142             =cut