File Coverage

blib/lib/Perl/Tidy/LineSink.pm
Criterion Covered Total %
statement 31 41 75.6
branch 6 12 50.0
condition 2 3 66.6
subroutine 5 8 62.5
pod 0 4 0.0
total 44 68 64.7


line stmt bran cond sub pod time code
1             #####################################################################
2             #
3             # the Perl::Tidy::LineSink class supplies a write_line method for
4             # actual file writing
5             #
6             #####################################################################
7              
8             package Perl::Tidy::LineSink;
9 38     38   299 use strict;
  38         103  
  38         1210  
10 38     38   247 use warnings;
  38         103  
  38         21125  
11             our $VERSION = '20230701';
12              
13             sub AUTOLOAD {
14              
15             # Catch any undefined sub calls so that we are sure to get
16             # some diagnostic information. This sub should never be called
17             # except for a programming error.
18 0     0   0 our $AUTOLOAD;
19 0 0       0 return if ( $AUTOLOAD =~ /\bDESTROY$/ );
20 0         0 my ( $pkg, $fname, $lno ) = caller();
21 0         0 my $my_package = __PACKAGE__;
22 0         0 print STDERR <<EOM;
23             ======================================================================
24             Error detected in package '$my_package', version $VERSION
25             Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
26             Called from package: '$pkg'
27             Called from File '$fname' at line '$lno'
28             This error is probably due to a recent programming change
29             ======================================================================
30             EOM
31 0         0 exit 1;
32             }
33              
34       0     sub DESTROY {
35              
36             # required to avoid call to AUTOLOAD in some versions of perl
37             }
38              
39             sub new {
40              
41 560     560 0 2689 my ( $class, @args ) = @_;
42              
43 560         3609 my %defaults = (
44             output_file => undef,
45             line_separator => undef,
46             is_encoded_data => undef,
47             );
48 560         3121 my %args = ( %defaults, @args );
49              
50 560         2039 my $output_file = $args{output_file};
51 560         1572 my $line_separator = $args{line_separator};
52 560         1458 my $is_encoded_data = $args{is_encoded_data};
53              
54 560         1348 my $fh = undef;
55              
56 560         1200 my $output_file_open = 0;
57              
58 560         2264 ( $fh, $output_file ) =
59             Perl::Tidy::streamhandle( $output_file, 'w', $is_encoded_data );
60 560 50       3284 unless ($fh) { Perl::Tidy::Die("Cannot write to output stream\n"); }
  0         0  
61 560         1627 $output_file_open = 1;
62              
63 560         5667 return bless {
64             _fh => $fh,
65             _output_file => $output_file,
66             _output_file_open => $output_file_open,
67             _line_separator => $line_separator,
68             _is_encoded_data => $is_encoded_data,
69             }, $class;
70             }
71              
72             sub set_line_separator {
73 0     0 0 0 my ( $self, $val ) = @_;
74 0         0 $self->{_line_separator} = $val;
75 0         0 return;
76             }
77              
78             sub write_line {
79              
80 8511     8511 0 16497 my ( $self, $line ) = @_;
81 8511         15322 my $fh = $self->{_fh};
82              
83 8511         14104 my $line_separator = $self->{_line_separator};
84 8511 50       17675 if ( defined($line_separator) ) {
85 8511         14595 chomp $line;
86 8511         12994 $line .= $line_separator;
87             }
88              
89 8511 50       34939 $fh->print($line) if ( $self->{_output_file_open} );
90              
91 8511         16610 return;
92             }
93              
94             sub close_output_file {
95 560     560 0 1489 my $self = shift;
96              
97             # Only close physical files, not STDOUT and other objects
98 560         1669 my $output_file = $self->{_output_file};
99 560 100 66     3826 if ( $output_file ne '-' && !ref $output_file ) {
100 559 50       3239 $self->{_fh}->close() if $self->{_output_file_open};
101             }
102 560         2490 return;
103             }
104              
105             1;