]> git.donarmstrong.com Git - perltidy.git/blob - examples/delete_ending_blank_lines.pl
New upstream version 20190601
[perltidy.git] / examples / delete_ending_blank_lines.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 # Example script for removing trailing blank lines of code from a perl script
5 # This is from the examples/ directory of the perltidy distribution and may
6 # be modified as needed. 
7
8 # This was written in response to RT #118553, "leave only one newline at end of file".
9 # Adding the requested feature to perltidy itself would have very undesirable
10 # side-effects when perltidy is operated from within an editor. So it is best
11 # done with a separate filter script on entire files.
12
13 # usage:
14 # delete_ending_blank_lines.pl myfile.pl >myfile.new
15 # delete_ending_blank_lines.pl <myfile.pl >myfile.new
16 use Getopt::Std;
17 use Perl::Tidy;
18 use IO::File;
19 $| = 1;
20 use vars qw($opt_h);
21 my $usage = <<EOM;
22    usage: $0 filename >outfile
23 EOM
24 getopts('h') or die "$usage";
25 if ($opt_h) { die $usage }
26
27 # Make the source for perltidy, which will be a filehandle
28 # or just '-' if the source is stdin
29 my ($file, $fh, $source);
30 if ( @ARGV == 0 ) {
31     $source = '-';
32 }
33 elsif ( @ARGV == 1 ) {
34     $file = $ARGV[0];
35     $fh = IO::File->new( $file, 'r' );
36     unless ($fh) { die "cannot open '$file': $!\n" }
37     $source = $fh;
38 }
39 else { die $usage }
40
41 # make the callback object
42 my $formatter = MyFormatter->new(); 
43
44 my $dest;
45
46 # start perltidy, which will start calling our write_line()
47 my $err=perltidy(
48     'formatter'   => $formatter,     # callback object
49     'source'      => $source,
50     'destination' => \$dest,         # (not really needed)
51     'argv'        => "-npro -se",    # dont need .perltidyrc
52                                      # errors to STDOUT
53 );
54 if ($err) {
55     die "Error calling perltidy\n";
56 }
57 $fh->close() if $fh;
58
59 package MyFormatter;
60
61 my @lines;
62
63 sub new {
64     my ($class) = @_;
65     bless {}, $class;
66 }
67
68 sub write_line {
69
70     # This is called from perltidy line-by-line; we just save lines
71     my $self              = shift;
72     my $line_of_tokens    = shift;
73     push @lines, $line_of_tokens;
74 }
75
76 # called once after the last line of a file
77 sub finish_formatting {
78     my $self = shift;
79
80     # remove all trailing blank lines of code
81     while (my $line_of_tokens = pop(@lines)) {
82         my $line_type         = $line_of_tokens->{_line_type};
83         my $input_line        = $line_of_tokens->{_line_text};
84         if ( $line_type eq 'CODE' ) {
85             chomp $input_line;
86             next unless ($input_line);
87         }
88         push @lines, $line_of_tokens; 
89         last;
90     }
91
92     # write remaining lines
93     foreach my $line_of_tokens (@lines) {
94         my $line_type         = $line_of_tokens->{_line_type};
95         my $input_line        = $line_of_tokens->{_line_text};
96         print $input_line;
97     }
98     return;
99 }