From 1a320bd739c30d9d25481ca5b54b04c27dda4223 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Mon, 16 Oct 2023 06:37:22 -0700 Subject: [PATCH] restructure to eliminate a global variable --- lib/Perl/Tidy/FileWriter.pm | 48 +++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/Perl/Tidy/FileWriter.pm b/lib/Perl/Tidy/FileWriter.pm index f07c669d..29e5f27c 100644 --- a/lib/Perl/Tidy/FileWriter.pm +++ b/lib/Perl/Tidy/FileWriter.pm @@ -1,6 +1,15 @@ ##################################################################### # -# the Perl::Tidy::FileWriter class writes the output file +# The Perl::Tidy::FileWriter class writes the output file created +# by the formatter. It receives each output line and performs some +# important monitoring services. These include: +# +# - Verifying that lines do not go out with tokens in the wrong order +# - Checking for obvious iteration convergence when all output tokens +# match all input tokens +# - Keeping track of consecutive blank and non-blank lines +# - Looking for line lengths which exceed the maximum requested length +# - Reporting results to the log file # ##################################################################### @@ -12,6 +21,12 @@ our $VERSION = '20230912.03'; use constant DEVEL_MODE => 0; use constant EMPTY_STRING => q{}; +# A limit on message length when a fault is detected +use constant LONG_MESSAGE => 256; + +# Maximum number of little messages; probably need not be changed. +use constant MAX_NAG_MESSAGES => 6; + sub AUTOLOAD { # Catch any undefined sub calls so that we are sure to get @@ -38,11 +53,6 @@ sub DESTROY { # required to avoid call to AUTOLOAD in some versions of perl } -my $input_stream_name = EMPTY_STRING; - -# Maximum number of little messages; probably need not be changed. -use constant MAX_NAG_MESSAGES => 6; - BEGIN { # Array index names for variables. @@ -70,6 +80,7 @@ BEGIN { _K_last_arrival_ => $i++, _save_logfile_ => $i++, _routput_string_ => $i++, + _input_stream_name_ => $i++, }; } ## end BEGIN @@ -80,7 +91,7 @@ sub Die { } sub Fault { - my ($msg) = @_; + my ( $self, $msg ) = @_; # This routine is called for errors that really should not occur # except if there has been a bug introduced by a recent program change. @@ -91,6 +102,18 @@ sub Fault { my ( $package2, $filename2, $line2, $subroutine2 ) = caller(2); my $pkg = __PACKAGE__; + # Catch potential error of Fault not called as a method + my $input_stream_name; + if ( !ref($self) ) { + $input_stream_name = "(UNKNOWN)"; + $msg = "Fault not called as a method - please fix\n"; + if ( $self && length($self) < LONG_MESSAGE ) { $msg .= $self } + $self = undef; + } + else { + $input_stream_name = $self->[_input_stream_name_]; + } + Die(<Fault("FileWriter expects line_sink_object to be a ref\n"); } elsif ( $ref eq 'SCALAR' ) { $self->[_routput_string_] = $line_sink_object; @@ -160,17 +183,17 @@ sub new { else { my $str = $ref; if ( length($str) > 63 ) { $str = substr( $str, 0, 60 ) . '...' } - Fault(<Fault(<get_input_stream_name(); } + $self->[_input_stream_name_] = $input_stream_name; bless $self, $class; return $self; @@ -340,7 +363,6 @@ sub write_code_line { } my $msg = <Fault($msg) } # Otherwise warn if string is not empty (added for b1378) $self->warning($msg) if ( length($str) ); -- 2.39.5