]> git.donarmstrong.com Git - perltidy.git/blob - lib/Perl/Tidy/LineSink.pm
New upstream version 20221112
[perltidy.git] / lib / Perl / Tidy / LineSink.pm
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 use strict;
10 use warnings;
11 our $VERSION = '20221112';
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     our $AUTOLOAD;
19     return if ( $AUTOLOAD =~ /\bDESTROY$/ );
20     my ( $pkg, $fname, $lno ) = caller();
21     my $my_package = __PACKAGE__;
22     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     exit 1;
32 }
33
34 sub DESTROY {
35
36     # required to avoid call to AUTOLOAD in some versions of perl
37 }
38
39 sub new {
40
41     my ( $class, @args ) = @_;
42
43     my %defaults = (
44         output_file     => undef,
45         line_separator  => undef,
46         is_encoded_data => undef,
47     );
48     my %args = ( %defaults, @args );
49
50     my $output_file     = $args{output_file};
51     my $line_separator  = $args{line_separator};
52     my $is_encoded_data = $args{is_encoded_data};
53
54     my $fh = undef;
55
56     my $output_file_open = 0;
57
58     ( $fh, $output_file ) =
59       Perl::Tidy::streamhandle( $output_file, 'w', $is_encoded_data );
60     unless ($fh) { Perl::Tidy::Die("Cannot write to output stream\n"); }
61     $output_file_open = 1;
62
63     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     my ( $self, $val ) = @_;
74     $self->{_line_separator} = $val;
75     return;
76 }
77
78 sub write_line {
79
80     my ( $self, $line ) = @_;
81     my $fh = $self->{_fh};
82
83     my $line_separator = $self->{_line_separator};
84     if ( defined($line_separator) ) {
85         chomp $line;
86         $line .= $line_separator;
87     }
88
89     $fh->print($line) if ( $self->{_output_file_open} );
90
91     return;
92 }
93
94 sub close_output_file {
95     my $self = shift;
96
97     # Only close physical files, not STDOUT and other objects
98     my $output_file = $self->{_output_file};
99     if ( $output_file ne '-' && !ref $output_file ) {
100         $self->{_fh}->close() if $self->{_output_file_open};
101     }
102     return;
103 }
104
105 1;