Tidy.pm

filter: Statementstmt 62.2% 1580 / 2540 Branchbran 40.8% 462 / 1132 Conditioncond 35.0% 125 / 357 MC/DCmcdc 11.8% 25 / 211 Subroutinesub 65.8% 83 / 126 Podpod 0.0% 0 / 62 totaltotal 51.3% 2275 / 4428 CC 732 SCAR 54.7
CC
732
Cov
54%
perltidy
912.4
compare_string_buffers
650.0
find_config_file
650.0
CRAP 236.7
« Tidy/HtmlWriter.pm ↑ Summary Tidy/Tokenizer.pm »
1 package Perl::Tidy;
2  
3 ###########################################################
4 #
5 # perltidy - a perl script indenter and formatter
6 #
7 # Copyright (c) 2000-2026 by Steve Hancock
8 # Distributed under the GPL license agreement; see file COPYING
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #
24 # For brief instructions, try 'perltidy -h'.
25 # For more complete documentation, try 'man perltidy'
26 # or visit the GitHub site https://perltidy.github.io/perltidy/
27 #
28 # This script is an example of the default style. It was formatted with:
29 #
30 # perltidy Tidy.pm
31 #
32 # Code Contributions: See ChangeLog.html for a complete history.
33 # Michael Cartmell supplied code for adaptation to VMS and helped with
34 # v-strings.
35 # Hugh S. Myers supplied sub streamhandle and the supporting code to
36 # create a Perl::Tidy module which can operate on strings, arrays, etc.
37 # Yves Orton supplied coding to help detect Windows versions.
38 # Axel Rose supplied a patch for MacPerl.
39 # Sebastien Aperghis-Tramoni supplied a patch for the defined or operator.
40 # Dan Tyrell contributed a patch for binary I/O.
41 # Ueli Hugenschmidt contributed a patch for -fpsc
42 # Sam Kington supplied a patch to identify the initial indentation of
43 # entabbed code.
44 # jonathan swartz supplied patches for:
45 # * .../ pattern, which looks upwards from directory
46 # * --notidy, to be used in directories where we want to avoid
47 # accidentally tidying
48 # * prefilter and postfilter
49 # * iterations option
50 #
51 # Many others have supplied key ideas, suggestions, and bug reports;
52 # see the CHANGES file.
53 #
54 ############################################################
55  
56 # perlver reports minimum version needed is 5.8.1
57 # 5.004 needed for IO::File
58 # 5.008 needed for wide characters
59 # 5.008001 needed for utf8::is_utf8
60 # 5.008001 needed for Scalar::Util::refaddr
61 45 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 45 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 45 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 45 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
67 45 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 45 ▶use Perl::Tidy::Debugger;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 45 ▶use Perl::Tidy::Diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 45 ▶use Perl::Tidy::FileWriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 45 ▶use Perl::Tidy::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 45 ▶use Perl::Tidy::HtmlWriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 45 ▶use Perl::Tidy::IOScalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 45 ▶use Perl::Tidy::IOScalarArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 45 ▶use Perl::Tidy::IndentationItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 45 ▶use Perl::Tidy::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 45 ▶use Perl::Tidy::Tokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 45 ▶use Perl::Tidy::VerticalAligner;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 45 local $OUTPUT_AUTOFLUSH = 1;
80  
81 # DEVEL_MODE can be turned on for extra checking during development
82 45 ▶use constant DEVEL_MODE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 45 ▶use constant DIAGNOSTICS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 45 ▶use constant EMPTY_STRING => q{};
Subroutine BEGIN: called CC=1 SCAR=0.0
85 45 ▶use constant SPACE => q{ };
Subroutine BEGIN: called CC=1 SCAR=0.0
86 45 ▶use constant CONST_1024 => 1024; # bytes per kb; 2**10
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 45 ▶use vars qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
89 $VERSION
90 @ISA
91 @EXPORT
92 45 };
93  
94 45 @ISA = qw( Exporter );
95 45 @EXPORT = qw( &perltidy );
96  
97 45 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 45 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
99 45 ▶use Encode::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 45 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 45 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 45 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 45 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 # perl stat function index names, based on
106 # https://perldoc.perl.org/functions/stat
107 45 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 45 _mode_ => 2, # file mode (type and permissions)
110 _uid_ => 4, # numeric user ID of file's owner
111 _gid_ => 5, # numeric group ID of file's owner
112 _atime_ => 8, # last access time in seconds since the epoch
113 _mtime_ => 9, # last modify time in seconds since the epoch
114  
115 ## _dev_ => 0, # device number of filesystem
116 ## _ino_ => 1, # inode number
117 ## _nlink_ => 3, # number of (hard) links to the file
118 ## _rdev_ => 6, # the device identifier (special files only)
119 ## _size_ => 7, # total size of file, in bytes
120 ## _ctime_ => 10, # inode change time in seconds since the epoch (*)
121 ## _blksize_ => 11, # preferred I/O size in bytes for interacting with
122 ## # the file (may vary from file to file)
123 ## _blocks_ => 12, # actual number of system-specific blocks allocated
124 ## # on disk (often, but not always, 512 bytes each)
125 45 };
126  
127 45 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 # Release version is the approximate YYYYMMDD of the release.
130 # Development version is (Last Release).(Development Number)
131  
132 # To make the number continually increasing, the Development Number is a 2
133 # digit number starting at 01 after a release. It is continually bumped
134 # along at significant points during development. If it ever reaches 99
135 # then the Release version must be bumped, and it is probably past time for
136 # a release anyway.
137  
138 45 $VERSION = '20260826';
139 } ## end BEGIN
140  
141 {
142 # List of hash keys to prevent -duk from listing them.
143 45 my @unique_hash_keys_uu = qw(
144 *
145 html-src-extension
146 html-toc-extension
147 allow_module_path
148 );
149 }
150  
151 664 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
152 664 my $self = shift;
153  
154 # required to avoid call to AUTOLOAD in some versions of perl
155 664 return;
156 } ## end sub DESTROY
157  
158 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
159  
160 # Catch any undefined sub calls so that we are sure to get
161 # some diagnostic information. This sub should never be called
162 # except for a programming error.
163 0 our $AUTOLOAD;
164 0 ▶ return if ( $AUTOLOAD =~ /\bDESTROY$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::Tidy::AUTOLOAD =~ /\bDESTROY$/ 0 0 0
165 0 my ( $pkg, $fname, $lno ) = caller();
166 0 print {*STDERR} <<EOM;
167 ======================================================================
168 Unexpected call to Autoload looking for sub $AUTOLOAD
169 Called from package: '$pkg'
170 Called from File '$fname' at line '$lno'
171 This error is probably due to a recent programming change
172 ======================================================================
173 EOM
174 0 exit 1;
175 } ## end sub AUTOLOAD
176  
177 653 ▶sub streamhandle {
Subroutine streamhandle: called CC=16 SCAR=47.3
Pod undocumented
178  
179 653 my ( $filename, $mode, ($is_encoded_data) ) = @_;
180  
181 # Given:
182 # $filename
183 # $mode = 'r' or 'w' (only 'w' is used now, see note below)
184 # $is_encoded_data (optional flag)
185  
186 # Create an object which:
187 # has a 'getline' method if mode='r', and
188 # has a 'print' method if mode='w'.
189 # The objects also need a 'close' method.
190 #
191 # How the object is made:
192 #
193 # if $filename is: Make object using:
194 # ---------------- -----------------
195 # '-' (STDIN if mode = 'r', STDOUT if mode='w')
196 # string IO::File
197 # ARRAY ref Perl::Tidy::IOScalarArray (formerly IO::ScalarArray)
198 # STRING ref Perl::Tidy::IOScalar (formerly IO::Scalar)
199 # object object
200 # (check for 'print' method for 'w' mode)
201 # (check for 'getline' method for 'r' mode)
202  
203 # An optional flag '$is_encoded_data' may be given, as follows:
204 # - true: encoded data is being transferred,
205 # set encoding to be utf8 for files and for stdin.
206 # - false: unencoded binary data is being transferred,
207 # set binary mode for files and for stdin.
208  
209 # NOTE: mode 'r' works but is no longer used.
210 # Use sub stream_slurp instead for mode 'r', for efficiency.
211 653 $mode = lc($mode);
212 653 ▶ if ( $mode ne 'w' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "w") 0 653 653
213 0 ▶ if ( DEVEL_MODE || $mode ne 'r' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne "r") 0 0 0
214 0 Fault("streamhandle called in unexpected mode '$mode'\n");
215 }
216 }
217  
218 653 my $ref = ref($filename);
219 653 my $New;
220 my $fh;
221  
222 #-------------------
223 # handle a reference
224 #-------------------
225 653 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 653 0 653
226 653 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 653 653
elsif ($ref eq 'SCALAR') { } 653 0 653
227 0 ▶ $New = sub { Perl::Tidy::IOScalarArray->new( $filename, $mode ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
228 }
229 elsif ( $ref eq 'SCALAR' ) {
230 653 ▶ $New = sub { Perl::Tidy::IOScalar->new( $filename, $mode ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
231 }
232 else {
233  
234 # Accept an object with a getline method for reading. Note:
235 # IO::File is built-in and does not respond to the defined
236 # operator. If this causes trouble, the check can be
237 # skipped and we can just let it crash if there is no
238 # getline.
239 0 ▶ if ( $mode eq 'r' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "r") 0 0 0
240  
241 # RT#97159; part 1 of 2: updated to use 'can'
242 0 ▶ if ( $ref->can('getline') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->can('getline')) { } 0 0 0
243 0 ▶ $New = sub { $filename };
Subroutine __ANON__: not called CC=1 SCAR=6.9
244 }
245 else {
246 0 ▶ $New = sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 0 confess <<EOM;
248 ------------------------------------------------------------------------
249 No 'getline' method is defined for object of class '$ref'
250 Please check your call to Perl::Tidy::perltidy. Trace follows.
251 ------------------------------------------------------------------------
252 EOM
253 }
254 }
255  
256 # Accept an object with a print method for writing.
257 # See note above about IO::File
258 0 ▶ if ( $mode eq 'w' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "w") 0 0 0
259  
260 # RT#97159; part 2 of 2: updated to use 'can'
261 0 ▶ if ( $ref->can('print') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->can('print')) { } 0 0 0
262 0 ▶ $New = sub { $filename };
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 }
264 else {
265 0 ▶ $New = sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
266 0 confess <<EOM;
267 ------------------------------------------------------------------------
268 No 'print' method is defined for object of class '$ref'
269 Please check your call to Perl::Tidy::perltidy. Trace follows.
270 ------------------------------------------------------------------------
271 EOM
272 }
273 }
274 }
275 }
276  
277 #----------------
278 # handle a string
279 #----------------
280 else {
281 0 ▶ if ( $filename eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 0 0
282 0 ▶ $New = sub { $mode eq 'w' ? *STDOUT : *STDIN }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'w' ? : 0 0 0
283 0 }
284 else {
285 0 ▶ $New = sub { IO::File->new( $filename, $mode ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
286 }
287 }
288  
289 #--------------
290 # Open the file
291 #--------------
292 653 $fh = $New->( $filename, $mode );
293  
294 653 ▶ if ( !$fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fh) { } 0 653 653
295 0 Warn("Couldn't open file:'$filename' in mode:$mode : $OS_ERROR\n");
296 }
297  
298 #------------
299 # Set binmode
300 #------------
301 else {
302 653 ▶ if ( ref($fh) eq 'IO::File' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $fh eq 'IO::File') { } 0 653 653
elsif ($filename eq '-') { } 0 653 653
303 ## binmode object call not available in older perl versions
304 ## $fh->binmode(":raw:encoding(UTF-8)");
305 0 ▶ if ($is_encoded_data) { binmode $fh, ":raw:encoding(UTF-8)"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encoded_data) { } 0 0 0
306 0 else { binmode $fh }
307 }
308 elsif ( $filename eq '-' ) {
309 0 ▶ if ($is_encoded_data) { binmode STDOUT, ":raw:encoding(UTF-8)"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encoded_data) { } 0 0 0
310 0 else { binmode STDOUT }
311 }
312 else {
313  
314 # shouldn't get here
315 653 if (DEVEL_MODE) {
316 my $ref_fh = ref($fh);
317 Fault(<<EOM);
318 unexpected streamhandle state for file='$filename' mode='$mode' ref(fh)=$ref_fh
319 EOM
320 }
321 }
322 }
323  
324 653 return $fh;
325 } ## end sub streamhandle
326  
327 1320 ▶sub stream_slurp {
Subroutine stream_slurp: called CC=12 SCAR=37.4
Pod undocumented
328  
329 1320 my ( $filename, ($timeout_in_seconds) ) = @_;
330  
331 # Given:
332 # $filename
333 # $timeout_in_seconds (optional timeout, in seconds)
334  
335 # Read the text in $filename and
336 # return:
337 # undef if read error, or
338 # $rinput_string = ref to string of text
339  
340 # if $filename is: Read
341 # ---------------- -----------------
342 # ARRAY ref array ref
343 # SCALAR ref string ref
344 # object ref object with 'getline' method (exit if no 'getline')
345 # '-' STDIN
346 # string file named $filename
347  
348 # Note that any decoding from utf8 must be done by the caller
349  
350 1320 my $ref = ref($filename);
351 1320 my $rinput_string;
352  
353 # handle a reference
354 1320 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 1317 3 1320
355 1317 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 2 1315 1317
elsif ($ref eq 'SCALAR') { } 1315 0 1315
356 2 my $buf = join( EMPTY_STRING, @{$filename} );
357 2 $rinput_string = \$buf;
358 }
359 elsif ( $ref eq 'SCALAR' ) {
360 1315 $rinput_string = $filename;
361 }
362 else {
363 0 ▶ if ( $ref->can('getline') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->can('getline')) { } 0 0 0
364 0 my $buf = EMPTY_STRING;
365 0 ▶ while ( defined( my $line = $filename->getline() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $filename->getline)) 0 0 0
366 0 $buf .= $line;
367 }
368 0 $rinput_string = \$buf;
369 }
370 else {
371 0 confess <<EOM;
372 ------------------------------------------------------------------------
373 No 'getline' method is defined for object of class '$ref'
374 Please check your call to Perl::Tidy::perltidy. Trace follows.
375 ------------------------------------------------------------------------
376 EOM
377 }
378 }
379 }
380  
381 # handle a string
382 else {
383 3 ▶ if ( $filename eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 3 3
384 0 local $INPUT_RECORD_SEPARATOR = undef;
385 0 my $buf;
386 0 ▶ if ( $timeout_in_seconds && $timeout_in_seconds > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout_in_seconds and $timeout_in_seconds > 0) { } 0 0 0
Condition0%0/3
$timeout_in_seconds and $timeout_in_seconds > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$timeout_in_seconds and $timeout_in_seconds > 0
$timeout_in_seconds $timeout_in_seconds > 0
Truth table0%0/3
A and B
A= $timeout_in_secondsB= $timeout_in_seconds > 0
ABresult
0X0
100
111
387 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"ALRM"} = sub { die "alarm\n"; } ; alarm $timeout_in_seconds; $buf = readline ARGV; alarm 0; 1 } } or Die("Timeout reading stdin using -tos=$timeout_in_seconds seconds. Use -tos=0 to skip timeout check.\n") 0 0 0
388 0 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
389 0 alarm($timeout_in_seconds);
390 0 $buf = <>;
391 0 alarm(0);
392 0 1;
393 }
394 or Die(
395 "Timeout reading stdin using -tos=$timeout_in_seconds seconds. Use -tos=0 to skip timeout check.\n"
396 );
397 }
398 else {
399 0 $buf = <>;
400 }
401 0 $rinput_string = \$buf;
402 }
403 else {
404 3 ▶ if ( open( my $fh, '<', $filename ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $filename) { } 3 0 3
405 3 local $INPUT_RECORD_SEPARATOR = undef;
406 3 my $buf = <$fh>;
407 3 ▶ $fh->close() or Warn("Cannot close $filename\n");
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or Warn("Cannot close $filename\n") 3 0 3
408 3 $rinput_string = \$buf;
409 }
410 else {
411 0 Warn("Cannot open $filename: $OS_ERROR\n");
412 0 return;
413 }
414 }
415 }
416  
417 1320 return $rinput_string;
418 } ## end sub stream_slurp
419  
420 # Here is a map of the flow of data from the input source to the output
421 # line sink:
422 #
423 # -->Tokenizer-->Formatter-->VerticalAligner-->FileWriter-->
424 # input groups output
425 # lines tokens lines of lines lines
426 # lines
427 #
428 # The names correspond to the package names responsible for the unit processes.
429 #
430 # The overall process is controlled by the "main" package.
431 #
432 # Tokenizer analyzes a line and breaks it into tokens, peeking ahead
433 # if necessary. A token is any section of the input line which should be
434 # manipulated as a single entity during formatting. For example, a single
435 # ',' character is a token, and so is an entire side comment. It handles
436 # the complexities of Perl syntax, such as distinguishing between '<<' as
437 # a shift operator and as a here-document, or distinguishing between '/'
438 # as a divide symbol and as a pattern delimiter.
439 #
440 # Formatter inserts and deletes whitespace between tokens, and breaks
441 # sequences of tokens at appropriate points as output lines. It bases its
442 # decisions on the default rules as modified by any command-line options.
443 #
444 # VerticalAligner collects groups of lines together and tries to line up
445 # certain tokens, such as '=>', '#', and '=' by adding whitespace.
446 #
447 # FileWriter simply writes lines to the output stream.
448 #
449 # The Logger package, not shown, records significant events and warning
450 # messages. It writes a .LOG file, which may be saved with a
451 # '-log' or a '-g' flag.
452  
453 { #<<< (this side comment avoids excessive indentation in a closure)
454  
455 45 my $Warn_count;
456 my $fh_stderr;
457 45 my $loaded_unicode_gcstring;
458 45 my $rstatus;
459 45 my $nag_message;
460  
461 0 ▶sub get_input_stream_name {
Subroutine get_input_stream_name: not called CC=2 SCAR=17.9
Pod undocumented
462  
463 # Make input stream name available for Fault calls
464 0 my $display_name = $rstatus->{'input_name'};
465 0 ▶ my $input_stream_name = $display_name ? $display_name : "??";
Branch: 1 branch
BranchTrueFalseTotal
$display_name ? : 0 0 0
466 0 return $input_stream_name;
467 } ## end sub get_input_stream_name
468  
469 # Flush any accumulated nag message(s) if possible
470 0 ▶sub nag_flush {
Subroutine nag_flush: not called CC=3 SCAR=24.8
Pod undocumented
471 0 my ($fh) = @_;
472 0 ▶ if ( length($nag_message) && $Warn_count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $nag_message and $Warn_count > 0) 0 0 0
Condition0%0/3
length $nag_message and $Warn_count > 0
!ll&&!rl&&r
000
MC/DC0%0/2
length $nag_message and $Warn_count > 0
length $nag_message $Warn_count > 0
Truth table0%0/3
A and B
A= length $nag_messageB= $Warn_count > 0
ABresult
0X0
100
111
473 0 $fh->print($nag_message);
474 0 $nag_message = EMPTY_STRING;
475 }
476 0 return;
477 } ## end sub nag_flush
478  
479 # Accept a warning message that will only go out if regular warnings also go
480 # out. This is useful when we want to output a non-critical error message, but
481 # only if another regular error message goes out, so that test runs do not fail
482 # on unimportant issues, but that the user eventually gets to see the issue.
483 # For example, we might want to inform the user that a certain parameter in his
484 # perltidyrc will be deprecated, but do not want that to cause a test to fail.
485 0 ▶sub Nag {
Subroutine Nag: not called CC=1 SCAR=6.9
Pod undocumented
486 0 my $msg = shift;
487 0 $nag_message .= $msg;
488 0 nag_flush($fh_stderr);
489 0 return;
490 } ## end sub Nag
491  
492 # Bump Warn_count only: it is essential to bump the count on all warnings, even
493 # if no message goes out, so that the correct exit status is set.
494 0 ▶sub Warn_count_bump { $Warn_count++; return }
Subroutine Warn_count_bump: not called CC=1 SCAR=6.9
Pod undocumented
495  
496 # Output Warn message
497 0 ▶sub Warn_msg {
Subroutine Warn_msg: not called CC=1 SCAR=6.9
Pod undocumented
498 0 my $msg = shift;
499 0 $fh_stderr->print($msg);
500 0 nag_flush($fh_stderr);
501 0 return;
502 } ## end sub Warn_msg
503  
504 # Bump Warn count and output Warn message
505 0 ▶sub Warn { my $msg = shift; $Warn_count++; Warn_msg($msg); return }
Subroutine Warn: not called CC=1 SCAR=6.9
Pod undocumented
506  
507 0 ▶sub Exit {
Subroutine Exit: not called CC=2 SCAR=17.9
Pod undocumented
508 0 my $flag = shift;
509 0 ▶ if ($flag) { goto ERROR_EXIT }
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 0 0 0
510 0 else { goto NORMAL_EXIT }
511 0 croak "unexpected return to sub Exit";
512 } ## end sub Exit
513  
514 0 ▶sub Die {
Subroutine Die: not called CC=1 SCAR=6.9
Pod undocumented
515 0 my $msg = shift;
516 0 Warn($msg);
517 0 Exit(1);
518 0 croak "unexpected return from sub Exit";
519 } ## end sub Die
520  
521 0 ▶sub Fault {
Subroutine Fault: not called CC=1 SCAR=6.9
Pod undocumented
522 0 my ($msg) = @_;
523  
524 # This routine is called for errors that really should not occur
525 # except if there has been a bug introduced by a recent program change.
526 # Please add comments at calls to Fault to explain why the call
527 # should not occur, and where to look to fix it.
528 0 my ( $package0_uu, $filename0_uu, $line0, $subroutine0_uu ) = caller(0);
529 0 my ( $package1_uu, $filename1, $line1, $subroutine1 ) = caller(1);
530 0 my ( $package2_uu, $filename2_uu, $line2_uu, $subroutine2 ) = caller(2);
531 0 my $pkg = __PACKAGE__;
532 0 my $input_stream_name = get_input_stream_name();
533  
534 0 Die(<<EOM);
535 ==============================================================================
536 While operating on input stream with name: '$input_stream_name'
537 A fault was detected at line $line0 of sub '$subroutine1'
538 in file '$filename1'
539 which was called from line $line1 of sub '$subroutine2'
540 Message: '$msg'
541 This is probably an error introduced by a recent programming change.
542 $pkg reports VERSION='$VERSION'.
543 ==============================================================================
544 EOM
545  
546 0 croak "unexpected return from sub Die";
547 } ## end sub Fault
548  
549 664 ▶sub is_char_mode {
Subroutine is_char_mode: called CC=2 SCAR=6.9
Pod undocumented
550  
551 664 my ($string) = @_;
552  
553 # Returns:
554 # true if $string is in Perl's internal character mode
555 # (also called the 'upgraded form', or UTF8=1)
556 # false if $string is in Perl's internal byte mode
557  
558 # This function isolates the call to Perl's internal function
559 # utf8::is_utf8() which is true for strings represented in an 'upgraded
560 # form'. It is available AFTER Perl version 5.8.
561 # See https://perldoc.perl.org/Encode.
562 # See also comments in Carp.pm and other modules using this function
563  
564 664 ▶ return 1 if ( utf8::is_utf8($string) );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($string) 2 662 664
565 662 return;
566 } ## end sub is_char_mode
567  
568 11 ▶my $md5_hex = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
569 11 my ($buf) = @_;
570  
571 # Evaluate the MD5 sum for a string:
572 # Given:
573 # $buf = a string
574 # Return:
575 # $digest = its MD5 sum
576 11 my $octets = Encode::encode( "utf8", $buf );
577 11 my $digest = md5_hex($octets);
578 11 return $digest;
579 45 }; ## end $md5_hex = sub
580  
581 68 ▶sub get_iteration_count {
Subroutine get_iteration_count: called CC=1 SCAR=0.0
Pod undocumented
582 68 return $rstatus->{iteration_count};
583 }
584  
585 0 ▶sub get_num_files {
Subroutine get_num_files: not called CC=1 SCAR=6.9
Pod undocumented
586 0 return $rstatus->{num_files};
587 }
588  
589 0 ▶sub get_line_range_clipped {
Subroutine get_line_range_clipped: not called CC=1 SCAR=6.9
Pod undocumented
590 0 return $rstatus->{line_range_clipped};
591 }
592  
593 751 ▶sub check_for_valid_words {
Subroutine check_for_valid_words: called CC=27 SCAR=50.7
Pod undocumented
594  
595 751 my ($rcall_hash) = @_;
596  
597 # Given hash ref with these keys:
598 # rinput_list = ref to ARRAY of possible words
599 # option_name = name of option to use for a warn or die message
600 # (caller should add leading dash(es))
601 # on_error =
602 # 'warn' ? call Warn and return ref to unknown words
603 # : 'die' ? call Die
604 # : return ref to list of unknown words
605 # allow_module_path = true if words can have module paths with '::'
606 # rexceptions = optional ref to ARRAY or HASH of acceptable non-words
607 # rvalid_words = optional ref to ARRAY or HASH of acceptable words
608 # - if given, only words in this list, or rexceptions, are valid
609 # - if not given, words must be identifier-like or be in rexceptions
610  
611 # Return (if it does not call Die):
612 # - nothing if no errors, or
613 # - ref to list of unknown words
614  
615 751 ▶ if ( !defined($rcall_hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rcall_hash) 0 751 751
616 0 Fault("received undefined arg\n");
617 0 return;
618 }
619  
620 751 my $rinput_list = $rcall_hash->{rinput_list};
621 751 my $option_name = $rcall_hash->{option_name};
622 751 my $on_error = $rcall_hash->{on_error};
623 751 my $allow_module_path = $rcall_hash->{allow_module_path};
624 751 my $rexceptions = $rcall_hash->{rexceptions};
625 751 my $rvalid_words = $rcall_hash->{rvalid_words};
626  
627 751 ▶ return if ( !defined($rinput_list) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rinput_list 0 751 751
628 751 ▶ my $msg_end = $option_name ? " with $option_name" : EMPTY_STRING;
Branch: 1 branch
BranchTrueFalseTotal
$option_name ? : 751 0 751
629  
630 1502 ▶ my $make_hash_ref = sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
631 1502 my ( $rthing, $key_name ) = @_;
632  
633 # If user supplied an array ref, make a corresponding hash ref
634  
635 1502 ▶ if ( defined($rthing) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rthing) 4 1498 1502
636 4 my $ref = ref($rthing);
637 4 ▶ if ( !$ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 0 4 4
638 0 Fault("expecting {$key_name} to be a ref in call '$msg_end'\n");
639 }
640 4 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 4 0 4
641 4 my %hash = map { $_ => 1 } @{$rthing};
642 4 $rthing = \%hash;
643 }
644 4 ▶ if ( ref($rthing) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rthing ne "HASH") 0 4 4
645 0 Fault(
646 "expecting {$key_name} to be a HASH or ARRAY ref in call '$msg_end\n"
647 );
648 }
649 }
650 1502 return $rthing;
651 751 }; ## end $make_hash_ref = sub
652  
653 751 $rexceptions = $make_hash_ref->( $rexceptions, 'rexceptions' );
654 751 $rvalid_words = $make_hash_ref->( $rvalid_words, 'rvalid_words' );
655  
656 751 my @non_words;
657  
658 # Must match specific valid words
659 751 ▶ if ( defined($rvalid_words) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rvalid_words) { } 4 747 751
660 4 foreach my $word ( @{$rinput_list} ) {
661 4 ▶ next if ( $rexceptions && $rexceptions->{$word} );
Branch: 1 branch
BranchTrueFalseTotal
if $rexceptions and $rexceptions->{$word} 0 4 4
Condition33%1/3
$rexceptions and $rexceptions->{$word}
!ll&&!rl&&r
400
MC/DC0%0/2
$rexceptions and $rexceptions->{$word}
$rexceptions $rexceptions->{$word}
Truth table33%1/3
A and B
A= $rexceptionsB= $rexceptions->{$word}
ABresult
0X0
100
111
662 4 ▶ if ( !$rvalid_words->{$word} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rvalid_words->{$word}) 0 4 4
663  
664 # Note: not currently checking for module prefixes in this case
665 0 push @non_words, $word;
666 }
667 }
668 }
669  
670 # Must be identifier-like words, or an exception
671 else {
672 747 foreach my $word ( @{$rinput_list} ) {
673  
674 4742 ▶ next if ( $rexceptions && $rexceptions->{$word} );
Branch: 1 branch
BranchTrueFalseTotal
if $rexceptions and $rexceptions->{$word} 0 4742 4742
Condition33%1/3
$rexceptions and $rexceptions->{$word}
!ll&&!rl&&r
474200
MC/DC0%0/2
$rexceptions and $rexceptions->{$word}
$rexceptions $rexceptions->{$word}
Truth table33%1/3
A and B
A= $rexceptionsB= $rexceptions->{$word}
ABresult
0X0
100
111
675 4742 ▶ next if ( $word =~ /^\w+$/ && $word !~ /^\d/ );
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^\w+$/ and not $word =~ /^\d/ 4742 0 4742
Condition33%1/3
$word =~ /^\w+$/ and not $word =~ /^\d/
!ll&&!rl&&r
004742
MC/DC0%0/2
$word =~ /^\w+$/ and not $word =~ /^\d/
$word =~ /^\w+$/ not $word =~ /^\d/
Truth table33%1/3
A and not(B)
A= $word =~ /^\w+$/B= not $word =~ /^\d/
ABresult
0X0
100
111
676  
677 # Words with a module path, like My::Module::function
678 0 ▶ if ( $allow_module_path && index( $word, ':' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_module_path and index $word, ":") 0 0 0
Condition0%0/3
$allow_module_path and index $word, ":"
!ll&&!rl&&r
000
MC/DC0%0/2
$allow_module_path and index $word, ":"
$allow_module_path index $word, ":"
Truth table0%0/3
A and B
A= $allow_module_pathB= index $word, ":"
ABresult
0X0
100
111
679 0 my @parts = split /::/, $word;
680 0 my $ok = 1;
681 0 foreach my $sub_word (@parts) {
682 0 ▶ next if ( !length($sub_word) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $sub_word 0 0 0
683 0 ▶ next if ( $sub_word =~ /^\w+$/ && $sub_word !~ /^\d/ );
Branch: 1 branch
BranchTrueFalseTotal
if $sub_word =~ /^\w+$/ and not $sub_word =~ /^\d/ 0 0 0
Condition0%0/3
$sub_word =~ /^\w+$/ and not $sub_word =~ /^\d/
!ll&&!rl&&r
000
MC/DC0%0/2
$sub_word =~ /^\w+$/ and not $sub_word =~ /^\d/
$sub_word =~ /^\w+$/ not $sub_word =~ /^\d/
Truth table0%0/3
A and not(B)
A= $sub_word =~ /^\w+$/B= not $sub_word =~ /^\d/
ABresult
0X0
100
111
684 0 $ok = 0;
685 0 last;
686 }
687 0 ▶ next if ($ok);
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
688 }
689 0 push @non_words, $word;
690 }
691 }
692  
693 751 ▶ return if ( !@non_words );
Branch: 1 branch
BranchTrueFalseTotal
unless @non_words 751 0 751
694 0 ▶ if ($on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_error) 0 0 0
695 0 $on_error = lc($on_error);
696 0 ▶ if ( $on_error eq 'warn' || $on_error eq 'die' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_error eq "warn" or $on_error eq "die") 0 0 0
Condition0%0/3
$on_error eq "warn" or $on_error eq "die"
l!l&&r!l&&!r
000
MC/DC0%0/2
$on_error eq "warn" or $on_error eq "die"
$on_error eq "warn" $on_error eq "die"
Truth table0%0/3
A or B
A= $on_error eq "warn"B= $on_error eq "die"
ABresult
1X1
011
000
697 0 my $num = @non_words;
698 0 my $str = join( SPACE, @non_words );
699 0 my $max_str_len = 120;
700 0 ▶ if ( length($str) > $max_str_len - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > $max_str_len - 1) 0 0 0
701 0 $str = substr( $str, 0, $max_str_len - 4 ) . "...";
702 }
703 0 my $msg = <<EOM;
704 $num unrecognized words were input$msg_end :
705 $str
706 EOM
707 0 ▶ Die($msg) if ( $on_error eq 'die' );
Branch: 1 branch
BranchTrueFalseTotal
if $on_error eq "die" 0 0 0
708 0 Warn($msg);
709 }
710 }
711 0 return \@non_words;
712 } ## end sub check_for_valid_words
713  
714 45 my %is_known_markup_word;
715  
716 45 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
717 45 my @q = qw( ?xml !doctype !-- html meta );
718 45 $is_known_markup_word{$_} = 1 for @q;
719 }
720  
721 0 ▶sub is_not_perl {
Subroutine is_not_perl: not called CC=5 SCAR=34.0
Pod undocumented
722  
723 0 my ( $rinput_string, $input_file, $is_named_file ) = @_;
724  
725 # Given:
726 # $rinput_string = ref to the string of text being processed
727 # $input_file = the name of the input stream
728 # $is_named_file = true if $input_file is a named file
729 # Return:
730 # true if this is clearly not a perl script
731 # false otherwise
732 # Note:
733 # This sub is called when the first character of a file is '<' in order
734 # to catch the obvious cases of some kind of markup language.
735 # See also some related code in 'find_angle_operator_termination which
736 # will catch some markup syntax which gets past this preliminary check.
737  
738 0 my $text;
739 0 ▶ if ( ${$rinput_string} =~ m/\s*\<\s*([\?\!]?[\-\w]+)/ ) { $text = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (${$rinput_string;} =~ /\s*\<\s*([\?\!]?[\-\w]+)/) { } 0 0 0
740 0 else { return }
741  
742 0 ▶ return 1 if ( $is_known_markup_word{ lc($text) } );
Branch: 1 branch
BranchTrueFalseTotal
if $is_known_markup_word{lc $text} 0 0 0
743  
744 # require a named file with known extension for other markup words
745 0 ▶ return if ( !$is_named_file );
Branch: 1 branch
BranchTrueFalseTotal
unless $is_named_file 0 0 0
746  
747 # check filename
748 0 ▶ return 1 if ( $input_file =~ /html?$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $input_file =~ /html?$/i 0 0 0
749  
750 0 return;
751 } ## end sub is_not_perl
752  
753 45 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
754  
755 # Array index names for $self.
756 # Do not combine with other BEGIN blocks (c101).
757 45 my $i = 0;
758 45 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
759 45 _actual_output_extension_ => $i++,
760 _debugfile_stream_ => $i++,
761 _decoded_input_as_ => $i++,
762 _destination_stream_ => $i++,
763 _diagnostics_object_ => $i++,
764 _display_name_ => $i++,
765 _file_extension_separator_ => $i++,
766 _fileroot_ => $i++,
767 _is_pure_ascii_data_ => $i++,
768 _is_encoded_data_ => $i++,
769 _length_function_ => $i++,
770 _line_separator_default_ => $i++,
771 _line_separator_ => $i++,
772 _line_tidy_begin_ => $i++,
773 _line_tidy_end_ => $i++,
774 _logger_object_ => $i++,
775 _output_file_ => $i++,
776 _postfilter_ => $i++,
777 _prefilter_ => $i++,
778 _rOpts_ => $i++,
779 _rOpts_in_profile_ => $i++,
780 _saw_pbp_ => $i++,
781 _teefile_stream_ => $i++,
782 _user_formatter_ => $i++,
783 _input_copied_verbatim_ => $i++,
784 _input_output_difference_ => $i++,
785 _dump_to_stdout_ => $i++,
786 45 };
787 } ## end BEGIN
788  
789 664 ▶sub perltidy {
Subroutine perltidy: called CC=73 SCAR=68.2
Pod undocumented
790  
791 664 my %input_hash = @_;
792  
793 # This is the main perltidy routine
794  
795 664 my %defaults = (
796 argv => undef,
797 destination => undef,
798 formatter => undef,
799 logfile => undef,
800 errorfile => undef,
801 teefile => undef,
802 debugfile => undef,
803 perltidyrc => undef,
804 source => undef,
805 stderr => undef,
806 dump_options => undef,
807 dump_options_type => undef,
808 dump_getopt_flags => undef,
809 dump_options_category => undef,
810 dump_abbreviations => undef,
811 prefilter => undef,
812 postfilter => undef,
813 );
814  
815 # Status information which can be returned for diagnostic purposes.
816 # NOTE: This is intended only for testing and subject to change.
817  
818 # List of "key => value" hash entries:
819  
820 # Some relevant user input parameters for convenience:
821 # opt_format => value of --format: 'tidy', 'html', or 'user'
822 # opt_encoding => value of -enc flag: 'utf8', 'none', or 'guess'
823 # opt_encode_output => value of -eos flag: 'eos' or 'neos'
824 # opt_max_iterations => value of --iterations=n
825  
826 # file_count => number of files processed in this call
827  
828 # If multiple files are processed, then the following values will be for
829 # the last file only:
830  
831 # input_name => name of the input stream
832 # output_name => name of the output stream
833  
834 # The following two variables refer to Perl's two internal string modes,
835 # and have the values 0 for 'byte' mode and 1 for 'char' mode:
836 # char_mode_source => true if source is in 'char' mode. Will be false
837 # unless we received a source string ref with utf8::is_utf8() set.
838 # char_mode_used => true if text processed by perltidy in 'char' mode.
839 # Normally true for text identified as utf8, otherwise false.
840  
841 # This tells if Unicode::GCString was used
842 # gcs_used => true if -gcs and Unicode::GCString found & used
843  
844 # These variables tell what utf8 decoding/encoding was done:
845 # input_decoded_as => non-blank if perltidy decoded the source text
846 # output_encoded_as => non-blank if perltidy encoded before return
847  
848 # These variables are related to iterations and convergence testing:
849 # iteration_count => number of iterations done
850 # ( can be from 1 to opt_max_iterations )
851 # converged => true if stopped on convergence
852 # ( can only happen if opt_max_iterations > 1 )
853 # blinking => true if stopped on blinking states
854 # ( i.e., unstable formatting, should not happen )
855  
856 664 $rstatus = {
857  
858 file_count => 0,
859 opt_format => EMPTY_STRING,
860 opt_encoding => EMPTY_STRING,
861 opt_encode_output => EMPTY_STRING,
862 opt_max_iterations => EMPTY_STRING,
863  
864 input_name => '(unknown)',
865 output_name => EMPTY_STRING,
866 char_mode_source => 0,
867 char_mode_used => 0,
868 input_decoded_as => EMPTY_STRING,
869 output_encoded_as => EMPTY_STRING,
870 gcs_used => 0,
871 iteration_count => 0,
872 converged => 0,
873 blinking => 0,
874 num_files => 0,
875 line_range_clipped => 0,
876 };
877  
878 # Fix for issue git #57
879 664 $Warn_count = 0;
880 664 $nag_message = EMPTY_STRING;
881  
882 # don't overwrite callers ARGV
883 # Localization of @ARGV could be avoided by calling GetOptionsFromArray
884 # instead of GetOptions, but that is not available before perl 5.10
885 664 local @ARGV = @ARGV;
886 664 local *STDERR = *STDERR;
887  
888 664 ▶ if ( my @bad_keys = grep { !exists $defaults{$_} } keys %input_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad_keys) = grep({not exists $defaults{$_};} keys %input_hash)) 0 664 664
889 0 local $LIST_SEPARATOR = ')(';
890 0 my @good_keys = sort keys %defaults;
891 0 @bad_keys = sort @bad_keys;
892 0 confess <<EOM;
893 ------------------------------------------------------------------------
894 Unknown perltidy parameter : (@bad_keys)
895 perltidy only understands : (@good_keys)
896 ------------------------------------------------------------------------
897  
898 EOM
899 }
900  
901 2656 ▶ my $get_hash_ref = sub {
Subroutine __ANON__: called CC=4 SCAR=21.1
902  
903 2656 my ($key) = @_;
904  
905 # Get and check a parameter from the input hash
906  
907 2656 my $hash_ref = $input_hash{$key};
908 2656 ▶ if ( defined($hash_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash_ref) 0 2656 2656
909 0 ▶ if ( ref($hash_ref) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash_ref ne "HASH") 0 0 0
910 0 my $what = ref($hash_ref);
911 0 ▶ my $but_is =
Branch: 1 branch
BranchTrueFalseTotal
$what ? : 0 0 0
912 $what ? "but is ref to $what" : "but is not a reference";
913 0 croak <<EOM;
914 ------------------------------------------------------------------------
915 error in call to perltidy:
916 -$key must be reference to HASH $but_is
917 ------------------------------------------------------------------------
918 EOM
919 }
920 }
921 2656 return $hash_ref;
922 664 }; ## end $get_hash_ref = sub
923  
924 664 %input_hash = ( %defaults, %input_hash );
925 664 my $argv = $input_hash{'argv'};
926 664 my $destination_stream = $input_hash{'destination'};
927 664 my $perltidyrc_stream = $input_hash{'perltidyrc'};
928 664 my $source_stream = $input_hash{'source'};
929 664 my $stderr_stream = $input_hash{'stderr'};
930 664 my $user_formatter = $input_hash{'formatter'};
931 664 my $prefilter = $input_hash{'prefilter'};
932 664 my $postfilter = $input_hash{'postfilter'};
933  
934 664 ▶ if ($stderr_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr_stream) { } 648 16 664
935 648 $fh_stderr = Perl::Tidy::streamhandle( $stderr_stream, 'w' );
936 648 ▶ if ( !$fh_stderr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh_stderr) 0 648 648
937 0 croak <<EOM;
938 ------------------------------------------------------------------------
939 Unable to redirect STDERR to $stderr_stream
940 Please check value of -stderr in call to perltidy
941 ------------------------------------------------------------------------
942 EOM
943 }
944 }
945 else {
946 16 $fh_stderr = *STDERR;
947 }
948  
949 664 my $self = [];
950 664 bless $self, __PACKAGE__;
951  
952 # extract various dump parameters
953 664 my $dump_options_type = $input_hash{'dump_options_type'};
954 664 my $dump_options = $get_hash_ref->('dump_options');
955 664 my $dump_getopt_flags = $get_hash_ref->('dump_getopt_flags');
956 664 my $dump_options_category = $get_hash_ref->('dump_options_category');
957 664 my $dump_abbreviations = $get_hash_ref->('dump_abbreviations');
958  
959 # validate dump_options_type
960 664 ▶ if ( defined($dump_options) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dump_options) { } 0 664 664
961 0 ▶ if ( !defined($dump_options_type) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dump_options_type) 0 0 0
962 0 $dump_options_type = 'perltidyrc';
963 }
964 0 ▶ if ( $dump_options_type ne 'perltidyrc'
Branch: 1 branch
BranchTrueFalseTotal
if ($dump_options_type ne "perltidyrc" and $dump_options_type ne "full") 0 0 0
Condition0%0/3
$dump_options_type ne "perltidyrc" and $dump_options_type ne "full"
!ll&&!rl&&r
000
MC/DC0%0/2
$dump_options_type ne "perltidyrc" and $dump_options_type ne "full"
$dump_options_type ne "perltidyrc" $dump_options_type ne "full"
Truth table0%0/3
A and B
A= $dump_options_type ne "perltidyrc"B= $dump_options_type ne "full"
ABresult
0X0
100
111
965 && $dump_options_type ne 'full' )
966 {
967 0 croak <<EOM;
968 ------------------------------------------------------------------------
969 Please check value of -dump_options_type in call to perltidy;
970 saw: '$dump_options_type'
971 expecting: 'perltidyrc' or 'full'
972 ------------------------------------------------------------------------
973 EOM
974  
975 }
976 }
977 else {
978 664 $dump_options_type = EMPTY_STRING;
979 }
980  
981 664 ▶ if ($user_formatter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_formatter) 0 664 664
982  
983 # if the user defines a formatter, there is no output stream,
984 # but we need a null stream to keep coding simple
985 0 $destination_stream = \my $tmp;
986 }
987  
988 # see if ARGV is overridden
989 664 ▶ if ( defined($argv) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argv) 664 0 664
990  
991 664 my $rargv = ref($argv);
992 0 ▶ if ( $rargv eq 'SCALAR' ) { $argv = ${$argv}; $rargv = undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($rargv eq "SCALAR") 0 664 664
993  
994 # ref to ARRAY
995 664 ▶ if ($rargv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rargv) { } 0 664 664
996 0 ▶ if ( $rargv eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rargv eq 'ARRAY') { } 0 0 0
997 0 @ARGV = @{$argv};
998 }
999 else {
1000 0 croak <<EOM;
1001 ------------------------------------------------------------------------
1002 Please check value of -argv in call to perltidy;
1003 it must be a string or ref to ARRAY but is: $rargv
1004 ------------------------------------------------------------------------
1005 EOM
1006 }
1007 }
1008  
1009 # string
1010 else {
1011 664 my ( $rargv_str, $msg ) = parse_args($argv);
1012 664 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 664 664
1013 0 Die(<<EOM);
1014 Error parsing this string passed to perltidy with 'argv':
1015 $msg
1016 EOM
1017 }
1018 664 @ARGV = @{$rargv_str};
1019 }
1020 }
1021  
1022 # These string refs will hold any warnings and error messages to be written
1023 # to the logfile object when it eventually gets created.
1024 664 my $rpending_complaint;
1025 664 ${$rpending_complaint} = EMPTY_STRING;
1026  
1027 664 my $rpending_logfile_message;
1028 664 ${$rpending_logfile_message} = EMPTY_STRING;
1029  
1030 664 my ( $is_Windows, $Windows_type ) = look_for_Windows($rpending_complaint);
1031  
1032 # VMS file names are restricted to a 40.40 format, so we append _tdy
1033 # instead of .tdy, etc. (but see also sub check_vms_filename)
1034 664 my $dot;
1035 my $dot_pattern;
1036 664 ▶ if ( $OSNAME eq 'VMS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::Tidy::OSNAME eq 'VMS') { } 0 664 664
1037 0 $dot = '_';
1038 0 $dot_pattern = '_';
1039 }
1040 else {
1041 664 $dot = '.';
1042 664 $dot_pattern = '\.'; # must escape for use in regex
1043 }
1044 664 $self->[_file_extension_separator_] = $dot;
1045  
1046 # save a copy of the last two input args for error checking later
1047 664 my @ARGV_saved;
1048 664 ▶ if ( @ARGV > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV > 1) 9 655 664
1049 9 @ARGV_saved = ( $ARGV[-2], $ARGV[-1] );
1050 }
1051  
1052 #-------------------------
1053 # get command line options
1054 #-------------------------
1055 my (
1056 664 $rOpts, $config_file, $rraw_options,
1057 $roption_string, $rexpansion, $roption_category,
1058 $rinteger_option_range, $ris_string_option, $rOpts_in_profile
1059 )
1060 = process_command_line(
1061 $perltidyrc_stream, $is_Windows, $Windows_type,
1062 $rpending_complaint, $dump_options_type,
1063 );
1064  
1065 # Only filenames should remain in @ARGV
1066 664 my @Arg_files = @ARGV;
1067  
1068 664 $self->[_rOpts_] = $rOpts;
1069 664 $self->[_rOpts_in_profile_] = $rOpts_in_profile;
1070  
1071 my $saw_pbp =
1072 664 ▶ grep { $_ eq '-pbp' || $_ eq '-perl-best-practices' } @{$rraw_options};
Condition66%2/3
$_ eq '-pbp' || $_ eq '-perl-best-practices'
l!l&&r!l&&!r
90715
MC/DC50%1/2
$_ eq '-pbp' || $_ eq '-perl-best-practices'
$_ eq '-pbp' $_ eq '-perl-best-practices'
Truth table66%2/3
A || B
A= $_ eq '-pbp'B= $_ eq '-perl-best-practices'
ABresult
1X1
011
000
1073 664 $self->[_saw_pbp_] = $saw_pbp;
1074  
1075 #------------------------------------
1076 # Handle requests to dump information
1077 #------------------------------------
1078  
1079 # return or exit immediately after all dumps
1080 664 my $quit_now = 0;
1081  
1082 # Getopt parameters and their flags
1083 664 ▶ if ( defined($dump_getopt_flags) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dump_getopt_flags) 0 664 664
1084 0 $quit_now = 1;
1085 0 foreach my $op ( @{$roption_string} ) {
1086 0 my $opt = $op;
1087 0 my $flag = EMPTY_STRING;
1088  
1089 # Examples:
1090 # some-option=s
1091 # some-option=i
1092 # some-option:i
1093 # some-option!
1094 0 ▶ if ( $opt =~ /(.*)(!|=.*|:.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt =~ /(.*)(!|=.*|:.*)$/) 0 0 0
1095 0 $opt = $1;
1096 0 $flag = $2;
1097 }
1098 0 $dump_getopt_flags->{$opt} = $flag;
1099 }
1100 }
1101  
1102 664 ▶ if ( defined($dump_options_category) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dump_options_category) 0 664 664
1103 0 $quit_now = 1;
1104 0 %{$dump_options_category} = %{$roption_category};
1105 }
1106  
1107 664 ▶ if ( defined($dump_abbreviations) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dump_abbreviations) 0 664 664
1108 0 $quit_now = 1;
1109 0 %{$dump_abbreviations} = %{$rexpansion};
1110 }
1111  
1112 664 ▶ if ( defined($dump_options) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dump_options) 0 664 664
1113 0 $quit_now = 1;
1114 0 %{$dump_options} = %{$rOpts};
1115 }
1116  
1117 664 ▶ Exit(0) if ($quit_now);
Branch: 1 branch
BranchTrueFalseTotal
if $quit_now 0 664 664
1118  
1119 # make printable string of options for this run as possible diagnostic
1120 664 my $readable_options = readable_options( $rOpts, $roption_string );
1121  
1122 # dump from command line
1123 664 ▶ if ( $rOpts->{'dump-options'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"dump-options"}) 0 664 664
1124 0 print {*STDOUT} $readable_options;
1125 0 Exit(0);
1126 }
1127  
1128 # some dump options require one filename in the arg list. This is a safety
1129 # precaution in case a user accidentally adds such an option to the command
1130 # line parameters and is expecting formatted output to stdout. Another
1131 # precaution, added elsewhere, is to ignore these in a .perltidyrc
1132 664 my $num_files = @Arg_files;
1133 664 $rstatus->{num_files} = $num_files;
1134 664 foreach my $opt_name (
1135 qw(
1136 dump-block-summary
1137 dump-unusual-variables
1138 dump-mixed-call-parens
1139 dump-mismatched-args
1140 dump-mismatched-returns
1141 dump-unique-keys
1142 dump-hash-keys
1143 dump-similar-keys
1144 dump-keyword-usage
1145 dump-label-usage
1146 )
1147 )
1148 {
1149  
1150 6640 ▶ if ( $rOpts->{$opt_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{$opt_name}) 0 6640 6640
1151 0 $self->[_dump_to_stdout_] = 1;
1152 0 ▶ if ( $num_files != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_files != 1) 0 0 0
1153 0 Die(<<EOM);
1154 --$opt_name expects 1 filename in the arg list but saw $num_files filenames
1155 EOM
1156 }
1157 0 ▶ if ( $rOpts->{'outfile'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"outfile"}) 0 0 0
1158 0 Die(<<EOM);
1159 --outfile is not allowed with --$opt_name because dump output goes to STDOUT
1160 EOM
1161 }
1162 }
1163 }
1164  
1165 #----------------------------------------
1166 # check parameters and their interactions
1167 #----------------------------------------
1168 664 $self->check_options( $num_files, $rinteger_option_range,
1169 $ris_string_option );
1170  
1171 664 ▶ if ($user_formatter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_formatter) 0 664 664
1172 0 $rOpts->{'format'} = 'user';
1173 }
1174  
1175 # there must be one entry here for every possible format
1176 664 my %default_file_extension = (
1177 tidy => 'tdy',
1178 html => 'html',
1179 user => EMPTY_STRING,
1180 );
1181  
1182 664 $rstatus->{'opt_format'} = $rOpts->{'format'};
1183 664 $rstatus->{'opt_max_iterations'} = $rOpts->{'iterations'};
1184 $rstatus->{'opt_encode_output'} =
1185 664 ▶ $rOpts->{'encode-output-strings'} ? 'eos' : 'neos';
Branch: 1 branch
BranchTrueFalseTotal
$rOpts->{'encode-output-strings'} ? : 664 0 664
1186  
1187 # be sure we have a valid output format
1188 664 ▶ if ( !exists $default_file_extension{ $rOpts->{'format'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $default_file_extension{$rOpts->{"format"}}) 0 664 664
1189 my $formats = join( SPACE,
1190 0 sort map { "'" . $_ . "'" } keys %default_file_extension );
1191 0 my $fmt = $rOpts->{'format'};
1192 0 Die("-format='$fmt' but must be one of: $formats\n");
1193 }
1194  
1195 my $output_extension =
1196 $self->make_file_extension( $rOpts->{'output-file-extension'},
1197 664 $default_file_extension{ $rOpts->{'format'} } );
1198  
1199 # get parameters associated with the -b option
1200 664 ▶ my $source =
Branch: 2 branches
BranchTrueFalseTotal
defined $source_stream ? : 664 0 664
@Arg_files ? : 0 0 0
1201 defined($source_stream) ? $source_stream
1202 : @Arg_files ? $Arg_files[-1]
1203 : undef;
1204 664 my ( $in_place_modify, $backup_extension, $delete_backup ) =
1205 $self->check_in_place_modify( $source, $destination_stream );
1206  
1207 664 ▶ my $line_range_clipped = $rOpts->{'line-range-tidy'}
Condition50%3/6
$rOpts->{'line-range-tidy'} && ($self->[13] > 1 || defined $self->[14])
!ll&&!rl&&r
66301
$self->[13] > 1 or defined $self->[14]
l!l&&r!l&&!r
100
MC/DC33%1/3
$rOpts->{'line-range-tidy'} && ($self->[13] > 1 || defined $self->[14])
$rOpts->{'line-range-tidy'} $self->[13] > 1 defined $self->[14]
Truth table50%2/4
A && B or C
A= $rOpts->{'line-range-tidy'}B= $self->[13] > 1C= defined $self->[14]
ABCresult
0XX0
1000
11X1
1011
1208 && ( $self->[_line_tidy_begin_] > 1
1209 || defined( $self->[_line_tidy_end_] ) );
1210 664 $rstatus->{line_range_clipped} = $line_range_clipped;
1211  
1212 664 Perl::Tidy::Formatter::check_options($rOpts);
1213 664 Perl::Tidy::Tokenizer::check_options($rOpts);
1214 664 Perl::Tidy::VerticalAligner::check_options($rOpts);
1215 664 ▶ if ( $rOpts->{'format'} eq 'html' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"format"} eq "html") 1 663 664
1216 1 Perl::Tidy::HtmlWriter->check_options($rOpts);
1217 }
1218  
1219 # Try to catch an unusual missing string parameter error, where the
1220 # intention is to format infile.pl, like this:
1221 # perltidy -title infile.pl
1222 # The problem is that -title wants a string, so it grabs 'infile.pl'. Then
1223 # there is no filename, so input is assumed to be stdin. This make
1224 # perltidy unexpectedly wait for input. To the user, it appears that
1225 # perltidy has gone into an infinite loop. For most options, but not all,
1226 # previous checks for bad string input will have already caught the
1227 # problem. A timeout will eventually occur as a final backup method for
1228 # catching this problem. Issue c312.
1229 664 ▶ if ( !$num_files && @ARGV_saved > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $num_files and @ARGV_saved > 1) 9 655 664
Condition66%2/3
not $num_files and @ARGV_saved > 1
!ll&&!rl&&r
06559
MC/DC50%1/2
not $num_files and @ARGV_saved > 1
not $num_files @ARGV_saved > 1
Truth table66%2/3
not(A) and B
A= not $num_filesB= @ARGV_saved > 1
ABresult
0X0
100
111
1230 9 my $opt_test = $ARGV_saved[-2];
1231 9 my $file_test = $ARGV_saved[-1];
1232 9 ▶ if ( $opt_test =~ s/^[-]+//
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_test =~ s/^[-]+// and not $file_test =~ /^[-]/ and not $file_test =~ /^\d+$/ and -e $file_test) 0 9 9
Condition33%3/9
$opt_test =~ s/^[-]+// and not $file_test =~ /^[-]/ and not $file_test =~ /^\d+$/ and -e $file_test
!ll&&!rl&&r
900
$opt_test =~ s/^[-]+// and not $file_test =~ /^[-]/ and not $file_test =~ /^\d+$/
!ll&&!rl&&r
900
$opt_test =~ s/^[-]+// and not $file_test =~ /^[-]/
!ll&&!rl&&r
090
MC/DC0%0/4
$opt_test =~ s/^[-]+// and not $file_test =~ /^[-]/ and not $file_test =~ /^\d+$/ and -e $file_test
$opt_test =~ s/^[-]+// not $file_test =~ /^[-]/ not $file_test =~ /^\d+$/ -e $file_test
Truth table20%1/5
A and not(B) and not(C) and D
A= $opt_test =~ s/^[-]+//B= not $file_test =~ /^[-]/C= not $file_test =~ /^\d+$/D= -e $file_test
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1233 && $file_test !~ /^[-]/
1234 && $file_test !~ /^\d+$/
1235 && -e $file_test )
1236 {
1237  
1238 # These options can take filenames, so we will ignore them here
1239 my %is_option_with_file_parameter =
1240 0 map { $_ => 1 } qw( outfile profile );
1241  
1242 # Expand an abbreviation into a long name
1243 0 my $long_name;
1244 0 my $exp = $rexpansion->{$opt_test};
1245 0 ▶ if ( !$exp ) { $long_name = $opt_test }
Branch: 2 branches
BranchTrueFalseTotal
if (not $exp) { } 0 0 0
elsif (@{$exp;} == 1) { } 0 0 0
1246 0 elsif ( @{$exp} == 1 ) { $long_name = $exp->[0] }
1247 else { }
1248  
1249 # If this arg grabbed the file, then it must take a string arg
1250 0 ▶ if ( $long_name
Branch: 1 branch
BranchTrueFalseTotal
if ($long_name and defined $rOpts->{$long_name} and $rOpts->{$long_name} eq $file_test and not $is_option_with_file_parameter{$long_name}) 0 0 0
Condition0%0/9
$long_name and defined $rOpts->{$long_name} and $rOpts->{$long_name} eq $file_test and not $is_option_with_file_parameter{$long_name}
!ll&&!rl&&r
000
$long_name and defined $rOpts->{$long_name} and $rOpts->{$long_name} eq $file_test
!ll&&!rl&&r
000
$long_name and defined $rOpts->{$long_name}
!ll&&!rl&&r
000
MC/DC0%0/4
$long_name and defined $rOpts->{$long_name} and $rOpts->{$long_name} eq $file_test and not $is_option_with_file_parameter{$long_name}
$long_name defined $rOpts->{$long_name} $rOpts->{$long_name} eq $file_test not $is_option_with_file_parameter{$long_name}
Truth table0%0/5
A and B and C and not(D)
A= $long_nameB= defined $rOpts->{$long_name}C= $rOpts->{$long_name} eq $file_testD= not $is_option_with_file_parameter{$long_name}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1251 && defined( $rOpts->{$long_name} )
1252 && $rOpts->{$long_name} eq $file_test
1253 && !$is_option_with_file_parameter{$long_name} )
1254 {
1255 0 Die(<<EOM);
1256 Stopping on possible missing string parameter for '-$opt_test':
1257 This parameter takes a string and has been set equal to file '$file_test',
1258 and formatted output will go to standard output. If this is actually correct,
1259 you can skip this message by entering this as '-$opt_test=$file_test'.
1260 EOM
1261 }
1262 }
1263 }
1264  
1265 # make the pattern of file extensions that we shouldn't touch
1266 664 my $forbidden_file_extensions = "(($dot_pattern)(LOG|DEBUG|ERR|TEE)";
1267 664 ▶ if ($output_extension) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output_extension) 664 0 664
1268 664 my $ext = quotemeta($output_extension);
1269 664 $forbidden_file_extensions .= "|$ext";
1270 }
1271 664 ▶ if ( $in_place_modify && $backup_extension ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_place_modify and $backup_extension) 0 664 664
Condition33%1/3
$in_place_modify and $backup_extension
!ll&&!rl&&r
66400
MC/DC0%0/2
$in_place_modify and $backup_extension
$in_place_modify $backup_extension
Truth table33%1/3
A and B
A= $in_place_modifyB= $backup_extension
ABresult
0X0
100
111
1272 0 my $ext = quotemeta($backup_extension);
1273 0 $forbidden_file_extensions .= "|$ext";
1274 }
1275 664 $forbidden_file_extensions .= ')$';
1276  
1277 # Create a diagnostics object if requested;
1278 # This is only useful for code development
1279 664 my $diagnostics_object = undef;
1280 664 if (DIAGNOSTICS) {
1281 $diagnostics_object = Perl::Tidy::Diagnostics->new();
1282 }
1283  
1284 # no filenames should be given if input is from an array
1285 664 ▶ if ($source_stream) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source_stream) { } 664 0 664
elsif (not @Arg_files) { } 0 0 0
1286 664 ▶ if ( @Arg_files > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Arg_files > 0) 0 664 664
1287 0 Die(
1288 "You may not specify any filenames when a source array is given\n"
1289 );
1290 }
1291  
1292 # we'll stuff the source array into Arg_files
1293 664 unshift @Arg_files, $source_stream;
1294  
1295 # No special treatment for source stream which is a filename.
1296 # This will enable checks for binary files and other bad stuff.
1297 664 ▶ $source_stream = undef unless ( ref($source_stream) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $source_stream 3 661 664
1298 }
1299  
1300 # use stdin by default if no source array and no args
1301 elsif ( !@Arg_files ) {
1302 0 unshift @Arg_files, '-';
1303 }
1304  
1305 # check file existence and expand any globs
1306 else {
1307 0 my @updated_files;
1308 0 foreach my $input_file (@Arg_files) {
1309 0 ▶ if ( -e $input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $input_file) { } 0 0 0
1310 0 push @updated_files, $input_file;
1311 }
1312 else {
1313  
1314 # file doesn't exist - check for a file glob
1315 0 ▶ if ( $input_file =~ /([\?\*\[\{])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_file =~ /([\?\*\[\{])/) 0 0 0
1316  
1317 # Windows shell may not remove quotes, so do it
1318 0 my $ifile = $input_file;
1319 0 ▶ if ( $ifile =~ /^\'(.+)\'$/ ) { $ifile = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ifile =~ /^\'(.+)\'$/) 0 0 0
1320 0 ▶ if ( $ifile =~ /^\"(.+)\"$/ ) { $ifile = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ifile =~ /^\"(.+)\"$/) 0 0 0
1321 0 my $pattern = fileglob_to_re($ifile);
1322 0 my $dh;
1323 0 ▶ if ( opendir( $dh, './' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir $dh, "./") 0 0 0
1324 my @files =
1325 0 ▶ grep { /$pattern/ && !-d } readdir($dh);
Condition0%0/3
/$pattern/ && !(-d $_)
!ll&&!rl&&r
000
MC/DC0%0/2
/$pattern/ && !(-d $_)
/$pattern/ !(-d $_)
Truth table0%0/3
A && not(B)
A= /$pattern/B= !(-d $_)
ABresult
0X0
100
111
1326 0 closedir($dh);
1327 0 ▶ next unless (@files);
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
1328 0 push @updated_files, @files;
1329 0 next;
1330 }
1331 }
1332 0 Warn("skipping file: '$input_file': no matches found\n");
1333 0 next;
1334 }
1335 } ## end loop over input filenames
1336  
1337 0 @Arg_files = @updated_files;
1338 0 ▶ if ( !@Arg_files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@Arg_files) 0 0 0
1339 0 Die("no matching input files found\n");
1340 }
1341 }
1342  
1343 # Flag for loading module Unicode::GCString for evaluating text width:
1344 # undef = ok to use but not yet loaded
1345 # 0 = do not use; failed to load or not wanted
1346 # 1 = successfully loaded and ok to use
1347 # The module is not actually loaded unless/until it is needed
1348 664 ▶ if ( !$rOpts->{'use-unicode-gcstring'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rOpts->{"use-unicode-gcstring"}) 664 0 664
1349 664 $loaded_unicode_gcstring = 0;
1350 }
1351  
1352 # Remove duplicate filenames. Otherwise, for example if the user entered
1353 # perltidy -b myfile.pl myfile.pl
1354 # the backup version of the original would be lost.
1355 664 ▶ if ( @Arg_files > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Arg_files > 1) 0 664 664
1356 0 my %seen = ();
1357 0 @Arg_files = grep { !$seen{$_}++ } @Arg_files;
1358 }
1359  
1360 # If requested, process in order of increasing file size
1361 # This can significantly reduce perl's virtual memory usage during testing.
1362 664 ▶ if ( @Arg_files > 1 && $rOpts->{'file-size-order'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Arg_files > 1 and $rOpts->{"file-size-order"}) 0 664 664
Condition33%1/3
@Arg_files > 1 and $rOpts->{"file-size-order"}
!ll&&!rl&&r
66400
MC/DC0%0/2
@Arg_files > 1 and $rOpts->{"file-size-order"}
@Arg_files > 1 $rOpts->{"file-size-order"}
Truth table33%1/3
A and B
A= @Arg_files > 1B= $rOpts->{"file-size-order"}
ABresult
0X0
100
111
1363 @Arg_files =
1364 0 map { $_->[0] }
1365 0 sort { $a->[1] <=> $b->[1] }
1366 0 ▶ map { [ $_, -e $_ ? -s $_ : 0 ] } @Arg_files;
Branch: 1 branch
BranchTrueFalseTotal
-e $_ ? : 0 0 0
1367 }
1368  
1369 664 my $logfile_header = make_logfile_header( $rOpts, $config_file,
1370 $rraw_options, $Windows_type, $readable_options );
1371  
1372 # Store some values needed by lower level routines
1373 664 $self->[_diagnostics_object_] = $diagnostics_object;
1374 664 $self->[_postfilter_] = $postfilter;
1375 664 $self->[_prefilter_] = $prefilter;
1376 664 $self->[_user_formatter_] = $user_formatter;
1377  
1378 #--------------------------
1379 # loop to process all files
1380 #--------------------------
1381 664 $self->process_all_files(
1382 {
1383 rinput_hash => \%input_hash,
1384 rfiles => \@Arg_files,
1385 line_range_clipped => $line_range_clipped,
1386  
1387 # filename stuff...
1388 source_stream => $source_stream,
1389 output_extension => $output_extension,
1390 forbidden_file_extensions => $forbidden_file_extensions,
1391 in_place_modify => $in_place_modify,
1392 backup_extension => $backup_extension,
1393 delete_backup => $delete_backup,
1394  
1395 # logfile stuff...
1396 logfile_header => $logfile_header,
1397 rpending_complaint => $rpending_complaint,
1398 rpending_logfile_message => $rpending_logfile_message,
1399 }
1400 );
1401  
1402 #-----
1403 # Exit
1404 #-----
1405  
1406 # Fix for RT #130297: return a true value if anything was written to the
1407 # standard error output, even non-fatal warning messages, otherwise return
1408 # false.
1409  
1410 # These exit codes are returned:
1411 # 0 = perltidy ran to completion with no errors
1412 # 1 = perltidy could not run to completion due to errors
1413 # 2 = perltidy ran to completion with error messages
1414  
1415 # Note that if perltidy is run with multiple files, any single file with
1416 # errors or warnings will write a line like
1417 # '## Please see file testing.t.ERR'
1418 # to standard output for each file with errors, so the flag will be true,
1419 # even if only some of the multiple files may have had errors.
1420  
1421 664 ▶ NORMAL_EXIT:
Branch: 1 branch
BranchTrueFalseTotal
$Warn_count ? : 0 664 664
1422 my $ret = $Warn_count ? 2 : 0;
1423 664 ▶ return wantarray ? ( $ret, $rstatus ) : $ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 664 664
1424  
1425 0 ▶ ERROR_EXIT:
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1426 return wantarray ? ( 1, $rstatus ) : 1;
1427  
1428 } ## end sub perltidy
1429  
1430 666 ▶sub make_file_extension {
Subroutine make_file_extension: called CC=5 SCAR=16.4
Pod undocumented
1431  
1432 # Make a file extension, adding any leading '.' if necessary.
1433 # (the '.' may actually be an '_' under VMS).
1434 666 my ( $self, $extension, ($default) ) = @_;
1435  
1436 # Given:
1437 # $extension = the first choice (usually a user entry)
1438 # $default = an optional backup extension
1439 # Return:
1440 # $extension = the actual file extension
1441  
1442 666 ▶ $extension = EMPTY_STRING unless ( defined($extension) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extension 666 0 666
1443 666 $extension =~ s/^\s+//;
1444 666 $extension =~ s/\s+$//;
1445  
1446 # Use default extension if nothing remains of the first choice
1447 666 ▶ if ( length($extension) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $extension == 0) 666 0 666
1448 666 $extension = $default;
1449 666 ▶ $extension = EMPTY_STRING unless ( defined($extension) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $extension 0 666 666
1450 666 $extension =~ s/^\s+//;
1451 666 $extension =~ s/\s+$//;
1452 }
1453  
1454 # Only extensions with these leading characters get a '.'
1455 # This rule gives the user some freedom.
1456 666 ▶ if ( $extension =~ /^[a-zA-Z0-9]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extension =~ /^[a-zA-Z0-9]/) 666 0 666
1457 666 my $dot = $self->[_file_extension_separator_];
1458 666 $extension = $dot . $extension;
1459 }
1460 666 return $extension;
1461 } ## end sub make_file_extension
1462  
1463 664 ▶sub check_in_place_modify {
Subroutine check_in_place_modify: called CC=17 SCAR=43.6
Pod undocumented
1464  
1465 664 my ( $self, $source, $destination_stream ) = @_;
1466  
1467 # See if --backup-and-modify-in-place (-b) is set, and if so,
1468 # return its associated parameters
1469 664 my $rOpts = $self->[_rOpts_];
1470  
1471 # check for -b option;
1472 # silently ignore unless beautify mode
1473 my $in_place_modify = $rOpts->{'backup-and-modify-in-place'}
1474 664 ▶ && $rOpts->{'format'} eq 'tidy';
Condition66%2/3
$rOpts->{'backup-and-modify-in-place'} && $rOpts->{'format'} eq 'tidy'
!ll&&!rl&&r
66202
MC/DC50%1/2
$rOpts->{'backup-and-modify-in-place'} && $rOpts->{'format'} eq 'tidy'
$rOpts->{'backup-and-modify-in-place'} $rOpts->{'format'} eq 'tidy'
Truth table66%2/3
A && B
A= $rOpts->{'backup-and-modify-in-place'}B= $rOpts->{'format'} eq 'tidy'
ABresult
0X0
100
111
1475  
1476 664 my ( $backup_extension, $delete_backup );
1477  
1478 # Turn off -b with warnings in case of conflicts with other options.
1479 # NOTE: Do this silently, without warnings, if there is a source or
1480 # destination stream, or standard output is used. This is because the -b
1481 # flag may have been in a .perltidyrc file and warnings break
1482 # Test::NoWarnings. See email discussion with Merijn Brand 26 Feb 2014.
1483 664 ▶ if ($in_place_modify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_place_modify) 2 662 664
1484 2 ▶ if ( $destination_stream
Branch: 2 branches
BranchTrueFalseTotal
if ($destination_stream or not defined $source or ref $source or $source eq '-' or $rOpts->{'outfile'} or defined $rOpts->{'output-path'}) { } 2 0 2
elsif ($rOpts->{'standard-output'}) { } 0 0 0
Condition33%5/15
$destination_stream or not defined $source or ref $source or $source eq "-" or $rOpts->{"outfile"} or defined $rOpts->{"output-path"}
l!l&&r!l&&!r
200
$destination_stream or not defined $source or ref $source or $source eq "-" or $rOpts->{"outfile"}
l!l&&r!l&&!r
200
$destination_stream or not defined $source or ref $source or $source eq "-"
l!l&&r!l&&!r
200
$destination_stream or not defined $source or ref $source
l!l&&r!l&&!r
200
$destination_stream or not defined $source
l!l&&r!l&&!r
200
MC/DC0%0/6
$destination_stream or not defined $source or ref $source or $source eq "-" or $rOpts->{"outfile"} or defined $rOpts->{"output-path"}
$destination_stream not defined $source ref $source $source eq "-" $rOpts->{"outfile"} defined $rOpts->{"output-path"}
Truth table14%1/7
A or not(B) or C or D or E or F
A= $destination_streamB= not defined $sourceC= ref $sourceD= $source eq "-"E= $rOpts->{"outfile"}F= defined $rOpts->{"output-path"}
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
1485 || !defined($source)
1486 || ref($source)
1487 || $source eq '-'
1488 || $rOpts->{'outfile'}
1489 || defined( $rOpts->{'output-path'} ) )
1490 {
1491 2 $in_place_modify = 0;
1492 }
1493  
1494 # But Warn or Nag for certain conflicts with -st. This can happen for
1495 # example if user chooses -pbp and -b because -st is hidden in -pbp.
1496 elsif ( $rOpts->{'standard-output'} ) {
1497 0 $in_place_modify = 0;
1498  
1499 0 my $rOpts_in_profile = $self->[_rOpts_in_profile_];
1500 0 ▶ if ( !$rOpts_in_profile->{'backup-and-modify-in-place'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $rOpts_in_profile->{'backup-and-modify-in-place'}) { } 0 0 0
elsif ($rOpts_in_profile->{'standard-output'}) { } 0 0 0
1501 0 Warn(
1502 "## warning: conflict of -st with -b: -st has priority; use -nst to activate -b\n"
1503 );
1504 }
1505 elsif ( $rOpts_in_profile->{'standard-output'} ) {
1506 0 Nag(
1507 "## warning: conflict of -st and -b in profile: -st has priority; use -nst to activate -b\n"
1508 );
1509 }
1510 else {
1511 ## keep quiet
1512 }
1513 }
1514 else {
1515 ## ok to use -b
1516 }
1517 }
1518  
1519 664 ▶ if ($in_place_modify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_place_modify) 0 664 664
1520  
1521 # If the backup extension contains a / character then the backup should
1522 # be deleted when the -b option is used. On older versions of
1523 # perltidy this will generate an error message due to an illegal
1524 # file name.
1525 #
1526 # A backup file will still be generated but will be deleted
1527 # at the end. If -bext='/' then this extension will be
1528 # the default 'bak'. Otherwise it will be whatever characters
1529 # remains after all '/' characters are removed. For example:
1530 # -bext extension slashes
1531 # '/' bak 1
1532 # '/delete' delete 1
1533 # 'delete/' delete 1
1534 # '/dev/null' devnull 2 (Currently not allowed)
1535 0 my $bext = $rOpts->{'backup-file-extension'};
1536 0 $delete_backup = ( $rOpts->{'backup-file-extension'} =~ s/\///g );
1537  
1538 # At present only one forward slash is allowed. In the future multiple
1539 # slashes may be allowed to allow for other options
1540 0 ▶ if ( $delete_backup > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_backup > 1) 0 0 0
1541 0 Die("-bext=$bext contains more than one '/'\n");
1542 }
1543  
1544 $backup_extension =
1545 0 $self->make_file_extension( $rOpts->{'backup-file-extension'},
1546 'bak' );
1547 }
1548  
1549 664 my $backup_method = $rOpts->{'backup-method'};
1550 664 ▶ if ( defined($backup_method)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $backup_method and $backup_method ne "copy" and $backup_method ne "move") 0 664 664
Condition33%2/6
defined $backup_method and $backup_method ne "copy" and $backup_method ne "move"
!ll&&!rl&&r
66400
defined $backup_method and $backup_method ne "copy"
!ll&&!rl&&r
06640
MC/DC0%0/3
defined $backup_method and $backup_method ne "copy" and $backup_method ne "move"
defined $backup_method $backup_method ne "copy" $backup_method ne "move"
Truth table25%1/4
A and B and C
A= defined $backup_methodB= $backup_method ne "copy"C= $backup_method ne "move"
ABCresult
0XX0
10X0
1100
1111
1551 && $backup_method ne 'copy'
1552 && $backup_method ne 'move' )
1553 {
1554 0 Die(
1555 "Unexpected --backup-method='$backup_method'; must be one of: 'move', 'copy'\n"
1556 );
1557 }
1558  
1559 664 return ( $in_place_modify, $backup_extension, $delete_backup );
1560 } ## end sub check_in_place_modify
1561  
1562 0 ▶sub backup_method_copy {
Subroutine backup_method_copy: not called CC=23 SCAR=63.1
Pod undocumented
1563  
1564 0 my ( $self, $input_file, $routput_string, $backup_extension,
1565 $delete_backup )
1566 = @_;
1567  
1568 # Handle the -b (--backup-and-modify-in-place) option with -bm='copy':
1569 # - First copy $input file to $backup_name.
1570 # - Then open input file and rewrite with contents of $routput_string
1571 # - Then delete the backup if requested
1572  
1573 # NOTES:
1574 # - Die immediately on any error.
1575 # - $routput_string is a SCALAR ref
1576  
1577 0 my $backup_file = $input_file . $backup_extension;
1578  
1579 0 ▶ if ( !-f $input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $input_file) 0 0 0
1580  
1581 # no real file to backup ..
1582 # This shouldn't happen because of numerous preliminary checks
1583 0 Die(
1584 "problem with -b backing up input file '$input_file': not a file\n"
1585 );
1586 }
1587  
1588 0 ▶ if ( -f $backup_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $backup_file) 0 0 0
1589 0 ▶ unlink($backup_file)
Branch: 1 branch
BranchTrueFalseTotal
unlink $backup_file or Die("unable to remove previous '${backup_file}' for -b option; check permissions: $Perl::Tidy::OS_ERROR\n") 0 0 0
1590 or Die(
1591 "unable to remove previous '$backup_file' for -b option; check permissions: $OS_ERROR\n"
1592 );
1593 }
1594  
1595 # Copy input file to backup
1596 0 ▶ File::Copy::copy( $input_file, $backup_file )
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($input_file, $backup_file) or Die("File::Copy failed trying to backup source: $Perl::Tidy::OS_ERROR") 0 0 0
1597 or Die("File::Copy failed trying to backup source: $OS_ERROR");
1598  
1599 # set permissions of the backup file to match the input file
1600 0 my @input_file_stat = stat($input_file);
1601 0 my $in_place_modify = 1;
1602 0 $self->set_output_file_permissions( $backup_file, \@input_file_stat,
1603 $in_place_modify );
1604  
1605 # set the modification time of the copy to the original value (rt#145999)
1606 0 my ( $read_time, $write_time ) = @input_file_stat[ _atime_, _mtime_ ];
1607 0 ▶ if ( defined($write_time) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $write_time) 0 0 0
1608 0 ▶ utime( $read_time, $write_time, $backup_file )
Branch: 1 branch
BranchTrueFalseTotal
utime $read_time, $write_time, $backup_file or Warn("error setting mtime for backup file '${backup_file}'\n") 0 0 0
1609 || Warn("error setting mtime for backup file '$backup_file'\n");
1610 }
1611  
1612 # Open the original input file for writing ... opening with ">" will
1613 # truncate the existing data.
1614 0 ▶ open( my $fout, ">", $input_file )
Branch: 1 branch
BranchTrueFalseTotal
open my $fout, ">", $input_file or Die("problem re-opening $input_file for write for -b option; check file and directory permissions: $Perl::Tidy::OS_ERROR\n") 0 0 0
1615 or Die(
1616 "problem re-opening $input_file for write for -b option; check file and directory permissions: $OS_ERROR\n"
1617 );
1618  
1619 0 ▶ if ( $self->[_is_encoded_data_] ) { binmode $fout, ":raw:encoding(UTF-8)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[9]) { } 0 0 0
1620 0 else { binmode $fout }
1621  
1622 # Now copy the formatted output to it..
1623 # output must be SCALAR ref..
1624 0 ▶ if ( ref($routput_string) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $routput_string eq 'SCALAR') { } 0 0 0
1625 0 ▶ $fout->print( ${$routput_string} )
Branch: 1 branch
BranchTrueFalseTotal
$fout->print(${$routput_string;}) or Die("cannot print to '${input_file}' with -b option: $Perl::Tidy::OS_ERROR\n") 0 0 0
1626 or Die("cannot print to '$input_file' with -b option: $OS_ERROR\n");
1627 }
1628  
1629 # Error if anything else ...
1630 else {
1631 0 my $ref = ref($routput_string);
1632 0 Die(<<EOM);
1633 Programming error: unable to print to '$input_file' with -b option:
1634 unexpected ref type '$ref'; expecting 'ARRAY' or 'SCALAR'
1635 EOM
1636 }
1637  
1638 0 ▶ $fout->close()
Branch: 1 branch
BranchTrueFalseTotal
$fout->close or Die("cannot close '${input_file}' with -b option: $Perl::Tidy::OS_ERROR\n") 0 0 0
1639 or Die("cannot close '$input_file' with -b option: $OS_ERROR\n");
1640  
1641 # Set permissions of the output file to match the input file. This is
1642 # necessary even if the inode remains unchanged because suid/sgid bits may
1643 # have been reset.
1644 0 $self->set_output_file_permissions( $input_file, \@input_file_stat,
1645 $in_place_modify );
1646  
1647 # Keep original modification time if no change (rt#145999)
1648 0 ▶ if ( !$self->[_input_output_difference_] && defined($write_time) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->[25] and defined $write_time) 0 0 0
Condition0%0/3
not $self->[25] and defined $write_time
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->[25] and defined $write_time
not $self->[25] defined $write_time
Truth table0%0/3
not(A) and B
A= not $self->[25]B= defined $write_time
ABresult
0X0
100
111
1649 0 ▶ utime( $read_time, $write_time, $input_file )
Branch: 1 branch
BranchTrueFalseTotal
utime $read_time, $write_time, $input_file or Warn("error setting mtime for '${input_file}'\n") 0 0 0
1650 || Warn("error setting mtime for '$input_file'\n");
1651 }
1652  
1653 #---------------------------------------------------------
1654 # remove the original file for in-place modify as follows:
1655 # $delete_backup=0 never
1656 # $delete_backup=1 only if no errors
1657 # $delete_backup>1 always : NOT ALLOWED, too risky
1658 #---------------------------------------------------------
1659 0 ▶ if ( $delete_backup && -f $backup_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_backup and -f $backup_file) 0 0 0
Condition0%0/3
$delete_backup and -f $backup_file
!ll&&!rl&&r
000
MC/DC0%0/2
$delete_backup and -f $backup_file
$delete_backup -f $backup_file
Truth table0%0/3
A and B
A= $delete_backupB= -f $backup_file
ABresult
0X0
100
111
1660  
1661 # Currently, $delete_backup may only be 1. But if a future update
1662 # allows a value > 1, then reduce it to 1 if there were warnings.
1663 0 ▶ if ( $delete_backup > 1
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_backup > 1 and $self->[15]->get_warning_count) 0 0 0
Condition0%0/3
$delete_backup > 1 and $self->[15]->get_warning_count
!ll&&!rl&&r
000
MC/DC0%0/2
$delete_backup > 1 and $self->[15]->get_warning_count
$delete_backup > 1 $self->[15]->get_warning_count
Truth table0%0/3
A and B
A= $delete_backup > 1B= $self->[15]->get_warning_count
ABresult
0X0
100
111
1664 && $self->[_logger_object_]->get_warning_count() )
1665 {
1666 0 $delete_backup = 1;
1667 }
1668  
1669 # As an added safety precaution, do not delete the source file
1670 # if its size has dropped from positive to zero, since this
1671 # could indicate a disaster of some kind, including a hardware
1672 # failure. Actually, this could happen if you had a file of
1673 # all comments (or pod) and deleted everything with -dac (-dap)
1674 # for some reason.
1675 0 ▶ if ( !-s $input_file && -s $backup_file && $delete_backup == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -s $input_file and -s $backup_file and $delete_backup == 1) { } 0 0 0
Condition0%0/6
not -s $input_file and -s $backup_file and $delete_backup == 1
!ll&&!rl&&r
000
not -s $input_file and -s $backup_file
!ll&&!rl&&r
000
MC/DC0%0/3
not -s $input_file and -s $backup_file and $delete_backup == 1
not -s $input_file -s $backup_file $delete_backup == 1
Truth table0%0/4
not(A) and B and C
A= not -s $input_fileB= -s $backup_fileC= $delete_backup == 1
ABCresult
0XX0
10X0
1100
1111
1676 0 Warn(
1677 "output file '$input_file' missing or zero length; original '$backup_file' not deleted\n"
1678 );
1679 }
1680 else {
1681 0 ▶ unlink($backup_file)
Branch: 1 branch
BranchTrueFalseTotal
unlink $backup_file or Die("unable to remove backup file '${backup_file}' for -b option; check permissions: $Perl::Tidy::OS_ERROR\n") 0 0 0
1682 or Die(
1683 "unable to remove backup file '$backup_file' for -b option; check permissions: $OS_ERROR\n"
1684 );
1685 }
1686 }
1687  
1688 # Verify that inode is unchanged during development
1689 0 if (DEVEL_MODE) {
1690 my @output_file_stat = stat($input_file);
1691 my $inode_input = $input_file_stat[1];
1692 my $inode_output = $output_file_stat[1];
1693 if ( $inode_input != $inode_output ) {
1694 Fault(<<EOM);
1695 inode changed with -bm=copy for file '$input_file': inode_input=$inode_input inode_output=$inode_output
1696 EOM
1697 }
1698 }
1699  
1700 0 return;
1701 } ## end sub backup_method_copy
1702  
1703 0 ▶sub backup_method_move {
Subroutine backup_method_move: not called CC=23 SCAR=63.1
Pod undocumented
1704  
1705 0 my ( $self, $input_file, $routput_string, $backup_extension,
1706 $delete_backup )
1707 = @_;
1708  
1709 # Handle the -b (--backup-and-modify-in-place) option with -bm='move':
1710 # - First move $input file to $backup_name.
1711 # - Then copy $routput_string to $input_file.
1712 # - Then delete the backup if requested
1713  
1714 # NOTES:
1715 # - Die immediately on any error.
1716 # - $routput_string is a SCALAR ref
1717 # - $input_file permissions will be set by sub set_output_file_permissions
1718  
1719 0 my $backup_name = $input_file . $backup_extension;
1720  
1721 0 ▶ if ( !-f $input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $input_file) 0 0 0
1722  
1723 # oh, oh, no real file to backup ..
1724 # shouldn't happen because of numerous preliminary checks
1725 0 Die(
1726 "problem with -b backing up input file '$input_file': not a file\n"
1727 );
1728 }
1729 0 ▶ if ( -f $backup_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $backup_name) 0 0 0
1730 0 ▶ unlink($backup_name)
Branch: 1 branch
BranchTrueFalseTotal
unlink $backup_name or Die("unable to remove previous '${backup_name}' for -b option; check permissions: $Perl::Tidy::OS_ERROR\n") 0 0 0
1731 or Die(
1732 "unable to remove previous '$backup_name' for -b option; check permissions: $OS_ERROR\n"
1733 );
1734 }
1735  
1736 0 my @input_file_stat = stat($input_file);
1737  
1738 # backup the input file
1739 # we use copy for symlinks, move for regular files
1740 0 ▶ if ( -l $input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $input_file) { } 0 0 0
1741 0 ▶ File::Copy::copy( $input_file, $backup_name )
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($input_file, $backup_name) or Die("File::Copy failed trying to backup source: $Perl::Tidy::OS_ERROR") 0 0 0
1742 or Die("File::Copy failed trying to backup source: $OS_ERROR");
1743 }
1744 else {
1745 0 ▶ rename( $input_file, $backup_name )
Branch: 1 branch
BranchTrueFalseTotal
rename $input_file, $backup_name or Die("problem renaming $input_file to $backup_name for -b option: $Perl::Tidy::OS_ERROR\n") 0 0 0
1746 or Die(
1747 "problem renaming $input_file to $backup_name for -b option: $OS_ERROR\n"
1748 );
1749 }
1750  
1751 # Open a file with the original input file name for writing ...
1752 0 ▶ open( my $fout, ">", $input_file )
Branch: 1 branch
BranchTrueFalseTotal
open my $fout, ">", $input_file or Die("problem re-opening $input_file for write for -b option; check file and directory permissions: $Perl::Tidy::OS_ERROR\n") 0 0 0
1753 or Die(
1754 "problem re-opening $input_file for write for -b option; check file and directory permissions: $OS_ERROR\n"
1755 );
1756  
1757 0 ▶ if ( $self->[_is_encoded_data_] ) { binmode $fout, ":raw:encoding(UTF-8)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[9]) { } 0 0 0
1758 0 else { binmode $fout }
1759  
1760 # Now copy the formatted output to it..
1761 # output must be SCALAR ref..
1762 0 ▶ if ( ref($routput_string) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $routput_string eq 'SCALAR') { } 0 0 0
1763 0 ▶ $fout->print( ${$routput_string} )
Branch: 1 branch
BranchTrueFalseTotal
$fout->print(${$routput_string;}) or Die("cannot print to '${input_file}' with -b option: $Perl::Tidy::OS_ERROR\n") 0 0 0
1764 or Die("cannot print to '$input_file' with -b option: $OS_ERROR\n");
1765 }
1766  
1767 # Error if anything else ...
1768 else {
1769 0 my $ref = ref($routput_string);
1770 0 Die(<<EOM);
1771 Programming error: unable to print to '$input_file' with -b option:
1772 unexpected ref type '$ref'; expecting 'ARRAY' or 'SCALAR'
1773 EOM
1774 }
1775  
1776 0 ▶ $fout->close()
Branch: 1 branch
BranchTrueFalseTotal
$fout->close or Die("cannot close '${input_file}' with -b option: $Perl::Tidy::OS_ERROR\n") 0 0 0
1777 or Die("cannot close '$input_file' with -b option: $OS_ERROR\n");
1778  
1779 # set permissions of the output file to match the input file
1780 0 my $in_place_modify = 1;
1781 0 $self->set_output_file_permissions( $input_file, \@input_file_stat,
1782 $in_place_modify );
1783  
1784 # Keep original modification time if no change (rt#145999)
1785 0 my ( $read_time, $write_time ) = @input_file_stat[ _atime_, _mtime_ ];
1786 0 ▶ if ( !$self->[_input_output_difference_] && defined($write_time) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->[25] and defined $write_time) 0 0 0
Condition0%0/3
not $self->[25] and defined $write_time
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->[25] and defined $write_time
not $self->[25] defined $write_time
Truth table0%0/3
not(A) and B
A= not $self->[25]B= defined $write_time
ABresult
0X0
100
111
1787 0 ▶ utime( $read_time, $write_time, $input_file )
Branch: 1 branch
BranchTrueFalseTotal
utime $read_time, $write_time, $input_file or Warn("error setting mtime for '${input_file}'\n") 0 0 0
1788 || Warn("error setting mtime for '$input_file'\n");
1789 }
1790  
1791 #---------------------------------------------------------
1792 # remove the original file for in-place modify as follows:
1793 # $delete_backup=0 never
1794 # $delete_backup=1 only if no errors
1795 # $delete_backup>1 always : NOT ALLOWED, too risky
1796 #---------------------------------------------------------
1797 0 ▶ if ( $delete_backup && -f $backup_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_backup and -f $backup_name) 0 0 0
Condition0%0/3
$delete_backup and -f $backup_name
!ll&&!rl&&r
000
MC/DC0%0/2
$delete_backup and -f $backup_name
$delete_backup -f $backup_name
Truth table0%0/3
A and B
A= $delete_backupB= -f $backup_name
ABresult
0X0
100
111
1798  
1799 # Currently, $delete_backup may only be 1. But if a future update
1800 # allows a value > 1, then reduce it to 1 if there were warnings.
1801 0 ▶ if ( $delete_backup > 1
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_backup > 1 and $self->[15]->get_warning_count) 0 0 0
Condition0%0/3
$delete_backup > 1 and $self->[15]->get_warning_count
!ll&&!rl&&r
000
MC/DC0%0/2
$delete_backup > 1 and $self->[15]->get_warning_count
$delete_backup > 1 $self->[15]->get_warning_count
Truth table0%0/3
A and B
A= $delete_backup > 1B= $self->[15]->get_warning_count
ABresult
0X0
100
111
1802 && $self->[_logger_object_]->get_warning_count() )
1803 {
1804 0 $delete_backup = 1;
1805 }
1806  
1807 # As an added safety precaution, do not delete the source file
1808 # if its size has dropped from positive to zero, since this
1809 # could indicate a disaster of some kind, including a hardware
1810 # failure. Actually, this could happen if you had a file of
1811 # all comments (or pod) and deleted everything with -dac (-dap)
1812 # for some reason.
1813 0 ▶ if ( !-s $input_file && -s $backup_name && $delete_backup == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -s $input_file and -s $backup_name and $delete_backup == 1) { } 0 0 0
Condition0%0/6
not -s $input_file and -s $backup_name and $delete_backup == 1
!ll&&!rl&&r
000
not -s $input_file and -s $backup_name
!ll&&!rl&&r
000
MC/DC0%0/3
not -s $input_file and -s $backup_name and $delete_backup == 1
not -s $input_file -s $backup_name $delete_backup == 1
Truth table0%0/4
not(A) and B and C
A= not -s $input_fileB= -s $backup_nameC= $delete_backup == 1
ABCresult
0XX0
10X0
1100
1111
1814 0 Warn(
1815 "output file '$input_file' missing or zero length; original '$backup_name' not deleted\n"
1816 );
1817 }
1818 else {
1819 0 ▶ unlink($backup_name)
Branch: 1 branch
BranchTrueFalseTotal
unlink $backup_name or Die("unable to remove previous '${backup_name}' for -b option; check permissions: $Perl::Tidy::OS_ERROR\n") 0 0 0
1820 or Die(
1821 "unable to remove previous '$backup_name' for -b option; check permissions: $OS_ERROR\n"
1822 );
1823 }
1824 }
1825  
1826 0 return;
1827  
1828 } ## end sub backup_method_move
1829  
1830 # masks for file permissions
1831 45 ▶use constant OCT_777 => oct(777); # All users (O+G+W) + r/w/x bits
Subroutine BEGIN: called CC=1 SCAR=0.0
1832 45 ▶use constant OCT_7777 => oct(7777); # Same + suid/sgid/sbit
Subroutine BEGIN: called CC=1 SCAR=0.0
1833 45 ▶use constant OCT_600 => oct(600); # Owner RW permission
Subroutine BEGIN: called CC=1 SCAR=0.0
1834  
1835 2 ▶sub set_output_file_permissions {
Subroutine set_output_file_permissions: called CC=8 SCAR=29.1
Pod undocumented
1836  
1837 2 my ( $self, $output_file, $rinput_file_stat, $in_place_modify ) = @_;
1838  
1839 # Set the permissions for the output file
1840  
1841 # Given:
1842 # $output_file = the file whose permissions we will set
1843 # $rinput_file_stat = the result of stat($input_file)
1844 # $in_place_modify = true if --backup-and-modify-in-place is set
1845  
1846 my ( $mode_i, $uid_i, $gid_i ) =
1847 2 @{$rinput_file_stat}[ _mode_, _uid_, _gid_ ];
1848 2 my ( $uid_o, $gid_o ) = ( stat($output_file) )[ _uid_, _gid_ ];
1849 2 my $input_file_permissions = $mode_i & OCT_7777;
1850 2 my $output_file_permissions = $input_file_permissions;
1851  
1852 #rt128477: avoid inconsistent owner/group and suid/sgid
1853 2 ▶ if ( $uid_i != $uid_o || $gid_i != $gid_o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uid_i != $uid_o or $gid_i != $gid_o) 0 2 2
Condition33%1/3
$uid_i != $uid_o or $gid_i != $gid_o
l!l&&r!l&&!r
002
MC/DC0%0/2
$uid_i != $uid_o or $gid_i != $gid_o
$uid_i != $uid_o $gid_i != $gid_o
Truth table33%1/3
A or B
A= $uid_i != $uid_oB= $gid_i != $gid_o
ABresult
1X1
011
000
1854  
1855 # try to change owner and group to match input file if
1856 # in -b mode. Note: chown returns number of files
1857 # successfully changed.
1858 0 ▶ if ( $in_place_modify
Branch: 1 branch
BranchTrueFalseTotal
if ($in_place_modify and chown $uid_i, $gid_i, $output_file) { } 0 0 0
Condition0%0/3
$in_place_modify and chown $uid_i, $gid_i, $output_file
!ll&&!rl&&r
000
MC/DC0%0/2
$in_place_modify and chown $uid_i, $gid_i, $output_file
$in_place_modify chown $uid_i, $gid_i, $output_file
Truth table0%0/3
A and B
A= $in_place_modifyB= chown $uid_i, $gid_i, $output_file
ABresult
0X0
100
111
1859 && chown( $uid_i, $gid_i, $output_file ) )
1860 {
1861 # owner/group successfully changed
1862 }
1863 else {
1864  
1865 # owner or group differ: do not copy suid and sgid
1866 0 $output_file_permissions = $mode_i & OCT_777;
1867 0 ▶ if ( $input_file_permissions != $output_file_permissions ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_file_permissions != $output_file_permissions) 0 0 0
1868 0 Warn(
1869 "Unable to copy setuid and/or setgid bits for output file '$output_file'\n"
1870 );
1871 }
1872 }
1873 }
1874  
1875 # Mark the output file for rw unless we are in -b mode.
1876 # Explanation: perltidy does not unlink existing output
1877 # files before writing to them, for safety. If a
1878 # designated output file exists and is not writable,
1879 # perltidy will halt. This can prevent a data loss if a
1880 # user accidentally enters "perltidy infile -o
1881 # important_ro_file", or "perltidy infile -st
1882 # >important_ro_file". But it also means that perltidy can
1883 # get locked out of rerunning unless it marks its own
1884 # output files writable. The alternative, of always
1885 # unlinking the designated output file, is less safe and
1886 # not always possible, except in -b mode, where there is an
1887 # assumption that a previous backup can be unlinked even if
1888 # not writable.
1889 2 ▶ if ( !$in_place_modify ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_place_modify) 2 0 2
1890 2 $output_file_permissions |= OCT_600;
1891 }
1892  
1893 2 ▶ if ( !chmod( $output_file_permissions, $output_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod $output_file_permissions, $output_file) 0 2 2
1894  
1895 # couldn't change file permissions
1896 0 my $operm = sprintf( "%04o", $output_file_permissions );
1897 0 Warn(
1898 "Unable to set permissions for output file '$output_file' to $operm\n"
1899 );
1900 }
1901 2 return;
1902 } ## end sub set_output_file_permissions
1903  
1904 664 ▶sub get_decoded_string_buffer {
Subroutine get_decoded_string_buffer: called CC=24 SCAR=42.7
Pod undocumented
1905  
1906 664 my ( $self, $input_file, $display_name ) = @_;
1907  
1908 # Decode the input buffer from utf8 if necessary or requested
1909  
1910 # Given:
1911 # $input_file = the input file or stream
1912 # $display_name = its name to use in error messages
1913  
1914 # Set $self->[_line_separator_], and
1915  
1916 # Return:
1917 # $rinput_string = ref to input string, decoded from utf8 if necessary
1918 # $is_encoded_data = true if $buf is decoded from utf8
1919 # $decoded_input_as = true if perltidy decoded input buf
1920 # $encoding_log_message = messages for log file,
1921 # $length_function = function to use for measuring string width
1922  
1923 # Return nothing on any error; this is a signal to skip this file
1924  
1925 664 my $rOpts = $self->[_rOpts_];
1926  
1927 my $rinput_string =
1928 664 stream_slurp( $input_file, $rOpts->{'timeout-in-seconds'} );
1929 664 ▶ return unless ( defined($rinput_string) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rinput_string 0 664 664
1930  
1931 # Note that we could have a zero size input string here if it
1932 # arrived from standard input or from a string ref. For example
1933 # 'perltidy <null.pl'. If we issue a warning and stop, as we would
1934 # for a zero length file ('perltidy null.pl'), then we could cause
1935 # a call to the perltidy module to misbehave as a filter. So we will
1936 # process this as any other file in this case without any warning (c286).
1937 664 ▶ if ( !length( ${$rinput_string} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length ${$rinput_string;}) 2 662 664
1938  
1939 # zero length, but keep going
1940 }
1941  
1942 # Check size of strings arriving from the standard input. These
1943 # could not be checked until now.
1944 664 ▶ if ( $input_file eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_file eq "-") 0 664 664
1945 my $size_in_mb =
1946 0 length( ${$rinput_string} ) / ( CONST_1024 * CONST_1024 );
1947 0 my $maximum_file_size_mb = $rOpts->{'maximum-file-size-mb'};
1948 0 ▶ if ( $size_in_mb > $maximum_file_size_mb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size_in_mb > $maximum_file_size_mb) 0 0 0
1949 0 $size_in_mb = sprintf( "%0.1f", $size_in_mb );
1950 0 Warn(
1951 "skipping file: <stdin>: size $size_in_mb MB exceeds limit $maximum_file_size_mb; use -maxfs=i to change\n"
1952 );
1953 0 return;
1954 }
1955 }
1956  
1957 664 $rinput_string = $self->set_line_separator($rinput_string);
1958  
1959 664 my $encoding_in = EMPTY_STRING;
1960 664 my $rOpts_character_encoding = $rOpts->{'character-encoding'};
1961 664 my $encoding_log_message;
1962 664 my $decoded_input_as = EMPTY_STRING;
1963 664 $rstatus->{'char_mode_source'} = 0;
1964  
1965 664 my $is_pure_ascii_data = !( ${$rinput_string} =~ /[^[:ascii:]]/ );
1966 664 $self->[_is_pure_ascii_data_] = $is_pure_ascii_data;
1967  
1968 # Case 1: If Perl is already in a character-oriented mode for this
1969 # string rather than a byte-oriented mode. Normally, this happens if
1970 # the caller has decoded a utf8 string before calling perltidy. But it
1971 # could also happen if the user has done some unusual manipulations of
1972 # the source. In any case, we will not attempt to decode it because
1973 # that could result in an output string in a different mode.
1974 664 ▶ if ( is_char_mode( ${$rinput_string} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_char_mode(${$rinput_string;})) { } 2 662 664
elsif (not $rOpts_character_encoding or $rOpts_character_encoding eq 'none') { } 0 662 662
elsif (lc $rOpts_character_encoding eq 'guess') { } 653 9 662
1975 2 $encoding_in = "utf8";
1976 2 $rstatus->{'char_mode_source'} = 1;
1977 }
1978  
1979 # Case 2. No input stream encoding requested. This is appropriate
1980 # for single-byte encodings like ascii, latin-1, etc
1981 ▶ elsif ( !$rOpts_character_encoding
Condition33%1/3
not $rOpts_character_encoding or $rOpts_character_encoding eq "none"
l!l&&r!l&&!r
00662
MC/DC0%0/2
not $rOpts_character_encoding or $rOpts_character_encoding eq "none"
not $rOpts_character_encoding $rOpts_character_encoding eq "none"
Truth table33%1/3
not(A) or B
A= not $rOpts_character_encodingB= $rOpts_character_encoding eq "none"
ABresult
1X1
011
000
1982 || $rOpts_character_encoding eq 'none' )
1983 {
1984  
1985 # nothing to do
1986 }
1987  
1988 # Case 3. guess input stream encoding if requested
1989 elsif ( lc($rOpts_character_encoding) eq 'guess' ) {
1990  
1991 # The guessing strategy is simple: use Encode::Guess to guess
1992 # an encoding. If and only if the guess is utf8, try decoding and
1993 # use it if successful. Otherwise, we proceed assuming the
1994 # characters are encoded as single bytes (same as if 'none' had
1995 # been specified as the encoding).
1996  
1997 # In testing I have found that including additional guess 'suspect'
1998 # encodings sometimes works but can sometimes lead to disaster by
1999 # using an incorrect decoding.
2000  
2001 653 my $decoder;
2002 653 ▶ if ( !$is_pure_ascii_data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_pure_ascii_data) 2 651 653
2003 2 $decoder = guess_encoding( ${$rinput_string}, 'utf8' );
2004 }
2005 653 ▶ if ( $decoder && ref($decoder) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoder and ref $decoder) { } 0 653 653
Condition66%2/3
$decoder and ref $decoder
!ll&&!rl&&r
65120
MC/DC0%0/2
$decoder and ref $decoder
$decoder ref $decoder
Truth table66%2/3
A and B
A= $decoderB= ref $decoder
ABresult
0X0
100
111
2006 0 $encoding_in = $decoder->name;
2007 0 ▶ if ( $encoding_in ne 'UTF-8' && $encoding_in ne 'utf8' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding_in ne 'UTF-8' and $encoding_in ne 'utf8') { } 0 0 0
Condition0%0/3
$encoding_in ne "UTF-8" and $encoding_in ne "utf8"
!ll&&!rl&&r
000
MC/DC0%0/2
$encoding_in ne "UTF-8" and $encoding_in ne "utf8"
$encoding_in ne "UTF-8" $encoding_in ne "utf8"
Truth table0%0/3
A and B
A= $encoding_in ne "UTF-8"B= $encoding_in ne "utf8"
ABresult
0X0
100
111
2008 0 $encoding_in = EMPTY_STRING;
2009 0 $encoding_log_message .= <<EOM;
2010 Guessed encoding '$encoding_in' is not utf8; no encoding will be used
2011 EOM
2012 }
2013 else {
2014  
2015 0 my $buf;
2016 0 ▶ if ( !eval { $buf = $decoder->decode( ${$rinput_string} ); 1 } )
Branch: 1 branch
BranchTrueFalseTotal
if (not eval { do { $buf = $decoder->decode(${$rinput_string;}); 1 } }) { } 0 0 0
2017 {
2018  
2019 0 $encoding_log_message .= <<EOM;
2020 Guessed encoding '$encoding_in' but decoding was unsuccessful; no encoding is used
2021 EOM
2022  
2023 # Note that a guess failed, but keep going
2024 # This warning can eventually be removed
2025 0 Warn(
2026 "file: $display_name: bad guess to decode source as $encoding_in\n"
2027 );
2028 0 $encoding_in = EMPTY_STRING;
2029 }
2030 else {
2031 0 $encoding_log_message .= <<EOM;
2032 Guessed encoding '$encoding_in' successfully decoded
2033 EOM
2034 0 $decoded_input_as = $encoding_in;
2035 0 $rinput_string = \$buf;
2036 }
2037 }
2038 }
2039 else {
2040 653 $encoding_log_message .= <<EOM;
2041 Does not look like utf8 encoded text so processing as raw bytes
2042 EOM
2043 }
2044 }
2045  
2046 # Case 4. Decode with a specific encoding
2047 else {
2048 9 $encoding_in = $rOpts_character_encoding;
2049 9 my $buf;
2050 9 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not eval { do { $buf = &Encode::decode($encoding_in, ${$rinput_string;}, 9); 1 } }) { } 0 9 9
2051 !eval {
2052 9 $buf = Encode::decode( $encoding_in, ${$rinput_string},
2053 Encode::FB_CROAK | Encode::LEAVE_SRC );
2054 9 1;
2055 }
2056 )
2057 {
2058  
2059 # Quit if we cannot decode by the requested encoding;
2060 # Something is not right.
2061 0 Warn(
2062 "skipping file: $display_name: Unable to decode source as $encoding_in\n"
2063 );
2064  
2065 # return nothing on error
2066 0 return;
2067 }
2068 else {
2069 9 $encoding_log_message .= <<EOM;
2070 Specified encoding '$encoding_in' successfully decoded
2071 EOM
2072 9 $decoded_input_as = $encoding_in;
2073 9 $rinput_string = \$buf;
2074 }
2075 }
2076  
2077 # Set the encoding to be used for all further i/o: If we have
2078 # decoded the data with any format, then we must continue to
2079 # read and write it as encoded data, and we will normalize these
2080 # operations with utf8. If we have not decoded the data, then
2081 # we must not treat it as encoded data.
2082 664 ▶ my $is_encoded_data = $encoding_in ? 'utf8' : EMPTY_STRING;
Branch: 1 branch
BranchTrueFalseTotal
$encoding_in ? : 11 653 664
2083 664 $self->[_is_encoded_data_] = $is_encoded_data;
2084  
2085 # Delete any Byte Order Mark (BOM), which can cause trouble
2086 664 ▶ if ($is_encoded_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encoded_data) 11 653 664
2087 11 ${$rinput_string} =~ s/^\x{FEFF}//;
2088 }
2089  
2090 664 $rstatus->{'input_name'} = $display_name;
2091 664 $rstatus->{'opt_encoding'} = $rOpts_character_encoding;
2092 664 ▶ $rstatus->{'char_mode_used'} = $encoding_in ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$encoding_in ? : 11 653 664
2093 664 $rstatus->{'input_decoded_as'} = $decoded_input_as;
2094  
2095 # Define the function to determine the display width of character
2096 # strings
2097 664 my $length_function;
2098 664 ▶ if ($is_encoded_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encoded_data) 11 653 664
2099  
2100 # Try to load Unicode::GCString for defining text display width, if
2101 # requested, when the first encoded file is encountered
2102 11 ▶ if ( !defined($loaded_unicode_gcstring) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $loaded_unicode_gcstring) 0 11 11
2103 0 ▶ if ( eval { require Unicode::GCString; 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Unicode::GCString; 1 } }) { } 0 0 0
2104 0 $loaded_unicode_gcstring = 1;
2105 }
2106 else {
2107 0 $loaded_unicode_gcstring = 0;
2108 0 ▶ if ( $rOpts->{'use-unicode-gcstring'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"use-unicode-gcstring"}) 0 0 0
2109 0 Warn(<<EOM);
2110 ----------------------
2111 Unable to load Unicode::GCString: $EVAL_ERROR
2112 Processing continues but some vertical alignment may be poor
2113 To prevent this warning message, you can either:
2114 - install module Unicode::GCString, or
2115 - remove '--use-unicode-gcstring' or '-gcs' from your perltidyrc or command line
2116 ----------------------
2117 EOM
2118 }
2119 }
2120 }
2121 11 ▶ if ($loaded_unicode_gcstring) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loaded_unicode_gcstring) 0 11 11
2122 0 ▶ $length_function = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2123 0 return Unicode::GCString->new( $_[0] )->columns;
2124 0 };
2125 0 $encoding_log_message .= <<EOM;
2126 Using 'Unicode::GCString' to measure horizontal character widths
2127 EOM
2128 0 $rstatus->{'gcs_used'} = 1;
2129 }
2130 }
2131 return (
2132 664 $rinput_string,
2133 $is_encoded_data,
2134 $decoded_input_as,
2135 $encoding_log_message,
2136 $length_function,
2137  
2138 );
2139 } ## end sub get_decoded_string_buffer
2140  
2141 { #<<<
2142  
2143 45 my $LF;
2144 my $CR;
2145 45 my $CRLF;
2146  
2147 45 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
2148 45 $LF = chr(10);
2149 45 $CR = chr(13);
2150 45 $CRLF = $CR . $LF;
2151 }
2152  
2153 664 ▶sub get_line_separator_default {
Subroutine get_line_separator_default: called CC=4 SCAR=14.8
Pod undocumented
2154  
2155 664 my ($rOpts) = @_;
2156  
2157 # Get the line separator that will apply unless overridden by a
2158 # --preserve-line-endings flag for a specific file
2159  
2160 664 my $line_separator_default = "\n";
2161  
2162 664 my $opt_ole = 'output-line-ending';
2163 664 my $ole = $rOpts->{$opt_ole};
2164 664 ▶ if ($ole) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ole) 4 660 664
2165 4 my %endings = (
2166 dos => $CRLF,
2167 win => $CRLF,
2168 mac => $CR,
2169 unix => $LF,
2170 );
2171  
2172 4 $line_separator_default = $endings{ lc($ole) };
2173  
2174 4 ▶ if ( !$line_separator_default ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line_separator_default) 0 4 4
2175 0 my $str = join( SPACE, keys %endings );
2176 0 Die(<<EOM);
2177 Unrecognized line ending '$ole'; expecting one of: $str
2178 EOM
2179 }
2180  
2181 # Check for conflict with -ple
2182 4 my $opt_ple = 'preserve-line-endings';
2183 4 ▶ if ( $rOpts->{$opt_ple} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{$opt_ple}) 0 4 4
2184 0 Warn("Ignoring '--$opt_ple': conflicts with '--$opt_ole'\n");
2185 0 $rOpts->{$opt_ple} = undef;
2186 }
2187 }
2188  
2189 664 return $line_separator_default;
2190  
2191 } ## end sub get_line_separator_default
2192  
2193 664 ▶sub set_line_separator {
Subroutine set_line_separator: called CC=15 SCAR=32.0
Pod undocumented
2194  
2195 664 my ( $self, $rinput_string ) = @_;
2196  
2197 # Set the (output) line separator as requested or necessary
2198  
2199 664 my $rOpts = $self->[_rOpts_];
2200  
2201 # Start with the default (output) line separator
2202 664 my $line_separator = $self->[_line_separator_default_];
2203  
2204 # First try to find the line separator of the input stream
2205 664 my $input_line_separator;
2206  
2207 # Limit the search to a reasonable number of characters, in case we
2208 # have a weird file
2209 664 my $str = substr( ${$rinput_string}, 0, CONST_1024 );
2210 664 ▶ if ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 661 3 664
2211  
2212 661 ▶ if ( $str =~ m/(($CR|$LF)+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(($CR|$LF)+)/) { } 661 0 661
2213  
2214 661 my $test = $1;
2215  
2216 # dos
2217 661 ▶ if ( $test =~ /^($CRLF)+\z/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($test =~ /^($CRLF)+\z/) { } 4 657 661
elsif ($test =~ /^($CR)+\z/) { } 0 657 657
elsif ($test =~ /^($LF)+\z/) { } 657 0 657
2218 4 $input_line_separator = $CRLF;
2219 }
2220  
2221 # mac
2222 elsif ( $test =~ /^($CR)+\z/ ) {
2223 0 $input_line_separator = $CR;
2224 }
2225  
2226 # unix
2227 elsif ( $test =~ /^($LF)+\z/ ) {
2228 657 $input_line_separator = $LF;
2229 }
2230  
2231 # unknown
2232 else { }
2233 }
2234  
2235 # no ending seen
2236 else { }
2237 }
2238  
2239 664 ▶ if ( defined($input_line_separator) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_line_separator) 661 3 664
2240  
2241 # Remember the input line separator if needed
2242 661 ▶ if ( $rOpts->{'preserve-line-endings'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"preserve-line-endings"}) 0 661 661
2243 0 $line_separator = $input_line_separator;
2244 }
2245  
2246 # Convert line endings to "\n" for processing if necessary.
2247 661 ▶ if ( $input_line_separator ne "\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_line_separator ne "\n") 4 657 661
2248 4 my @lines = split /^/, ${$rinput_string};
2249  
2250 # try to convert CR to \n
2251 4 ▶ if ( $input_line_separator eq $CR ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input_line_separator eq $CR) { } 0 4 4
elsif ($input_line_separator eq $CRLF and "\n" eq $LF) { } 4 0 4
2252  
2253 # if this file is currently a single line ..
2254 0 ▶ if ( @lines == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines == 1) 0 0 0
2255  
2256 # and becomes multiple lines with the change ..
2257 0 @lines = map { $_ . "\n" } split /$CR/, ${$rinput_string};
2258 0 ▶ if ( @lines > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines > 1) 0 0 0
2259  
2260 # then make the change
2261 0 my $buf = join( EMPTY_STRING, @lines );
2262 0 $rinput_string = \$buf;
2263 }
2264 }
2265 }
2266  
2267 # convert CR-LF to LF
2268 ▶ elsif ( ( $input_line_separator eq $CRLF ) && ( "\n" eq $LF ) ) {
Condition33%1/3
$input_line_separator eq $CRLF and "\n" eq $LF
!ll&&!rl&&r
004
MC/DC0%0/2
$input_line_separator eq $CRLF and "\n" eq $LF
$input_line_separator eq $CRLF "\n" eq $LF
Truth table33%1/3
A and B
A= $input_line_separator eq $CRLFB= "\n" eq $LF
ABresult
0X0
100
111
2269 4 foreach my $line (@lines) { $line =~ s/$CRLF$/\n/ }
2270 4 my $buf = join( EMPTY_STRING, @lines );
2271 4 $rinput_string = \$buf;
2272 }
2273  
2274 # unknown line ending scheme
2275 else {
2276 ## leave it alone and let the tokenizer deal with it
2277 }
2278 }
2279 }
2280  
2281 664 $self->[_line_separator_] = $line_separator;
2282 664 return $rinput_string;
2283 } ## end sub set_line_separator
2284 }
2285  
2286 45 ▶sub process_all_files {
Subroutine process_all_files: called CC=58 SCAR=60.9
Pod undocumented
2287  
2288 664 my ( $self, $rcall_hash ) = @_;
2289  
2290 # This routine is the main loop to process all files.
2291 # Total formatting is done with these layers of subroutines:
2292 # perltidy - main routine; checks run parameters
2293 # *process_all_files - main loop to process all files; *THIS LAYER
2294 # process_filter_layer - do any pre and post processing;
2295 # process_iteration_layer - handle any iterations on formatting
2296 # process_single_case - solves one formatting problem
2297  
2298 664 my $rinput_hash = $rcall_hash->{rinput_hash};
2299 664 my $rfiles = $rcall_hash->{rfiles};
2300 664 my $line_range_clipped = $rcall_hash->{line_range_clipped};
2301 664 my $source_stream = $rcall_hash->{source_stream};
2302 664 my $output_extension = $rcall_hash->{output_extension};
2303 664 my $forbidden_file_extensions = $rcall_hash->{forbidden_file_extensions};
2304 664 my $in_place_modify = $rcall_hash->{in_place_modify};
2305 664 my $backup_extension = $rcall_hash->{backup_extension};
2306 664 my $delete_backup = $rcall_hash->{delete_backup};
2307 664 my $logfile_header = $rcall_hash->{logfile_header};
2308 664 my $rpending_complaint = $rcall_hash->{rpending_complaint};
2309 664 my $rpending_logfile_message = $rcall_hash->{rpending_logfile_message};
2310  
2311 664 my $rOpts = $self->[_rOpts_];
2312 664 my $dot = $self->[_file_extension_separator_];
2313 664 my $diagnostics_object = $self->[_diagnostics_object_];
2314  
2315 664 my $destination_stream = $rinput_hash->{'destination'};
2316 664 my $errorfile_stream = $rinput_hash->{'errorfile'};
2317 664 my $logfile_stream = $rinput_hash->{'logfile'};
2318 664 my $teefile_stream = $rinput_hash->{'teefile'};
2319 664 my $debugfile_stream = $rinput_hash->{'debugfile'};
2320  
2321 664 my $number_of_files = @{$rfiles};
2322 664 foreach my $input_file ( @{$rfiles} ) {
2323 664 my $fileroot;
2324 my @input_file_stat;
2325 664 my $display_name;
2326  
2327 #--------------------------
2328 # prepare this input stream
2329 #--------------------------
2330 664 ▶ if ($source_stream) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source_stream) { } 661 3 664
elsif ($input_file eq '-') { } 0 3 3
2331 661 $fileroot = "perltidy";
2332 661 $display_name = "<source_stream>";
2333  
2334 # If the source is from an array or string, then .LOG output
2335 # is only possible if a logfile stream is specified. This prevents
2336 # unexpected perltidy.LOG files. If the stream is not defined
2337 # then we will capture it in a string ref but it will not be
2338 # accessible. Previously by Perl::Tidy::DevNull (fix c255);
2339 661 ▶ if ( !defined($logfile_stream) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $logfile_stream) 660 1 661
2340 660 $logfile_stream = \my $tmp;
2341  
2342 # Likewise for .TEE and .DEBUG output
2343 }
2344 661 ▶ if ( !defined($teefile_stream) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $teefile_stream) 660 1 661
2345 660 $teefile_stream = \my $tmp;
2346 }
2347 661 ▶ if ( !defined($debugfile_stream) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $debugfile_stream) 659 2 661
2348 659 $debugfile_stream = \my $tmp;
2349 }
2350 }
2351 elsif ( $input_file eq '-' ) { # '-' indicates input from STDIN
2352 0 $fileroot = "perltidy"; # root name to use for .ERR, .LOG, etc
2353 0 $display_name = "<stdin>";
2354 0 $in_place_modify = 0;
2355 }
2356 else {
2357 3 $fileroot = $input_file;
2358 3 $display_name = $input_file;
2359 3 ▶ if ( !-e $input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $input_file) 0 3 3
2360 0 Warn("skipping file: '$input_file': no matches found\n");
2361 0 next;
2362 }
2363  
2364 3 ▶ if ( !-f $input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $input_file) 0 3 3
2365 0 Warn("skipping file: $input_file: not a regular file\n");
2366 0 next;
2367 }
2368  
2369 # As a safety precaution, skip zero length files.
2370 # If for example a source file got clobbered somehow,
2371 # the old .tdy or .bak files might still exist so we
2372 # shouldn't overwrite them with zero length files.
2373 3 ▶ if ( !-s $input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $input_file) 0 3 3
2374 0 Warn("skipping file: $input_file: Zero size\n");
2375 0 next;
2376 }
2377  
2378 # And avoid formatting extremely large files. Since perltidy reads
2379 # files into memory, trying to process an extremely large file
2380 # could cause system problems.
2381 3 my $size_in_mb = ( -s $input_file ) / ( CONST_1024 * CONST_1024 );
2382 3 my $maximum_file_size_mb = $rOpts->{'maximum-file-size-mb'};
2383 3 ▶ if ( $size_in_mb > $maximum_file_size_mb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size_in_mb > $maximum_file_size_mb) 0 3 3
2384 0 $size_in_mb = sprintf( "%0.1f", $size_in_mb );
2385 0 Warn(
2386 "skipping file: $input_file: size $size_in_mb MB exceeds limit $maximum_file_size_mb; use -maxfs=i to change\n"
2387 );
2388 0 next;
2389 }
2390  
2391 3 ▶ if ( !-T $input_file && !$rOpts->{'force-read-binary'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-T $input_file or $rOpts->{"force-read-binary"}) 0 3 3
Condition33%1/3
-T $input_file or $rOpts->{"force-read-binary"}
l!l&&r!l&&!r
300
MC/DC0%0/2
-T $input_file or $rOpts->{"force-read-binary"}
-T $input_file $rOpts->{"force-read-binary"}
Truth table33%1/3
A or B
A= -T $input_fileB= $rOpts->{"force-read-binary"}
ABresult
1X1
011
000
2392 0 Warn("skipping file: $input_file: Non-text (override with -f)\n"
2393 );
2394 0 next;
2395 }
2396  
2397 # Input file must be writable for -b -bm='copy'. We must catch
2398 # this early to prevent encountering trouble after unlinking the
2399 # previous backup.
2400 3 ▶ if ( $in_place_modify && !-w $input_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_place_modify and not -w $input_file) 0 3 3
Condition33%1/3
$in_place_modify and not -w $input_file
!ll&&!rl&&r
300
MC/DC0%0/2
$in_place_modify and not -w $input_file
$in_place_modify not -w $input_file
Truth table33%1/3
A and not(B)
A= $in_place_modifyB= not -w $input_file
ABresult
0X0
100
111
2401 0 my $backup_method = $rOpts->{'backup-method'};
2402 0 ▶ if ( defined($backup_method) && $backup_method eq 'copy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $backup_method and $backup_method eq "copy") 0 0 0
Condition0%0/3
defined $backup_method and $backup_method eq "copy"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $backup_method and $backup_method eq "copy"
defined $backup_method $backup_method eq "copy"
Truth table0%0/3
A and B
A= defined $backup_methodB= $backup_method eq "copy"
ABresult
0X0
100
111
2403 0 Warn(
2404 "skipping file '$input_file' for -b option: file reported as non-writable\n"
2405 );
2406 0 next;
2407 }
2408 }
2409  
2410 # we should have a valid filename now
2411 3 $fileroot = $input_file;
2412 3 @input_file_stat = stat($input_file);
2413  
2414 3 ▶ if ( $OSNAME eq 'VMS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::Tidy::OSNAME eq "VMS") 0 3 3
2415 0 ( $fileroot, $dot ) = check_vms_filename($fileroot);
2416 0 $self->[_file_extension_separator_] = $dot;
2417 }
2418  
2419 # add option to change path here
2420 3 ▶ if ( defined( $rOpts->{'output-path'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{"output-path"}) 0 3 3
2421  
2422 0 my ( $base, $old_path_uu ) = fileparse($fileroot);
2423 0 my $new_path = $rOpts->{'output-path'};
2424 0 ▶ if ( !-d $new_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $new_path) 0 0 0
2425 0 ▶ mkdir($new_path) # Default MODE is 0777
Branch: 1 branch
BranchTrueFalseTotal
mkdir $new_path or Die("unable to create directory ${new_path}: $Perl::Tidy::OS_ERROR\n") 0 0 0
2426 or
2427 Die("unable to create directory $new_path: $OS_ERROR\n");
2428 }
2429 0 my $path = $new_path;
2430 0 $fileroot = File::Spec->catfile( $path, $base );
2431 0 ▶ if ( !$fileroot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fileroot) 0 0 0
2432 0 Die(<<EOM);
2433 ------------------------------------------------------------------------
2434 Problem combining $new_path and $base to make a filename; check -opath
2435 ------------------------------------------------------------------------
2436 EOM
2437 }
2438 }
2439 }
2440  
2441 # Skip files with same extension as the output files because
2442 # this can lead to a messy situation with files like
2443 # script.tdy.tdy.tdy ... or worse problems ... when you
2444 # rerun perltidy over and over with wildcard input.
2445 664 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $source_stream and $input_file =~ /$forbidden_file_extensions/ || $input_file eq "DIAGNOSTICS") 0 664 664
Condition50%3/6
not $source_stream and $input_file =~ /$forbidden_file_extensions/ || $input_file eq "DIAGNOSTICS"
!ll&&!rl&&r
66130
$input_file =~ /$forbidden_file_extensions/ or $input_file eq "DIAGNOSTICS"
l!l&&r!l&&!r
003
MC/DC0%0/3
not $source_stream and $input_file =~ /$forbidden_file_extensions/ || $input_file eq "DIAGNOSTICS"
not $source_stream $input_file =~ /$forbidden_file_extensions/ $input_file eq "DIAGNOSTICS"
Truth table50%2/4
not(A) and B or C
A= not $source_streamB= $input_file =~ /$forbidden_file_extensions/C= $input_file eq "DIAGNOSTICS"
ABCresult
0XX0
1000
11X1
1011
2446 !$source_stream
2447 && ( $input_file =~ /$forbidden_file_extensions/
2448 || $input_file eq 'DIAGNOSTICS' )
2449 )
2450 {
2451 0 Warn("skipping file: $input_file: wrong extension\n");
2452 0 next;
2453 }
2454  
2455 # copy source to a string buffer, decoding from utf8 if necessary
2456 my (
2457 664 $rinput_string,
2458 $is_encoded_data,
2459 $decoded_input_as,
2460 $encoding_log_message,
2461 $length_function,
2462  
2463 ) = $self->get_decoded_string_buffer( $input_file, $display_name );
2464  
2465 # Skip this file on any error
2466 664 ▶ next if ( !defined($rinput_string) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rinput_string 0 664 664
2467  
2468 # If we are formatting a named file, skip if it looks like a markup
2469 # language. Do not do this for a non-named file (it could be a glob
2470 # from an editor).
2471 # Examples of valid perl starting text: '<<END' '<>' '<<>>'
2472 664 ▶ if ( ${$rinput_string} =~ /^\s*<[^<>]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$rinput_string;} =~ /^\s*<[^<>]/) 0 664 664
2473 0 ▶ my $is_named_file = $number_of_files > 0 && !$line_range_clipped;
Condition0%0/3
$number_of_files > 0 && !$line_range_clipped
!ll&&!rl&&r
000
MC/DC0%0/2
$number_of_files > 0 && !$line_range_clipped
$number_of_files > 0 !$line_range_clipped
Truth table0%0/3
A && not(B)
A= $number_of_files > 0B= !$line_range_clipped
ABresult
0X0
100
111
2474 0 ▶ if ( is_not_perl( $rinput_string, $input_file, $is_named_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_not_perl($rinput_string, $input_file, $is_named_file)) 0 0 0
2475 0 Warn(
2476 "skipping file: $input_file: does not look like Perl code\n"
2477 );
2478 0 next;
2479 }
2480 }
2481  
2482 # Register this file name with the Diagnostics package, if any.
2483 664 ▶ $diagnostics_object->set_input_file($input_file)
Branch: 1 branch
BranchTrueFalseTotal
if $diagnostics_object 0 664 664
2484 if ($diagnostics_object);
2485  
2486 # The (possibly decoded) input is now in string ref $rinput_string.
2487 # Now prepare the output stream and error logger.
2488  
2489 #--------------------------
2490 # prepare the output stream
2491 #--------------------------
2492 664 my $output_file = $rOpts->{'outfile'};
2493 664 my $output_name = EMPTY_STRING;
2494 664 my $actual_output_extension;
2495  
2496 664 ▶ if ( defined($output_file) && length($output_file) ) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $output_file and length $output_file) { } 0 664 664
elsif ($rOpts->{'standard-output'}) { } 1 663 664
elsif ($destination_stream) { } 663 0 663
elsif ($source_stream) { } 0 0 0
elsif ($input_file eq '-') { } 0 0 0
Condition33%1/3
defined $output_file and length $output_file
!ll&&!rl&&r
66400
MC/DC0%0/2
defined $output_file and length $output_file
defined $output_file length $output_file
Truth table33%1/3
A and B
A= defined $output_fileB= length $output_file
ABresult
0X0
100
111
2497  
2498 0 ▶ if ( $number_of_files > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number_of_files > 1) 0 0 0
2499 0 Die("You may not use -o with more than one input file\n");
2500 }
2501  
2502 0 ▶ if ( $rOpts->{'standard-output'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"standard-output"}) 0 0 0
2503 0 my $saw_pbp = $self->[_saw_pbp_];
2504 0 my $msg = "You may not use -o and -st together\n";
2505 0 ▶ if ($saw_pbp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_pbp) 0 0 0
2506 0 $msg .= <<EOM;
2507 Note: -pbp is set and includes -st (see manual); use -nst to turn it off
2508 EOM
2509 }
2510 0 Die("$msg");
2511 }
2512  
2513 0 ▶ if ($destination_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destination_stream) 0 0 0
2514 0 Die(
2515 "You may not specify a destination array and -o together\n"
2516 );
2517 }
2518  
2519 0 ▶ if ( defined( $rOpts->{'output-path'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{"output-path"}) 0 0 0
2520 0 Die("You may not specify -o and -opath together\n");
2521 }
2522  
2523 0 ▶ if ( defined( $rOpts->{'output-file-extension'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{"output-file-extension"}) 0 0 0
2524 0 Die("You may not specify -o and -oext together\n");
2525 }
2526  
2527 0 $output_name = $output_file;
2528  
2529 # do not overwrite input file with -o
2530 0 ▶ if ( @input_file_stat && ( $output_file eq $input_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@input_file_stat and $output_file eq $input_file) 0 0 0
Condition0%0/3
@input_file_stat and $output_file eq $input_file
!ll&&!rl&&r
000
MC/DC0%0/2
@input_file_stat and $output_file eq $input_file
@input_file_stat $output_file eq $input_file
Truth table0%0/3
A and B
A= @input_file_statB= $output_file eq $input_file
ABresult
0X0
100
111
2531 0 Die("Use 'perltidy -b $input_file' to modify in-place\n");
2532 }
2533 }
2534 elsif ( $rOpts->{'standard-output'} ) {
2535 1 ▶ if ($destination_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destination_stream) 0 1 1
2536 0 my $saw_pbp = $self->[_saw_pbp_];
2537 0 my $msg =
2538 "You may not specify a destination array and -st together\n";
2539 0 ▶ $msg .= " (note: -pbp contains -st; see manual)" if ($saw_pbp);
Branch: 1 branch
BranchTrueFalseTotal
if $saw_pbp 0 0 0
2540 0 Die("$msg\n");
2541 }
2542 1 $output_file = '-';
2543 1 $output_name = "<stdout>";
2544  
2545 1 ▶ if ( $number_of_files <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number_of_files <= 1) { } 1 0 1
2546 }
2547 else {
2548 0 Die("You may not use -st with more than one input file\n");
2549 }
2550 }
2551 elsif ($destination_stream) {
2552  
2553 663 $output_file = $destination_stream;
2554 663 $output_name = "<destination_stream>";
2555 }
2556 elsif ($source_stream) { # source but no destination goes to stdout
2557 0 $output_file = '-';
2558 0 $output_name = "<stdout>";
2559 }
2560 elsif ( $input_file eq '-' ) {
2561 0 $output_file = '-';
2562 0 $output_name = "<stdout>";
2563 }
2564 else {
2565 0 ▶ if ($in_place_modify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_place_modify) { } 0 0 0
2566 0 $output_name = $display_name;
2567 }
2568 else {
2569 0 $actual_output_extension = $output_extension;
2570 0 $output_file = $fileroot . $output_extension;
2571 0 $output_name = $output_file;
2572 }
2573 }
2574  
2575 # prepare standard output in case of a dump to stdout
2576 664 ▶ if ( $is_encoded_data && $self->[_dump_to_stdout_] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encoded_data and $self->[26]) 0 664 664
Condition66%2/3
$is_encoded_data and $self->[26]
!ll&&!rl&&r
653110
MC/DC0%0/2
$is_encoded_data and $self->[26]
$is_encoded_data $self->[26]
Truth table66%2/3
A and B
A= $is_encoded_dataB= $self->[26]
ABresult
0X0
100
111
2577 0 binmode *STDOUT, ':encoding(UTF-8)';
2578 }
2579  
2580 664 $rstatus->{'file_count'} += 1;
2581 664 $rstatus->{'output_name'} = $output_name;
2582 664 $rstatus->{'iteration_count'} = 0;
2583 664 $rstatus->{'converged'} = 0;
2584  
2585 #------------------------------------------
2586 # initialize the error logger for this file
2587 #------------------------------------------
2588 664 my $warning_file = $fileroot . $dot . "ERR";
2589 664 ▶ if ($errorfile_stream) { $warning_file = $errorfile_stream }
Branch: 1 branch
BranchTrueFalseTotal
if ($errorfile_stream) 648 16 664
2590 664 my $log_file = $fileroot . $dot . "LOG";
2591 664 ▶ if ($logfile_stream) { $log_file = $logfile_stream }
Branch: 1 branch
BranchTrueFalseTotal
if ($logfile_stream) 661 3 664
2592  
2593 # The logger object handles warning messages, logfile messages,
2594 # and can supply basic run information to lower level routines.
2595 664 my $logger_object = Perl::Tidy::Logger->new(
2596 rOpts => $rOpts,
2597 log_file => $log_file,
2598 warning_file => $warning_file,
2599 fh_stderr => $fh_stderr,
2600 display_name => $display_name,
2601 is_encoded_data => $is_encoded_data,
2602 );
2603 664 $logger_object->write_logfile_entry($logfile_header);
2604 664 ▶ $logger_object->write_logfile_entry($encoding_log_message)
Branch: 1 branch
BranchTrueFalseTotal
if $encoding_log_message 662 2 664
2605 if ($encoding_log_message);
2606  
2607 # Now we can add any pending messages to the log
2608 664 ▶ if ( ${$rpending_logfile_message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$rpending_logfile_message;}) 0 664 664
2609 0 $logger_object->write_logfile_entry( ${$rpending_logfile_message} );
2610 }
2611 664 ▶ if ( ${$rpending_complaint} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$rpending_complaint;}) 0 664 664
2612 0 $logger_object->complain( ${$rpending_complaint} );
2613 }
2614  
2615 # additional parameters needed by lower level routines
2616 664 $self->[_actual_output_extension_] = $actual_output_extension;
2617 664 $self->[_debugfile_stream_] = $debugfile_stream;
2618 664 $self->[_decoded_input_as_] = $decoded_input_as;
2619 664 $self->[_destination_stream_] = $destination_stream;
2620 664 $self->[_display_name_] = $display_name;
2621 664 $self->[_fileroot_] = $fileroot;
2622 664 $self->[_is_encoded_data_] = $is_encoded_data;
2623 664 $self->[_length_function_] = $length_function;
2624 664 $self->[_logger_object_] = $logger_object;
2625 664 $self->[_output_file_] = $output_file;
2626 664 $self->[_teefile_stream_] = $teefile_stream;
2627 664 $self->[_input_copied_verbatim_] = 0;
2628 664 $self->[_input_output_difference_] = 1; ## updated later if -b used
2629  
2630 #--------------------
2631 # process this buffer
2632 #--------------------
2633 664 my $routput_string = $self->process_filter_layer($rinput_string);
2634  
2635 #------------------------------------------------
2636 # send the tidied output to its final destination
2637 #------------------------------------------------
2638 664 ▶ if ( $rOpts->{'format'} eq 'tidy' && defined($routput_string) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"format"} eq "tidy" and defined $routput_string) 663 1 664
Condition66%2/3
$rOpts->{"format"} eq "tidy" and defined $routput_string
!ll&&!rl&&r
10663
MC/DC50%1/2
$rOpts->{"format"} eq "tidy" and defined $routput_string
$rOpts->{"format"} eq "tidy" defined $routput_string
Truth table66%2/3
A and B
A= $rOpts->{"format"} eq "tidy"B= defined $routput_string
ABresult
0X0
100
111
2639  
2640 663 $self->write_tidy_output(
2641 {
2642 routput_string => $routput_string,
2643 rinput_file_stat => \@input_file_stat,
2644 in_place_modify => $in_place_modify,
2645 input_file => $input_file,
2646 backup_extension => $backup_extension,
2647 delete_backup => $delete_backup,
2648 }
2649 );
2650 }
2651  
2652 $logger_object->finish()
2653 664 ▶ if ($logger_object);
Branch: 1 branch
BranchTrueFalseTotal
if $logger_object 664 0 664
2654 } ## end loop over files
2655  
2656 664 return;
2657 } ## end sub process_all_files
2658  
2659 663 ▶sub write_tidy_output {
Subroutine write_tidy_output: called CC=16 SCAR=31.8
Pod undocumented
2660  
2661 663 my ( $self, $rcall_hash ) = @_;
2662  
2663 # Write tidied output in '$routput_string' to its final destination
2664  
2665 663 my $routput_string = $rcall_hash->{routput_string};
2666 663 my $rinput_file_stat = $rcall_hash->{rinput_file_stat};
2667 663 my $in_place_modify = $rcall_hash->{in_place_modify};
2668 663 my $input_file = $rcall_hash->{input_file};
2669 663 my $backup_extension = $rcall_hash->{backup_extension};
2670 663 my $delete_backup = $rcall_hash->{delete_backup};
2671  
2672 663 my $rOpts = $self->[_rOpts_];
2673 663 my $is_encoded_data = $self->[_is_encoded_data_];
2674 663 my $output_file = $self->[_output_file_];
2675  
2676 # There are three main output paths:
2677  
2678 #-------------------------------------------------------------------------
2679 # PATH 1: $output_file is not defined: --backup and modify in-place option
2680 #-------------------------------------------------------------------------
2681 663 ▶ if ($in_place_modify) {
Branch: 2 branches
BranchTrueFalseTotal
if ($in_place_modify) { } 0 663 663
elsif (ref $output_file) { } 658 5 663
2682  
2683 # For -b option, leave the file unchanged if a severe error caused
2684 # formatting to be skipped. Otherwise we will overwrite any backup.
2685 0 ▶ if ( !$self->[_input_copied_verbatim_] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[24]) 0 0 0
2686  
2687 0 my $backup_method = $rOpts->{'backup-method'};
2688  
2689 #-------------------------------------------------------------
2690 # PATH 1a: -bm='copy': uses newer version in which original is
2691 # copied to the backup and rewritten; see git #103.
2692 #-------------------------------------------------------------
2693 0 ▶ if ( defined($backup_method) && $backup_method eq 'copy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $backup_method and $backup_method eq 'copy') { } 0 0 0
Condition0%0/3
defined $backup_method and $backup_method eq "copy"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $backup_method and $backup_method eq "copy"
defined $backup_method $backup_method eq "copy"
Truth table0%0/3
A and B
A= defined $backup_methodB= $backup_method eq "copy"
ABresult
0X0
100
111
2694 0 $self->backup_method_copy(
2695 $input_file, $routput_string,
2696 $backup_extension, $delete_backup
2697 );
2698 }
2699  
2700 #-------------------------------------------------------------
2701 # PATH 1b: -bm='move': uses older version, where original is
2702 # moved to the backup and formatted output goes to a new file.
2703 #-------------------------------------------------------------
2704 else {
2705 0 $self->backup_method_move(
2706 $input_file, $routput_string,
2707 $backup_extension, $delete_backup
2708 );
2709 }
2710 }
2711 }
2712  
2713 #--------------------------------------------------------------------------
2714 # PATH 2: $output_file is a reference (=destination_stream): send output to
2715 # a destination stream ref received from an external perl program. We use
2716 # a sub to do this because the encoding rules are a bit tricky.
2717 #--------------------------------------------------------------------------
2718 elsif ( ref($output_file) ) {
2719 658 $self->copy_buffer_to_external_ref( $routput_string, $output_file );
2720 }
2721  
2722 #--------------------------------------------------------------------------
2723 # PATH 3: $output_file is named file or '-'; send output to the file system
2724 #--------------------------------------------------------------------------
2725 else {
2726  
2727 #--------------------------
2728 # PATH 3a: output to STDOUT
2729 #--------------------------
2730 5 ▶ if ( $output_file eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output_file eq '-') { } 1 4 5
2731 1 my $fh = *STDOUT;
2732 1 ▶ if ($is_encoded_data) { binmode $fh, ":raw:encoding(UTF-8)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encoded_data) { } 1 0 1
2733 0 else { binmode $fh }
2734 1 $fh->print( ${$routput_string} );
2735 }
2736  
2737 #--------------------------------
2738 # PATH 3b: output to a named file
2739 #--------------------------------
2740 else {
2741 4 ▶ if ( open( my $fh, '>', $output_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $output_file) { } 4 0 4
2742 4 ▶ if ($is_encoded_data) { binmode $fh, ":raw:encoding(UTF-8)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encoded_data) { } 3 1 4
2743 1 else { binmode $fh }
2744 4 $fh->print( ${$routput_string} );
2745 4 ▶ $fh->close() or Die("Cannot close '$output_file': $OS_ERROR\n");
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or Die("Cannot close '${output_file}': $Perl::Tidy::OS_ERROR\n") 4 0 4
2746 }
2747 else {
2748 0 Die("Cannot open $output_file to write: $OS_ERROR\n");
2749 }
2750  
2751 # set output file ownership and permissions if appropriate
2752 4 ▶ if ( $output_file && -f $output_file && !-l $output_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output_file and -f $output_file and not -l $output_file) 4 0 4
Condition33%2/6
$output_file and -f $output_file and not -l $output_file
!ll&&!rl&&r
004
$output_file and -f $output_file
!ll&&!rl&&r
004
MC/DC0%0/3
$output_file and -f $output_file and not -l $output_file
$output_file -f $output_file not -l $output_file
Truth table25%1/4
A and B and not(C)
A= $output_fileB= -f $output_fileC= not -l $output_file
ABCresult
0XX0
10X0
1100
1111
2753 4 ▶ if ( @{$rinput_file_stat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rinput_file_stat;}) 2 2 4
2754 $self->set_output_file_permissions( $output_file,
2755 2 \@{$rinput_file_stat}, $in_place_modify );
2756 }
2757 }
2758 }
2759  
2760 # Save diagnostic info
2761 5 ▶ if ($is_encoded_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_encoded_data) 4 1 5
2762 4 $rstatus->{'output_encoded_as'} = 'UTF-8';
2763 }
2764 }
2765  
2766 663 return;
2767  
2768 } ## end sub write_tidy_output
2769  
2770 664 ▶sub process_filter_layer {
Subroutine process_filter_layer: called CC=26 SCAR=38.0
Pod undocumented
2771  
2772 664 my ( $self, $rinput_string ) = @_;
2773  
2774 # This is the filter layer of processing.
2775 # Do all requested formatting on the string ref '$rinput_string', including
2776 # any pre- and post-processing with filters.
2777 # Returns:
2778 # $routput_string = ref to tidied output if in 'tidy' mode
2779 # (nothing) if not in 'tidy' mode [these modes handle output separately]
2780  
2781 # Total formatting is done with these layers of subroutines:
2782 # perltidy - main routine; checks run parameters
2783 # process_all_files - main loop to process all files;
2784 # *process_filter_layer - do any pre and post processing; *THIS LAYER
2785 # process_iteration_layer - handle any iterations on formatting
2786 # process_single_case - solves one formatting problem
2787  
2788 # Data Flow in this layer:
2789 # $rinput_string
2790 # -> optional prefilter operations
2791 # -> [ formatting by sub process_iteration_layer ]
2792 # -> return if not in 'tidy' mode
2793 # -> optional postfilter operations
2794 # -> $routput_string
2795  
2796 # What is done based on format type:
2797 # utf8 decoding is done for all format types
2798 # prefiltering is applied to all format types
2799 # - because it may be needed to get through the tokenizer
2800 # postfiltering is only done for format='tidy'
2801 # - not appropriate for html text, which has already been output
2802 # encoding of decoded output is only done for format='tidy'
2803 # - because html does its own encoding; user formatter does what it wants
2804  
2805 # Be sure the string we received is defined
2806 664 ▶ if ( !defined($rinput_string) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rinput_string) 0 664 664
2807 0 Fault("bad call: the source string ref \$rinput_string is undefined\n");
2808 }
2809 664 ▶ if ( ref($rinput_string) ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rinput_string ne "SCALAR") 0 664 664
2810 0 Fault("bad call: the source string ref is not SCALAR\n");
2811 }
2812  
2813 664 my $rOpts = $self->[_rOpts_];
2814 664 my $logger_object = $self->[_logger_object_];
2815  
2816 # vars for --line-range-tidy filter, if needed
2817 664 my @input_lines_pre;
2818 my @input_lines_post;
2819  
2820 # vars for checking assertions, if needed
2821 664 my $digest_input;
2822 664 my $saved_input_buf;
2823  
2824 # var for checking --noadd-terminal-newline
2825 664 my $chomp_terminal_newline;
2826  
2827 # Setup post-filter vars; these apply to 'tidy' mode only
2828 664 ▶ if ( $rOpts->{'format'} eq 'tidy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"format"} eq "tidy") 663 1 664
2829  
2830 #---------------------------------------------------------------------
2831 # for --line-range-tidy, clip '$rinput_string' to a limited line range
2832 #---------------------------------------------------------------------
2833 663 my $line_tidy_begin = $self->[_line_tidy_begin_];
2834 663 ▶ if ($line_tidy_begin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_tidy_begin) 1 662 663
2835  
2836 1 my @input_lines = split /^/, ${$rinput_string};
2837  
2838 1 my $num = @input_lines;
2839 1 ▶ if ( $line_tidy_begin > $num ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_tidy_begin > $num) { } 0 1 1
2840 0 Die(<<EOM);
2841 #--line-range-tidy=n1:n2 has n1=$line_tidy_begin which exceeds max line number of $num
2842 EOM
2843 }
2844 else {
2845 1 my $line_tidy_end = $self->[_line_tidy_end_];
2846 1 ▶ if ( !defined($line_tidy_end) || $line_tidy_end > $num ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line_tidy_end or $line_tidy_end > $num) 0 1 1
Condition33%1/3
not defined $line_tidy_end or $line_tidy_end > $num
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $line_tidy_end or $line_tidy_end > $num
not defined $line_tidy_end $line_tidy_end > $num
Truth table33%1/3
not(A) or B
A= not defined $line_tidy_endB= $line_tidy_end > $num
ABresult
1X1
011
000
2847 0 $line_tidy_end = $num;
2848 }
2849 1 my $input_string = join( EMPTY_STRING,
2850 @input_lines[ $line_tidy_begin - 1 .. $line_tidy_end - 1 ]
2851 );
2852 1 $rinput_string = \$input_string;
2853  
2854 1 @input_lines_pre = @input_lines[ 0 .. $line_tidy_begin - 2 ];
2855 1 @input_lines_post = @input_lines[ $line_tidy_end .. $num - 1 ];
2856 }
2857 }
2858  
2859 #------------------------------------------
2860 # evaluate MD5 sum of input file, if needed
2861 #------------------------------------------
2862 663 ▶ if ( $rOpts->{'assert-tidy'}
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"assert-tidy"} or $rOpts->{"assert-untidy"} or $rOpts->{"backup-and-modify-in-place"}) 2 661 663
Condition50%3/6
$rOpts->{"assert-tidy"} or $rOpts->{"assert-untidy"} or $rOpts->{"backup-and-modify-in-place"}
l!l&&r!l&&!r
02661
$rOpts->{"assert-tidy"} or $rOpts->{"assert-untidy"}
l!l&&r!l&&!r
00663
MC/DC33%1/3
$rOpts->{"assert-tidy"} or $rOpts->{"assert-untidy"} or $rOpts->{"backup-and-modify-in-place"}
$rOpts->{"assert-tidy"} $rOpts->{"assert-untidy"} $rOpts->{"backup-and-modify-in-place"}
Truth table50%2/4
A or B or C
A= $rOpts->{"assert-tidy"}B= $rOpts->{"assert-untidy"}C= $rOpts->{"backup-and-modify-in-place"}
ABCresult
1XX1
01X1
0011
0000
2863 || $rOpts->{'assert-untidy'}
2864 || $rOpts->{'backup-and-modify-in-place'} )
2865 {
2866 2 $digest_input = $md5_hex->( ${$rinput_string} );
2867 2 $saved_input_buf = ${$rinput_string};
2868 }
2869  
2870 # When -noadd-terminal-newline is set, and the input does not
2871 # have a newline, then we remove the final newline of the output
2872 $chomp_terminal_newline = !$rOpts->{'add-terminal-newline'}
2873 663 ▶ && substr( ${$rinput_string}, -1, 1 ) !~ /\n/;
Condition66%2/3
$rOpts->{'add-terminal-newline'} || substr(${$rinput_string;}, -1, 1) =~ /\n/
l!l&&r!l&&!r
66201
MC/DC50%1/2
$rOpts->{'add-terminal-newline'} || substr(${$rinput_string;}, -1, 1) =~ /\n/
$rOpts->{'add-terminal-newline'} substr(${$rinput_string;}, -1, 1) =~ /\n/
Truth table66%2/3
A || B
A= $rOpts->{'add-terminal-newline'}B= substr(${$rinput_string;}, -1, 1) =~ /\n/
ABresult
1X1
011
000
2874  
2875 }
2876  
2877 #-----------------------------------------------------------------------
2878 # Apply any prefilter. The prefilter is a code reference that will be
2879 # applied to the source before tokenizing. Note that we are doing this
2880 # for all format types ('tidy', 'html', 'user') because it may be needed
2881 # to avoid tokenization errors.
2882 #-----------------------------------------------------------------------
2883 664 my $prefilter = $self->[_prefilter_];
2884 664 ▶ if ($prefilter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefilter) 1 663 664
2885 1 my $input_string = $prefilter->( ${$rinput_string} );
2886 1 $rinput_string = \$input_string;
2887 }
2888  
2889 #-------------------------------------------
2890 # Format contents of string '$rinput_string'
2891 #-------------------------------------------
2892 664 my $routput_string = $self->process_iteration_layer($rinput_string);
2893  
2894 #-------------------------------
2895 # All done if not in 'tidy' mode
2896 #-------------------------------
2897 664 ▶ if ( $rOpts->{'format'} ne 'tidy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"format"} ne "tidy") 1 663 664
2898 1 return;
2899 }
2900  
2901 #---------------------
2902 # apply any postfilter
2903 #---------------------
2904 663 my $postfilter = $self->[_postfilter_];
2905 663 ▶ if ($postfilter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postfilter) 1 662 663
2906 1 my $output_string = $postfilter->( ${$routput_string} );
2907 1 $routput_string = \$output_string;
2908 }
2909  
2910 663 ▶ if ( defined($digest_input) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $digest_input) 2 661 663
2911 2 my $digest_output = $md5_hex->( ${$routput_string} );
2912 2 $self->[_input_output_difference_] = $digest_output ne $digest_input;
2913 }
2914  
2915 #-----------------------------------------------------
2916 # check for changes if requested by 'assert-...' flags
2917 #-----------------------------------------------------
2918 663 ▶ if ( $rOpts->{'assert-tidy'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"assert-tidy"}) 0 663 663
2919 0 ▶ if ( $self->[_input_output_difference_] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[25]) 0 0 0
2920 0 my $diff_msg =
2921 compare_string_buffers( \$saved_input_buf, $routput_string );
2922 0 $logger_object->warning(<<EOM);
2923 assertion failure: '--assert-tidy' is set but output differs from input
2924 EOM
2925 0 $logger_object->interrupt_logfile();
2926 0 $logger_object->warning( $diff_msg . "\n" );
2927 0 $logger_object->resume_logfile();
2928 }
2929 }
2930  
2931 663 ▶ if ( $rOpts->{'assert-untidy'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"assert-untidy"}) 0 663 663
2932 0 ▶ if ( !$self->[_input_output_difference_] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[25]) 0 0 0
2933 0 $logger_object->warning(
2934 "assertion failure: '--assert-untidy' is set but output equals input\n"
2935 );
2936 }
2937 }
2938  
2939 #----------------------------------------
2940 # do --line-range-tidy line recombination
2941 #----------------------------------------
2942 663 ▶ if ( @input_lines_pre || @input_lines_post ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@input_lines_pre or @input_lines_post) 1 662 663
Condition66%2/3
@input_lines_pre or @input_lines_post
l!l&&r!l&&!r
10662
MC/DC50%1/2
@input_lines_pre or @input_lines_post
@input_lines_pre @input_lines_post
Truth table66%2/3
A or B
A= @input_lines_preB= @input_lines_post
ABresult
1X1
011
000
2943 1 my $str_pre = join( EMPTY_STRING, @input_lines_pre );
2944 1 my $str_post = join( EMPTY_STRING, @input_lines_post );
2945 1 my $output_string = $str_pre . ${$routput_string} . $str_post;
2946 1 $routput_string = \$output_string;
2947 }
2948  
2949 #-----------------------------------------
2950 # handle a '--noadd-terminal-newline' flag
2951 #-----------------------------------------
2952 663 ▶ if ($chomp_terminal_newline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chomp_terminal_newline) 1 662 663
2953 1 chomp ${$routput_string};
2954 }
2955  
2956 #-------------------------------------------------------------
2957 # handle --preserve-line-endings or -output-line-ending flags
2958 #-------------------------------------------------------------
2959 # The native line separator has been used in all intermediate
2960 # iterations and filter operations until here so that string
2961 # operations work ok.
2962 663 ▶ if ( $self->[_line_separator_] ne "\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[12] ne "\n") 0 663 663
2963 0 my $line_separator = $self->[_line_separator_];
2964 0 my @output_lines = split /^/, ${$routput_string};
2965 0 foreach my $line (@output_lines) {
2966  
2967 # must check chomp because last line might not have a newline
2968 # if --noadd-terminal-newline is also set (c283)
2969 0 ▶ if ( chomp $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (chomp $line) 0 0 0
2970 0 $line .= $line_separator;
2971 }
2972 }
2973 0 my $output_string = join( EMPTY_STRING, @output_lines );
2974 0 $routput_string = \$output_string;
2975 }
2976  
2977 663 return $routput_string;
2978 } ## end sub process_filter_layer
2979  
2980 # For safety, set an upper bound on number of iterations before stopping.
2981 # The average number of iterations is 2. No known cases exceed 5.
2982 45 ▶use constant ITERATION_LIMIT => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
2983  
2984 664 ▶sub process_iteration_layer {
Subroutine process_iteration_layer: called CC=51 SCAR=49.6
Pod undocumented
2985  
2986 664 my ( $self, $rinput_string ) = @_;
2987  
2988 # This is the iteration layer of processing.
2989 # Do all formatting, iterating if requested, on the source $rinput_string
2990 # Output depends on format type:
2991 # For 'tidy' formatting, output goes to sink object
2992 # For 'html' formatting, output goes to the ultimate destination
2993 # For 'user' formatting, user formatter handles output
2994  
2995 # Total formatting is done with these layers of subroutines:
2996 # perltidy - main routine; checks run parameters
2997 # process_all_files - main loop to process all files;
2998 # process_filter_layer - do any pre and post processing
2999 # *process_iteration_layer - do any iterations on formatting; *THIS LAYER
3000 # process_single_case - solves one formatting problem
3001  
3002 # Data Flow in this layer:
3003 # $rinput_string -> [ loop over iterations ] -> $routput_string
3004  
3005 664 my $diagnostics_object = $self->[_diagnostics_object_];
3006 664 my $display_name = $self->[_display_name_];
3007 664 my $fileroot = $self->[_fileroot_];
3008 664 my $is_encoded_data = $self->[_is_encoded_data_];
3009 664 my $is_pure_ascii_data = $self->[_is_pure_ascii_data_];
3010 664 my $length_function = $self->[_length_function_];
3011 664 my $logger_object = $self->[_logger_object_];
3012 664 my $rOpts = $self->[_rOpts_];
3013 664 my $user_formatter = $self->[_user_formatter_];
3014  
3015 # make a debugger object if requested
3016 664 my $debugger_object;
3017 664 ▶ if ( $rOpts->{DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"DEBUG"}) 2 662 664
3018 2 ▶ my $debug_file = $self->[_debugfile_stream_]
Condition33%1/3
$self->[1] || $fileroot . $self->make_file_extension('DEBUG')
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->[1] || $fileroot . $self->make_file_extension('DEBUG')
$self->[1] $fileroot . $self->make_file_extension('DEBUG')
Truth table33%1/3
A || B
A= $self->[1]B= $fileroot . $self->make_file_extension('DEBUG')
ABresult
1X1
011
000
3019 || $fileroot . $self->make_file_extension('DEBUG');
3020 2 $debugger_object =
3021 Perl::Tidy::Debugger->new( $debug_file, $is_encoded_data );
3022 }
3023  
3024 # make a tee file handle if requested
3025 664 my $fh_tee;
3026 my $tee_file;
3027 664 ▶ if ( $rOpts->{'tee-pod'}
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"tee-pod"} or $rOpts->{"tee-block-comments"} or $rOpts->{"tee-side-comments"}) 1 663 664
Condition66%4/6
$rOpts->{"tee-pod"} or $rOpts->{"tee-block-comments"} or $rOpts->{"tee-side-comments"}
l!l&&r!l&&!r
10663
$rOpts->{"tee-pod"} or $rOpts->{"tee-block-comments"}
l!l&&r!l&&!r
10663
MC/DC33%1/3
$rOpts->{"tee-pod"} or $rOpts->{"tee-block-comments"} or $rOpts->{"tee-side-comments"}
$rOpts->{"tee-pod"} $rOpts->{"tee-block-comments"} $rOpts->{"tee-side-comments"}
Truth table50%2/4
A or B or C
A= $rOpts->{"tee-pod"}B= $rOpts->{"tee-block-comments"}C= $rOpts->{"tee-side-comments"}
ABCresult
1XX1
01X1
0011
0000
3028 || $rOpts->{'tee-block-comments'}
3029 || $rOpts->{'tee-side-comments'} )
3030 {
3031 1 ▶ $tee_file = $self->[_teefile_stream_]
Condition33%1/3
$self->[22] || $fileroot . $self->make_file_extension('TEE')
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->[22] || $fileroot . $self->make_file_extension('TEE')
$self->[22] $fileroot . $self->make_file_extension('TEE')
Truth table33%1/3
A || B
A= $self->[22]B= $fileroot . $self->make_file_extension('TEE')
ABresult
1X1
011
000
3032 || $fileroot . $self->make_file_extension('TEE');
3033 1 $fh_tee = Perl::Tidy::streamhandle( $tee_file, 'w', $is_encoded_data );
3034 1 ▶ if ( !$fh_tee ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh_tee) 0 1 1
3035 0 Warn("couldn't open TEE file $tee_file: $OS_ERROR\n");
3036 }
3037 }
3038  
3039 # vars for iterations and convergence test
3040 664 my $max_iterations = 1;
3041 664 my $convergence_log_message;
3042 my %saw_md5;
3043  
3044 # Only 'tidy' formatting can use multiple iterations
3045 664 ▶ if ( $rOpts->{'format'} eq 'tidy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"format"} eq "tidy") 663 1 664
3046  
3047 # check iteration count and quietly fix if necessary:
3048 # - iterations option only applies to code beautification mode
3049 # - the convergence check should stop most runs on iteration 2, and
3050 # virtually all on iteration 3. We allow up to ITERATION_LIMIT.
3051 663 $max_iterations = $rOpts->{'iterations'};
3052 663 ▶ if ( !defined($max_iterations)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $max_iterations or $max_iterations <= 0) 0 663 663
Condition33%1/3
not defined $max_iterations or $max_iterations <= 0
l!l&&r!l&&!r
00663
MC/DC0%0/2
not defined $max_iterations or $max_iterations <= 0
not defined $max_iterations $max_iterations <= 0
Truth table33%1/3
not(A) or B
A= not defined $max_iterationsB= $max_iterations <= 0
ABresult
1X1
011
000
3053 || $max_iterations <= 0 )
3054 {
3055 0 $max_iterations = 1;
3056 }
3057  
3058 663 ▶ if ( $max_iterations > ITERATION_LIMIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_iterations > 6) 0 663 663
3059 0 $max_iterations = ITERATION_LIMIT;
3060 }
3061  
3062 # get starting MD5 sum for convergence test
3063 663 ▶ if ( $max_iterations > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_iterations > 1) 3 660 663
3064 3 my $digest = $md5_hex->( ${$rinput_string} );
3065 3 $saw_md5{$digest} = 0;
3066 }
3067 }
3068  
3069 # save objects to allow redirecting output during iterations
3070 664 my $logger_object_final = $logger_object;
3071 664 my $iteration_of_formatter_convergence;
3072 my $routput_string;
3073  
3074 #---------------------
3075 # Loop over iterations
3076 #---------------------
3077 664 foreach my $iter ( 1 .. $max_iterations ) {
3078  
3079 666 $rstatus->{'iteration_count'} += 1;
3080  
3081 # create a string to capture the output
3082 666 my $sink_buffer = EMPTY_STRING;
3083 666 $routput_string = \$sink_buffer;
3084  
3085 # Save logger, debugger and tee output only on pass 1 because:
3086 # (1) line number references must be to the starting
3087 # source, not an intermediate result, and
3088 # (2) we need to know if there are errors so we can stop the
3089 # iterations early if necessary.
3090 # (3) the tee option only works on first pass if comments are also
3091 # being deleted.
3092 666 ▶ if ( $iter > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter > 1) 2 664 666
3093  
3094 2 ▶ $debugger_object->close_debug_file()
Branch: 1 branch
BranchTrueFalseTotal
if $debugger_object 0 2 2
3095 if ($debugger_object);
3096  
3097 2 ▶ if ( $fh_tee
Branch: 1 branch
BranchTrueFalseTotal
if ($fh_tee and $fh_tee->can("close") and not ref $tee_file and $tee_file ne "-") 0 2 2
Condition33%3/9
$fh_tee and $fh_tee->can("close") and not ref $tee_file and $tee_file ne "-"
!ll&&!rl&&r
200
$fh_tee and $fh_tee->can("close") and not ref $tee_file
!ll&&!rl&&r
200
$fh_tee and $fh_tee->can("close")
!ll&&!rl&&r
200
MC/DC0%0/4
$fh_tee and $fh_tee->can("close") and not ref $tee_file and $tee_file ne "-"
$fh_tee $fh_tee->can("close") not ref $tee_file $tee_file ne "-"
Truth table20%1/5
A and B and not(C) and D
A= $fh_teeB= $fh_tee->can("close")C= not ref $tee_fileD= $tee_file ne "-"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
3098 && $fh_tee->can('close')
3099 && !ref($tee_file)
3100 && $tee_file ne '-' )
3101 {
3102 0 ▶ $fh_tee->close()
Branch: 1 branch
BranchTrueFalseTotal
$fh_tee->close or Warn("couldn't close TEE file ${tee_file}: $Perl::Tidy::OS_ERROR\n") 0 0 0
3103 or Warn("couldn't close TEE file $tee_file: $OS_ERROR\n");
3104 }
3105  
3106 2 $debugger_object = undef;
3107 2 $logger_object = undef;
3108 2 $fh_tee = undef;
3109 }
3110  
3111 #---------------------------------
3112 # create a formatter for this file
3113 #---------------------------------
3114  
3115 666 my $formatter;
3116  
3117 666 ▶ if ($user_formatter) {
Branch: 3 branches
BranchTrueFalseTotal
if ($user_formatter) { } 0 666 666
elsif ($rOpts->{'format'} eq 'html') { } 1 665 666
elsif ($rOpts->{'format'} eq 'tidy') { } 665 0 665
3118 0 $formatter = $user_formatter;
3119 }
3120 elsif ( $rOpts->{'format'} eq 'html' ) {
3121  
3122 my $html_toc_extension =
3123 1 $self->make_file_extension( $rOpts->{'html-toc-extension'},
3124 'toc' );
3125  
3126 my $html_src_extension =
3127 1 $self->make_file_extension( $rOpts->{'html-src-extension'},
3128 'src' );
3129  
3130 1 $formatter = Perl::Tidy::HtmlWriter->new(
3131 input_file => $fileroot,
3132 html_file => $self->[_output_file_],
3133 extension => $self->[_actual_output_extension_],
3134 html_toc_extension => $html_toc_extension,
3135 html_src_extension => $html_src_extension,
3136 is_encoded_data => $is_encoded_data,
3137 is_pure_ascii_data => $is_pure_ascii_data,
3138 logger_object => $logger_object,
3139 );
3140 }
3141 elsif ( $rOpts->{'format'} eq 'tidy' ) {
3142 665 $formatter = Perl::Tidy::Formatter->new(
3143 logger_object => $logger_object,
3144 diagnostics_object => $diagnostics_object,
3145 sink_object => $routput_string,
3146 length_function => $length_function,
3147 is_encoded_data => $is_encoded_data,
3148 fh_tee => $fh_tee,
3149 display_name => $display_name,
3150 );
3151 }
3152 else {
3153 0 Die("I don't know how to do -format=$rOpts->{'format'}\n");
3154 }
3155  
3156 666 ▶ if ( !$formatter ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($formatter) 0 666 666
3157 0 Die("Unable to continue with $rOpts->{'format'} formatting\n");
3158 }
3159  
3160 #-----------------------------------
3161 # create the tokenizer for this file
3162 #-----------------------------------
3163 my $tokenizer = Perl::Tidy::Tokenizer->new(
3164 source_object => $rinput_string,
3165 logger_object => $logger_object,
3166 debugger_object => $debugger_object,
3167 diagnostics_object => $diagnostics_object,
3168 rOpts => $rOpts,
3169 666 starting_level => $rOpts->{'starting-indentation-level'},
3170 );
3171  
3172 #---------------------------------
3173 # do processing for this iteration
3174 #---------------------------------
3175 666 $self->process_single_case( $tokenizer, $formatter );
3176  
3177 #--------------
3178 # report errors
3179 #--------------
3180  
3181 # see if the formatter is converged
3182 666 ▶ if ( $max_iterations > 1
Branch: 1 branch
BranchTrueFalseTotal
if ($max_iterations > 1 and not defined $iteration_of_formatter_convergence and $formatter->can("get_convergence_check")) 5 661 666
Condition66%4/6
$max_iterations > 1 and not defined $iteration_of_formatter_convergence and $formatter->can("get_convergence_check")
!ll&&!rl&&r
66105
$max_iterations > 1 and not defined $iteration_of_formatter_convergence
!ll&&!rl&&r
66105
MC/DC33%1/3
$max_iterations > 1 and not defined $iteration_of_formatter_convergence and $formatter->can("get_convergence_check")
$max_iterations > 1 not defined $iteration_of_formatter_convergence $formatter->can("get_convergence_check")
Truth table50%2/4
A and not(B) and C
A= $max_iterations > 1B= not defined $iteration_of_formatter_convergenceC= $formatter->can("get_convergence_check")
ABCresult
0XX0
10X0
1100
1111
3183 && !defined($iteration_of_formatter_convergence)
3184 && $formatter->can('get_convergence_check') )
3185 {
3186 5 ▶ if ( $formatter->get_convergence_check() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($formatter->get_convergence_check) 3 2 5
3187 3 $iteration_of_formatter_convergence = $iter;
3188 3 $rstatus->{'converged'} = 1;
3189 }
3190 }
3191  
3192 # line source for next iteration (if any) comes from the current
3193 # temporary output buffer
3194 666 ▶ if ( $iter < $max_iterations ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter < $max_iterations) 4 662 666
3195  
3196 4 $rinput_string = \$sink_buffer;
3197  
3198 # stop iterations if errors or converged
3199 4 my $stop_now = $self->[_input_copied_verbatim_];
3200 4 ▶ $stop_now ||= $tokenizer->get_unexpected_error_count();
Condition33%1/3
$stop_now ||= $tokenizer->get_unexpected_error_count
l!l&&r!l&&!r
004
MC/DC0%0/2
$stop_now ||= $tokenizer->get_unexpected_error_count
$stop_now $tokenizer->get_unexpected_error_count
Truth table33%1/3
A ||= B
A= $stop_nowB= $tokenizer->get_unexpected_error_count
ABresult
1X1
011
000
3201 4 my $stopping_on_error = $stop_now;
3202 4 ▶ if ($stop_now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stop_now) { } 0 4 4
3203 0 $convergence_log_message = <<EOM;
3204 Stopping iterations because of severe errors.
3205 EOM
3206 }
3207  
3208 # or do convergence test
3209 else {
3210  
3211 # stop if the formatter has converged
3212 4 ▶ $stop_now ||= defined($iteration_of_formatter_convergence);
Condition66%2/3
$stop_now ||= defined $iteration_of_formatter_convergence
l!l&&r!l&&!r
022
MC/DC50%1/2
$stop_now ||= defined $iteration_of_formatter_convergence
$stop_now defined $iteration_of_formatter_convergence
Truth table66%2/3
A ||= B
A= $stop_nowB= defined $iteration_of_formatter_convergence
ABresult
1X1
011
000
3213  
3214 4 my $digest = $md5_hex->($sink_buffer);
3215 4 ▶ if ( !defined( $saw_md5{$digest} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $saw_md5{$digest}) { } 3 1 4
elsif ($iter == 1 and not $stop_now and $formatter->can('want_second_iteration') and $formatter->want_second_iteration) { } 0 1 1
3216 3 $saw_md5{$digest} = $iter;
3217 }
3218  
3219 # do a second iteration if all ok and requested by formatter
3220 # to allow delayed adding/deleting of commas (git156, git143)
3221 ▶ elsif ( $iter == 1
Condition33%3/9
$iter == 1 and not $stop_now and $formatter->can("want_second_iteration") and $formatter->want_second_iteration
!ll&&!rl&&r
100
$iter == 1 and not $stop_now and $formatter->can("want_second_iteration")
!ll&&!rl&&r
100
$iter == 1 and not $stop_now
!ll&&!rl&&r
100
MC/DC0%0/4
$iter == 1 and not $stop_now and $formatter->can("want_second_iteration") and $formatter->want_second_iteration
$iter == 1 not $stop_now $formatter->can("want_second_iteration") $formatter->want_second_iteration
Truth table20%1/5
A and not(B) and C and D
A= $iter == 1B= not $stop_nowC= $formatter->can("want_second_iteration")D= $formatter->want_second_iteration
ABCDresult
0XXX0
10XX0
110X0
11100
11111
3222 && !$stop_now
3223 && $formatter->can('want_second_iteration')
3224 && $formatter->want_second_iteration() )
3225 {
3226 ## deja vu, but do not set $stop_now
3227 0 $saw_md5{$digest} = $iter;
3228 }
3229 else {
3230  
3231 # Deja vu, stop iterating
3232  
3233 1 $stop_now = 1;
3234 1 my $iterm = $iter - 1;
3235 1 ▶ if ( $saw_md5{$digest} != $iterm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_md5{$digest} != $iterm) { } 0 1 1
3236  
3237 # Blinking (oscillating) between two or more stable
3238 # end states. This is unlikely to occur with normal
3239 # parameters, but it can occur in stress testing
3240 # with extreme parameter values, such as very short
3241 # maximum line lengths. We want to catch and fix
3242 # them when they happen.
3243 0 $rstatus->{'blinking'} = 1;
3244 0 $convergence_log_message = <<EOM;
3245 BLINKER. Output for iteration $iter same as for $saw_md5{$digest}.
3246 EOM
3247 0 ▶ $stopping_on_error ||= $convergence_log_message;
Condition0%0/3
$stopping_on_error ||= $convergence_log_message
l!l&&r!l&&!r
000
MC/DC0%0/2
$stopping_on_error ||= $convergence_log_message
$stopping_on_error $convergence_log_message
Truth table0%0/3
A ||= B
A= $stopping_on_errorB= $convergence_log_message
ABresult
1X1
011
000
3248 DEVEL_MODE
3249 0 && print {*STDERR} $convergence_log_message;
3250 0 ▶ $diagnostics_object->write_diagnostics(
Branch: 1 branch
BranchTrueFalseTotal
if $diagnostics_object 0 0 0
3251 $convergence_log_message)
3252 if ($diagnostics_object);
3253  
3254 # Uncomment to search for blinking states:
3255 # Warn( "$display_name: blinking; iter $iter same as for $saw_md5{$digest}\n" );
3256  
3257 }
3258 else {
3259 1 $convergence_log_message = <<EOM;
3260 Converged. Output for iteration $iter same as for iter $iterm.
3261 EOM
3262 1 ▶ $diagnostics_object->write_diagnostics(
Branch: 1 branch
BranchTrueFalseTotal
if $diagnostics_object and $iterm > 2 0 1 1
Condition33%1/3
$diagnostics_object and $iterm > 2
!ll&&!rl&&r
100
MC/DC0%0/2
$diagnostics_object and $iterm > 2
$diagnostics_object $iterm > 2
Truth table33%1/3
A and B
A= $diagnostics_objectB= $iterm > 2
ABresult
0X0
100
111
3263 $convergence_log_message)
3264 if ( $diagnostics_object && $iterm > 2 );
3265 1 $rstatus->{'converged'} = 1;
3266 }
3267 }
3268 }
3269  
3270 4 ▶ if ($stop_now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stop_now) 2 2 4
3271  
3272 2 if (DEVEL_MODE) {
3273  
3274 if ( defined($iteration_of_formatter_convergence) ) {
3275  
3276 # This message cannot appear unless the formatter
3277 # convergence test above is temporarily skipped for
3278 # testing.
3279 if ( $iteration_of_formatter_convergence < $iter - 1 ) {
3280 print {*STDERR}
3281 "STRANGE Early conv in $display_name: Stopping on it=$iter, converged in formatter on $iteration_of_formatter_convergence\n";
3282 }
3283 }
3284 elsif ( !$stopping_on_error ) {
3285  
3286 # The md5 sum implies convergence but the convergence
3287 # was not detected by the Formatter. This is not
3288 # critical but should be investigated. It happened
3289 # once when a line break was placed before a phantom
3290 # comma under -qwaf, and a semicolon under -nasc,
3291 # and has been fixed (search for 'STRANGE').
3292 print {*STDERR}
3293 "STRANGE no conv in $display_name: stopping on it=$iter, but not converged in formatter\n";
3294 }
3295 else {
3296 ## looks ok
3297 }
3298 }
3299  
3300 # we are stopping the iterations early;
3301 2 last;
3302 }
3303 } ## end if ( $iter < $max_iterations)
3304 } ## end loop over iterations for one source file
3305  
3306 $debugger_object->close_debug_file()
3307 664 ▶ if ($debugger_object);
Branch: 1 branch
BranchTrueFalseTotal
if $debugger_object 2 662 664
3308  
3309 664 ▶ if ( $fh_tee
Branch: 1 branch
BranchTrueFalseTotal
if ($fh_tee and $fh_tee->can("close") and not ref $tee_file and $tee_file ne "-") 0 664 664
Condition44%4/9
$fh_tee and $fh_tee->can("close") and not ref $tee_file and $tee_file ne "-"
!ll&&!rl&&r
66400
$fh_tee and $fh_tee->can("close") and not ref $tee_file
!ll&&!rl&&r
66400
$fh_tee and $fh_tee->can("close")
!ll&&!rl&&r
66310
MC/DC0%0/4
$fh_tee and $fh_tee->can("close") and not ref $tee_file and $tee_file ne "-"
$fh_tee $fh_tee->can("close") not ref $tee_file $tee_file ne "-"
Truth table40%2/5
A and B and not(C) and D
A= $fh_teeB= $fh_tee->can("close")C= not ref $tee_fileD= $tee_file ne "-"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
3310 && $fh_tee->can('close')
3311 && !ref($tee_file)
3312 && $tee_file ne '-' )
3313 {
3314 0 ▶ $fh_tee->close()
Branch: 1 branch
BranchTrueFalseTotal
$fh_tee->close or Warn("couldn't close TEE file ${tee_file}: $Perl::Tidy::OS_ERROR\n") 0 0 0
3315 or Warn("couldn't close TEE file $tee_file: $OS_ERROR\n");
3316 }
3317  
3318 # leave logger object open for additional messages
3319 664 $logger_object = $logger_object_final;
3320 664 ▶ $logger_object->write_logfile_entry($convergence_log_message)
Branch: 1 branch
BranchTrueFalseTotal
if $convergence_log_message 1 663 664
3321 if ($convergence_log_message);
3322  
3323 664 return $routput_string;
3324  
3325 } ## end sub process_iteration_layer
3326  
3327 666 ▶sub process_single_case {
Subroutine process_single_case: called CC=3 SCAR=11.0
Pod undocumented
3328  
3329 666 my ( $self, $tokenizer, $formatter ) = @_;
3330  
3331 # Run the formatter on a single defined case
3332  
3333 # Total formatting is done with these layers of subroutines:
3334 # perltidy - main routine; checks run parameters
3335 # process_all_files - main loop to process all files;
3336 # process_filter_layer - do any pre and post processing;
3337 # process_iteration_layer - do any iterations on formatting
3338 # *process_single_case - solve one formatting problem; *THIS LAYER
3339  
3340 666 ▶ while ( my $line = $tokenizer->get_line() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $tokenizer->get_line) 9234 666 9900
3341 9234 $formatter->write_line($line);
3342 }
3343  
3344 # user-defined formatters are possible, and may not have a
3345 # sub 'finish_formatting', so we have to check
3346 666 ▶ if ( $formatter->can('finish_formatting') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($formatter->can("finish_formatting")) 666 0 666
3347 666 my $rtok_report = $tokenizer->report_tokenization_errors();
3348 666 my $verbatim = $formatter->finish_formatting($rtok_report);
3349 666 $self->[_input_copied_verbatim_] = $verbatim;
3350 }
3351  
3352 666 return;
3353 } ## end sub process_single_case
3354  
3355 658 ▶sub copy_buffer_to_external_ref {
Subroutine copy_buffer_to_external_ref: called CC=14 SCAR=31.2
Pod undocumented
3356  
3357 658 my ( $self, $routput, $destination_stream ) = @_;
3358  
3359 # Copy $routput to the final $destination_stream,
3360 # encoding if the flag $encode_destination_buffer is true.
3361  
3362 # Data Flow:
3363 # $destination_buffer -> [ encode? ] -> $destination_stream
3364  
3365 658 my $destination_buffer = EMPTY_STRING;
3366 658 ▶ if ( ref($routput) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $routput eq 'ARRAY') { } 0 658 658
elsif (ref $routput eq 'SCALAR') { } 658 0 658
3367 0 $destination_buffer = join( EMPTY_STRING, @{$routput} );
3368 }
3369 elsif ( ref($routput) eq 'SCALAR' ) {
3370 658 $destination_buffer = ${$routput};
3371 }
3372 else {
3373 0 Fault(
3374 "'copy_buffer_to_external_ref' expecting ref to ARRAY or SCALAR\n");
3375 }
3376  
3377 658 $rstatus->{'output_encoded_as'} = EMPTY_STRING;
3378 658 my $ref_destination_stream = ref($destination_stream);
3379  
3380 # Encode output? Strings and arrays use special encoding rules; see:
3381 # https://github.com/perltidy/perltidy/blob/master/docs/eos_flag.md
3382 658 my $encode_destination_buffer;
3383 658 ▶ if ( $ref_destination_stream eq 'SCALAR'
Branch: 2 branches
BranchTrueFalseTotal
if ($ref_destination_stream eq 'SCALAR' or $ref_destination_stream eq 'ARRAY') { } 658 0 658
elsif ($ref_destination_stream->can('print')) { } 0 0 0
Condition66%2/3
$ref_destination_stream eq "SCALAR" or $ref_destination_stream eq "ARRAY"
l!l&&r!l&&!r
65530
MC/DC0%0/2
$ref_destination_stream eq "SCALAR" or $ref_destination_stream eq "ARRAY"
$ref_destination_stream eq "SCALAR" $ref_destination_stream eq "ARRAY"
Truth table66%2/3
A or B
A= $ref_destination_stream eq "SCALAR"B= $ref_destination_stream eq "ARRAY"
ABresult
1X1
011
000
3384 || $ref_destination_stream eq 'ARRAY' )
3385 {
3386 658 my $rOpts = $self->[_rOpts_];
3387 $encode_destination_buffer =
3388 658 ▶ $rOpts->{'encode-output-strings'} && $self->[_decoded_input_as_];
Condition66%2/3
$rOpts->{'encode-output-strings'} && $self->[2]
!ll&&!rl&&r
06526
MC/DC50%1/2
$rOpts->{'encode-output-strings'} && $self->[2]
$rOpts->{'encode-output-strings'} $self->[2]
Truth table66%2/3
A && B
A= $rOpts->{'encode-output-strings'}B= $self->[2]
ABresult
0X0
100
111
3389 }
3390  
3391 # An object with a print method will use file encoding rules
3392 elsif ( $ref_destination_stream->can('print') ) {
3393 0 $encode_destination_buffer = $self->[_is_encoded_data_];
3394 }
3395 else {
3396 0 confess <<EOM;
3397 ------------------------------------------------------------------------
3398 No 'print' method is defined for object of class '$ref_destination_stream'
3399 Please check your call to Perl::Tidy::perltidy. Trace follows.
3400 ------------------------------------------------------------------------
3401 EOM
3402 }
3403  
3404 658 ▶ if ($encode_destination_buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encode_destination_buffer) 6 652 658
3405 6 my $encoded_buffer;
3406 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not eval { do { $encoded_buffer = &Encode::encode('UTF-8', $destination_buffer, 9); 1 } }) { } 0 6 6
3407 !eval {
3408 6 $encoded_buffer =
3409 Encode::encode( "UTF-8", $destination_buffer,
3410 Encode::FB_CROAK | Encode::LEAVE_SRC );
3411 6 1;
3412 }
3413 )
3414 {
3415 0 Warn(
3416 "Error attempting to encode output string ref; encoding not done\n"
3417 );
3418 }
3419 else {
3420 6 $destination_buffer = $encoded_buffer;
3421 6 $rstatus->{'output_encoded_as'} = 'UTF-8';
3422 }
3423 }
3424  
3425 # Send data for SCALAR, ARRAY & OBJ refs to its final destination
3426 658 ▶ if ( $ref_destination_stream eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref_destination_stream eq 'SCALAR') { } 655 3 658
elsif (defined $destination_buffer) { } 3 0 3
3427 655 ${$destination_stream} = $destination_buffer;
3428 }
3429 elsif ( defined($destination_buffer) ) {
3430 3 my @lines = split /^/, $destination_buffer;
3431 3 ▶ if ( $ref_destination_stream eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_destination_stream eq 'ARRAY') { } 3 0 3
3432 3 @{$destination_stream} = @lines;
3433 }
3434  
3435 # destination stream must be an object with print method
3436 else {
3437 0 foreach my $line (@lines) {
3438 0 $destination_stream->print($line);
3439 }
3440 0 ▶ if ( $ref_destination_stream->can('close') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_destination_stream->can("close")) 0 0 0
3441 0 $destination_stream->close();
3442 }
3443 }
3444 }
3445 else {
3446  
3447 # Empty destination buffer not going to a string ... could
3448 # happen for example if user deleted all pod or comments
3449 }
3450 658 return;
3451 } ## end sub copy_buffer_to_external_ref
3452  
3453 } ## end of closure for sub perltidy
3454  
3455 45 ▶sub line_diff {
Subroutine line_diff: not called CC=5 SCAR=33.0
Pod undocumented
3456  
3457 0 my ( $s1, $s2 ) = @_;
3458  
3459 # Given two strings, Return
3460 # $diff_marker = a string with caret (^) symbols indicating differences
3461 # $pos1 = character position of first difference; pos1=-1 if no difference
3462  
3463 # Form exclusive or of the strings, which has null characters where strings
3464 # have same common characters so non-null characters indicate character
3465 # differences.
3466 0 my $diff_marker = EMPTY_STRING;
3467 0 my $pos = -1;
3468 0 my $pos1 = -1;
3469 0 ▶ if ( defined($s1) && defined($s2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s1 and defined $s2) 0 0 0
Condition0%0/3
defined $s1 and defined $s2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s1 and defined $s2
defined $s1 defined $s2
Truth table0%0/3
A and B
A= defined $s1B= defined $s2
ABresult
0X0
100
111
3470 0 my $mask = $s1 ^ $s2;
3471  
3472 0 ▶ while ( $mask =~ /[^\0]/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask =~ /[^\0]/g) 0 0 0
3473 0 my $pos_last = $pos;
3474 0 $pos = $LAST_MATCH_START[0];
3475 0 ▶ if ( $pos1 < 0 ) { $pos1 = $pos; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos1 < 0) 0 0 0
3476 0 $diff_marker .= SPACE x ( $pos - $pos_last - 1 ) . '^';
3477  
3478 # we could continue to mark all differences, but there is no point
3479 0 last;
3480 } ## end while ( $mask =~ /[^\0]/g)
3481 }
3482 0 return ( $diff_marker, $pos1 );
3483 } ## end sub line_diff
3484  
3485 0 ▶sub compare_string_buffers {
Subroutine compare_string_buffers: not called CC=25 SCAR=64.8
Pod undocumented
3486  
3487 0 my ( $string_i, $string_o, ($max_diff_count) ) = @_;
3488  
3489 # Compare input and output string buffers and return a brief text
3490 # description of the first difference.
3491  
3492 # Given:
3493 # $string_i = input string, or ref to input string
3494 # $string_o = output string, or ref to output string
3495 # $max_diff_count = optional maximum number of differences to show,
3496 # default=1
3497 # Return:
3498 # a string showing differences
3499  
3500 0 ▶ my $rbufi = ref($string_i) ? $string_i : \$string_i;
Branch: 1 branch
BranchTrueFalseTotal
ref $string_i ? : 0 0 0
3501 0 ▶ my $rbufo = ref($string_o) ? $string_o : \$string_o;
Branch: 1 branch
BranchTrueFalseTotal
ref $string_o ? : 0 0 0
3502  
3503 0 ▶ if ( !defined($max_diff_count) ) { $max_diff_count = 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max_diff_count) 0 0 0
3504  
3505 0 my ( @aryi, @aryo );
3506 0 my ( $leni, $leno ) = ( 0, 0 );
3507 0 ▶ if ( defined($rbufi) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rbufi) 0 0 0
3508 0 $leni = length( ${$rbufi} );
3509 0 @aryi = split /^/, ${$rbufi};
3510 }
3511 0 ▶ if ( defined($rbufo) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rbufo) 0 0 0
3512 0 $leno = length( ${$rbufo} );
3513 0 @aryo = split /^/, ${$rbufo};
3514 }
3515 0 my $nlines_i = @aryi;
3516 0 my $nlines_o = @aryo;
3517 0 my $msg = <<EOM;
3518 Input file length has $leni chars in $nlines_i lines
3519 Output file length has $leno chars in $nlines_o lines
3520 EOM
3521 0 ▶ return $msg unless ( $leni && $leno );
Branch: 1 branch
BranchTrueFalseTotal
unless $leni and $leno 0 0 0
Condition0%0/3
$leni and $leno
!ll&&!rl&&r
000
MC/DC0%0/2
$leni and $leno
$leni $leno
Truth table0%0/3
A and B
A= $leniB= $leno
ABresult
0X0
100
111
3522  
3523 0 ▶ my $truncate = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
3524 0 my ( $str, $lenmax ) = @_;
3525 0 ▶ if ( length($str) > $lenmax ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > $lenmax) 0 0 0
3526 0 $str = substr( $str, 0, $lenmax ) . "...";
3527 }
3528 0 return $str;
3529 0 }; ## end $truncate = sub
3530  
3531 0 my $last_nonblank_line = EMPTY_STRING;
3532 0 my $last_nonblank_count = 0;
3533  
3534 # loop over lines until we find a difference
3535 0 my $count = 0;
3536 0 my $diff_count = 0;
3537 0 ▶ while ( @aryi && @aryo ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@aryi and @aryo) 0 0 0
Condition0%0/3
@aryi and @aryo
!ll&&!rl&&r
000
MC/DC0%0/2
@aryi and @aryo
@aryi @aryo
Truth table0%0/3
A and B
A= @aryiB= @aryo
ABresult
0X0
100
111
3538 0 $count++;
3539 0 my $linei = shift @aryi;
3540 0 my $lineo = shift @aryo;
3541 0 chomp $linei;
3542 0 chomp $lineo;
3543 0 ▶ if ( $linei eq $lineo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linei eq $lineo) 0 0 0
3544 0 ▶ if ( length($linei) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $linei) 0 0 0
3545 0 $last_nonblank_line = $linei;
3546 0 $last_nonblank_count = $count;
3547 }
3548 0 next;
3549 }
3550  
3551 #---------------------------
3552 # lines differ ... finish up
3553 #---------------------------
3554 0 my ( $line_diff, $pos1 ) = line_diff( $linei, $lineo );
3555 0 my $ch1 = $pos1 + 1;
3556 0 my $reason = "Files first differ at character $ch1 of line $count";
3557  
3558 0 my ( $leading_ws_i, $leading_ws_o ) = ( EMPTY_STRING, EMPTY_STRING );
3559 0 ▶ if ( $linei =~ /^(\s+)/ ) { $leading_ws_i = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($linei =~ /^(\s+)/) 0 0 0
3560 0 ▶ if ( $lineo =~ /^(\s+)/ ) { $leading_ws_o = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($lineo =~ /^(\s+)/) 0 0 0
3561 0 ▶ if ( $leading_ws_i ne $leading_ws_o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_ws_i ne $leading_ws_o) { } 0 0 0
3562 0 $reason .= "; leading whitespace differs";
3563 0 ▶ if ( $leading_ws_i =~ /\t/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_ws_i =~ /\t/) 0 0 0
3564 0 $reason .= "; input has tab char";
3565 }
3566 }
3567 else {
3568 0 my ( $trailing_ws_i, $trailing_ws_o ) =
3569 ( EMPTY_STRING, EMPTY_STRING );
3570 0 ▶ if ( $linei =~ /(\s+)$/ ) { $trailing_ws_i = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($linei =~ /(\s+)$/) 0 0 0
3571 0 ▶ if ( $lineo =~ /(\s+)$/ ) { $trailing_ws_o = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($lineo =~ /(\s+)$/) 0 0 0
3572 0 ▶ if ( $trailing_ws_i ne $trailing_ws_o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailing_ws_i ne $trailing_ws_o) 0 0 0
3573 0 $reason .= "; trailing whitespace differs";
3574 }
3575 }
3576 0 $msg .= $reason . "\n";
3577  
3578 # limit string display length
3579 0 ▶ if ( $pos1 > 60 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos1 > 60) 0 0 0
3580 0 my $drop = $pos1 - 40;
3581 0 $linei = "..." . substr( $linei, $drop );
3582 0 $lineo = "..." . substr( $lineo, $drop );
3583 0 $line_diff = SPACE x 3 . substr( $line_diff, $drop );
3584 }
3585 0 $linei = $truncate->( $linei, 72 );
3586 0 $lineo = $truncate->( $lineo, 72 );
3587 0 $last_nonblank_line = $truncate->( $last_nonblank_line, 72 );
3588  
3589 0 ▶ if ($last_nonblank_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_nonblank_line) 0 0 0
3590 0 $msg .= <<EOM;
3591 $last_nonblank_count:$last_nonblank_line
3592 EOM
3593 }
3594 0 $line_diff = SPACE x ( 2 + length($count) ) . $line_diff;
3595 0 $msg .= <<EOM;
3596 <$count:$linei
3597 >$count:$lineo
3598 $line_diff
3599 EOM
3600 0 $diff_count++;
3601 0 ▶ last if ( $diff_count >= $max_diff_count );
Branch: 1 branch
BranchTrueFalseTotal
if $diff_count >= $max_diff_count 0 0 0
3602 } ## end while ( @aryi && @aryo )
3603  
3604 0 ▶ if ($diff_count) { return $msg }
Branch: 1 branch
BranchTrueFalseTotal
if ($diff_count) 0 0 0
3605  
3606 #------------------------------------------------------
3607 # no differences found, see if one file has fewer lines
3608 #------------------------------------------------------
3609 0 ▶ if ( $nlines_i > $nlines_o ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nlines_i > $nlines_o) { } 0 0 0
elsif ($nlines_i < $nlines_o) { } 0 0 0
3610 0 $msg .= <<EOM;
3611 Files initially match file but output file has fewer lines
3612 EOM
3613 }
3614 elsif ( $nlines_i < $nlines_o ) {
3615 0 $msg .= <<EOM;
3616 Files initially match file but input file has fewer lines
3617 EOM
3618 }
3619 else {
3620 0 $msg .= <<EOM;
3621 Text in lines of file match but checksums differ. Perhaps line endings differ.
3622 EOM
3623 }
3624 0 return $msg;
3625 } ## end sub compare_string_buffers
3626  
3627 0 ▶sub fileglob_to_re {
Subroutine fileglob_to_re: not called CC=1 SCAR=6.9
Pod undocumented
3628  
3629 # modified (corrected) from version in find2perl
3630 0 my $x = shift;
3631 0 $x =~ s/([.\/^\$()])/\\$1/g; # escape special characters
3632 0 $x =~ s/\*/.*/g; # '*' -> '.*'
3633 0 $x =~ s/\?/./g; # '?' -> '.'
3634 0 return "^$x\\z"; # match whole word
3635 } ## end sub fileglob_to_re
3636  
3637 664 ▶sub make_logfile_header {
Subroutine make_logfile_header: called CC=4 SCAR=15.2
Pod undocumented
3638 664 my ( $rOpts, $config_file, $rraw_options, $Windows_type, $readable_options )
3639 = @_;
3640  
3641 # Note: the punctuation variable '$]' is not in older versions of
3642 # English.pm so leave it as is to avoid failing installation tests.
3643 664 my $msg =
3644 "perltidy version $VERSION log file on a $OSNAME system, OLD_PERL_VERSION=$]\n";
3645 664 ▶ if ($Windows_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Windows_type) 0 664 664
3646 0 $msg .= "Windows type is $Windows_type\n";
3647 }
3648 664 my $options_string = join( SPACE, @{$rraw_options} );
3649  
3650 664 ▶ if ( defined($config_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file) 656 8 664
3651 656 $msg .= "Found Configuration File >>> $config_file \n";
3652 }
3653 664 $msg .= "Configuration and command line parameters for this run:\n";
3654 664 $msg .= "$options_string\n";
3655  
3656 664 ▶ if ( $rOpts->{'show-options'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"show-options"}) 0 664 664
3657 0 $rOpts->{'logfile'} = 1; # force logfile to be saved
3658 0 $msg .= "Final parameter set for this run\n";
3659 0 $msg .= "------------------------------------\n";
3660  
3661 0 $msg .= $readable_options;
3662  
3663 0 $msg .= "------------------------------------\n";
3664 }
3665 664 $msg .= "To find error messages search for 'WARNING' with your editor\n";
3666 664 return $msg;
3667 } ## end sub make_logfile_header
3668  
3669 662 ▶sub generate_options {
Subroutine generate_options: called CC=10 SCAR=23.0
Pod undocumented
3670  
3671 ######################################################################
3672 # Generate and return references to:
3673 # @option_string - the list of options to be passed to Getopt::Long
3674 # @defaults - the list of default options
3675 # %expansion - a hash showing how all abbreviations are expanded
3676 # %category - a hash giving the general category of each option
3677 # %integer_option_range - valid ranges of certain options
3678  
3679 # Note: a few options are not documented in the man page and usage
3680 # message. This is because these are deprecated, experimental or debug
3681 # options and may or may not be retained in future versions:
3682  
3683 # These undocumented flags are for debugging:
3684 # --recombine # used to debug line breaks
3685 # --short-concatenation-item-length # used to break a '.' chain
3686 #
3687 ######################################################################
3688  
3689 # here is a summary of the Getopt codes:
3690 # <none> does not take an argument
3691 # =s takes a mandatory string
3692 # :s takes an optional string (DO NOT USE - filenames will get eaten up)
3693 # =i takes a mandatory integer
3694 # :i takes an optional integer (NOT RECOMMENDED - can cause trouble)
3695 # ! does not take an argument and may be negated
3696 # i.e., -foo and -nofoo are allowed
3697 # a double dash signals the end of the options list
3698 #
3699 #-----------------------------------------------
3700 # Define the option string passed to GetOptions.
3701 #-----------------------------------------------
3702  
3703 662 my @option_string = ();
3704 662 my %expansion = ();
3705 662 my %option_category = ();
3706 662 my %integer_option_range;
3707  
3708 # names of categories in manual
3709 # leading integers will allow sorting
3710 662 my @category_name = (
3711 '0. I/O control',
3712 '1. Basic formatting options',
3713 '2. Code indentation control',
3714 '3. Whitespace control',
3715 '4. Comment controls',
3716 '5. Linebreak controls',
3717 '6. Controlling list formatting',
3718 '7. Retaining or ignoring existing line breaks',
3719 '8. Blank line control',
3720 '9. Other controls',
3721 '10. HTML options',
3722 '11. pod2html options',
3723 '12. Controlling HTML properties',
3724 '13. Debugging',
3725 );
3726  
3727 # These options are parsed directly by perltidy:
3728 # help h
3729 # version v
3730 # However, they are included in the option set so that they will
3731 # be seen in the options dump.
3732  
3733 # These long option names have no abbreviations or are treated specially
3734 662 @option_string = qw(
3735 html!
3736 noprofile
3737 nopro
3738 no-profile
3739 npro
3740 recombine!
3741 notidy
3742 );
3743  
3744 662 my $category = 13; # Debugging
3745 662 foreach (@option_string) {
3746 4634 my $opt = $_; # must avoid changing the actual flag
3747 4634 $opt =~ s/!$//;
3748 4634 $option_category{$opt} = $category_name[$category];
3749 }
3750  
3751 662 $category = 11; # HTML
3752 662 $option_category{html} = $category_name[$category];
3753  
3754 # Routine to install and check options
3755 213164 ▶ my $add_option = sub {
Subroutine __ANON__: called CC=5 SCAR=17.3
3756  
3757 213164 my ( $long_name, $short_name, $flag ) = @_;
3758  
3759 # Given:
3760 # $long_name = the full option name, such as 'backup-method'
3761 # $short_name = the abbreviation, such as 'bm'
3762 # $flag = the Getopt code, such as '=s', see above list
3763  
3764 213164 push @option_string, $long_name . $flag;
3765 213164 $option_category{$long_name} = $category_name[$category];
3766 213164 ▶ if ($short_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short_name) 213164 0 213164
3767 213164 ▶ if ( $expansion{$short_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expansion{$short_name}) 0 213164 213164
3768 0 my $existing_name = $expansion{$short_name}->[0];
3769 0 Die(
3770 "redefining abbreviation $short_name for $long_name; already used for $existing_name\n"
3771 );
3772 }
3773 213164 $expansion{$short_name} = [$long_name];
3774 213164 ▶ if ( $flag eq '!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "!") 108568 104596 213164
3775 108568 my $nshort_name = 'n' . $short_name;
3776 108568 my $nolong_name = 'no' . $long_name;
3777 108568 ▶ if ( $expansion{$nshort_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expansion{$nshort_name}) 0 108568 108568
3778 0 my $existing_name = $expansion{$nshort_name}->[0];
3779 0 Die(
3780 "attempting to redefine abbreviation $nshort_name for $nolong_name; already used for $existing_name\n"
3781 );
3782 }
3783 108568 $expansion{$nshort_name} = [$nolong_name];
3784 }
3785 }
3786 213164 return;
3787 662 }; ## end $add_option = sub
3788  
3789 # Install long option names which have a simple abbreviation.
3790 # Options with code '!' get standard negation ('no' for long names,
3791 # 'n' for abbreviations). Categories follow the manual.
3792  
3793 # Be careful not to re-use any of these --html short names.
3794 # Use --dump-short-names to make this list.
3795 # hbc hbcm hbco hbh hbhh hbi hbj hbk hbm hbn hbp hbpd
3796 # hbpu hbq hbs hbsc hbv hbw hcbg hcc hccm hcco hch hchh
3797 # hci hcj hck hcm hcn hcp hcpd hcpu hcq hcs hcsc hcv
3798 # hcw hent hic hicm hico hih hihh hii hij hik him hin
3799 # hip hipd hipu hiq his hisc hiu hiv hiw
3800  
3801 ###########################
3802 662 $category = 0; # I/O_Control
3803 ###########################
3804 662 $add_option->( 'backup-and-modify-in-place', 'b', '!' );
3805 662 $add_option->( 'backup-file-extension', 'bext', '=s' );
3806 662 $add_option->( 'backup-method', 'bm', '=s' );
3807 662 $add_option->( 'character-encoding', 'enc', '=s' );
3808 662 $add_option->( 'force-read-binary', 'f', '!' );
3809 662 $add_option->( 'format', 'fmt', '=s' );
3810 662 $add_option->( 'iterations', 'it', '=i' );
3811 662 $add_option->( 'logfile', 'log', '!' );
3812 662 $add_option->( 'logfile-gap', 'g', ':i' );
3813 662 $add_option->( 'outfile', 'o', '=s' );
3814 662 $add_option->( 'output-file-extension', 'oext', '=s' );
3815 662 $add_option->( 'output-path', 'opath', '=s' );
3816 662 $add_option->( 'profile', 'pro', '=s' );
3817 662 $add_option->( 'quiet', 'q', '!' );
3818 662 $add_option->( 'standard-error-output', 'se', '!' );
3819 662 $add_option->( 'standard-output', 'st', '!' );
3820 662 $add_option->( 'use-unicode-gcstring', 'gcs', '!' );
3821 662 $add_option->( 'warning-output', 'w', '!' );
3822 662 $add_option->( 'add-terminal-newline', 'atnl', '!' );
3823 662 $add_option->( 'line-range-tidy', 'lrt', '=s' );
3824 662 $add_option->( 'timeout-in-seconds', 'tos', '=i' );
3825  
3826 # options which are both toggle switches and values moved here
3827 # to hide from tidyview (which does not show category 0 flags):
3828 # -ole moved here from category 1
3829 # -sil moved here from category 2
3830 662 $add_option->( 'output-line-ending', 'ole', '=s' );
3831 662 $add_option->( 'starting-indentation-level', 'sil', '=i' );
3832  
3833 ########################################
3834 662 $category = 1; # Basic formatting options
3835 ########################################
3836 662 $add_option->( 'entab-leading-whitespace', 'et', '=i' );
3837 662 $add_option->( 'indent-columns', 'i', '=i' );
3838 662 $add_option->( 'maximum-line-length', 'l', '=i' );
3839 662 $add_option->( 'variable-maximum-line-length', 'vmll', '!' );
3840 662 $add_option->( 'whitespace-cycle', 'wc', '=i' );
3841 662 $add_option->( 'preserve-line-endings', 'ple', '!' );
3842 662 $add_option->( 'tabs', 't', '!' );
3843 662 $add_option->( 'default-tabsize', 'dt', '=i' );
3844 662 $add_option->( 'extended-syntax', 'xs', '!' );
3845 662 $add_option->( 'assert-tidy', 'ast', '!' );
3846 662 $add_option->( 'assert-untidy', 'asu', '!' );
3847 662 $add_option->( 'encode-output-strings', 'eos', '!' );
3848 662 $add_option->( 'sub-alias-list', 'sal', '=s' );
3849 662 $add_option->( 'grep-alias-list', 'gal', '=s' );
3850 662 $add_option->( 'grep-alias-exclusion-list', 'gaxl', '=s' );
3851 662 $add_option->( 'use-feature', 'uf', '=s' );
3852  
3853 ########################################
3854 662 $category = 2; # Code indentation control
3855 ########################################
3856 662 $add_option->( 'continuation-indentation', 'ci', '=i' );
3857 662 $add_option->( 'extended-continuation-indentation', 'xci', '!' );
3858 662 $add_option->( 'minimize-continuation-indentation', 'mci', '!' );
3859 662 $add_option->( 'line-up-parentheses', 'lp', '!' );
3860 662 $add_option->( 'extended-line-up-parentheses', 'xlp', '!' );
3861 662 $add_option->( 'line-up-parentheses-exclusion-list', 'lpxl', '=s' );
3862 662 $add_option->( 'line-up-parentheses-inclusion-list', 'lpil', '=s' );
3863 662 $add_option->( 'outdent-keyword-list', 'okwl', '=s' );
3864 662 $add_option->( 'outdent-keywords', 'okw', '!' );
3865 662 $add_option->( 'outdent-labels', 'ola', '!' );
3866 662 $add_option->( 'outdent-long-quotes', 'olq', '!' );
3867 662 $add_option->( 'indent-closing-brace', 'icb', '!' );
3868 662 $add_option->( 'indent-leading-semicolon', 'ils', '!' );
3869 662 $add_option->( 'closing-token-indentation', 'cti', '=i' );
3870 662 $add_option->( 'closing-paren-indentation', 'cpi', '=i' );
3871 662 $add_option->( 'closing-brace-indentation', 'cbi', '=i' );
3872 662 $add_option->( 'closing-square-bracket-indentation', 'csbi', '=i' );
3873 662 $add_option->( 'brace-left-and-indent', 'bli', '!' );
3874 662 $add_option->( 'brace-left-and-indent-list', 'blil', '=s' );
3875 662 $add_option->( 'brace-left-and-indent-exclusion-list', 'blixl', '=s' );
3876 662 $add_option->( 'heredoc-indentation-update', 'hiu', '!' );
3877 662 $add_option->( 'heredoc-convert-to', 'hct', '=s' );
3878 662 $add_option->( 'heredoc-extra-spaces', 'hxs', '=s' );
3879 662 $add_option->( 'heredoc-excess-length-option', 'hxlo', '=i' );
3880 662 $add_option->( 'heredoc-tag-exclusion-pattern', 'htxp', '=s' );
3881  
3882 ########################################
3883 662 $category = 3; # Whitespace control
3884 ########################################
3885 662 $add_option->( 'add-trailing-commas', 'atc', '!' );
3886 662 $add_option->( 'add-lone-trailing-commas', 'altc', '!' );
3887 662 $add_option->( 'add-semicolons', 'asc', '!' );
3888 662 $add_option->( 'add-whitespace', 'aws', '!' );
3889 662 $add_option->( 'block-brace-tightness', 'bbt', '=i' );
3890 662 $add_option->( 'brace-tightness', 'bt', '=i' );
3891 662 $add_option->( 'delete-old-whitespace', 'dws', '!' );
3892 662 $add_option->( 'delete-repeated-commas', 'drc', '!' );
3893 662 $add_option->( 'delete-trailing-commas', 'dtc', '!' );
3894 662 $add_option->( 'delete-lone-trailing-commas', 'dltc', '!' );
3895 662 $add_option->( 'delete-weld-interfering-commas', 'dwic', '!' );
3896 662 $add_option->( 'delete-semicolons', 'dsm', '!' );
3897 662 $add_option->( 'function-paren-vertical-alignment', 'fpva', '!' );
3898 662 $add_option->( 'delay-trailing-comma-operations', 'dtco', '!' );
3899 662 $add_option->( 'keyword-paren-inner-tightness', 'kpit', '=i' );
3900 662 $add_option->( 'keyword-paren-inner-tightness-list', 'kpitl', '=s' );
3901 662 $add_option->( 'logical-padding', 'lop', '!' );
3902 662 $add_option->( 'multiple-token-tightness', 'mutt', '=s' );
3903 662 $add_option->( 'nospace-after-keyword', 'nsak', '=s' );
3904 662 $add_option->( 'nowant-left-space', 'nwls', '=s' );
3905 662 $add_option->( 'nowant-right-space', 'nwrs', '=s' );
3906 662 $add_option->( 'paren-tightness', 'pt', '=i' );
3907 662 $add_option->( 'space-after-keyword', 'sak', '=s' );
3908 662 $add_option->( 'space-for-semicolon', 'sfs', '!' );
3909 662 $add_option->( 'space-function-paren', 'sfp', '!' );
3910 662 $add_option->( 'space-keyword-paren', 'skp', '!' );
3911 662 $add_option->( 'space-terminal-semicolon', 'sts', '!' );
3912 662 $add_option->( 'square-bracket-tightness', 'sbt', '=i' );
3913 662 $add_option->( 'square-bracket-vertical-tightness', 'sbvt', '=i' );
3914 662 $add_option->( 'square-bracket-vertical-tightness-closing', 'sbvtc', '=i' );
3915 662 $add_option->( 'tight-secret-operators', 'tso', '!' );
3916 662 $add_option->( 'trim-qw', 'tqw', '!' );
3917 662 $add_option->( 'trim-pod', 'trp', '!' );
3918 662 $add_option->( 'want-left-space', 'wls', '=s' );
3919 662 $add_option->( 'want-right-space', 'wrs', '=s' );
3920 662 $add_option->( 'want-trailing-commas', 'wtc', '=s' );
3921 662 $add_option->( 'space-prototype-paren', 'spp', '=i' );
3922 662 $add_option->( 'space-signature-paren', 'ssp', '=i' );
3923 662 $add_option->( 'valign-code', 'vc', '!' );
3924 662 $add_option->( 'valign-block-comments', 'vbc', '!' );
3925 662 $add_option->( 'valign-side-comments', 'vsc', '!' );
3926 662 $add_option->( 'valign-exclusion-list', 'vxl', '=s' );
3927 662 $add_option->( 'valign-inclusion-list', 'vil', '=s' );
3928 662 $add_option->( 'valign-if-unless', 'viu', '!' );
3929 662 $add_option->( 'valign-comparison-operators', 'vco', '!' );
3930 662 $add_option->( 'valign-signed-numbers', 'vsn', '!' );
3931 662 $add_option->( 'valign-signed-numbers-limit', 'vsnl', '=i' );
3932 662 $add_option->( 'valign-wide-equals', 'vwe', '!' );
3933 662 $add_option->( 'extended-block-tightness', 'xbt', '!' );
3934 662 $add_option->( 'extended-block-tightness-list', 'xbtl', '=s' );
3935 662 $add_option->( 'qw-as-function', 'qwaf', '!' );
3936  
3937 ########################################
3938 662 $category = 4; # Comment controls
3939 ########################################
3940 662 $add_option->( 'closing-side-comment-else-flag', 'csce', '=i' );
3941 662 $add_option->( 'closing-side-comment-interval', 'csci', '=i' );
3942 662 $add_option->( 'closing-side-comment-list', 'cscl', '=s' );
3943 662 $add_option->( 'closing-side-comment-exclusion-list', 'cscxl', '=s' );
3944 662 $add_option->( 'closing-side-comment-maximum-text', 'csct', '=i' );
3945 662 $add_option->( 'closing-side-comment-prefix', 'cscp', '=s' );
3946 662 $add_option->( 'closing-side-comment-warnings', 'cscw', '!' );
3947 662 $add_option->( 'closing-side-comments', 'csc', '!' );
3948 662 $add_option->( 'closing-side-comments-balanced', 'cscb', '!' );
3949 662 $add_option->( 'code-skipping', 'cs', '!' );
3950 662 $add_option->( 'code-skipping-begin', 'csb', '=s' );
3951 662 $add_option->( 'code-skipping-end', 'cse', '=s' );
3952 662 $add_option->( 'code-skipping-from-start', 'csfs', '!' );
3953 662 $add_option->( 'format-skipping', 'fs', '!' );
3954 662 $add_option->( 'format-skipping-begin', 'fsb', '=s' );
3955 662 $add_option->( 'format-skipping-end', 'fse', '=s' );
3956 662 $add_option->( 'detect-format-skipping-from-start', 'dfsfs', '!' );
3957 662 $add_option->( 'hanging-side-comments', 'hsc', '!' );
3958 662 $add_option->( 'indent-block-comments', 'ibc', '!' );
3959 662 $add_option->( 'indent-spaced-block-comments', 'isbc', '!' );
3960 662 $add_option->( 'fixed-position-side-comment', 'fpsc', '=i' );
3961 662 $add_option->( 'minimum-space-to-comment', 'msc', '=i' );
3962 662 $add_option->( 'non-indenting-braces', 'nib', '!' );
3963 662 $add_option->( 'non-indenting-brace-prefix', 'nibp', '=s' );
3964 662 $add_option->( 'outdent-long-comments', 'olc', '!' );
3965 662 $add_option->( 'outdent-static-block-comments', 'osbc', '!' );
3966 662 $add_option->( 'skip-formatting-except-id', 'sfei', '=s' );
3967 662 $add_option->( 'static-block-comment-prefix', 'sbcp', '=s' );
3968 662 $add_option->( 'static-block-comments', 'sbc', '!' );
3969 662 $add_option->( 'static-side-comment-prefix', 'sscp', '=s' );
3970 662 $add_option->( 'static-side-comments', 'ssc', '!' );
3971 662 $add_option->( 'ignore-side-comment-lengths', 'iscl', '!' );
3972 662 $add_option->( 'ignore-perlcritic-comments', 'ipc', '!' );
3973  
3974 ########################################
3975 662 $category = 5; # Linebreak controls
3976 ########################################
3977 662 $add_option->( 'add-newlines', 'anl', '!' );
3978 662 $add_option->( 'blank-lines-prevent-cuddles', 'blpc', '!' );
3979 662 $add_option->( 'block-brace-vertical-tightness', 'bbvt', '=i' );
3980 662 $add_option->( 'block-brace-vertical-tightness-list', 'bbvtl', '=s' );
3981 662 $add_option->( 'brace-follower-vertical-tightness', 'bfvt', '=i' );
3982 662 $add_option->( 'brace-vertical-tightness', 'bvt', '=i' );
3983 662 $add_option->( 'brace-vertical-tightness-closing', 'bvtc', '=i' );
3984 662 $add_option->( 'cuddled-else', 'ce', '!' );
3985 662 $add_option->( 'cuddled-block-list', 'cbl', '=s' );
3986 662 $add_option->( 'cuddled-block-list-exclusive', 'cblx', '!' );
3987 662 $add_option->( 'cuddled-break-option', 'cbo', '=i' );
3988 662 $add_option->( 'cuddled-paren-brace', 'cpb', '!' );
3989 662 $add_option->( 'cuddled-paren-brace-weld', 'cpbw', '!' );
3990 662 $add_option->( 'delete-old-newlines', 'dnl', '!' );
3991 662 $add_option->( 'opening-brace-always-on-right', 'bar', '!' );
3992 662 $add_option->( 'opening-brace-on-new-line', 'bl', '!' );
3993 662 $add_option->( 'opening-hash-brace-right', 'ohbr', '!' );
3994 662 $add_option->( 'opening-paren-right', 'opr', '!' );
3995 662 $add_option->( 'opening-square-bracket-right', 'osbr', '!' );
3996 662 $add_option->( 'opening-anonymous-sub-brace-on-new-line', 'asbl', '!' );
3997 662 $add_option->( 'opening-sub-brace-on-new-line', 'sbl', '!' );
3998 662 $add_option->( 'paren-vertical-tightness', 'pvt', '=i' );
3999 662 $add_option->( 'paren-vertical-tightness-closing', 'pvtc', '=i' );
4000 662 $add_option->( 'weld-nested-containers', 'wn', '!' );
4001 662 $add_option->( 'weld-nested-exclusion-list', 'wnxl', '=s' );
4002 662 $add_option->( 'weld-fat-comma', 'wfc', '!' );
4003 662 $add_option->( 'space-backslash-quote', 'sbq', '=i' );
4004 662 $add_option->( 'stack-closing-block-brace', 'scbb', '!' );
4005 662 $add_option->( 'stack-closing-hash-brace', 'schb', '!' );
4006 662 $add_option->( 'stack-closing-paren', 'scp', '!' );
4007 662 $add_option->( 'stack-closing-square-bracket', 'scsb', '!' );
4008 662 $add_option->( 'stack-opening-hash-brace', 'sohb', '!' );
4009 662 $add_option->( 'stack-opening-paren', 'sop', '!' );
4010 662 $add_option->( 'stack-opening-square-bracket', 'sosb', '!' );
4011  
4012 # NOTE: --vt and --vtc are actually expansions now, so these two lines
4013 # might eventually be removed. But search for 'msdos' to see notes about
4014 # an issue with 'msdos' that could be a problem if msdos is still used.
4015 662 $add_option->( 'vertical-tightness', 'vt', '=i' );
4016 662 $add_option->( 'vertical-tightness-closing', 'vtc', '=i' );
4017  
4018 662 $add_option->( 'want-break-after', 'wba', '=s' );
4019 662 $add_option->( 'want-break-before', 'wbb', '=s' );
4020 662 $add_option->( 'break-after-all-operators', 'baao', '!' );
4021 662 $add_option->( 'break-before-all-operators', 'bbao', '!' );
4022 662 $add_option->( 'keep-interior-semicolons', 'kis', '!' );
4023 662 $add_option->( 'one-line-block-semicolons', 'olbs', '=i' );
4024 662 $add_option->( 'one-line-block-nesting', 'olbn', '=i' );
4025 662 $add_option->( 'one-line-block-exclusion-list', 'olbxl', '=s' );
4026 662 $add_option->( 'break-before-hash-brace', 'bbhb', '=i' );
4027 662 $add_option->( 'break-before-hash-brace-and-indent', 'bbhbi', '=i' );
4028 662 $add_option->( 'break-before-square-bracket', 'bbsb', '=i' );
4029 662 $add_option->( 'break-before-square-bracket-and-indent', 'bbsbi', '=i' );
4030 662 $add_option->( 'break-before-paren', 'bbp', '=i' );
4031 662 $add_option->( 'break-before-paren-and-indent', 'bbpi', '=i' );
4032 662 $add_option->( 'brace-left-list', 'bll', '=s' );
4033 662 $add_option->( 'brace-left-exclusion-list', 'blxl', '=s' );
4034 662 $add_option->( 'break-after-labels', 'bal', '=i' );
4035 662 $add_option->( 'pack-operator-types', 'pot', '=s' );
4036  
4037 # This was an experiment mentioned in git #78, originally named -bopl.
4038 662 $add_option->( 'break-open-compact-parens', 'bocp', '=s' );
4039  
4040 ########################################
4041 662 $category = 6; # Controlling list formatting
4042 ########################################
4043 662 $add_option->( 'break-at-old-comma-breakpoints', 'boc', '!' );
4044 662 $add_option->( 'break-at-old-comma-types', 'boct', '=s' );
4045 662 $add_option->( 'break-at-trailing-comma-types', 'btct', '=s' );
4046 662 $add_option->( 'comma-arrow-breakpoints', 'cab', '=i' );
4047 662 $add_option->( 'maximum-fields-per-table', 'mft', '=s' );
4048  
4049 ########################################
4050 662 $category = 7; # Retaining or ignoring existing line breaks
4051 ########################################
4052 662 $add_option->( 'break-at-old-keyword-breakpoints', 'bok', '!' );
4053 662 $add_option->( 'break-at-old-logical-breakpoints', 'bol', '!' );
4054 662 $add_option->( 'break-at-old-method-breakpoints', 'bom', '!' );
4055 662 $add_option->( 'break-at-old-semicolon-breakpoints', 'bos', '!' );
4056 662 $add_option->( 'break-at-old-ternary-breakpoints', 'bot', '!' );
4057 662 $add_option->( 'break-at-old-attribute-breakpoints', 'boa', '!' );
4058 662 $add_option->( 'break-at-old-trailing-conditionals', 'botc', '!' );
4059 662 $add_option->( 'break-at-old-trailing-loops', 'botl', '!' );
4060 662 $add_option->( 'keep-old-breakpoints-before', 'kbb', '=s' );
4061 662 $add_option->( 'keep-old-breakpoints-after', 'kba', '=s' );
4062 662 $add_option->( 'ignore-old-breakpoints', 'iob', '!' );
4063  
4064 ########################################
4065 662 $category = 8; # Blank line control
4066 ########################################
4067 662 $add_option->( 'blanks-before-blocks', 'bbb', '!' );
4068 662 $add_option->( 'blanks-before-comments', 'bbc', '!' );
4069 662 $add_option->( 'blanks-before-opening-comments', 'bboc', '!' );
4070 662 $add_option->( 'blank-lines-before-subs', 'blbs', '=i' );
4071 662 $add_option->( 'blank-lines-before-packages', 'blbp', '=i' );
4072 662 $add_option->( 'long-block-line-count', 'lbl', '=i' );
4073 662 $add_option->( 'maximum-consecutive-blank-lines', 'mbl', '=i' );
4074 662 $add_option->( 'keep-old-blank-lines', 'kbl', '=i' );
4075 662 $add_option->( 'keep-old-blank-lines-exceptions', 'kblx', '=s' );
4076  
4077 662 $add_option->( 'keyword-group-blanks-list', 'kgbl', '=s' );
4078 662 $add_option->( 'keyword-group-blanks-size', 'kgbs', '=s' );
4079 662 $add_option->( 'keyword-group-blanks-repeat-count', 'kgbr', '=i' );
4080 662 $add_option->( 'keyword-group-blanks-before', 'kgbb', '=i' );
4081 662 $add_option->( 'keyword-group-blanks-after', 'kgba', '=i' );
4082 662 $add_option->( 'keyword-group-blanks-inside', 'kgbi', '!' );
4083 662 $add_option->( 'keyword-group-blanks-delete', 'kgbd', '!' );
4084  
4085 662 $add_option->( 'blank-lines-after-opening-block', 'blao', '=i' );
4086 662 $add_option->( 'blank-lines-before-closing-block', 'blbc', '=i' );
4087 662 $add_option->( 'blank-lines-after-opening-block-list', 'blaol', '=s' );
4088 662 $add_option->( 'blank-lines-before-closing-block-list', 'blbcl', '=s' );
4089  
4090 ########################################
4091 662 $category = 9; # Other controls
4092 ########################################
4093 662 $add_option->( 'dump-nested-ternaries', 'dnt', '!' );
4094 662 $add_option->( 'warn-nested-ternaries', 'wnt', '!' );
4095 662 $add_option->( 'nested-ternary-maximum-depth', 'ntmd', '=i' );
4096 662 $add_option->( 'warn-missing-else', 'wme', '!' );
4097 662 $add_option->( 'add-missing-else', 'ame', '!' );
4098 662 $add_option->( 'add-missing-else-comment', 'amec', '=s' );
4099 662 $add_option->( 'delete-block-comments', 'dbc', '!' );
4100 662 $add_option->( 'delete-closing-side-comments', 'dcsc', '!' );
4101 662 $add_option->( 'delete-pod', 'dp', '!' );
4102 662 $add_option->( 'delete-side-comments', 'dsc', '!' );
4103  
4104 662 $add_option->( 'delete-side-comments-exception-pattern', 'dscxp', '=s' );
4105  
4106 662 $add_option->( 'tee-block-comments', 'tbc', '!' );
4107 662 $add_option->( 'tee-pod', 'tp', '!' );
4108 662 $add_option->( 'tee-side-comments', 'tsc', '!' );
4109 662 $add_option->( 'look-for-autoloader', 'lal', '!' );
4110 662 $add_option->( 'look-for-hash-bang', 'x', '!' );
4111 662 $add_option->( 'look-for-selfloader', 'lsl', '!' );
4112 662 $add_option->( 'pass-version-line', 'pvl', '!' );
4113 662 $add_option->( 'warn-variable-types', 'wvt', '=s' );
4114 662 $add_option->( 'warn-variable-exclusion-list', 'wvxl', '=s' );
4115 662 $add_option->( 'warn-label-types', 'wlt', '=s' );
4116 662 $add_option->( 'warn-label-exclusion-list', 'wlxl', '=s' );
4117 662 $add_option->( 'want-call-parens', 'wcp', '=s' );
4118 662 $add_option->( 'nowant-call-parens', 'nwcp', '=s' );
4119 662 $add_option->( 'warn-keyword-list', 'wkl', '=s' );
4120  
4121 662 $add_option->( 'warn-unique-keys', 'wuk', '!' );
4122 662 $add_option->( 'warn-unique-keys-cutoff', 'wukc', '=i' );
4123 662 $add_option->( 'warn-mismatched-args', 'wma', '!' );
4124 662 $add_option->( 'warn-mismatched-arg-types', 'wmat', '=s' );
4125 662 $add_option->( 'warn-mismatched-arg-undercount-cutoff', 'wmauc', '=i' );
4126 662 $add_option->( 'warn-mismatched-arg-overcount-cutoff', 'wmaoc', '=i' );
4127 662 $add_option->( 'warn-mismatched-arg-exclusion-list', 'wmaxl', '=s' );
4128 662 $add_option->( 'warn-mismatched-returns', 'wmr', '!' );
4129 662 $add_option->( 'warn-mismatched-return-types', 'wmrt', '=s' );
4130 662 $add_option->( 'warn-mismatched-return-exclusion-list', 'wmrxl', '=s' );
4131 662 $add_option->( 'warn-similar-keys', 'wsk', '!' );
4132 662 $add_option->( 'dump-c-style-for-loops', 'dcsfl', '!' );
4133 662 $add_option->( 'warn-c-style-for-loops', 'wcsfl', '!' );
4134 662 $add_option->( 'warn-unexpected-code-container', 'wucc', '!' );
4135  
4136 662 $add_option->( 'add-interbracket-arrows', 'aia', '!' );
4137 662 $add_option->( 'delete-interbracket-arrows', 'dia', '!' );
4138 662 $add_option->( 'warn-interbracket-arrows', 'wia', '!' );
4139 662 $add_option->( 'interbracket-arrow-style', 'ias', '=s' );
4140 662 $add_option->( 'interbracket-arrow-complexity', 'iac', '=i' );
4141  
4142 ########################################
4143 662 $category = 13; # Debugging
4144 ########################################
4145 662 $add_option->( 'DEBUG', 'D', '!' );
4146 662 $add_option->( 'dump-block-summary', 'dbs', '!' );
4147 662 $add_option->( 'dump-block-minimum-lines', 'dbl', '=i' );
4148 662 $add_option->( 'dump-block-types', 'dbt', '=s' );
4149 662 $add_option->( 'dump-cuddled-block-list', 'dcbl', '!' );
4150 662 $add_option->( 'dump-defaults', 'ddf', '!' );
4151 662 $add_option->( 'dump-hash-keys', 'dhk', '!' );
4152 662 $add_option->( 'dump-integer-option-range', 'dior', '!' );
4153 662 $add_option->( 'dump-long-names', 'dln', '!' );
4154 662 $add_option->( 'dump-mismatched-args', 'dma', '!' );
4155 662 $add_option->( 'dump-mismatched-returns', 'dmr', '!' );
4156 662 $add_option->( 'dump-mixed-call-parens', 'dmcp', '!' );
4157 662 $add_option->( 'dump-keyword-usage', 'dku', '!' );
4158 662 $add_option->( 'dump-keyword-usage-list', 'dkul', '=s' );
4159 662 $add_option->( 'dump-label-usage', 'dlu', '!' );
4160 662 $add_option->( 'dump-options', 'dop', '!' );
4161 662 $add_option->( 'dump-profile', 'dpro', '!' );
4162 662 $add_option->( 'dump-short-names', 'dsn', '!' );
4163 662 $add_option->( 'dump-similar-keys', 'dsk', '!' );
4164 662 $add_option->( 'dump-token-types', 'dtt', '!' );
4165 662 $add_option->( 'dump-unusual-variables', 'duv', '!' );
4166 662 $add_option->( 'dump-unique-keys', 'duk', '!' );
4167 662 $add_option->( 'dump-want-left-space', 'dwls', '!' );
4168 662 $add_option->( 'dump-want-right-space', 'dwrs', '!' );
4169 662 $add_option->( 'help', 'h', EMPTY_STRING );
4170 662 $add_option->( 'short-concatenation-item-length', 'scl', '=i' );
4171 662 $add_option->( 'show-options', 'opt', '!' );
4172 662 $add_option->( 'timestamp', 'ts', '!' );
4173 662 $add_option->( 'version', 'v', EMPTY_STRING );
4174 662 $add_option->( 'memoize', 'mem', '!' );
4175 662 $add_option->( 'file-size-order', 'fso', '!' );
4176 662 $add_option->( 'maximum-file-size-mb', 'maxfs', '=i' );
4177 662 $add_option->( 'maximum-level-errors', 'maxle', '=i' );
4178 662 $add_option->( 'maximum-unexpected-errors', 'maxue', '=i' );
4179 662 $add_option->( 'integer-range-check', 'irc', '=i' );
4180  
4181 662 $add_option->( 'similar-keys-maximum-difference', 'skmd', '=i' );
4182 662 $add_option->( 'similar-keys-minimum-length', 'skml', '=i' );
4183 662 $add_option->( 'similar-keys-maximum-pairs', 'skmp', '=i' );
4184  
4185 #---------------------------------------------------------------------
4186  
4187 # The Perl::Tidy::HtmlWriter will add its own options to the string
4188 662 Perl::Tidy::HtmlWriter->make_getopt_long_names( \@option_string );
4189  
4190 ########################################
4191 # Set categories 10, 11, 12
4192 ########################################
4193 # Based on their known order
4194 662 $category = 12; # HTML properties
4195 662 $add_option->( 'use-pod-formatter', 'upf', '=s' );
4196 662 foreach my $opt (@option_string) {
4197 270096 my $long_name = $opt;
4198 270096 $long_name =~ s/(!|=.*|:.*)$//;
4199 270096 ▶ if ( !defined( $option_category{$long_name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $option_category{$long_name}) 52298 217798 270096
4200 52298 ▶ if ( $long_name =~ /^html-linked/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($long_name =~ /^html-linked/) { } 662 51636 52298
elsif ($long_name =~ /^pod2html/) { } 662 50974 51636
4201 662 $category = 10; # HTML options
4202 }
4203 elsif ( $long_name =~ /^pod2html/ ) {
4204 662 $category = 11; # Pod2html
4205 }
4206 else {
4207 50974 $category = 12; # HTML properties
4208 }
4209 52298 $option_category{$long_name} = $category_name[$category];
4210 }
4211 }
4212  
4213 #----------------------------------------------------------------------
4214 # NON-INTEGER DEFAULTS: Assign default values to the above options here
4215 # except for integers, 'outfile' and 'help'
4216 # NOTES:
4217 # - Enter integer options in %integer_option_range, NOT HERE
4218 # - 'keyword-group-blanks-size=5' is ok here: the arg is a string
4219 # - These settings should approximate the perlstyle(1) suggestions.
4220 #----------------------------------------------------------------------
4221 662 my @defaults = qw(
4222 add-lone-trailing-commas
4223 add-newlines
4224 add-terminal-newline
4225 add-semicolons
4226 add-whitespace
4227 blanks-before-blocks
4228 blanks-before-comments
4229 blanks-before-opening-comments
4230  
4231 keyword-group-blanks-size=5
4232 nokeyword-group-blanks-inside
4233 nokeyword-group-blanks-delete
4234  
4235 break-at-old-logical-breakpoints
4236 break-at-old-trailing-conditionals
4237 break-at-old-trailing-loops
4238 break-at-old-ternary-breakpoints
4239 break-at-old-attribute-breakpoints
4240 break-at-old-keyword-breakpoints
4241 character-encoding=guess
4242 closing-side-comments-balanced
4243 noextended-continuation-indentation
4244 delete-old-newlines
4245 delete-repeated-commas
4246 delete-lone-trailing-commas
4247 delete-semicolons
4248 dump-block-types=sub
4249 extended-syntax
4250 encode-output-strings
4251 file-size-order
4252 function-paren-vertical-alignment
4253 hanging-side-comments
4254 indent-block-comments
4255 indent-leading-semicolon
4256 logical-padding
4257 look-for-autoloader
4258 look-for-selfloader
4259 memoize
4260 nobrace-left-and-indent
4261 nocuddled-else
4262 nodelete-old-whitespace
4263 nohtml
4264 nologfile
4265 non-indenting-braces
4266 noquiet
4267 noshow-options
4268 nostatic-side-comments
4269 notabs
4270 nowarning-output
4271 outdent-labels
4272 outdent-long-quotes
4273 outdent-long-comments
4274 pass-version-line
4275 noweld-nested-containers
4276 recombine
4277 nouse-unicode-gcstring
4278 valign-code
4279 valign-block-comments
4280 valign-comparison-operators
4281 valign-side-comments
4282 valign-signed-numbers
4283 space-for-semicolon
4284 static-block-comments
4285 timestamp
4286 trim-qw
4287 format=tidy
4288 backup-method=copy
4289 backup-file-extension=bak
4290 code-skipping
4291 format-skipping
4292 detect-format-skipping-from-start
4293 warn-unexpected-code-container
4294  
4295 pod2html
4296 html-table-of-contents
4297 html-entities
4298 );
4299  
4300 #------------------------------------------------------------
4301 # Set Ranges and defaults of all integer options (type '=i').
4302 #------------------------------------------------------------
4303 # NOTES:
4304 # 1. All integer options must be in this table, not in @defaults
4305 # 2. 'closing-token-indentation' (cti), 'vertical-tightness' (vt),
4306 # and 'vertical-tightness-closing' (vtc) are aliases which are included
4307 # to work around an old problem with msdos (see note in check_options).
4308 # 3. Use -dior to dump this table.
4309  
4310 # 'option-name' => [min, max, default]
4311 662 %integer_option_range = (
4312 'blank-lines-after-opening-block' => [ 0, undef, 0 ],
4313 'blank-lines-before-closing-block' => [ 0, undef, 0 ],
4314 'blank-lines-before-packages' => [ 0, undef, 1 ],
4315 'blank-lines-before-subs' => [ 0, undef, 1 ],
4316 'block-brace-tightness' => [ 0, 2, 0 ],
4317 'block-brace-vertical-tightness' => [ 0, 2, 0 ],
4318 'brace-follower-vertical-tightness' => [ 0, 2, 1 ],
4319 'brace-tightness' => [ 0, 2, 1 ],
4320 'brace-vertical-tightness' => [ 0, 2, 0 ],
4321 'brace-vertical-tightness-closing' => [ 0, 3, 0 ],
4322 'break-after-labels' => [ 0, 2, 0 ],
4323 'break-before-hash-brace' => [ 0, 3, 0 ],
4324 'break-before-hash-brace-and-indent' => [ 0, 2, 0 ],
4325 'break-before-paren' => [ 0, 3, 0 ],
4326 'break-before-paren-and-indent' => [ 0, 2, 0 ],
4327 'break-before-square-bracket' => [ 0, 3, 0 ],
4328 'break-before-square-bracket-and-indent' => [ 0, 2, 0 ],
4329 'closing-brace-indentation' => [ 0, 3, 0 ],
4330 'closing-paren-indentation' => [ 0, 3, 0 ],
4331 'closing-side-comment-else-flag' => [ 0, 2, 0 ],
4332 'closing-side-comment-interval' => [ 0, undef, 6 ],
4333 'closing-side-comment-maximum-text' => [ 0, undef, 20 ],
4334 'closing-square-bracket-indentation' => [ 0, 3, 0 ],
4335 'closing-token-indentation' => [ 0, 3, undef ],
4336 'comma-arrow-breakpoints' => [ 0, 5, 5 ],
4337 'continuation-indentation' => [ 0, undef, 2 ],
4338 'cuddled-break-option' => [ 0, 2, 1 ],
4339 'default-tabsize' => [ 0, undef, 8 ],
4340 'dump-block-minimum-lines' => [ 0, undef, 20 ],
4341 'entab-leading-whitespace' => [ 0, undef, 0 ],
4342 'fixed-position-side-comment' => [ 0, undef, undef ],
4343 'heredoc-excess-length-option' => [ 0, 3, 0 ],
4344 'indent-columns' => [ 0, undef, 4 ],
4345 'integer-range-check' => [ 1, 3, 2 ],
4346 'interbracket-arrow-complexity' => [ 0, 2, 1 ],
4347 'iterations' => [ 0, undef, 1 ],
4348 'keep-old-blank-lines' => [ 0, 2, 1 ],
4349 'keyword-group-blanks-after' => [ 0, 2, 1 ],
4350 'keyword-group-blanks-before' => [ 0, 2, 1 ],
4351 'keyword-group-blanks-repeat-count' => [ 0, undef, 0 ],
4352 'keyword-paren-inner-tightness' => [ 0, 2, 1 ],
4353 'long-block-line-count' => [ 0, undef, 8 ],
4354 'maximum-consecutive-blank-lines' => [ 0, undef, 1 ],
4355 'maximum-file-size-mb' => [ 0, undef, 10 ],
4356 'maximum-level-errors' => [ 0, undef, 1 ],
4357 'maximum-line-length' => [ 0, undef, 80 ],
4358 'maximum-unexpected-errors' => [ 0, undef, 0 ],
4359 'minimum-space-to-comment' => [ 0, undef, 4 ],
4360 'one-line-block-nesting' => [ 0, 1, 0 ],
4361 'one-line-block-semicolons' => [ 0, 2, 1 ],
4362 'paren-tightness' => [ 0, 2, 1 ],
4363 'paren-vertical-tightness' => [ 0, 2, 0 ],
4364 'paren-vertical-tightness-closing' => [ 0, 3, 0 ],
4365 'short-concatenation-item-length' => [ 0, undef, 8 ],
4366 'similar-keys-maximum-difference' => [ 1, undef, 1 ],
4367 'similar-keys-maximum-pairs' => [ 1, undef, 25 ],
4368 'similar-keys-minimum-length' => [ 1, undef, 4 ],
4369 'space-backslash-quote' => [ 0, 2, 1 ],
4370 'space-prototype-paren' => [ 0, 2, 1 ],
4371 'space-signature-paren' => [ 0, 2, 1 ],
4372 'square-bracket-tightness' => [ 0, 2, 1 ],
4373 'square-bracket-vertical-tightness' => [ 0, 2, 0 ],
4374 'square-bracket-vertical-tightness-closing' => [ 0, 3, 0 ],
4375 'starting-indentation-level' => [ 0, undef, undef ],
4376 'timeout-in-seconds' => [ 0, undef, 5 ],
4377 'valign-signed-numbers-limit' => [ 0, undef, 20 ],
4378 'vertical-tightness' => [ 0, 2, undef ],
4379 'vertical-tightness-closing' => [ 0, 3, undef ],
4380 'warn-mismatched-arg-overcount-cutoff' => [ 0, undef, 1 ],
4381 'warn-mismatched-arg-undercount-cutoff' => [ 0, undef, 4 ],
4382 'nested-ternary-maximum-depth' => [ 0, undef, 0 ],
4383 'warn-unique-keys-cutoff' => [ 1, undef, 1 ],
4384 'whitespace-cycle' => [ 0, undef, 0 ],
4385 );
4386  
4387 662 foreach my $key ( keys %integer_option_range ) {
4388 48326 my $val = $integer_option_range{$key}->[2];
4389 48326 ▶ if ( defined($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 45016 3310 48326
4390 45016 push @defaults, "$key=$val";
4391 }
4392 }
4393  
4394 #------------------------------------
4395 # Locate strings options of type '=s'
4396 #------------------------------------
4397 662 my %is_string_option;
4398 662 foreach my $opt (@option_string) {
4399 270096 ▶ next if ( substr( $opt, -2, 2 ) ne '=s' );
Branch: 1 branch
BranchTrueFalseTotal
if substr($opt, -2, 2) ne "=s" 197276 72820 270096
4400 72820 my $key = substr( $opt, 0, -2 );
4401 72820 $is_string_option{$key} = 1;
4402 }
4403  
4404 # Verify that only integers of type =i are in the above list during
4405 # development. This will guard against spelling errors.
4406 662 if (DEVEL_MODE) {
4407 my %option_flag;
4408 my $msg = EMPTY_STRING;
4409 foreach my $opt (@option_string) {
4410 my $key = $opt;
4411 my $flag = EMPTY_STRING;
4412 if ( $key =~ /(.*)(!|=.*|:.*)$/ ) {
4413 $key = $1;
4414 $flag = $2;
4415 }
4416 $option_flag{$key} = $flag;
4417 }
4418  
4419 # Be sure all keys of %integer_option_range have option type '=i'
4420 foreach my $opt ( keys %integer_option_range ) {
4421 my $flag = $option_flag{$opt};
4422 if ( !defined($flag) ) { $flag = EMPTY_STRING }
4423 if ( $flag ne '=i' ) {
4424  
4425 # If this fault occurs, one of the items in the previous hash
4426 # is not type =i, possibly due to incorrect spelling.
4427 $msg .=
4428 "Option '$opt' has an entry in '%integer_option_range' but is not an integer\n";
4429 }
4430 }
4431  
4432 # Be sure all '=i' options are in %integer_option_range. This is not
4433 # strictly necessary but helps insure that nothing was missed.
4434 foreach my $opt ( keys %option_flag ) {
4435 my $flag = $option_flag{$opt};
4436 next if ( $flag ne '=i' );
4437 if ( !defined( $integer_option_range{$opt} ) ) {
4438 $msg .=
4439 "Integer option '$opt' is needs an entry in '%integer_option_range'\n";
4440 }
4441 }
4442  
4443 # look for integer options without default values
4444 foreach my $opt ( keys %integer_option_range ) {
4445 if ( @{ $integer_option_range{$opt} } < 3 ) {
4446 $msg .= "Integer option '$opt' does not have a default value\n";
4447 }
4448 }
4449  
4450 if ($msg) {
4451 Fault($msg);
4452 }
4453 }
4454  
4455 #-----------------------------------------------------------------------
4456 # Define abbreviations which will be expanded into the above primitives.
4457 # These may be defined recursively.
4458 #-----------------------------------------------------------------------
4459 %expansion = (
4460 662 %expansion,
4461  
4462 'freeze-newlines' => [qw(noadd-newlines nodelete-old-newlines)],
4463 'fnl' => [qw(freeze-newlines)],
4464 'freeze-whitespace' => [qw(noadd-whitespace nodelete-old-whitespace)],
4465 'fws' => [qw(freeze-whitespace)],
4466 'freeze-blank-lines' =>
4467 [qw(maximum-consecutive-blank-lines=0 keep-old-blank-lines=2)],
4468 'fbl' => [qw(freeze-blank-lines)],
4469 'indent-only' => [qw(freeze-newlines freeze-whitespace)],
4470 'outdent-long-lines' => [qw(outdent-long-quotes outdent-long-comments)],
4471 'nooutdent-long-lines' =>
4472 [qw(nooutdent-long-quotes nooutdent-long-comments)],
4473 'oll' => [qw(outdent-long-lines)],
4474 'noll' => [qw(nooutdent-long-lines)],
4475 'io' => [qw(indent-only)],
4476 'delete-all-comments' =>
4477 [qw(delete-block-comments delete-side-comments delete-pod)],
4478 'nodelete-all-comments' =>
4479 [qw(nodelete-block-comments nodelete-side-comments nodelete-pod)],
4480 'dac' => [qw(delete-all-comments)],
4481 'ndac' => [qw(nodelete-all-comments)],
4482 'gnu' => [qw(gnu-style)],
4483 'pbp' => [qw(perl-best-practices)],
4484 'tee-all-comments' =>
4485 [qw(tee-block-comments tee-side-comments tee-pod)],
4486 'notee-all-comments' =>
4487 [qw(notee-block-comments notee-side-comments notee-pod)],
4488 'tac' => [qw(tee-all-comments)],
4489 'ntac' => [qw(notee-all-comments)],
4490 'html' => [qw(format=html)],
4491 'nhtml' => [qw(format=tidy)],
4492 'tidy' => [qw(format=tidy)],
4493  
4494 'brace-left' => [qw(opening-brace-on-new-line)],
4495  
4496 # -cb is now a synonym for -ce
4497 'cb' => [qw(cuddled-else)],
4498 'cuddled-blocks' => [qw(cuddled-else)],
4499  
4500 'utf8' => [qw(character-encoding=utf8)],
4501 'UTF8' => [qw(character-encoding=utf8)],
4502 'guess' => [qw(character-encoding=guess)],
4503  
4504 'swallow-optional-blank-lines' => [qw(kbl=0)],
4505 'noswallow-optional-blank-lines' => [qw(kbl=1)],
4506 'sob' => [qw(kbl=0)],
4507 'nsob' => [qw(kbl=1)],
4508  
4509 'break-after-comma-arrows' => [qw(cab=0)],
4510 'nobreak-after-comma-arrows' => [qw(cab=1)],
4511 'baa' => [qw(cab=0)],
4512 'nbaa' => [qw(cab=1)],
4513  
4514 'blanks-before-subs' => [qw(blbs=1 blbp=1)],
4515 'bbs' => [qw(blbs=1 blbp=1)],
4516 'noblanks-before-subs' => [qw(blbs=0 blbp=0)],
4517 'nbbs' => [qw(blbs=0 blbp=0)],
4518  
4519 'keyword-group-blanks' => [qw(kgbb=2 kgbi kgba=2)],
4520 'kgb' => [qw(kgbb=2 kgbi kgba=2)],
4521 'nokeyword-group-blanks' => [qw(kgbb=1 nkgbi kgba=1)],
4522 'nkgb' => [qw(kgbb=1 nkgbi kgba=1)],
4523  
4524 # allow spelling error 'trinary' vs 'ternary'
4525 'break-at-old-trinary-breakpoints' => [qw(bot)],
4526  
4527 'cti=0' => [qw(cpi=0 cbi=0 csbi=0)],
4528 'cti=1' => [qw(cpi=1 cbi=1 csbi=1)],
4529 'cti=2' => [qw(cpi=2 cbi=2 csbi=2)],
4530 'icp' => [qw(cpi=2 cbi=2 csbi=2)],
4531 'nicp' => [qw(cpi=0 cbi=0 csbi=0)],
4532  
4533 'closing-token-indentation=0' => [qw(cpi=0 cbi=0 csbi=0)],
4534 'closing-token-indentation=1' => [qw(cpi=1 cbi=1 csbi=1)],
4535 'closing-token-indentation=2' => [qw(cpi=2 cbi=2 csbi=2)],
4536 'indent-closing-paren' => [qw(cpi=2 cbi=2 csbi=2)],
4537 'noindent-closing-paren' => [qw(cpi=0 cbi=0 csbi=0)],
4538  
4539 'vt=0' => [qw(pvt=0 bvt=0 sbvt=0)],
4540 'vt=1' => [qw(pvt=1 bvt=1 sbvt=1)],
4541 'vt=2' => [qw(pvt=2 bvt=2 sbvt=2)],
4542  
4543 'vertical-tightness=0' => [qw(pvt=0 bvt=0 sbvt=0)],
4544 'vertical-tightness=1' => [qw(pvt=1 bvt=1 sbvt=1)],
4545 'vertical-tightness=2' => [qw(pvt=2 bvt=2 sbvt=2)],
4546  
4547 'vtc=0' => [qw(pvtc=0 bvtc=0 sbvtc=0)],
4548 'vtc=1' => [qw(pvtc=1 bvtc=1 sbvtc=1)],
4549 'vtc=2' => [qw(pvtc=2 bvtc=2 sbvtc=2)],
4550 'vtc=3' => [qw(pvtc=3 bvtc=3 sbvtc=3)],
4551  
4552 'vertical-tightness-closing=0' => [qw(pvtc=0 bvtc=0 sbvtc=0)],
4553 'vertical-tightness-closing=1' => [qw(pvtc=1 bvtc=1 sbvtc=1)],
4554 'vertical-tightness-closing=2' => [qw(pvtc=2 bvtc=2 sbvtc=2)],
4555 'vertical-tightness-closing=3' => [qw(pvtc=3 bvtc=3 sbvtc=3)],
4556  
4557 'otr' => [qw(opr ohbr osbr)],
4558 'opening-token-right' => [qw(opr ohbr osbr)],
4559 'notr' => [qw(nopr nohbr nosbr)],
4560 'noopening-token-right' => [qw(nopr nohbr nosbr)],
4561  
4562 'sot' => [qw(sop sohb sosb)],
4563 'nsot' => [qw(nsop nsohb nsosb)],
4564 'stack-opening-tokens' => [qw(sop sohb sosb)],
4565 'nostack-opening-tokens' => [qw(nsop nsohb nsosb)],
4566  
4567 'sct' => [qw(scp schb scsb)],
4568 'stack-closing-tokens' => [qw(scp schb scsb)],
4569 'nsct' => [qw(nscp nschb nscsb)],
4570 'nostack-closing-tokens' => [qw(nscp nschb nscsb)],
4571  
4572 'sac' => [qw(sot sct)],
4573 'nsac' => [qw(nsot nsct)],
4574 'stack-all-containers' => [qw(sot sct)],
4575 'nostack-all-containers' => [qw(nsot nsct)],
4576  
4577 'act=0' => [qw(pt=0 sbt=0 bt=0 bbt=0)],
4578 'act=1' => [qw(pt=1 sbt=1 bt=1 bbt=1)],
4579 'act=2' => [qw(pt=2 sbt=2 bt=2 bbt=2)],
4580 'all-containers-tightness=0' => [qw(pt=0 sbt=0 bt=0 bbt=0)],
4581 'all-containers-tightness=1' => [qw(pt=1 sbt=1 bt=1 bbt=1)],
4582 'all-containers-tightness=2' => [qw(pt=2 sbt=2 bt=2 bbt=2)],
4583  
4584 'stack-opening-block-brace' => [qw(bbvt=2 bbvtl=*)],
4585 'sobb' => [qw(bbvt=2 bbvtl=*)],
4586 'nostack-opening-block-brace' => [qw(bbvt=0)],
4587 'nsobb' => [qw(bbvt=0)],
4588  
4589 'converge' => [qw(it=4)],
4590 'noconverge' => [qw(it=1)],
4591 'conv' => [qw(it=4)],
4592 'nconv' => [qw(it=1)],
4593  
4594 'valign' => [qw(vc vsc vbc)],
4595 'novalign' => [qw(nvc nvsc nvbc)],
4596  
4597 # NOTE: This is a possible future shortcut. But it will remain
4598 # deactivated until the -lpxl flag is no longer experimental.
4599 # 'line-up-function-parentheses' => [ qw(lp), q#lpxl=[ { F(2# ],
4600 # 'lfp' => [qw(line-up-function-parentheses)],
4601  
4602 # 'mangle' originally deleted pod and comments, but to keep it
4603 # reversible, it no longer does. But if you really want to
4604 # delete them, just use:
4605 # -mangle -dac
4606  
4607 # An interesting use for 'mangle' is to do this:
4608 # perltidy -mangle myfile.pl -st | perltidy -o myfile.pl.new
4609 # which will form as many one-line blocks as possible
4610  
4611 'mangle' => [
4612 qw(
4613 keep-old-blank-lines=0
4614 delete-old-newlines
4615 delete-old-whitespace
4616 delete-semicolons
4617 indent-columns=0
4618 maximum-consecutive-blank-lines=0
4619 maximum-line-length=100000
4620 noadd-newlines
4621 noadd-semicolons
4622 noadd-whitespace
4623 noblanks-before-blocks
4624 blank-lines-before-subs=0
4625 blank-lines-before-packages=0
4626 notabs
4627 )
4628 ],
4629  
4630 # 'extrude' originally deleted pod and comments, but to keep it
4631 # reversible, it no longer does. But if you really want to
4632 # delete them, just use
4633 # extrude -dac
4634 #
4635 # An interesting use for 'extrude' is to do this:
4636 # perltidy -extrude myfile.pl -st | perltidy -o myfile.pl.new
4637 # which will break up all one-line blocks.
4638 'extrude' => [
4639 qw(
4640 ci=0
4641 delete-old-newlines
4642 delete-old-whitespace
4643 delete-semicolons
4644 indent-columns=0
4645 maximum-consecutive-blank-lines=0
4646 maximum-line-length=1
4647 noadd-semicolons
4648 noadd-whitespace
4649 noblanks-before-blocks
4650 blank-lines-before-subs=0
4651 blank-lines-before-packages=0
4652 notabs
4653 norecombine
4654 )
4655 ],
4656  
4657 # this style tries to follow the GNU Coding Standards (which do
4658 # not really apply to perl but which are followed by some perl
4659 # programmers).
4660 'gnu-style' => [
4661 qw(
4662 lp bl noll pt=2 bt=2 sbt=2 cpi=1 csbi=1 cbi=1
4663 )
4664 ],
4665  
4666 # Style suggested in Damian Conway's Perl Best Practices
4667 'perl-best-practices' => [
4668 qw(l=78 i=4 ci=4 st se vt=2 cti=0 pt=1 bt=1 sbt=1 bbt=1 nsfs nolq),
4669 q(wbb=% + - * / x != == >= <= =~ !~ < > | & = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x=)
4670 ],
4671  
4672 # Additional styles can be added here
4673  
4674 #----------------------
4675 # deprecated parameters
4676 #----------------------
4677  
4678 # These flags are accepted for backwards compatibility but ignored.
4679 'check-syntax' => [],
4680 'nocheck-syntax' => [],
4681 'no-check-syntax' => [],
4682 'syn' => [],
4683 'nsyn' => [],
4684 'perl-syntax-check-flags' => [],
4685 'pscf' => [],
4686 'fuzzy-line-length' => [],
4687 'nofuzzy-line-length' => [],
4688 'no-fuzzy-line-length' => [],
4689 'fll' => [],
4690 'nfll' => [],
4691 );
4692  
4693 662 Perl::Tidy::HtmlWriter->make_abbreviated_names( \%expansion );
4694  
4695 # Uncomment next line to dump all expansions for debugging:
4696 # dump_short_names(\%expansion);
4697 662 return ( \@option_string, \@defaults, \%expansion, \%option_category,
4698 \%integer_option_range, \%is_string_option );
4699  
4700 } ## end sub generate_options
4701  
4702 { #<<< closure process_command_line
4703  
4704 # Memoize process_command_line. Given same @ARGV passed in, return same
4705 # values and same @ARGV back.
4706 # This patch was supplied by Jonathan Swartz Nov 2012 and significantly speeds
4707 # up masontidy (https://metacpan.org/module/masontidy)
4708  
4709 45 my %process_command_line_cache;
4710  
4711 664 ▶sub process_command_line {
Subroutine process_command_line: called CC=5 SCAR=16.1
Pod undocumented
4712  
4713 # Use Getopt::Long to scan the command line for input parameters.
4714 # This is the outer sub which handles memoization
4715  
4716 664 my @q = @_;
4717 my (
4718 664 $perltidyrc_stream, $is_Windows_uu, $Windows_type_uu,
4719 $rpending_complaint_uu, $dump_options_type
4720 ) = @q;
4721  
4722 664 ▶ my $use_cache = !defined($perltidyrc_stream) && !$dump_options_type;
Condition66%2/3
defined $perltidyrc_stream || $dump_options_type
l!l&&r!l&&!r
65707
MC/DC50%1/2
defined $perltidyrc_stream || $dump_options_type
defined $perltidyrc_stream $dump_options_type
Truth table66%2/3
A || B
A= defined $perltidyrc_streamB= $dump_options_type
ABresult
1X1
011
000
4723 664 ▶ if ($use_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_cache) { } 7 657 664
4724 7 my $cache_key = join( chr(28), @ARGV );
4725 7 ▶ if ( my $result = $process_command_line_cache{$cache_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $process_command_line_cache{$cache_key}) { } 2 5 7
4726 2 my ( $argv, @retvals ) = @{$result};
4727 2 @ARGV = @{$argv};
4728 2 return @retvals;
4729 }
4730 else {
4731 5 my @retvals = _process_command_line(@q);
4732 $process_command_line_cache{$cache_key} = [ \@ARGV, @retvals ]
4733 5 ▶ if ( $retvals[0]->{'memoize'} );
Branch: 1 branch
BranchTrueFalseTotal
if $retvals[0]{"memoize"} 5 0 5
4734 5 return @retvals;
4735 }
4736 }
4737 else {
4738 657 return _process_command_line(@q);
4739 }
4740 } ## end sub process_command_line
4741 } ## end closure process_command_line
4742  
4743 # (note the underscore here)
4744 45 ▶sub _process_command_line {
Subroutine _process_command_line: called CC=52 SCAR=58.2
4745  
4746 my (
4747 662 $perltidyrc_stream, $is_Windows, $Windows_type,
4748 $rpending_complaint, $dump_options_type
4749 ) = @_;
4750  
4751 # Use Getopt::Long to scan the command line for input parameters.
4752 # This is the inner sub which actually processes the command line
4753  
4754 45 ▶ use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
4755  
4756 # Save any current Getopt::Long configuration
4757 # and set to Getopt::Long defaults. Use eval to avoid
4758 # breaking old versions of Perl without these routines.
4759 # Previous configuration is reset at the exit of this routine.
4760 662 my $glc;
4761 662 ▶ if ( eval { $glc = Getopt::Long::Configure(); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $glc = &Getopt::Long::Configure(); 1 } }) { } 662 0 662
4762 662 my $ok = eval { Getopt::Long::ConfigDefaults(); 1 };
4763 662 ▶ if ( !$ok && DEVEL_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and 0) 0 662 662
Condition50%1/2
not $ok and 0
!ll
6620
MC/DC0%0/1
not $ok and 0
not $ok
Truth table50%1/2
not(A)
A= not $ok
Aresult
00
11
4764 0 Fault("Failed call to Getopt::Long::ConfigDefaults: $EVAL_ERROR\n");
4765 }
4766 }
4767 0 else { $glc = undef }
4768  
4769 662 my ( $roption_string, $rdefaults, $rexpansion,
4770 $roption_category, $rinteger_option_range, $ris_string_option )
4771 = generate_options();
4772  
4773 #--------------------------------------------------------------
4774 # set the defaults by passing the above list through GetOptions
4775 #--------------------------------------------------------------
4776 662 my %Opts = ();
4777 {
4778 662 local @ARGV = ();
4779  
4780 # do not load the defaults if we are just dumping perltidyrc
4781 662 ▶ if ( $dump_options_type ne 'perltidyrc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump_options_type ne "perltidyrc") 662 0 662
4782 662 for my $i ( @{$rdefaults} ) { push @ARGV, "--" . $i }
4783 }
4784 662 ▶ if ( !GetOptions( \%Opts, @{$roption_string} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions(\%Opts, @{$roption_string;})) 0 662 662
4785 0 Die(
4786 "Programming Bug reported by 'GetOptions': error in setting default options"
4787 );
4788 }
4789 }
4790  
4791 662 my @raw_options = ();
4792 662 my $saw_ignore_profile = 0;
4793 662 my $saw_dump_profile = 0;
4794 662 my $config_file;
4795  
4796 #--------------------------------------------------------------
4797 # Take a first look at the command-line parameters. Do as many
4798 # immediate dumps as possible, which can avoid confusion if the
4799 # perltidyrc file has an error.
4800 #--------------------------------------------------------------
4801 662 foreach my $i (@ARGV) {
4802  
4803 21 $i =~ s/^--/-/;
4804 21 ▶ if ( $i =~ /^-(npro|noprofile|nopro|no-profile)$/ ) {
Branch: 11 branches
BranchTrueFalseTotal
if ($i =~ /^-(npro|noprofile|nopro|no-profile)$/) { } 6 15 21
elsif ($i =~ /^-(dump-profile|dpro)$/) { } 0 15 15
elsif ($i =~ /^-(pro|profile)=(.+)/) { } 0 15 15
elsif ($i =~ /^-(pro|profile)=?$/) { } 0 15 15
elsif ($i =~ /^-(?: help | [ h \? ] )$/xi) { } 0 15 15
elsif ($i =~ /^-(version|v)$/) { } 0 15 15
elsif ($i =~ /^-(dump-defaults|ddf)$/) { } 0 15 15
elsif ($i =~ /^-(dump-integer-option-range|dior)$/) { } 0 15 15
elsif ($i =~ /^-(dump-long-names|dln)$/) { } 0 15 15
elsif ($i =~ /^-(dump-short-names|dsn)$/) { } 0 15 15
elsif ($i =~ /^-(dump-token-types|dtt)$/) { } 0 15 15
4805 6 $saw_ignore_profile = 1;
4806 }
4807  
4808 # note: this must come before -pro and -profile, below:
4809 elsif ( $i =~ /^-(dump-profile|dpro)$/ ) {
4810 0 $saw_dump_profile = 1;
4811 }
4812 elsif ( $i =~ /^-(pro|profile)=(.+)/ ) {
4813 0 ▶ if ( defined($config_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file) 0 0 0
4814 0 Warn(
4815 "Only one -pro=filename allowed, using '$2' instead of '$config_file'\n"
4816 );
4817 }
4818 0 $config_file = $2;
4819  
4820 # resolve <dir>/.../<file>, meaning look upwards from directory
4821 0 ▶ if ( defined($config_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file) 0 0 0
4822 0 ▶ if ( my ( $start_dir, $search_file ) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $config_file =~ m[^(.*)\.\.\./(.*)$]) 0 0 0
4823 ( $config_file =~ m{^(.*)\.\.\./(.*)$} ) )
4824 {
4825 0 ▶ $start_dir = '.' if ( !$start_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless $start_dir 0 0 0
4826 0 $start_dir = Cwd::realpath($start_dir);
4827 0 my $found_file =
4828 find_file_upwards( $start_dir, $search_file );
4829 0 ▶ if ( defined($found_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found_file) 0 0 0
4830 0 $config_file = $found_file;
4831 }
4832 }
4833 }
4834 0 ▶ if ( !-e $config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $config_file) 0 0 0
4835 0 Die(
4836 "cannot find file given with -pro=$config_file: $OS_ERROR\n"
4837 );
4838 }
4839 }
4840 elsif ( $i =~ /^-(pro|profile)=?$/ ) {
4841 0 Die("usage: -pro=filename or --profile=filename, no spaces\n");
4842 }
4843 elsif ( $i =~ /^-(?: help | [ h \? ] )$/xi ) {
4844 0 usage();
4845 0 Exit(0);
4846 }
4847 elsif ( $i =~ /^-(version|v)$/ ) {
4848 0 show_version();
4849 0 Exit(0);
4850 }
4851 elsif ( $i =~ /^-(dump-defaults|ddf)$/ ) {
4852 0 dump_defaults( @{$rdefaults} );
4853 0 Exit(0);
4854 }
4855 elsif ( $i =~ /^-(dump-integer-option-range|dior)$/ ) {
4856 0 dump_integer_option_range($rinteger_option_range);
4857 0 Exit(0);
4858 }
4859 elsif ( $i =~ /^-(dump-long-names|dln)$/ ) {
4860 0 dump_long_names( @{$roption_string} );
4861 0 Exit(0);
4862 }
4863 elsif ( $i =~ /^-(dump-short-names|dsn)$/ ) {
4864 0 dump_short_names($rexpansion);
4865 0 Exit(0);
4866 }
4867 elsif ( $i =~ /^-(dump-token-types|dtt)$/ ) {
4868 0 Perl::Tidy::Tokenizer->dump_token_types(*STDOUT);
4869 0 Exit(0);
4870 }
4871 else {
4872 ## no more special cases
4873 }
4874 }
4875  
4876 # The above commands processed before disambiguation and then Exited. So
4877 # we need to check below to see if the user entered something like
4878 # '-dump-t' or '-he'. This will slip past here and not get processed.
4879 662 my %early_exit_commands = (
4880 'help' => 'h',
4881 'version' => 'v',
4882 'dump-defaults' => 'ddf',
4883 'dump-integer-option-range' => 'dior',
4884 'dump-long-names' => 'dln',
4885 'dump-short-names' => 'dsn',
4886 'dump-token-types' => 'dtt',
4887 );
4888  
4889 662 ▶ if ( $saw_dump_profile && $saw_ignore_profile ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_dump_profile and $saw_ignore_profile) 0 662 662
Condition33%1/3
$saw_dump_profile and $saw_ignore_profile
!ll&&!rl&&r
66200
MC/DC0%0/2
$saw_dump_profile and $saw_ignore_profile
$saw_dump_profile $saw_ignore_profile
Truth table33%1/3
A and B
A= $saw_dump_profileB= $saw_ignore_profile
ABresult
0X0
100
111
4890 0 Warn("No profile to dump because of -npro setting\n");
4891 0 Exit(1);
4892 }
4893  
4894 #----------------------------------------
4895 # read any .perltidyrc configuration file
4896 #----------------------------------------
4897 662 ▶ if ( !$saw_ignore_profile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_ignore_profile) 656 6 662
4898  
4899 # resolve possible conflict between $perltidyrc_stream passed
4900 # as call parameter to perltidy and -pro=filename on command
4901 # line.
4902 656 ▶ if ($perltidyrc_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perltidyrc_stream) 656 0 656
4903 656 ▶ if ( defined($config_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file) { } 0 656 656
4904 0 Warn(<<EOM);
4905 Conflict: a perltidyrc configuration file was specified both as this
4906 perltidy call parameter: $perltidyrc_stream
4907 and with this -profile=$config_file.
4908 Using -profile=$config_file.
4909 EOM
4910 }
4911 else {
4912 656 $config_file = $perltidyrc_stream;
4913 }
4914 }
4915  
4916 # look for a config file if we don't have one yet
4917 656 my $rconfig_file_chatter;
4918 656 ${$rconfig_file_chatter} = EMPTY_STRING;
4919 656 ▶ if ( !defined($config_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config_file) 0 656 656
4920 0 $config_file =
4921 find_config_file( $is_Windows, $Windows_type,
4922 $rconfig_file_chatter, $rpending_complaint );
4923 }
4924  
4925 # open any config file
4926 656 my $rconfig_string;
4927 656 ▶ if ( defined($config_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file) 656 0 656
4928 656 $rconfig_string = stream_slurp($config_file);
4929 656 ▶ if ( !defined($rconfig_string) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rconfig_string) 0 656 656
4930 0 Die(
4931 "exiting because profile '$config_file' could not be opened\n"
4932 );
4933 }
4934 filter_unknown_options(
4935 656 $rconfig_string, $roption_category,
4936 $rexpansion, $rconfig_file_chatter
4937 );
4938 }
4939 656 ▶ if ($saw_dump_profile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($saw_dump_profile) 0 656 656
4940 0 dump_config_file( $rconfig_string, $config_file,
4941 $rconfig_file_chatter );
4942 0 Exit(0);
4943 }
4944  
4945 656 ▶ if ( defined($rconfig_string) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rconfig_string) 656 0 656
4946  
4947 656 my ( $rconfig_list, $death_message ) =
4948 read_config_file( $rconfig_string, $config_file, $rexpansion );
4949 656 ▶ Die($death_message) if ($death_message);
Branch: 1 branch
BranchTrueFalseTotal
if $death_message 0 656 656
4950  
4951 # process any .perltidyrc parameters right now so we can
4952 # localize errors
4953 656 ▶ if ( @{$rconfig_list} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$rconfig_list;}) 278 378 656
4954 278 local @ARGV = @{$rconfig_list};
4955  
4956 278 expand_command_abbreviations( $rexpansion, \@raw_options,
4957 $config_file );
4958  
4959 278 check_for_missing_string_options( $ris_string_option,
4960 $config_file );
4961  
4962 278 ▶ if ( !GetOptions( \%Opts, @{$roption_string} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions(\%Opts, @{$roption_string;})) 0 278 278
4963 0 Die(
4964 "Error in this config file: $config_file \nUse -npro to ignore this file, -dpro to dump it, -h for help'\n"
4965 );
4966 }
4967  
4968 # Anything left in this local @ARGV is an error and must be
4969 # invalid bare words from the configuration file. We cannot
4970 # check this earlier because bare words may have been valid
4971 # values for parameters. We had to wait for GetOptions to have
4972 # a look at @ARGV.
4973 278 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 0 278 278
4974 0 my $count = @ARGV;
4975 0 my $str = EMPTY_STRING;
4976 0 foreach my $param (@ARGV) {
4977 0 ▶ if ( length($str) < 70 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str < 70) { } 0 0 0
4978 0 ▶ if ($str) { $str .= ', ' }
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 0 0 0
4979 0 $str .= "'$param'";
4980 }
4981 else {
4982 0 $str .= ", ...";
4983 0 last;
4984 }
4985 }
4986 0 Die(<<EOM);
4987 There are $count unrecognized values in the configuration file '$config_file':
4988 $str
4989 Use leading dashes for parameters. Use -npro to ignore this file.
4990 EOM
4991 }
4992  
4993 # Undo any options which cause premature exit. They are not
4994 # appropriate for a config file, and it could be hard to
4995 # diagnose the cause of the premature exit.
4996  
4997 # These are options include dump switches of the form
4998 # '--dump-xxx-xxx!'.
4999 my @dump_commands =
5000 278 grep { /^(dump-.*)!$/ } @{$roption_string};
5001 278 foreach (@dump_commands) { s/!$// }
5002  
5003 # Here is a current list of these @dump_commands:
5004 # dump-block-summary
5005 # dump-cuddled-block-list
5006 # dump-defaults
5007 # dump-integer-option-range
5008 # dump-long-names
5009 # dump-mismatched-args
5010 # dump-mismatched-returns
5011 # dump-mixed-call-parens
5012 # dump-options
5013 # dump-profile
5014 # dump-short-names
5015 # dump-token-types
5016 # dump-unusual-variables
5017 # dump-want-left-space
5018 # dump-want-right-space
5019 # dump-keyword-usage
5020 # dump-label-usage
5021  
5022 # The following dump configuration parameters which
5023 # take =i or =s would still be allowed:
5024 # dump-block-minimum-lines, 'dbl', '=i' );
5025 # dump-block-types, 'dbt', '=s' );
5026 # dump-keyword-usage-list, 'dkul', '=s' );
5027  
5028 278 foreach my $cmd (
5029 @dump_commands,
5030 qw{
5031 help
5032 stylesheet
5033 version
5034 }
5035 )
5036 {
5037 6950 ▶ if ( defined( $Opts{$cmd} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Opts{$cmd}) 0 6950 6950
5038 0 delete $Opts{$cmd};
5039 0 Warn("ignoring --$cmd in config file: $config_file\n");
5040 }
5041 }
5042 }
5043 }
5044 }
5045  
5046 # Save selected options seen in the profile for use in error checking
5047 662 my %Opts_in_profile = ();
5048 662 foreach my $opt (
5049 qw(
5050 backup-and-modify-in-place
5051 standard-output
5052 )
5053 )
5054 {
5055 1324 $Opts_in_profile{$opt} = $Opts{$opt};
5056 }
5057  
5058 #----------------------------------------
5059 # now process the command line parameters
5060 #----------------------------------------
5061 662 expand_command_abbreviations( $rexpansion, \@raw_options, $config_file );
5062  
5063 662 check_for_missing_string_options($ris_string_option);
5064  
5065 0 ▶ local $SIG{'__WARN__'} = sub { Warn( $_[0] ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
5066 662 ▶ if ( !GetOptions( \%Opts, @{$roption_string} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions(\%Opts, @{$roption_string;})) 0 662 662
5067 0 Die("Error on command line; for help try 'perltidy -h'\n");
5068 }
5069  
5070 # Catch ambiguous entries which should have exited above (c333)
5071 662 foreach my $long_name ( keys %early_exit_commands ) {
5072 4634 ▶ if ( $Opts{$long_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Opts{$long_name}) 0 4634 4634
5073 0 my $short_name = $early_exit_commands{$long_name};
5074 0 Die(<<EOM);
5075 Ambiguous entry; please enter '--$long_name' or '-$short_name'
5076 EOM
5077 }
5078 }
5079  
5080 # reset Getopt::Long configuration back to its previous value
5081 662 ▶ if ( defined($glc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $glc) 662 0 662
5082 662 my $ok = eval { Getopt::Long::Configure($glc); 1 };
5083 662 ▶ if ( !$ok && DEVEL_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and 0) 0 662 662
Condition50%1/2
not $ok and 0
!ll
6620
MC/DC0%0/1
not $ok and 0
not $ok
Truth table50%1/2
not(A)
A= not $ok
Aresult
00
11
5084 0 Fault("Could not reset Getopt::Long configuration: $EVAL_ERROR\n");
5085 }
5086 }
5087  
5088 return (
5089 662 \%Opts, $config_file, \@raw_options,
5090 $roption_string, $rexpansion, $roption_category,
5091 $rinteger_option_range, $ris_string_option, \%Opts_in_profile
5092 );
5093 } ## end sub _process_command_line
5094  
5095 664 ▶sub make_grep_alias_string {
Subroutine make_grep_alias_string: called CC=9 SCAR=23.8
Pod undocumented
5096  
5097 664 my ($rOpts) = @_;
5098  
5099 # pre-process the --grep-alias-list parameter
5100  
5101 # Defaults: list operators in List::Util
5102 # Possible future additions: pairfirst pairgrep pairmap
5103 664 my $default_string =
5104 join( SPACE, qw( all any first none notall reduce reductions ) );
5105  
5106 # make a hash of any excluded words
5107 664 my %is_excluded_word;
5108 664 my $opt_name = 'grep-alias-exclusion-list';
5109 664 my $exclude_string = $rOpts->{$opt_name};
5110 664 ▶ if ($exclude_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude_string) 0 664 664
5111 0 $exclude_string =~ s/,/ /g; # allow commas
5112 0 $exclude_string =~ s/^\s+//;
5113 0 $exclude_string =~ s/\s+$//;
5114 0 my @q = split /\s+/, $exclude_string;
5115 0 $is_excluded_word{$_} = 1 for @q;
5116 0 ▶ if ( !$is_excluded_word{'*'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_excluded_word{"*"}) 0 0 0
5117 0 check_for_valid_words(
5118 {
5119 rinput_list => \@q,
5120 option_name => "--$opt_name",
5121 on_error => 'die',
5122 }
5123 );
5124 }
5125 }
5126  
5127 # The special option -gaxl='*' removes all defaults
5128 0 ▶ if ( $is_excluded_word{'*'} ) { $default_string = EMPTY_STRING }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_excluded_word{"*"}) 0 664 664
5129  
5130 # combine the defaults and any input list
5131 664 $opt_name = 'grep-alias-list';
5132 664 my $input_string = $rOpts->{$opt_name};
5133 664 ▶ if ($input_string) { $input_string .= SPACE . $default_string }
Branch: 1 branch
BranchTrueFalseTotal
if ($input_string) { } 3 661 664
5134 661 else { $input_string = $default_string }
5135  
5136 # Now make the final list of unique grep alias words
5137 664 $input_string =~ s/,/ /g; # allow commas
5138 664 $input_string =~ s/^\s+//;
5139 664 $input_string =~ s/\s+$//;
5140 664 my @word_list = split /\s+/, $input_string;
5141 664 my @filtered_word_list;
5142 my %seen;
5143  
5144 664 foreach my $word (@word_list) {
5145 4664 ▶ if ( !$seen{$word} && !$is_excluded_word{$word} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$word} or $is_excluded_word{$word}) 4650 14 4664
Condition66%2/3
$seen{$word} or $is_excluded_word{$word}
l!l&&r!l&&!r
1404650
MC/DC50%1/2
$seen{$word} or $is_excluded_word{$word}
$seen{$word} $is_excluded_word{$word}
Truth table66%2/3
A or B
A= $seen{$word}B= $is_excluded_word{$word}
ABresult
1X1
011
000
5146 4650 $seen{$word}++;
5147 4650 push @filtered_word_list, $word;
5148 }
5149 }
5150  
5151 check_for_valid_words(
5152 {
5153 664 rinput_list => \@filtered_word_list,
5154 option_name => "--$opt_name",
5155 on_error => 'die',
5156 }
5157 );
5158  
5159 664 my $joined_words = join( SPACE, @filtered_word_list );
5160 664 $rOpts->{$opt_name} = $joined_words;
5161  
5162 664 return;
5163 } ## end sub make_grep_alias_string
5164  
5165 3 ▶sub cleanup_word_list {
Subroutine cleanup_word_list: called CC=6 SCAR=18.3
Pod undocumented
5166  
5167 3 my ( $rOpts, $option_name, $rforced_words ) = @_;
5168  
5169 # Clean up the list of words in a user option to simplify use by
5170 # later routines (delete repeats, replace commas with single space,
5171 # remove non-words)
5172  
5173 # Given:
5174 # $rOpts - the global option hash
5175 # $option_name - hash key of this option
5176 # $rforced_words - ref to list of any words to be added
5177  
5178 # Returns:
5179 # \%seen - hash of the final list of words
5180  
5181 3 my %seen;
5182 my @input_list;
5183  
5184 3 my $input_string = $rOpts->{$option_name};
5185 3 ▶ if ( defined($input_string) && length($input_string) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_string and length $input_string) 3 0 3
Condition33%1/3
defined $input_string and length $input_string
!ll&&!rl&&r
003
MC/DC0%0/2
defined $input_string and length $input_string
defined $input_string length $input_string
Truth table33%1/3
A and B
A= defined $input_stringB= length $input_string
ABresult
0X0
100
111
5186 3 $input_string =~ s/,/ /g; # allow commas
5187 3 $input_string =~ s/^\s+//;
5188 3 $input_string =~ s/\s+$//;
5189 3 @input_list = split /\s+/, $input_string;
5190 }
5191  
5192 3 ▶ if ($rforced_words) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rforced_words) 3 0 3
5193 3 push @input_list, @{$rforced_words};
5194 }
5195  
5196 3 my @filtered_word_list;
5197 3 foreach my $word (@input_list) {
5198 11 ▶ if ( !$seen{$word} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$word}) 11 0 11
5199 11 $seen{$word}++;
5200 11 push @filtered_word_list, $word;
5201 }
5202 }
5203 check_for_valid_words(
5204 {
5205 3 rinput_list => \@filtered_word_list,
5206 option_name => "--$option_name",
5207 on_error => 'die',
5208 }
5209 );
5210 3 $rOpts->{$option_name} = join( SPACE, @filtered_word_list );
5211 3 return \%seen;
5212 } ## end sub cleanup_word_list
5213  
5214 664 ▶sub check_string_options {
Subroutine check_string_options: called CC=13 SCAR=32.6
Pod undocumented
5215 664 my ( $self, $ris_string_option ) = @_;
5216  
5217 # Make some basic checks for invalid characters in user-defined strings.
5218 # More detailed checks are made later in sub check_options.
5219  
5220 664 my $rOpts = $self->[_rOpts_];
5221 664 my $message = EMPTY_STRING;
5222  
5223 664 my @all_string_options = grep { $ris_string_option->{$_} } keys %{$rOpts};
5224 664 my @html_color_options = grep { /^html-color-/ } @all_string_options;
5225  
5226 664 my @filename_options = qw(
5227 cachedir
5228 html-linked-style-sheet
5229 htmlroot
5230 libpods
5231 outfile
5232 output-path
5233 podpath
5234 podroot
5235 );
5236  
5237 664 my @file_extension_options = qw(
5238 backup-file-extension
5239 html-src-extension
5240 html-toc-extension
5241 output-file-extension
5242 );
5243  
5244 # What to check:
5245 my %leading_dash_check =
5246 664 map { $_ => 1 } ( @filename_options, @html_color_options );
5247 my %leading_space_check =
5248 664 map { $_ => 1 } ( @filename_options, @file_extension_options );
5249 664 my %trailing_space_check = %leading_space_check;
5250  
5251 664 foreach my $opt_name (@all_string_options) {
5252 4123 my $test_string = $rOpts->{$opt_name};
5253  
5254 4123 ▶ next if ( !defined($test_string) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test_string 0 4123 4123
5255  
5256 # Printable character check for all string options
5257 4123 ▶ if ( $test_string =~ /[^[:print:]]/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_string =~ /[^[:print:]]/g) 0 4123 4123
5258 0 my $pos = pos($test_string);
5259 0 my $ch = substr( $test_string, $pos - 1, 1 );
5260 0 my $ord = ord($ch);
5261 0 $message .= <<EOM;
5262 --$opt_name has non-printable character(s) at character number $pos, decimal value=$ord
5263 EOM
5264 }
5265  
5266 # Leading dash check
5267 4123 ▶ if ( $leading_dash_check{$opt_name}
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_dash_check{$opt_name} and substr($test_string, 0, 1) eq "-") 0 4123 4123
Condition33%1/3
$leading_dash_check{$opt_name} and substr($test_string, 0, 1) eq "-"
!ll&&!rl&&r
412300
MC/DC0%0/2
$leading_dash_check{$opt_name} and substr($test_string, 0, 1) eq "-"
$leading_dash_check{$opt_name} substr($test_string, 0, 1) eq "-"
Truth table33%1/3
A and B
A= $leading_dash_check{$opt_name}B= substr($test_string, 0, 1) eq "-"
ABresult
0X0
100
111
5268 && substr( $test_string, 0, 1 ) eq '-' )
5269 {
5270 0 my $hint = EMPTY_STRING;
5271 0 ▶ if ( $opt_name eq 'outfile' || $opt_name eq 'output_path' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_name eq "outfile" or $opt_name eq "output_path") 0 0 0
Condition0%0/3
$opt_name eq "outfile" or $opt_name eq "output_path"
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt_name eq "outfile" or $opt_name eq "output_path"
$opt_name eq "outfile" $opt_name eq "output_path"
Truth table0%0/3
A or B
A= $opt_name eq "outfile"B= $opt_name eq "output_path"
ABresult
1X1
011
000
5272 0 $hint .= "; add leading path (like ./) if necessary";
5273 }
5274 0 $message .= <<EOM;
5275 --$opt_name string must not begin with a dash$hint
5276 EOM
5277 }
5278  
5279 # Leading space checks
5280 4123 ▶ if ( $leading_space_check{$opt_name} && $test_string =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leading_space_check{$opt_name} and $test_string =~ /^\s/) 0 4123 4123
Condition66%2/3
$leading_space_check{$opt_name} and $test_string =~ /^\s/
!ll&&!rl&&r
34596640
MC/DC0%0/2
$leading_space_check{$opt_name} and $test_string =~ /^\s/
$leading_space_check{$opt_name} $test_string =~ /^\s/
Truth table66%2/3
A and B
A= $leading_space_check{$opt_name}B= $test_string =~ /^\s/
ABresult
0X0
100
111
5281 0 $message .= "--$opt_name must not contain leading spaces\n";
5282 }
5283  
5284 # Trailing space check
5285 4123 ▶ if ( $trailing_space_check{$opt_name} && $test_string =~ /\s$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailing_space_check{$opt_name} and $test_string =~ /\s$/) 0 4123 4123
Condition66%2/3
$trailing_space_check{$opt_name} and $test_string =~ /\s$/
!ll&&!rl&&r
34596640
MC/DC0%0/2
$trailing_space_check{$opt_name} and $test_string =~ /\s$/
$trailing_space_check{$opt_name} $test_string =~ /\s$/
Truth table66%2/3
A and B
A= $trailing_space_check{$opt_name}B= $test_string =~ /\s$/
ABresult
0X0
100
111
5286 0 $message .= "--$opt_name must not contain trailing spaces\n";
5287 }
5288 }
5289  
5290 664 ▶ if ($message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message) 0 664 664
5291 0 Die($message);
5292 }
5293  
5294 664 return;
5295 } ## end sub check_string_options
5296  
5297 664 ▶sub check_options {
Subroutine check_options: called CC=51 SCAR=53.7
Pod undocumented
5298  
5299 664 my ( $self, $num_files, $rinteger_option_range, $ris_string_option ) = @_;
5300  
5301 # Check options at a high level. Note that other modules have their
5302 # own sub 'check_options' for lower level checking.
5303  
5304 # Input parameters:
5305 # $num_files = the number of files to be processed in this call to
5306 # perltidy, needed for error checks.
5307 # $rinteger_option-range = hash with valid ranges of parameters which
5308 # take an integer
5309  
5310 664 my $rOpts = $self->[_rOpts_];
5311  
5312 #------------------------------------------------------------
5313 # check and handle any interactions among the basic options..
5314 #------------------------------------------------------------
5315  
5316 # Since perltidy only encodes in utf8, problems can occur if we let it
5317 # decode anything else. See discussions for issue git #83.
5318 664 my $encoding = $rOpts->{'character-encoding'};
5319 664 ▶ if ( $encoding !~ /^\s*(?:guess|none|utf8|utf-8)\s*$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($encoding =~ /^\s*(?:guess|none|utf8|utf-8)\s*$/i) 0 664 664
5320 0 Die(<<EOM);
5321 --character-encoding = '$encoding' is not allowed; the options are: 'none', 'guess', 'utf8'
5322 EOM
5323 }
5324  
5325 # Check for integer values out of bounds as follows:
5326 # $integer_range_check=
5327 # 1 => quietly reset bad values to defaults
5328 # 2 => issue warning and reset bad values to defaults [DEFAULT]
5329 # 3 => stop if any values are out of bounds
5330 # Note: Previously a value of 0 meant to skip this check. This provided a
5331 # workaround in case this logic caused a problem. This is no longer needed.
5332 664 my $integer_range_check = $rOpts->{'integer-range-check'};
5333 664 ▶ if ( !defined($integer_range_check)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $integer_range_check or $integer_range_check <= 0 or $integer_range_check > 3) 0 664 664
Condition33%2/6
not defined $integer_range_check or $integer_range_check <= 0 or $integer_range_check > 3
l!l&&r!l&&!r
00664
not defined $integer_range_check or $integer_range_check <= 0
l!l&&r!l&&!r
00664
MC/DC0%0/3
not defined $integer_range_check or $integer_range_check <= 0 or $integer_range_check > 3
not defined $integer_range_check $integer_range_check <= 0 $integer_range_check > 3
Truth table25%1/4
not(A) or B or C
A= not defined $integer_range_checkB= $integer_range_check <= 0C= $integer_range_check > 3
ABCresult
1XX1
01X1
0011
0000
5334 || $integer_range_check <= 0
5335 || $integer_range_check > 3 )
5336 {
5337 0 $integer_range_check = 2;
5338 }
5339  
5340 664 ▶ if ($integer_range_check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer_range_check) 664 0 664
5341 664 my $Error_message;
5342 664 foreach my $opt ( keys %{$rinteger_option_range} ) {
5343 48472 my $val = $rOpts->{$opt};
5344 48472 ▶ next unless ( defined($val) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 3318 45154 48472
5345 45154 my $range = $rinteger_option_range->{$opt};
5346 45154 ▶ next unless ( defined($range) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $range 0 45154 45154
5347 45154 my ( $min, $max, $default ) = @{$range};
5348  
5349 45154 ▶ if ( defined($min) && $val < $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min and $val < $min) 0 45154 45154
Condition33%1/3
defined $min and $val < $min
!ll&&!rl&&r
0451540
MC/DC0%0/2
defined $min and $val < $min
defined $min $val < $min
Truth table33%1/3
A and B
A= defined $minB= $val < $min
ABresult
0X0
100
111
5350 0 $Error_message .= "--$opt=$val but should be >= $min";
5351 0 ▶ if ( $integer_range_check < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer_range_check < 3) 0 0 0
5352 0 $rOpts->{$opt} = $default;
5353 0 ▶ my $def = defined($default) ? $default : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 0 0 0
5354 0 $Error_message .= "; using default $def";
5355 }
5356 0 $Error_message .= "\n";
5357 }
5358 45154 ▶ if ( defined($max) && $val > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max and $val > $max) 0 45154 45154
Condition66%2/3
defined $max and $val > $max
!ll&&!rl&&r
20586245680
MC/DC0%0/2
defined $max and $val > $max
defined $max $val > $max
Truth table66%2/3
A and B
A= defined $maxB= $val > $max
ABresult
0X0
100
111
5359 0 $Error_message .= "--$opt=$val but should be <= $max";
5360 0 ▶ if ( $integer_range_check < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer_range_check < 3) 0 0 0
5361 0 $rOpts->{$opt} = $default;
5362 0 ▶ my $def = defined($default) ? $default : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $default ? : 0 0 0
5363 0 $Error_message .= "; using default $def";
5364 }
5365 0 $Error_message .= "\n";
5366 }
5367 }
5368 664 ▶ if ($Error_message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Error_message) 0 664 664
5369 0 ▶ if ( $integer_range_check == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($integer_range_check == 1) { } 0 0 0
elsif ($integer_range_check == 2) { } 0 0 0
5370 ## no warning
5371 }
5372 elsif ( $integer_range_check == 2 ) {
5373 0 Warn($Error_message);
5374 }
5375 else {
5376 0 Die($Error_message);
5377 }
5378 }
5379 }
5380  
5381 # Do some very basic checks on string options
5382 664 $self->check_string_options($ris_string_option);
5383  
5384 # Note that -vt, -vtc, and -cti are abbreviations. But under
5385 # msdos, an unquoted input parameter like vtc=1 will be
5386 # seen as 2 parameters, vtc and 1, so the abbreviations
5387 # won't be seen. Therefore, we will catch them here if
5388 # they get through.
5389 664 ▶ if ( defined( $rOpts->{'vertical-tightness'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{"vertical-tightness"}) 0 664 664
5390 0 my $vt = $rOpts->{'vertical-tightness'};
5391 0 $rOpts->{'paren-vertical-tightness'} = $vt;
5392 0 $rOpts->{'square-bracket-vertical-tightness'} = $vt;
5393 0 $rOpts->{'brace-vertical-tightness'} = $vt;
5394 }
5395  
5396 664 ▶ if ( defined( $rOpts->{'vertical-tightness-closing'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{"vertical-tightness-closing"}) 0 664 664
5397 0 my $vtc = $rOpts->{'vertical-tightness-closing'};
5398 0 $rOpts->{'paren-vertical-tightness-closing'} = $vtc;
5399 0 $rOpts->{'square-bracket-vertical-tightness-closing'} = $vtc;
5400 0 $rOpts->{'brace-vertical-tightness-closing'} = $vtc;
5401 }
5402  
5403 664 ▶ if ( defined( $rOpts->{'closing-token-indentation'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{"closing-token-indentation"}) 0 664 664
5404 0 my $cti = $rOpts->{'closing-token-indentation'};
5405 0 $rOpts->{'closing-square-bracket-indentation'} = $cti;
5406 0 $rOpts->{'closing-brace-indentation'} = $cti;
5407 0 $rOpts->{'closing-paren-indentation'} = $cti;
5408 }
5409  
5410 664 my $MAX_BLANK_COUNT = 100;
5411 2656 ▶ my $check_blank_count = sub {
Subroutine __ANON__: called CC=4 SCAR=16.1
5412 2656 my ( $key, $abbrev ) = @_;
5413  
5414 # Check certain user input for unreasonable numbers of blank lines
5415  
5416 2656 ▶ if ( $rOpts->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{$key}) 1288 1368 2656
5417 1288 ▶ if ( $rOpts->{$key} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{$key} < 0) 0 1288 1288
5418 0 $rOpts->{$key} = 0;
5419 0 Warn("negative value of $abbrev, resetting to 0\n");
5420 }
5421 1288 ▶ if ( $rOpts->{$key} > $MAX_BLANK_COUNT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{$key} > $MAX_BLANK_COUNT) 0 1288 1288
5422 0 Warn(
5423 "unreasonably large value of $abbrev, reducing to $MAX_BLANK_COUNT\n"
5424 );
5425 0 $rOpts->{$key} = $MAX_BLANK_COUNT;
5426 }
5427 }
5428 2656 return;
5429 664 }; ## end $check_blank_count = sub
5430  
5431 # check for reasonable number of blank lines and fix to avoid problems
5432 664 $check_blank_count->( 'blank-lines-before-subs', '-blbs' );
5433 664 $check_blank_count->( 'blank-lines-before-packages', '-blbp' );
5434 664 $check_blank_count->( 'blank-lines-after-block-opening', '-blao' );
5435 664 $check_blank_count->( 'blank-lines-before-block-closing', '-blbc' );
5436  
5437 # setting a non-negative logfile gap causes logfile to be saved
5438 664 ▶ if ( defined( $rOpts->{'logfile-gap'} ) && $rOpts->{'logfile-gap'} >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{"logfile-gap"} and $rOpts->{"logfile-gap"} >= 0) 1 663 664
Condition66%2/3
defined $rOpts->{"logfile-gap"} and $rOpts->{"logfile-gap"} >= 0
!ll&&!rl&&r
66301
MC/DC50%1/2
defined $rOpts->{"logfile-gap"} and $rOpts->{"logfile-gap"} >= 0
defined $rOpts->{"logfile-gap"} $rOpts->{"logfile-gap"} >= 0
Truth table66%2/3
A and B
A= defined $rOpts->{"logfile-gap"}B= $rOpts->{"logfile-gap"} >= 0
ABresult
0X0
100
111
5439 1 $rOpts->{'logfile'} = 1;
5440 }
5441  
5442 # set short-cut flag when only indentation is to be done.
5443 # Note that the user may or may not have already set the
5444 # indent-only flag.
5445 664 ▶ if ( !$rOpts->{'add-whitespace'}
Branch: 1 branch
BranchTrueFalseTotal
unless ($rOpts->{"add-whitespace"} or $rOpts->{"delete-old-whitespace"} or $rOpts->{"add-newlines"} or $rOpts->{"delete-old-newlines"}) 3 661 664
Condition88%8/9
$rOpts->{"add-whitespace"} or $rOpts->{"delete-old-whitespace"} or $rOpts->{"add-newlines"} or $rOpts->{"delete-old-newlines"}
l!l&&r!l&&!r
66103
$rOpts->{"add-whitespace"} or $rOpts->{"delete-old-whitespace"} or $rOpts->{"add-newlines"}
l!l&&r!l&&!r
66013
$rOpts->{"add-whitespace"} or $rOpts->{"delete-old-whitespace"}
l!l&&r!l&&!r
647134
MC/DC75%3/4
$rOpts->{"add-whitespace"} or $rOpts->{"delete-old-whitespace"} or $rOpts->{"add-newlines"} or $rOpts->{"delete-old-newlines"}
$rOpts->{"add-whitespace"} $rOpts->{"delete-old-whitespace"} $rOpts->{"add-newlines"} $rOpts->{"delete-old-newlines"}
Truth table80%4/5
A or B or C or D
A= $rOpts->{"add-whitespace"}B= $rOpts->{"delete-old-whitespace"}C= $rOpts->{"add-newlines"}D= $rOpts->{"delete-old-newlines"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
5446 && !$rOpts->{'delete-old-whitespace'}
5447 && !$rOpts->{'add-newlines'}
5448 && !$rOpts->{'delete-old-newlines'} )
5449 {
5450 3 $rOpts->{'indent-only'} = 1;
5451 }
5452  
5453 # -isbc implies -ibc
5454 664 ▶ if ( $rOpts->{'indent-spaced-block-comments'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"indent-spaced-block-comments"}) 5 659 664
5455 5 $rOpts->{'indent-block-comments'} = 1;
5456 }
5457  
5458 # -bar cannot be used with -bl or -bli; arbitrarily keep -bar
5459 664 ▶ if ( $rOpts->{'opening-brace-always-on-right'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"opening-brace-always-on-right"}) 3 661 664
5460  
5461 3 ▶ if ( $rOpts->{'opening-brace-on-new-line'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"opening-brace-on-new-line"}) 0 3 3
5462 0 Warn(<<EOM);
5463 Conflict: you specified both 'opening-brace-always-on-right' (-bar) and
5464 'opening-brace-on-new-line' (-bl). Ignoring -bl.
5465 EOM
5466 0 $rOpts->{'opening-brace-on-new-line'} = 0;
5467 }
5468 3 ▶ if ( $rOpts->{'brace-left-and-indent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"brace-left-and-indent"}) 0 3 3
5469 0 Warn(<<EOM);
5470 Conflict: you specified both 'opening-brace-always-on-right' (-bar) and
5471 '--brace-left-and-indent' (-bli). Ignoring -bli.
5472 EOM
5473 0 $rOpts->{'brace-left-and-indent'} = 0;
5474 }
5475 }
5476  
5477 # it simplifies things if -bl is 0 rather than undefined
5478 664 ▶ if ( !defined( $rOpts->{'opening-brace-on-new-line'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rOpts->{"opening-brace-on-new-line"}) 640 24 664
5479 640 $rOpts->{'opening-brace-on-new-line'} = 0;
5480 }
5481  
5482 664 ▶ if ( $rOpts->{'entab-leading-whitespace'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"entab-leading-whitespace"}) 2 662 664
5483 2 ▶ if ( $rOpts->{'entab-leading-whitespace'} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"entab-leading-whitespace"} < 0) 0 2 2
5484 0 Warn("-et=n must use a positive integer; ignoring -et\n");
5485 0 $rOpts->{'entab-leading-whitespace'} = undef;
5486 }
5487  
5488 # entab leading whitespace has priority over the older 'tabs' option
5489 2 ▶ if ( $rOpts->{'tabs'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"tabs"}) 1 1 2
5490  
5491 # The following warning could be added but would annoy a lot of
5492 # users who have a perltidyrc with both -t and -et=n. So instead
5493 # there is a note in the manual that -et overrides -t.
5494 ##Warn("-tabs and -et=n conflict; ignoring -tabs\n");
5495 1 $rOpts->{'tabs'} = 0;
5496 }
5497 }
5498  
5499 # Set a default tabsize to be used in guessing the starting indentation
5500 # level if and only if this run does not use tabs and the old code does
5501 # use tabs
5502 664 my $MAX_DEFAULT_TABSIZE = 20;
5503 664 ▶ if ( $rOpts->{'default-tabsize'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{'default-tabsize'}) { } 664 0 664
5504 664 ▶ if ( $rOpts->{'default-tabsize'} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"default-tabsize"} < 0) 0 664 664
5505 0 Warn("negative value of -dt, resetting to 0\n");
5506 0 $rOpts->{'default-tabsize'} = 0;
5507 }
5508 664 ▶ if ( $rOpts->{'default-tabsize'} > $MAX_DEFAULT_TABSIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"default-tabsize"} > $MAX_DEFAULT_TABSIZE) 0 664 664
5509 0 Warn(
5510 "unreasonably large value of -dt, reducing to $MAX_DEFAULT_TABSIZE\n"
5511 );
5512 0 $rOpts->{'default-tabsize'} = $MAX_DEFAULT_TABSIZE;
5513 }
5514 }
5515 else {
5516 0 $rOpts->{'default-tabsize'} = 8;
5517 }
5518  
5519 # Check and clean up any sub-alias-list
5520 664 ▶ if ( defined( $rOpts->{'sub-alias-list'} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{"sub-alias-list"} and length $rOpts->{"sub-alias-list"}) 3 661 664
Condition66%2/3
defined $rOpts->{"sub-alias-list"} and length $rOpts->{"sub-alias-list"}
!ll&&!rl&&r
66103
MC/DC50%1/2
defined $rOpts->{"sub-alias-list"} and length $rOpts->{"sub-alias-list"}
defined $rOpts->{"sub-alias-list"} length $rOpts->{"sub-alias-list"}
Truth table66%2/3
A and B
A= defined $rOpts->{"sub-alias-list"}B= length $rOpts->{"sub-alias-list"}
ABresult
0X0
100
111
5521 && length( $rOpts->{'sub-alias-list'} ) )
5522 {
5523 3 my @forced_words;
5524  
5525 # include 'sub' for convenience if this option is used
5526 3 push @forced_words, 'sub';
5527  
5528 3 cleanup_word_list( $rOpts, 'sub-alias-list', \@forced_words );
5529 }
5530  
5531 664 make_grep_alias_string($rOpts);
5532  
5533 # Large values of -scl can cause convergence problems, issue c167
5534 664 ▶ if ( $rOpts->{'short-concatenation-item-length'} > 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"short-concatenation-item-length"} > 12) 0 664 664
5535 0 $rOpts->{'short-concatenation-item-length'} = 12;
5536 }
5537  
5538 # The freeze-whitespace option is currently a derived option which has its
5539 # own key
5540 $rOpts->{'freeze-whitespace'} = !$rOpts->{'add-whitespace'}
5541 664 ▶ && !$rOpts->{'delete-old-whitespace'};
Condition100%3/3
$rOpts->{'add-whitespace'} || $rOpts->{'delete-old-whitespace'}
l!l&&r!l&&!r
647134
MC/DC100%2/2
$rOpts->{'add-whitespace'} || $rOpts->{'delete-old-whitespace'}
$rOpts->{'add-whitespace'} $rOpts->{'delete-old-whitespace'}
Truth table100%3/3
A || B
A= $rOpts->{'add-whitespace'}B= $rOpts->{'delete-old-whitespace'}
ABresult
1X1
011
000
5542  
5543 # Turn off certain options if whitespace is frozen
5544 # Note: vertical alignment will be automatically shut off
5545 664 ▶ if ( $rOpts->{'freeze-whitespace'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rOpts->{"freeze-whitespace"}) 4 660 664
5546 4 $rOpts->{'logical-padding'} = 0;
5547 }
5548  
5549 # Define the default line ending, before any -ple option is applied
5550 664 $self->[_line_separator_default_] = get_line_separator_default($rOpts);
5551  
5552 664 $self->[_line_tidy_begin_] = undef;
5553 664 $self->[_line_tidy_end_] = undef;
5554 664 my $line_range_tidy = $rOpts->{'line-range-tidy'};
5555 664 ▶ if ($line_range_tidy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_range_tidy) 1 663 664
5556  
5557 1 ▶ if ( $num_files > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_files > 1) 0 1 1
5558 0 Die(<<EOM);
5559 --line-range-tidy expects no more than 1 filename in the arg list but saw $num_files filenames
5560 EOM
5561 }
5562  
5563 1 $line_range_tidy =~ s/\s+//g;
5564 1 ▶ if ( $line_range_tidy =~ /^(\d+):(\d+)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_range_tidy =~ /^(\d+):(\d+)?$/) { } 1 0 1
5565 1 my $n1 = $1;
5566 1 my $n2 = $2;
5567 1 ▶ if ( $n1 < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n1 < 1) 0 1 1
5568 0 Die(<<EOM);
5569 --line-range-tidy=n1:n2 expects starting line number n1>=1 but n1=$n1
5570 EOM
5571 }
5572 1 ▶ if ( defined($n2) && $n2 < $n1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n2 and $n2 < $n1) 0 1 1
Condition33%1/3
defined $n2 and $n2 < $n1
!ll&&!rl&&r
010
MC/DC0%0/2
defined $n2 and $n2 < $n1
defined $n2 $n2 < $n1
Truth table33%1/3
A and B
A= defined $n2B= $n2 < $n1
ABresult
0X0
100
111
5573 0 Die(<<EOM);
5574 --line-range-tidy=n1:n2 expects ending line number n2>=n1 but n1=$n1 and n2=$n2
5575 EOM
5576 }
5577 1 $self->[_line_tidy_begin_] = $n1;
5578 1 $self->[_line_tidy_end_] = $n2;
5579 }
5580 else {
5581 0 Die(
5582 "unrecognized 'line-range-tidy'; expecting format '-lrt=n1:n2'\n"
5583 );
5584 }
5585 }
5586  
5587 664 return;
5588 } ## end sub check_options
5589  
5590 0 ▶sub find_file_upwards {
Subroutine find_file_upwards: not called CC=3 SCAR=24.8
Pod undocumented
5591  
5592 0 my ( $search_dir, $search_file ) = @_;
5593  
5594 # This implements the ... upward search for a file
5595  
5596 0 $search_dir =~ s{/+$}{};
5597 0 $search_file =~ s{^/+}{};
5598  
5599 0 while (1) {
5600 0 my $try_path = "$search_dir/$search_file";
5601 0 ▶ if ( -f $try_path ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $try_path) { } 0 0 0
elsif ($search_dir eq '/') { } 0 0 0
5602 0 return $try_path;
5603 }
5604 elsif ( $search_dir eq '/' ) {
5605 0 return;
5606 }
5607 else {
5608 0 $search_dir = dirname($search_dir);
5609 }
5610 } ## end while (1)
5611  
5612 # This return is for Perl-Critic.
5613 # We shouldn't get out of the while loop without a return
5614 0 return;
5615 } ## end sub find_file_upwards
5616  
5617 940 ▶sub expand_command_abbreviations {
Subroutine expand_command_abbreviations: called CC=14 SCAR=29.4
Pod undocumented
5618  
5619 940 my ( $rexpansion, $rraw_options, $config_file ) = @_;
5620  
5621 # Go through @ARGV and expand any abbreviations
5622 # Note that @ARGV has been localized
5623  
5624 # Set a pass limit to prevent an infinite loop;
5625 # 10 should be plenty, but it may be increased to allow deeply
5626 # nested expansions.
5627 940 my $max_passes = 10;
5628  
5629 # keep looping until all expansions have been converted into actual
5630 # dash parameters..
5631 940 foreach my $pass_count ( 0 .. $max_passes ) {
5632 1324 my @new_argv = ();
5633 1324 my $abbrev_count = 0;
5634  
5635 # loop over each item in @ARGV..
5636 1324 foreach my $word (@ARGV) {
5637  
5638 # convert any leading 'no-' to just 'no'
5639 2892 ▶ if ( $word =~ /^(-[-]?no)-(.*)/ ) { $word = $1 . $2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /^(-[-]?no)-(.*)/) 5 2887 2892
5640  
5641 # if it is a dash flag (instead of a file name)..
5642 2892 ▶ if ( $word =~ /^-[-]?([\w\-]+)(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /^-[-]?([\w\-]+)(.*)/) { } 2892 0 2892
5643  
5644 2892 my $abr = $1;
5645 2892 my $flags = $2;
5646  
5647 # save the raw input for debug output in case of circular refs
5648 2892 ▶ if ( $pass_count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass_count == 0) 718 2174 2892
5649 718 push @{$rraw_options}, $word;
5650 }
5651  
5652 # recombine abbreviation and flag, if necessary,
5653 # to allow abbreviations with arguments such as '-vt=1'
5654 2892 ▶ if ( $rexpansion->{ $abr . $flags } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rexpansion{$abr . $flags}) 541 2351 2892
5655 541 $abr = $abr . $flags;
5656 541 $flags = EMPTY_STRING;
5657 }
5658  
5659 # if we see this dash item in the expansion hash..
5660 2892 ▶ if ( $rexpansion->{$abr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rexpansion->{$abr}) { } 1105 1787 2892
5661 1105 $abbrev_count++;
5662  
5663 # stuff all of the words that it expands to into the
5664 # new arg list for the next pass
5665 1105 foreach my $abbrev ( @{ $rexpansion->{$abr} } ) {
5666 1610 ▶ next unless ($abbrev); # for safety; shouldn't happen
Branch: 1 branch
BranchTrueFalseTotal
unless $abbrev 0 1610 1610
5667 1610 push @new_argv, '--' . $abbrev . $flags;
5668 }
5669 }
5670  
5671 # not in expansion hash, must be actual long name
5672 else {
5673 1787 push @new_argv, $word;
5674 }
5675 }
5676  
5677 # not a dash item, so just save it for the next pass
5678 else {
5679 0 push @new_argv, $word;
5680 }
5681 } ## end of this pass
5682  
5683 # update parameter list @ARGV to the new one
5684 1324 @ARGV = @new_argv;
5685 1324 ▶ last if ( !$abbrev_count );
Branch: 1 branch
BranchTrueFalseTotal
unless $abbrev_count 940 384 1324
5686  
5687 # make sure we are not in an infinite loop
5688 384 ▶ if ( $pass_count == $max_passes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass_count == $max_passes) 0 384 384
5689 0 local $LIST_SEPARATOR = ')(';
5690 0 Warn(<<EOM);
5691 I'm tired. We seem to be in an infinite loop trying to expand aliases.
5692 Here are the raw options;
5693 (rraw_options)
5694 EOM
5695 0 my $num = @new_argv;
5696 0 ▶ if ( $num < 50 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num < 50) { } 0 0 0
5697 0 Warn(<<EOM);
5698 After $max_passes passes here is ARGV
5699 (@new_argv)
5700 EOM
5701 }
5702 else {
5703 0 Warn(<<EOM);
5704 After $max_passes passes ARGV has $num entries
5705 EOM
5706 }
5707  
5708 0 ▶ if ( defined($config_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file) { } 0 0 0
5709 0 Die(<<"DIE");
5710 Please check your configuration file $config_file for circular-references.
5711 To deactivate it, use -npro.
5712 DIE
5713 }
5714 else {
5715 0 Die(<<'DIE');
5716 Program bug - circular-references in the %expansion hash, probably due to
5717 a recent program change.
5718 DIE
5719 }
5720 } ## end of check for circular references
5721 } ## end of loop over all passes
5722 940 return;
5723 } ## end sub expand_command_abbreviations
5724  
5725 940 ▶sub check_for_missing_string_options {
Subroutine check_for_missing_string_options: called CC=10 SCAR=28.5
Pod undocumented
5726 940 my ( $ris_string_option, ($config_file) ) = @_;
5727  
5728 # Given:
5729 # $ris_string_option = hash with keys are options of type '=s'
5730 # ($config_file) = optional parameter:
5731 # - name of config file if processing config file
5732 # - undef if processing command line args
5733  
5734 # Task:
5735 # Look through @ARGV for string options which are not immediately followed
5736 # by '=string'. If the next word looks like another --option, then it may
5737 # get gobbled up as the string arg. In that case, exit with an error
5738 # message. The user can always force a string arg which looks like an
5739 # option by using the '=string' input form.
5740  
5741 # Example of the type of error this sub checks for:
5742  
5743 # perltidy -lpil -l=9 filename
5744  
5745 # In this sub, any short option forms have already been expanded into their
5746 # long forms, so this will appear here in the local copy of @ARGV as three
5747 # list items:
5748  
5749 # @ARGV = qw(
5750 # --line-up-parentheses-inclusion-list
5751 # --maximum-line-length=9
5752 # filename
5753 # );
5754  
5755 # Then, since -lpil wants a string value, it will be set equal to
5756 # '--line-up-parentheses=9' by sub GetOptions, which is probably not the
5757 # desired value.
5758  
5759 # This sub will catch most errors of this type at the earliest possible
5760 # stage. One exception is if the user enters just part of an option name
5761 # and relies on name completion by sub GetOptions. Another exception is if
5762 # a filename follows the missing string option on the command line. In
5763 # those cases we have to rely on later checks.
5764  
5765 940 my $arg_seeking_string_last;
5766 940 my $error_message = EMPTY_STRING;
5767 940 foreach my $arg (@ARGV) {
5768  
5769 1223 my $arg_seeking_string;
5770  
5771 # something like --option ?
5772 1223 ▶ if ( substr( $arg, 0, 2 ) eq '--' && length($arg) > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($arg, 0, 2) eq "--" and length $arg > 2) 1208 15 1223
Condition66%2/3
substr($arg, 0, 2) eq "--" and length $arg > 2
!ll&&!rl&&r
1501208
MC/DC50%1/2
substr($arg, 0, 2) eq "--" and length $arg > 2
substr($arg, 0, 2) eq "--" length $arg > 2
Truth table66%2/3
A and B
A= substr($arg, 0, 2) eq "--"B= length $arg > 2
ABresult
0X0
100
111
5773  
5774 # Will the previous string without arg try to grab this option?
5775 1208 ▶ if ( $arg_seeking_string_last && $arg =~ /^\-\-[A-Za-z]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_seeking_string_last and $arg =~ /^\-\-[A-Za-z]/) 0 1208 1208
Condition33%1/3
$arg_seeking_string_last and $arg =~ /^\-\-[A-Za-z]/
!ll&&!rl&&r
120800
MC/DC0%0/2
$arg_seeking_string_last and $arg =~ /^\-\-[A-Za-z]/
$arg_seeking_string_last $arg =~ /^\-\-[A-Za-z]/
Truth table33%1/3
A and B
A= $arg_seeking_string_lastB= $arg =~ /^\-\-[A-Za-z]/
ABresult
0X0
100
111
5776 0 $error_message .= <<EOM;
5777 '$arg_seeking_string_last' may be missing its string parameter.
5778 EOM
5779 }
5780  
5781 # Is this a string option without a following '=value' ?
5782 1208 ▶ if ( index( $arg, '=' ) < 0
Branch: 1 branch
BranchTrueFalseTotal
if (index($arg, "=") == -1 and $$ris_string_option{substr $arg, 2}) 0 1208 1208
Condition66%2/3
index($arg, "=") == -1 and $$ris_string_option{substr $arg, 2}
!ll&&!rl&&r
6565520
MC/DC0%0/2
index($arg, "=") == -1 and $$ris_string_option{substr $arg, 2}
index($arg, "=") == -1 $$ris_string_option{substr $arg, 2}
Truth table66%2/3
A and B
A= index($arg, "=") == -1B= $$ris_string_option{substr $arg, 2}
ABresult
0X0
100
111
5783 && $ris_string_option->{ substr( $arg, 2 ) } )
5784 {
5785 0 $arg_seeking_string = $arg;
5786 }
5787  
5788 }
5789 1223 $arg_seeking_string_last = $arg_seeking_string;
5790 }
5791  
5792 940 ▶ if ($error_message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_message) 0 940 940
5793 0 my $pre_note = "Possible error ";
5794 0 ▶ $pre_note .=
Branch: 1 branch
BranchTrueFalseTotal
defined $config_file ? : 0 0 0
5795 defined($config_file)
5796 ? "in config file '$config_file':\n"
5797 : "on the command line:\n";
5798 0 my $post_note =
5799 "Use the equals form '--option=string' to avoid this message.\n";
5800 0 Die( $pre_note . $error_message . $post_note );
5801 }
5802 940 return;
5803 } ## end sub check_for_missing_string_options
5804  
5805 0 ▶sub dump_short_names {
Subroutine dump_short_names: not called CC=2 SCAR=17.9
Pod undocumented
5806  
5807 0 my $rexpansion = shift;
5808  
5809 # do --dump-short-names (-dsn)
5810 # Debug routine -- this will dump the expansion hash
5811  
5812 0 print {*STDOUT} <<EOM;
5813 List of short names. This list shows how all abbreviations are
5814 translated into other abbreviations and, eventually, into long names.
5815 New abbreviations may be defined in a .perltidyrc file.
5816 For a list of all long names, use perltidy --dump-long-names (-dln).
5817 --------------------------------------------------------------------------
5818 EOM
5819 0 foreach my $abbrev ( sort keys %{$rexpansion} ) {
5820 0 my @list = @{ $rexpansion->{$abbrev} };
5821 0 print {*STDOUT} "$abbrev --> @list\n";
5822 }
5823 0 return;
5824 } ## end sub dump_short_names
5825  
5826 0 ▶sub check_vms_filename {
Subroutine check_vms_filename: not called CC=4 SCAR=30.0
Pod undocumented
5827  
5828 0 my $filename = shift;
5829  
5830 # Given a valid filename (the perltidy input file)
5831 # create a modified filename and separator character
5832 # suitable for VMS.
5833 #
5834 # Contributed by Michael Cartmell
5835 #
5836 0 my ( $base, $path ) = fileparse($filename);
5837  
5838 # remove explicit ; version
5839 0 ▶ $base =~ s/;-?\d*$//
Branch: 1 branch
BranchTrueFalseTotal
$base =~ s/;-?\d*$// or $base =~ s/( # begin capture $1 (?:^|[^^])\. # match a dot not preceded by a caret (?: # followed by nothing | # or .*[^^] # anything ending in a non caret ) ) # end capture $1 \.-?\d*$ # match . version number /$1/x 0 0 0
5840  
5841 # remove explicit . version, i.e. two dots in filename NB ^ escapes a dot
5842 or $base =~ s{( # begin capture $1
5843 (?:^|[^^])\. # match a dot not preceded by a caret
5844 (?: # followed by nothing
5845 | # or
5846 .*[^^] # anything ending in a non caret
5847 )
5848 ) # end capture $1
5849 \.-?\d*$ # match . version number
5850 }{$1}x;
5851  
5852 # normalize filename, if there are no unescaped dots then append one
5853 0 ▶ $base .= '.' unless ( $base =~ /(?:^|[^^])\./ );
Branch: 1 branch
BranchTrueFalseTotal
unless $base =~ /(?:^|[^^])\./ 0 0 0
5854  
5855 # if we don't already have an extension then we just append the extension
5856 0 ▶ my $separator = ( $base =~ /\.$/ ) ? EMPTY_STRING : "_";
Branch: 1 branch
BranchTrueFalseTotal
$base =~ /\.$/ ? : 0 0 0
5857 0 return ( $path . $base, $separator );
5858 } ## end sub check_vms_filename
5859  
5860 0 ▶sub Win_OS_Type {
Subroutine Win_OS_Type: not called CC=8 SCAR=42.8
Pod undocumented
5861  
5862 0 my $rpending_complaint = shift;
5863  
5864 # Returns a string that determines what MS OS we are on.
5865 # Returns win32s,95,98,Me,NT3.51,NT4,2000,XP/.Net,Win2003
5866 # Returns blank string if not an MS system.
5867 # Original code contributed by: Yves Orton
5868 # We need to know this to decide where to look for config files
5869  
5870 # TODO: are these more standard names?
5871 # Win32s Win95 Win98 WinMe WinNT3.51 WinNT4 Win2000 WinXP/.Net Win2003
5872  
5873 0 my $os = EMPTY_STRING;
5874 0 ▶ return $os unless ( $OSNAME =~ /win32|dos/i ); # is it a MS box?
Branch: 1 branch
BranchTrueFalseTotal
unless $Perl::Tidy::OSNAME =~ /win32|dos/i 0 0 0
5875  
5876 # Systems built from Perl source may not have Win32.pm
5877 # But probably have Win32::GetOSVersion() anyway so the
5878 # following line is not 'required':
5879 # return $os unless eval('require Win32');
5880  
5881 # Use the standard API call to determine the version
5882 0 my ( $undef, $major, $minor, $build, $id );
5883 0 my $ok = eval {
5884 0 ( $undef, $major, $minor, $build, $id ) = Win32::GetOSVersion();
5885 0 1;
5886 };
5887 0 ▶ if ( !$ok && DEVEL_MODE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and 0) 0 0 0
Condition0%0/2
not $ok and 0
!ll
00
MC/DC0%0/1
not $ok and 0
not $ok
Truth table0%0/2
not(A)
A= not $ok
Aresult
00
11
5888 0 Fault("Could not cal Win32::GetOSVersion(): $EVAL_ERROR\n");
5889 }
5890  
5891 #
5892 # NAME ID MAJOR MINOR
5893 # Windows NT 4 2 4 0
5894 # Windows 2000 2 5 0
5895 # Windows XP 2 5 1
5896 # Windows Server 2003 2 5 2
5897  
5898 0 ▶ return "win32s" unless ($id); # If id==0 then its a win32s box.
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
5899 $os = { # Magic numbers from MSDN
5900 # documentation of GetOSVersion
5901 1 => {
5902 0 => "95",
5903 10 => "98",
5904 90 => "Me",
5905 },
5906 2 => {
5907 0 => "2000", # or NT 4, see below
5908 1 => "XP/.Net",
5909 2 => "Win2003",
5910 51 => "NT3.51",
5911 },
5912 0 }->{$id}->{$minor};
5913  
5914 # If $os is undefined, the above code is out of date. Suggested updates
5915 # are welcome.
5916 0 ▶ if ( !defined($os) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $os) 0 0 0
5917 0 $os = EMPTY_STRING;
5918  
5919 # Deactivated this message 20180322 because it was needlessly
5920 # causing some test scripts to fail. Need help from someone
5921 # with expertise in Windows to decide what is possible with windows.
5922 0 ${$rpending_complaint} .= <<EOS if (0);
5923 Error trying to discover Win_OS_Type: $id:$major:$minor Has no name of record!
5924 We won't be able to look for a system-wide config file.
5925 EOS
5926 }
5927  
5928 # Unfortunately the logic used for the various versions isn't so clever..
5929 # so we have to handle an outside case.
5930 0 ▶ return ( $os eq "2000" && $major != 5 ) ? "NT4" : $os;
Branch: 1 branch
BranchTrueFalseTotal
$os eq '2000' && $major != 5 ? : 0 0 0
Condition0%0/3
$os eq 2000 and $major != 5
!ll&&!rl&&r
000
MC/DC0%0/2
$os eq 2000 and $major != 5
$os eq 2000 $major != 5
Truth table0%0/3
A and B
A= $os eq 2000B= $major != 5
ABresult
0X0
100
111
5931 } ## end sub Win_OS_Type
5932  
5933 664 ▶sub look_for_Windows {
Subroutine look_for_Windows: called CC=2 SCAR=7.0
Pod undocumented
5934  
5935 664 my $rpending_complaint = shift;
5936  
5937 # Determine Windows sub-type and location of
5938 # system-wide configuration files
5939 664 my $is_Windows = ( $OSNAME =~ /win32|dos/i );
5940 664 my $Windows_type;
5941 664 ▶ $Windows_type = Win_OS_Type($rpending_complaint) if ($is_Windows);
Branch: 1 branch
BranchTrueFalseTotal
if $is_Windows 0 664 664
5942 664 return ( $is_Windows, $Windows_type );
5943 } ## end sub look_for_Windows
5944  
5945 0 ▶sub find_config_file {
Subroutine find_config_file: not called CC=25 SCAR=64.8
Pod undocumented
5946  
5947 0 my ( $is_Windows, $Windows_type, $rconfig_file_chatter,
5948 $rpending_complaint )
5949 = @_;
5950  
5951 # Look for a .perltidyrc configuration file
5952 # For Windows also look for a file named perltidy.ini
5953  
5954 0 ${$rconfig_file_chatter} .= "# Config file search...system reported as:";
5955 0 ▶ if ($is_Windows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_Windows) { } 0 0 0
5956 0 ${$rconfig_file_chatter} .= "Windows $Windows_type\n";
5957 }
5958 else {
5959 0 ${$rconfig_file_chatter} .= " $OSNAME\n";
5960 }
5961  
5962 # sub to check file existence and record all tests
5963 0 ▶ my $exists_config_file = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
5964 0 my $config_file = shift;
5965 0 ▶ return 0 unless ( defined($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config_file 0 0 0
5966 0 ${$rconfig_file_chatter} .= "# Testing: $config_file\n";
5967 0 return -f $config_file;
5968 0 }; ## end $exists_config_file = sub
5969  
5970 # Sub to search upward for config file
5971 0 ▶ my $resolve_config_file = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
5972  
5973 # resolve <dir>/.../<file>, meaning look upwards from directory
5974 0 my $config_file = shift;
5975 0 ▶ if ( defined($config_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_file) 0 0 0
5976 0 ▶ if ( my ( $start_dir, $search_file ) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = $config_file =~ m[^(.*)\.\.\./(.*)$]) 0 0 0
5977 ( $config_file =~ m{^(.*)\.\.\./(.*)$} ) )
5978 {
5979 0 ${$rconfig_file_chatter} .=
5980 "# Searching Upward: $config_file\n";
5981 0 ▶ $start_dir = '.' if ( !$start_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless $start_dir 0 0 0
5982 0 $start_dir = Cwd::realpath($start_dir);
5983 0 my $found_file = find_file_upwards( $start_dir, $search_file );
5984 0 ▶ if ( defined($found_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found_file) 0 0 0
5985 0 $config_file = $found_file;
5986 0 ${$rconfig_file_chatter} .= "# Found: $config_file\n";
5987 }
5988 }
5989 }
5990 0 return $config_file;
5991 0 }; ## end $resolve_config_file = sub
5992  
5993 0 my $config_file;
5994  
5995 # look in current directory first
5996 0 $config_file = ".perltidyrc";
5997 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
5998 0 ▶ if ($is_Windows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_Windows) 0 0 0
5999 0 $config_file = "perltidy.ini";
6000 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6001 }
6002  
6003 # Default environment vars.
6004 0 my @envs = qw( PERLTIDY HOME );
6005  
6006 # Check the NT/2k/XP locations, first a local machine def, then a
6007 # network def
6008 0 ▶ push @envs, qw( USERPROFILE HOMESHARE ) if ( $OSNAME =~ /win32/i );
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::Tidy::OSNAME =~ /win32/i 0 0 0
6009  
6010 # Now go through the environment ...
6011 0 foreach my $var (@envs) {
6012 0 ${$rconfig_file_chatter} .= "# Examining: \$ENV{$var}";
6013 0 ▶ if ( defined( $ENV{$var} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{$var}) { } 0 0 0
6014 0 ${$rconfig_file_chatter} .= " = $ENV{$var}\n";
6015  
6016 # test ENV{ PERLTIDY } as file:
6017 0 ▶ if ( $var eq 'PERLTIDY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var eq "PERLTIDY") 0 0 0
6018 0 $config_file = "$ENV{$var}";
6019 0 $config_file = $resolve_config_file->($config_file);
6020 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6021 }
6022  
6023 # test ENV as directory:
6024 0 $config_file = File::Spec->catfile( $ENV{$var}, ".perltidyrc" );
6025 0 $config_file = $resolve_config_file->($config_file);
6026 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6027  
6028 0 ▶ if ($is_Windows) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_Windows) 0 0 0
6029 $config_file =
6030 0 File::Spec->catfile( $ENV{$var}, "perltidy.ini" );
6031 0 $config_file = $resolve_config_file->($config_file);
6032 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6033 }
6034 }
6035 else {
6036 0 ${$rconfig_file_chatter} .= "\n";
6037 }
6038 }
6039  
6040 # then look for a system-wide definition
6041 # where to look varies with OS
6042 0 ▶ if ($is_Windows) {
Branch: 4 branches
BranchTrueFalseTotal
if ($is_Windows) { } 0 0 0
elsif ($Perl::Tidy::OSNAME eq 'OS2') { } 0 0 0
elsif ($Perl::Tidy::OSNAME eq 'MacOS') { } 0 0 0
elsif ($Perl::Tidy::OSNAME eq 'VMS') { } 0 0 0
6043  
6044 0 ▶ if ($Windows_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Windows_type) 0 0 0
6045 0 my ( $os_uu, $system, $allusers ) =
6046 Win_Config_Locs( $rpending_complaint, $Windows_type );
6047  
6048 # Check All Users directory, if there is one.
6049 # i.e. C:\Documents and Settings\User\perltidy.ini
6050 0 ▶ if ($allusers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allusers) 0 0 0
6051  
6052 0 $config_file = File::Spec->catfile( $allusers, ".perltidyrc" );
6053 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6054  
6055 0 $config_file = File::Spec->catfile( $allusers, "perltidy.ini" );
6056 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6057 }
6058  
6059 # Check system directory.
6060 # retain old code in case someone has been able to create
6061 # a file with a leading period.
6062 0 $config_file = File::Spec->catfile( $system, ".perltidyrc" );
6063 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6064  
6065 0 $config_file = File::Spec->catfile( $system, "perltidy.ini" );
6066 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6067 }
6068 }
6069  
6070 # Place to add customization code for other systems
6071 elsif ( $OSNAME eq 'OS2' ) {
6072 }
6073 elsif ( $OSNAME eq 'MacOS' ) {
6074 }
6075 elsif ( $OSNAME eq 'VMS' ) {
6076 }
6077  
6078 # Assume some kind of Unix
6079 else {
6080  
6081 0 $config_file = "/usr/local/etc/perltidyrc";
6082 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6083  
6084 0 $config_file = "/etc/perltidyrc";
6085 0 ▶ return $config_file if ( $exists_config_file->($config_file) );
Branch: 1 branch
BranchTrueFalseTotal
if &$exists_config_file($config_file) 0 0 0
6086 }
6087  
6088 # Couldn't find a config file
6089 0 return;
6090 } ## end sub find_config_file
6091  
6092 0 ▶sub Win_Config_Locs {
Subroutine Win_Config_Locs: not called CC=7 SCAR=40.3
Pod undocumented
6093  
6094 0 my ( $rpending_complaint, $os ) = @_;
6095  
6096 # In scalar context returns the OS name (95 98 ME NT3.51 NT4 2000 XP),
6097 # or undef if its not a win32 OS. In list context returns OS, System
6098 # Directory, and All Users Directory. All Users will be empty on a
6099 # 9x/Me box. Contributed by: Yves Orton.
6100  
6101 0 ▶ if ( !$os ) { $os = Win_OS_Type($rpending_complaint) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($os) 0 0 0
6102  
6103 0 ▶ return unless ($os);
Branch: 1 branch
BranchTrueFalseTotal
unless $os 0 0 0
6104  
6105 0 my $system = EMPTY_STRING;
6106 0 my $allusers = EMPTY_STRING;
6107  
6108 0 ▶ if ( $os =~ /9[58]|Me/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($os =~ /9[58]|Me/) { } 0 0 0
elsif ($os =~ /NT|XP|200?/) { } 0 0 0
6109 0 $system = "C:/Windows";
6110 }
6111 elsif ( $os =~ /NT|XP|200?/ ) {
6112 0 ▶ $system = ( $os =~ /XP/ ) ? "C:/Windows/" : "C:/WinNT/";
Branch: 1 branch
BranchTrueFalseTotal
$os =~ /XP/ ? : 0 0 0
6113 0 ▶ $allusers =
Branch: 1 branch
BranchTrueFalseTotal
$os =~ /NT/ ? : 0 0 0
6114 ( $os =~ /NT/ )
6115 ? "C:/WinNT/profiles/All Users/"
6116 : "C:/Documents and Settings/All Users/";
6117 }
6118 else {
6119  
6120 # This currently would only happen on a win32s computer. I don't have
6121 # one to test, so I am unsure how to proceed. Suggestions welcome!
6122 0 ${$rpending_complaint} .=
6123 "I don't know a sensible place to look for config files on an $os system.\n";
6124 0 return;
6125 }
6126 0 return ( $os, $system, $allusers );
6127 } ## end sub Win_Config_Locs
6128  
6129 0 ▶sub dump_config_file {
Subroutine dump_config_file: not called CC=3 SCAR=24.8
Pod undocumented
6130  
6131 0 my ( $rconfig_string, $config_file, $rconfig_file_chatter ) = @_;
6132  
6133 # do --dump-profile (-dpro)
6134  
6135 0 print {*STDOUT} "${$rconfig_file_chatter}";
6136 0 ▶ if ($rconfig_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rconfig_string) { } 0 0 0
6137 0 my @lines = split /^/, ${$rconfig_string};
6138 0 print {*STDOUT} "# Dump of file: '$config_file'\n";
6139 0 foreach my $line (@lines) { print {*STDOUT} $line }
6140 }
6141 else {
6142 0 print {*STDOUT} "# ...no config file found\n";
6143 }
6144 0 return;
6145 } ## end sub dump_config_file
6146  
6147 656 ▶sub filter_unknown_options {
Subroutine filter_unknown_options: called CC=10 SCAR=23.2
Pod undocumented
6148  
6149 my (
6150 656 $rconfig_string, $roption_category,
6151 $rexpansion, $rconfig_file_chatter
6152 ) = @_;
6153  
6154 # Look through the configuration file for lines beginning with '---' and
6155 # - remove the line if the option is unknown, or
6156 # - remove the extra dash if the option is known
6157 # See git #146 for discussion
6158  
6159 # Given:
6160 # $rconfig_string = string ref to a .perltidyrc configuration file
6161 # $roption_category = ref to hash with long_names as key
6162 # $rexpansion = ref to hash with abbreviations as key
6163 # $rconfig_file_chatter = messages displayed in --dump-profile
6164 #
6165 # Update:
6166 # $rconfig_string and $rconfig_file_chatter
6167  
6168 # quick check to skip most files
6169 656 ▶ if ( ${$rconfig_string} !~ /^\s*---\w/m ) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless (${$rconfig_string;} =~ /^\s*---\w/m) 655 1 656
6170  
6171 1 my $new_config_string;
6172 1 my $change_notices = EMPTY_STRING;
6173 1 my @lines = split /^/, ${$rconfig_string};
6174 1 foreach my $line (@lines) {
6175 4 chomp $line;
6176  
6177 # look for lines beginning with '---'
6178 4 ▶ if ( $line && $line =~ /^\s*---(\w[\w-]*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line and $line =~ /^\s*---(\w[\w-]*)/) 3 1 4
Condition66%2/3
$line and $line =~ /^\s*---(\w[\w-]*)/
!ll&&!rl&&r
013
MC/DC50%1/2
$line and $line =~ /^\s*---(\w[\w-]*)/
$line $line =~ /^\s*---(\w[\w-]*)/
Truth table66%2/3
A and B
A= $lineB= $line =~ /^\s*---(\w[\w-]*)/
ABresult
0X0
100
111
6179 3 my $word = $1;
6180  
6181 # first look for a long name or an abbreviation
6182 3 ▶ my $is_known = $roption_category->{$word} || $rexpansion->{$word};
Condition100%3/3
$roption_category->{$word} || $rexpansion->{$word}
l!l&&r!l&&!r
111
MC/DC100%2/2
$roption_category->{$word} || $rexpansion->{$word}
$roption_category->{$word} $rexpansion->{$word}
Truth table100%3/3
A || B
A= $roption_category->{$word}B= $rexpansion->{$word}
ABresult
1X1
011
000
6183  
6184 # then look for prefix 'no' or 'no-' on a long name
6185 3 ▶ if ( !$is_known && $word =~ s/^no-?// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_known and $word =~ s/^no-?//) 0 3 3
Condition66%2/3
not $is_known and $word =~ s/^no-?//
!ll&&!rl&&r
210
MC/DC0%0/2
not $is_known and $word =~ s/^no-?//
not $is_known $word =~ s/^no-?//
Truth table66%2/3
not(A) and B
A= not $is_knownB= $word =~ s/^no-?//
ABresult
0X0
100
111
6186 0 $is_known = $roption_category->{$word};
6187 }
6188  
6189 3 ▶ if ( !$is_known ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_known) { } 1 2 3
6190 1 $change_notices .= "# removing unknown option line $line\n";
6191 1 next;
6192 }
6193 else {
6194 2 $change_notices .= "# accepting and fixing line $line\n";
6195 2 $line =~ s/-//;
6196 }
6197 }
6198 3 $new_config_string .= $line . "\n";
6199 }
6200  
6201 1 ▶ if ($change_notices) {
Branch: 1 branch
BranchTrueFalseTotal
if ($change_notices) 1 0 1
6202 1 ${$rconfig_file_chatter} .= "# Filter operations:\n" . $change_notices;
6203 1 ${$rconfig_string} = $new_config_string;
6204 }
6205 1 return;
6206 } ## end sub filter_unknown_options
6207  
6208 656 ▶sub read_config_file {
Subroutine read_config_file: called CC=17 SCAR=43.7
Pod undocumented
6209  
6210 656 my ( $rconfig_string, $config_file, $rexpansion ) = @_;
6211  
6212 # Read and process the contents of a perltidyrc command file
6213  
6214 # Given:
6215 # $rconfig_string = ref to the file as a string
6216 # $config_file = name of the file, for error reporting
6217 # $rexpansion = ref to hash of abbreviations; if this config file defines
6218 # any abbreviations they will be added to it
6219  
6220 # Return:
6221 # \@config_list = ref to final parameters and values which will be
6222 # placed in @ARGV for processing by GetOptions
6223 # $death_message = error message returned if a fatal error occurs
6224 656 my @config_list = ();
6225  
6226 # remove side comments and join multiline quotes
6227 656 my ( $rline_hash, $death_message ) =
6228 strip_comments_and_join_quotes( $rconfig_string, $config_file );
6229  
6230 # file is bad if non-empty $death_message is returned
6231 656 ▶ if ($death_message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($death_message) 0 656 656
6232 0 return ( \@config_list, $death_message );
6233 }
6234  
6235 656 my $name = undef;
6236 656 my $opening_brace_line;
6237 656 foreach my $item ( @{$rline_hash} ) {
6238 530 my $line = $item->{line};
6239 530 my $line_no = $item->{line_no};
6240 530 $line =~ s/^\s+//;
6241 530 $line =~ s/\s+$//;
6242 530 ▶ next unless ( length($line) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 530 530
6243  
6244 530 my $body = $line;
6245  
6246 # Look for complete or partial abbreviation definition of the form
6247 # name { body } or name { or name { body
6248 # See rules in perltidy's perldoc page
6249 # Section: Other Controls - Creating a new abbreviation
6250 530 ▶ if ( $line =~ /^(?: (\w+) \s* \{ ) (.*)? $/x ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^(?: (\w+) \s* \{ ) (.*)? $/x) { } 0 530 530
elsif ($line =~ /^{/) { } 0 530 530
elsif ($line =~ /^(.*)?\}$/) { } 0 530 530
6251 0 ( $name, $body ) = ( $1, $2 );
6252  
6253 # Cannot start new abbreviation unless old abbreviation is complete
6254 0 ▶ last if ($opening_brace_line);
Branch: 1 branch
BranchTrueFalseTotal
if $opening_brace_line 0 0 0
6255  
6256 0 ▶ $opening_brace_line = $line_no unless ( $body && $body =~ s/\}$// );
Branch: 1 branch
BranchTrueFalseTotal
unless $body and $body =~ s/\}$// 0 0 0
Condition0%0/3
$body and $body =~ s/\}$//
!ll&&!rl&&r
000
MC/DC0%0/2
$body and $body =~ s/\}$//
$body $body =~ s/\}$//
Truth table0%0/3
A and B
A= $bodyB= $body =~ s/\}$//
ABresult
0X0
100
111
6257  
6258 # handle a new alias definition
6259 0 ▶ if ( $rexpansion->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rexpansion->{$name}) 0 0 0
6260 0 local $LIST_SEPARATOR = ')(';
6261 0 my @names = sort keys %{$rexpansion};
6262 0 $death_message =
6263 "Here is a list of all installed aliases\n(@names)\n"
6264 . "Attempting to redefine alias ($name) in config file $config_file line $INPUT_LINE_NUMBER\n";
6265 0 last;
6266 }
6267 0 $rexpansion->{$name} = [];
6268 }
6269  
6270 # leading opening braces not allowed
6271 elsif ( $line =~ /^{/ ) {
6272 0 $opening_brace_line = undef;
6273 0 $death_message =
6274 "Unexpected '{' at line $line_no in config file '$config_file'\n";
6275 0 last;
6276 }
6277  
6278 # Look for abbreviation closing: body } or }
6279 elsif ( $line =~ /^(.*)?\}$/ ) {
6280 0 $body = $1;
6281 0 ▶ if ($opening_brace_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opening_brace_line) { } 0 0 0
6282 0 $opening_brace_line = undef;
6283 }
6284 else {
6285 0 $death_message =
6286 "Unexpected '}' at line $line_no in config file '$config_file'\n";
6287 0 last;
6288 }
6289 }
6290 else {
6291 ## no abbreviations to untangle
6292 }
6293  
6294 # Now store any parameters
6295 530 ▶ if ($body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body) 530 0 530
6296  
6297 530 my ( $rbody_parts, $msg ) = parse_args($body);
6298 530 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 530 530
6299 0 $death_message = <<EOM;
6300 Error reading file '$config_file' at line number $line_no.
6301 $msg
6302 Please fix this line or use -npro to avoid reading this file
6303 EOM
6304 0 last;
6305 }
6306  
6307 530 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 530 530
6308  
6309 # remove leading dashes if this is an alias
6310 0 foreach ( @{$rbody_parts} ) { s/^\-+//; }
6311 0 push @{ $rexpansion->{$name} }, @{$rbody_parts};
6312 }
6313 else {
6314 530 push @config_list, @{$rbody_parts};
6315 }
6316 }
6317 }
6318  
6319 656 ▶ if ($opening_brace_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opening_brace_line) 0 656 656
6320 0 $death_message =
6321 "Didn't see a '}' to match the '{' at line $opening_brace_line in config file '$config_file'\n";
6322 }
6323 656 return ( \@config_list, $death_message );
6324 } ## end sub read_config_file
6325  
6326 656 ▶sub strip_comments_and_join_quotes {
Subroutine strip_comments_and_join_quotes: called CC=16 SCAR=29.1
Pod undocumented
6327  
6328 656 my ( $rconfig_string, $config_file ) = @_;
6329  
6330 # Tasks:
6331 # 1. Strip comments from .perltidyrc lines
6332 # 2. Join lines which are spanned by a quote
6333  
6334 # Given:
6335 # $rconfig_string = the configuration file
6336 # $config_file = filename, for error messages
6337 # Return:
6338 # $rline_hash = hash with modified lines and their input numbers
6339 # $msg = any error message; code will die on any message.
6340  
6341 # return variables
6342 656 my $msg = EMPTY_STRING;
6343 656 my $rline_hash = [];
6344  
6345 # quote state variables
6346 656 my $quote_char = EMPTY_STRING;
6347 656 my $quote_start_line = EMPTY_STRING;
6348 656 my $quote_start_line_no = -1;
6349 656 my $in_string = EMPTY_STRING;
6350 656 my $out_string = EMPTY_STRING;
6351  
6352 656 my @lines = split /^/, ${$rconfig_string};
6353 656 my $line_no = 0;
6354  
6355 # loop over lines
6356 656 foreach my $line (@lines) {
6357 572 $line_no++;
6358 572 $line =~ s/^\s+//;
6359 572 $line =~ s/\s+$//;
6360 572 ▶ next unless ( length($line) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 6 566 572
6361  
6362 566 ▶ if ( !$quote_char ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $quote_char) { } 566 0 566
6363  
6364 # skip a full-line comment
6365 566 ▶ if ( substr( $line, 0, 1 ) eq '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, 0, 1) eq "#") 36 530 566
6366 36 next;
6367 }
6368 530 $in_string = $line;
6369 530 $out_string = EMPTY_STRING;
6370 }
6371 else {
6372  
6373 # treat previous newline as a space
6374 0 $in_string = SPACE . $line;
6375 }
6376  
6377 # loop over string characters
6378 # $in_string = the input string
6379 # $out_string = the output string
6380 # $quote_char = quote character being sought
6381 530 while (1) {
6382  
6383 # accumulating characters not in quote
6384 1368 ▶ if ( !$quote_char ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $quote_char) { } 1182 186 1368
6385  
6386 1182 ▶ if ( $in_string =~ /\G([\"\'])/gc ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($in_string =~ /\G([\"\'])/cg) { } 93 1089 1182
elsif ($in_string =~ /\G#/cg) { } 32 1057 1089
elsif ($in_string =~ /\G([^\#\'\"]+)/cg) { } 559 498 1057
6387  
6388 # starting new quote..
6389 93 $out_string .= $1;
6390 93 $quote_char = $1;
6391 93 $quote_start_line_no = $line_no;
6392 93 $quote_start_line = $line;
6393 }
6394 elsif ( $in_string =~ /\G#/gc ) {
6395  
6396 # A space is required before the # of a side comment
6397 # This allows something like:
6398 # -sbcp=#
6399 # Otherwise, it would have to be quoted:
6400 # -sbcp='#'
6401 32 ▶ if ( !length($out_string) || $out_string =~ s/\s+$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $out_string or $out_string =~ s/\s+$//) 32 0 32
Condition33%1/3
not length $out_string or $out_string =~ s/\s+$//
l!l&&r!l&&!r
0320
MC/DC0%0/2
not length $out_string or $out_string =~ s/\s+$//
not length $out_string $out_string =~ s/\s+$//
Truth table33%1/3
not(A) or B
A= not length $out_stringB= $out_string =~ s/\s+$//
ABresult
1X1
011
000
6402 32 last;
6403 }
6404 0 $out_string .= '#';
6405 }
6406 elsif ( $in_string =~ /\G([^\#\'\"]+)/gc ) {
6407  
6408 # neither quote nor side comment
6409 559 $out_string .= $1;
6410 }
6411 else {
6412  
6413 # end of line
6414 498 last;
6415 }
6416 }
6417  
6418 # looking for ending quote character
6419 else {
6420 186 ▶ if ( $in_string =~ /\G($quote_char)/gc ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($in_string =~ /\G($quote_char)/cg) { } 93 93 186
elsif ($in_string =~ /\G([^$quote_char]+)/cg) { } 93 0 93
6421  
6422 # end of quote
6423 93 $out_string .= $1;
6424 93 $quote_char = EMPTY_STRING;
6425 }
6426 elsif ( $in_string =~ /\G([^$quote_char]+)/gc ) {
6427  
6428 # accumulate quoted text
6429 93 $out_string .= $1;
6430 }
6431 else {
6432  
6433 # end of line
6434 0 last;
6435 }
6436 }
6437 } ## end while (1)
6438  
6439 530 ▶ if ( !$quote_char ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quote_char) 530 0 530
6440 530 push @{$rline_hash},
6441 {
6442 line => $out_string,
6443 line_no => $line_no,
6444 };
6445 }
6446  
6447 } ## end loop over lines
6448  
6449 656 ▶ if ($quote_char) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote_char) 0 656 656
6450 0 my $max_len = 80;
6451 0 ▶ if ( length($quote_start_line) > $max_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $quote_start_line > $max_len) 0 0 0
6452 0 $quote_start_line =
6453 substr( $quote_start_line, 0, $max_len - 3 ) . '...';
6454 }
6455 0 $msg = <<EOM;
6456 Error: hit EOF reading file '$config_file' looking for end of quoted text
6457 which started at line $quote_start_line_no with quote character <$quote_char>:
6458 $quote_start_line
6459 Please fix or use -npro to avoid reading this file
6460 EOM
6461 }
6462 656 return ( $rline_hash, $msg );
6463 } ## end sub strip_comments_and_join_quotes
6464  
6465 1194 ▶sub parse_args {
Subroutine parse_args: called CC=11 SCAR=25.0
Pod undocumented
6466  
6467 1194 my ($body) = @_;
6468  
6469 # Parse a command string $body containing multiple string with possible
6470 # quotes, into individual commands. It might look like this, for example:
6471 #
6472 # -wba=" + - " -some-thing -wbb='. && ||'
6473 #
6474 # There is no need, at present, to handle escaped quote characters.
6475 # (They are not perltidy tokens, so needn't be in strings).
6476  
6477 1194 my @body_parts = ();
6478 1194 my $quote_char = EMPTY_STRING;
6479 1194 my $part = EMPTY_STRING;
6480 1194 my $msg = EMPTY_STRING;
6481  
6482 # Check for external call with undefined $body - added to fix
6483 # github issue Perl-Tidy-Sweetened issue #23
6484 0 ▶ if ( !defined($body) ) { $body = EMPTY_STRING }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 1194 1194
6485  
6486 1194 while (1) {
6487  
6488 # looking for ending quote character
6489 7365 ▶ if ($quote_char) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote_char) { } 726 6639 7365
6490 726 ▶ if ( $body =~ /\G($quote_char)/gc ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($body =~ /\G($quote_char)/cg) { } 93 633 726
elsif ($body =~ /\G(.)/cg) { } 633 0 633
6491 93 $quote_char = EMPTY_STRING;
6492 }
6493 elsif ( $body =~ /\G(.)/gc ) {
6494 633 $part .= $1;
6495 }
6496  
6497 # error..we reached the end without seeing the ending quote char
6498 else {
6499 0 ▶ if ( length($part) ) { push @body_parts, $part; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $part) 0 0 0
6500 0 $msg = <<EOM;
6501 Did not see ending quote character <$quote_char> in this text:
6502 $body
6503 EOM
6504 0 last;
6505 }
6506 }
6507  
6508 # accumulating characters and looking for start of a quoted string
6509 else {
6510 6639 ▶ if ( $body =~ /\G([\"\'])/gc ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($body =~ /\G([\"\'])/cg) { } 93 6546 6639
elsif ($body =~ /\G(\s+)/cg) { } 182 6364 6546
elsif ($body =~ /\G(.)/cg) { } 5170 1194 6364
6511 93 $quote_char = $1;
6512 }
6513 elsif ( $body =~ /\G(\s+)/gc ) {
6514 182 ▶ if ( length($part) ) { push @body_parts, $part; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $part) 182 0 182
6515 182 $part = EMPTY_STRING;
6516 }
6517 elsif ( $body =~ /\G(.)/gc ) {
6518 5170 $part .= $1;
6519 }
6520 else {
6521 1194 ▶ if ( length($part) ) { push @body_parts, $part; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $part) 542 652 1194
6522 1194 last;
6523 }
6524 }
6525 } ## end while (1)
6526 1194 return ( \@body_parts, $msg );
6527 } ## end sub parse_args
6528  
6529 0 ▶sub dump_long_names {
Subroutine dump_long_names: not called CC=2 SCAR=17.9
Pod undocumented
6530  
6531 0 my @names = @_;
6532  
6533 # do --dump-long-names (-dln)
6534  
6535 0 print {*STDOUT} <<EOM;
6536 # Command line long names (passed to GetOptions)
6537 #--------------------------------------------------
6538 # here is a summary of the Getopt codes:
6539 # <none> does not take an argument
6540 # =s takes a mandatory string
6541 # :s takes an optional string
6542 # =i takes a mandatory integer
6543 # :i takes an optional integer
6544 # ! does not take an argument and may be negated
6545 # i.e., -foo and -nofoo are allowed
6546 # a double dash signals the end of the options list
6547 #
6548 #--------------------------------------------------
6549 EOM
6550  
6551 0 foreach my $name ( sort @names ) {
6552 0 print {*STDOUT} "$name\n";
6553 }
6554 0 return;
6555 } ## end sub dump_long_names
6556  
6557 0 ▶sub dump_integer_option_range {
Subroutine dump_integer_option_range: not called CC=4 SCAR=30.0
Pod undocumented
6558  
6559 0 my ($rinteger_option_range) = @_;
6560  
6561 # do --dump-integer-option-range (-dior)
6562  
6563 0 print {*STDOUT} "Option, min, max, default\n";
6564 0 foreach my $key ( sort keys %{$rinteger_option_range} ) {
6565 0 my ( $min, $max, $default ) = @{ $rinteger_option_range->{$key} };
6566 0 foreach ( $min, $max, $default ) {
6567 0 ▶ $_ = 'undef' unless ( defined($_) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
6568 }
6569 0 print {*STDOUT} "$key, $min, $max, $default\n";
6570 }
6571 0 return;
6572 } ## end sub dump_integer_option_range
6573  
6574 0 ▶sub dump_defaults {
Subroutine dump_defaults: not called CC=2 SCAR=17.9
Pod undocumented
6575  
6576 0 my @defaults = @_;
6577  
6578 # do --dump-defaults (-ddf)
6579 0 print {*STDOUT} "Default command line options:\n";
6580 0 foreach my $line ( sort @defaults ) { print {*STDOUT} "$line\n" }
6581 0 return;
6582 } ## end sub dump_defaults
6583  
6584 664 ▶sub readable_options {
Subroutine readable_options: called CC=10 SCAR=23.0
Pod undocumented
6585  
6586 664 my ( $rOpts, $roption_string ) = @_;
6587  
6588 # return options for this run as a string which could be
6589 # put in a perltidyrc file
6590 664 my %Getopt_flags;
6591 664 my $rGetopt_flags = \%Getopt_flags;
6592 664 my $readable_options = "# Final parameter set for this run.\n";
6593 664 $readable_options .=
6594 "# See utility 'perltidyrc_dump.pl' for nicer formatting.\n";
6595 664 foreach my $opt ( @{$roption_string} ) {
6596 270912 my $flag = EMPTY_STRING;
6597 270912 ▶ if ( $opt =~ /(.*)(!|=.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt =~ /(.*)(!|=.*)$/) 261490 9422 270912
6598 261490 $opt = $1;
6599 261490 $flag = $2;
6600 }
6601 270912 ▶ if ( defined( $rOpts->{$opt} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rOpts->{$opt}) 94011 176901 270912
6602 94011 $rGetopt_flags->{$opt} = $flag;
6603 }
6604 }
6605 664 foreach my $key ( sort keys %{$rOpts} ) {
6606 94016 my $flag = $rGetopt_flags->{$key};
6607 94016 my $value = $rOpts->{$key};
6608 94016 my $prefix = '--';
6609 94016 my $suffix = EMPTY_STRING;
6610 94016 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 93711 305 94016
6611 93711 ▶ if ( $flag =~ /^=/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($flag =~ /^=/) { } 49123 44588 93711
elsif ($flag =~ /^!/) { } 44588 0 44588
6612 49123 ▶ if ( $value !~ /^\d+$/ ) { $value = '"' . $value . '"' }
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^\d+$/) 3431 45692 49123
6613 49123 $suffix = "=" . $value;
6614 }
6615 elsif ( $flag =~ /^!/ ) {
6616 44588 ▶ $prefix .= "no" unless ($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $value 10058 34530 44588
6617 }
6618 else {
6619  
6620 # shouldn't happen
6621 0 $readable_options .=
6622 "# ERROR in dump_options: unrecognized flag $flag for $key\n";
6623 }
6624 }
6625 94016 $readable_options .= $prefix . $key . $suffix . "\n";
6626 }
6627 664 return $readable_options;
6628 } ## end sub readable_options
6629  
6630 0 ▶sub show_version {
Subroutine show_version: not called CC=1 SCAR=6.9
Pod undocumented
6631 0 print {*STDOUT} <<"EOM";
6632 This is perltidy, v$VERSION
6633  
6634 Copyright 2000-2026 by Steve Hancock
6635  
6636 Perltidy is free software and may be copied under the terms of the GNU
6637 General Public License, which is included in the distribution files.
6638  
6639 Documentation can be found using 'man perltidy'
6640 or at GitHub https://perltidy.github.io/perltidy/
6641 or at metacpan https://metacpan.org/pod/distribution/Perl-Tidy/bin/perltidy
6642 or at Sourceforge https://perltidy.sourceforge.net
6643 EOM
6644 0 return;
6645 } ## end sub show_version
6646  
6647 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
Pod undocumented
6648  
6649 # Dump brief usage message if arg is -help or -h, or on certain errors
6650  
6651 0 print {*STDOUT} <<EOF;
6652 This is perltidy version $VERSION, a perl script indenter. Usage:
6653  
6654 perltidy [ options ] file1 file2 file3 ...
6655 (output goes to file1.tdy, file2.tdy, file3.tdy, ...)
6656 perltidy [ options ] file1 -o outfile
6657 perltidy [ options ] file1 -st >outfile
6658 perltidy [ options ] <infile >outfile
6659  
6660 Options have short and long forms. Short forms are shown; see
6661 man pages for long forms. Note: '=s' indicates a required string,
6662 and '=n' indicates a required integer.
6663  
6664 I/O control
6665 -h show this help
6666 -o=file name of the output file (only if single input file)
6667 -oext=s change output extension from 'tdy' to s
6668 -opath=path change path to be 'path' for output files
6669 -b backup original to .bak and modify file in-place
6670 -bext=s change default backup extension from 'bak' to s
6671 -q deactivate error messages (for running under editor)
6672 -w include non-critical warning messages in the .ERR error output
6673 -log save .LOG file, which has useful diagnostics
6674 -f force perltidy to read a binary file
6675 -g like -log but writes more detailed .LOG file, for debugging scripts
6676 -opt write the set of options actually used to a .LOG file
6677 -npro ignore .perltidyrc configuration command file
6678 -pro=file read configuration commands from file instead of .perltidyrc
6679 -st send output to standard output, STDOUT
6680 -se send all error output to standard error output, STDERR
6681 -v display version number to standard output and quit
6682  
6683 Basic Options:
6684 -i=n use n columns per indentation level (default n=4)
6685 -t tabs: use one tab character per indentation level, not recommended
6686 -nt no tabs: use n spaces per indentation level (default)
6687 -et=n entab leading whitespace n spaces per tab; not recommended
6688 -io "indent only": just do indentation, no other formatting.
6689 -sil=n set starting indentation level to n; use if auto detection fails
6690 -ole=s specify output line ending (s=dos or win, mac, unix)
6691 -ple keep output line endings same as input (input must be filename)
6692  
6693 Whitespace Control
6694 -fws freeze whitespace; this disables all whitespace changes
6695 and disables the following switches:
6696 -bt=n sets brace tightness, n= (0 = loose, 1=default, 2 = tight)
6697 -bbt same as -bt but for code block braces; same as -bt if not given
6698 -bbvt block braces vertically tight; use with -bl or -bli
6699 -bbvtl=s make -bbvt to apply to selected list of block types
6700 -pt=n paren tightness (n=0, 1 or 2)
6701 -sbt=n square bracket tightness (n=0, 1, or 2)
6702 -bvt=n brace vertical tightness,
6703 n=(0=open, 1=close unless multiple steps on a line, 2=always close)
6704 -pvt=n paren vertical tightness (see -bvt for n)
6705 -sbvt=n square bracket vertical tightness (see -bvt for n)
6706 -bvtc=n closing brace vertical tightness:
6707 n=(0=open, 1=sometimes close, 2=always close)
6708 -pvtc=n closing paren vertical tightness, see -bvtc for n.
6709 -sbvtc=n closing square bracket vertical tightness, see -bvtc for n.
6710 -ci=n sets continuation indentation=n, default is n=2 spaces
6711 -lp line up parentheses, brackets, and non-BLOCK braces
6712 -sfs add space before semicolon in for( ; ; )
6713 -aws allow perltidy to add whitespace (default)
6714 -dws delete all old non-essential whitespace
6715 -icb indent closing brace of a code block
6716 -cti=n closing indentation of paren, square bracket, or non-block brace:
6717 n=0 none, =1 align with opening, =2 one full indentation level
6718 -icp equivalent to -cti=2
6719 -wls=s want space left of tokens in string; i.e. -nwls='+ - * /'
6720 -wrs=s want space right of tokens in string;
6721 -sts put space before terminal semicolon of a statement
6722 -sak=s put space between keywords given in s and '(';
6723 -nsak=s no space between keywords in s and '('; i.e. -nsak='my our local'
6724  
6725 Line Break Control
6726 -fnl freeze newlines; this disables all line break changes
6727 and disables the following switches:
6728 -anl add newlines; ok to introduce new line breaks
6729 -bbs add blank line before subs and packages
6730 -bbc add blank line before block comments
6731 -bbb add blank line between major blocks
6732 -kbl=n keep old blank lines? 0=no, 1=some, 2=all
6733 -mbl=n maximum consecutive blank lines to output (default=1)
6734 -ce cuddled else; use this style: '} else {'
6735 -cb cuddled blocks (other than 'if-elsif-else')
6736 -cbl=s list of blocks to cuddled, default 'try-catch-finally'
6737 -dnl delete old newlines (default)
6738 -l=n maximum line length; default n=80
6739 -bl opening brace on new line
6740 -sbl opening sub brace on new line. value of -bl is used if not given.
6741 -bli opening brace on new line and indented
6742 -bar opening brace always on right, even for long clauses
6743 -vt=n vertical tightness (requires -lp); n controls break after opening
6744 token: 0=never 1=no break if next line balanced 2=no break
6745 -vtc=n vertical tightness of closing container; n controls if closing
6746 token starts new line: 0=always 1=not unless list 1=never
6747 -wba=s want break after tokens in string; i.e. wba=': .'
6748 -wbb=s want break before tokens in string
6749 -wn weld nested: combines opening and closing tokens when both are adjacent
6750 -wnxl=s weld nested exclusion list: provides some control over the types of
6751 containers which can be welded
6752  
6753 Following Old Breakpoints
6754 -kis keep interior semicolons. Allows multiple statements per line.
6755 -boc break at old comma breaks: turns off all automatic list formatting
6756 -bol break at old logical breakpoints: or, and, ||, && (default)
6757 -bom break at old method call breakpoints: ->
6758 -bok break at old list keyword breakpoints such as map, sort (default)
6759 -bot break at old conditional (ternary ?:) operator breakpoints (default)
6760 -boa break at old attribute breakpoints
6761 -cab=n break at commas after a comma-arrow (=>):
6762 n=0 break at all commas after =>
6763 n=1 stable: break unless this breaks an existing one-line container
6764 n=2 break only if a one-line container cannot be formed
6765 n=3 do not treat commas after => specially at all
6766  
6767 Comment controls
6768 -ibc indent block comments (default)
6769 -isbc indent spaced block comments; may indent unless no leading space
6770 -msc=n minimum desired spaces to side comment, default 4
6771 -fpsc=n fix position for side comments; default 0;
6772 -csc add or update closing side comments after closing BLOCK brace
6773 -dcsc delete closing side comments created by a -csc command
6774 -cscp=s change closing side comment prefix to be other than '## end'
6775 -cscl=s change closing side comment to apply to selected list of blocks
6776 -csci=n minimum number of lines needed to apply a -csc tag, default n=6
6777 -csct=n maximum number of columns of appended text, default n=20
6778 -cscw causes warning if old side comment is overwritten with -csc
6779  
6780 -sbc use 'static block comments' identified by leading '##' (default)
6781 -sbcp=s change static block comment identifier to be other than '##'
6782 -osbc outdent static block comments
6783  
6784 -ssc use 'static side comments' identified by leading '##' (default)
6785 -sscp=s change static side comment identifier to be other than '##'
6786  
6787 Delete selected text
6788 -dac delete all comments AND pod
6789 -dbc delete block comments
6790 -dsc delete side comments
6791 -dp delete pod
6792  
6793 Send selected text to a '.TEE' file
6794 -tac tee all comments AND pod
6795 -tbc tee block comments
6796 -tsc tee side comments
6797 -tp tee pod
6798  
6799 Outdenting
6800 -olq outdent long quoted strings (default)
6801 -olc outdent a long block comment line
6802 -ola outdent statement labels
6803 -okw outdent control keywords (redo, next, last, goto, return)
6804 -okwl=s specify alternative keywords for -okw command
6805  
6806 Other controls
6807 -mft=n maximum fields per table; default n=0 (no limit)
6808 -x do not format lines before hash-bang line (i.e., for VMS)
6809 -asc allows perltidy to add a ';' when missing (default)
6810 -dsm allows perltidy to delete an unnecessary ';' (default)
6811  
6812 Combinations of other parameters
6813 -gnu attempt to follow GNU Coding Standards as applied to perl
6814 -mangle remove as many newlines as possible (but keep comments and pods)
6815 -extrude insert as many newlines as possible
6816  
6817 Dump and die, debugging
6818 -dop dump options used in this run to standard output and quit
6819 -ddf dump default options to standard output and quit
6820 -dsn dump all option short names to standard output and quit
6821 -dln dump option long names to standard output and quit
6822 -dpro dump whatever configuration file is in effect to standard output
6823 -dtt dump all token types to standard output and quit
6824  
6825 HTML
6826 -html write an html file (see 'man perl2web' for many options)
6827 Note: when -html is used, no indentation or formatting are done.
6828 Hint: try perltidy -html -css=mystyle.css filename.pl
6829 and edit mystyle.css to change the appearance of filename.html.
6830 -nnn gives line numbers
6831 -pre only writes out <pre>..</pre> code section
6832 -toc places a table of contents to subs at the top (default)
6833 -pod passes pod text through pod2html (default)
6834 -frm write html as a frame (3 files)
6835 -text=s extra extension for table of contents if -frm, default='toc'
6836 -sext=s extra extension for file content if -frm, default='src'
6837  
6838 A prefix of "n" negates short form toggle switches, and a prefix of "no"
6839 negates the long forms. For example, -nasc means don't add missing
6840 semicolons.
6841  
6842 If you are unable to see this entire text, try "perltidy -h | more"
6843 For more detailed information, and additional options, try "man perltidy",
6844 or see https://metacpan.org/pod/distribution/Perl-Tidy/bin/perltidy
6845 EOF
6846  
6847 0 return;
6848 } ## end sub usage
6849  
6850 45 1;
« Tidy/HtmlWriter.pm ↑ Summary Tidy/Tokenizer.pm »