From: Steve Hancock Date: Sat, 5 Oct 2024 19:59:57 +0000 (-0700) Subject: simplify several while loops X-Git-Tag: 20240903.04~2 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=23776e54d7d756728e45fc094973fa101522b552;p=perltidy.git simplify several while loops --- diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 375f581c..f996b96f 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -4688,10 +4688,11 @@ EOM # a look at @ARGV. if (@ARGV) { my $count = @ARGV; - my $str = "\'" . pop(@ARGV) . "\'"; - while ( my $param = pop(@ARGV) ) { + my $str = EMPTY_STRING; + foreach my $param (@ARGV) { if ( length($str) < 70 ) { - $str .= ", '$param'"; + if ($str) { $str .= ', ' } + $str .= "'$param'"; } else { $str .= ", ..."; @@ -5645,7 +5646,7 @@ sub dump_config_file { if ($rconfig_string) { my @lines = split /^/, ${$rconfig_string}; print {*STDOUT} "# Dump of file: '$config_file'\n"; - while ( defined( my $line = shift @lines ) ) { print {*STDOUT} $line } + foreach my $line (@lines) { print {*STDOUT} $line } } else { print {*STDOUT} "# ...no config file found\n"; @@ -5858,13 +5859,11 @@ sub strip_comments_and_join_quotes { my $in_string = EMPTY_STRING; my $out_string = EMPTY_STRING; - my @lines = split /^/, ${$rconfig_string}; - my $line_no; + my @lines = split /^/, ${$rconfig_string}; + my $line_no = 0; # loop over lines - while (@lines) { - - my $line = shift @lines; + foreach my $line (@lines) { $line_no++; $line =~ s/^\s+//; $line =~ s/\s+$//; @@ -5902,7 +5901,7 @@ sub strip_comments_and_join_quotes { $quote_start_line_no = $line_no; $quote_start_line = $line; } - elsif ( $in_string =~ /\G(#)/gc ) { + elsif ( $in_string =~ /\G#/gc ) { # A space is required before the # of a side comment # This allows something like: @@ -5912,7 +5911,7 @@ sub strip_comments_and_join_quotes { if ( !length($out_string) || $out_string =~ s/\s+$// ) { last; } - $out_string .= $1; + $out_string .= '#'; } elsif ( $in_string =~ /\G([^\#\'\"]+)/gc ) { @@ -5945,7 +5944,6 @@ sub strip_comments_and_join_quotes { last; } } - } ## end loop over line characters if ( !$quote_char ) { diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index c109938f..3968f9f1 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -10219,6 +10219,7 @@ sub scan_variable_usage { # Looking for something like $word, @word, $word[, $$word, ${word}, .. while ( $text =~ / ([\$\@] [\$]*) \{?(\w+)\}? ([\[\{]?) /gcx ) { + ## ------1------ -2- ---3--- my $sigil_string = $1; my $word = $2; my $brace = $3; @@ -15848,7 +15849,8 @@ sub count_prototype_args { $count_min = undef if ( !$saw_semicolon ); return; }; - while ( my $ch = shift @chars ) { + while (@chars) { + my $ch = shift @chars; if ( !defined($ch) ) { $saw_array->(); last } elsif ( $ch eq '(' ) { last if ($count_min) } elsif ( $ch eq ')' ) { last } @@ -19217,7 +19219,8 @@ sub weld_nested_containers { # Main loop over nested pairs... # We are working from outermost to innermost pairs so that # level changes will be complete when we arrive at the inner pairs. - while ( my $item = pop( @{$rnested_pairs} ) ) { + while ( @{$rnested_pairs} ) { + my $item = pop @{$rnested_pairs}; my ( $inner_seqno, $outer_seqno ) = @{$item}; my $Kouter_opening = $K_opening_container->{$outer_seqno}; @@ -22569,7 +22572,8 @@ EOM # always remove unwanted trailing blank lines from our list return unless (@iblanks); - while ( my $ibl = pop(@iblanks) ) { + while (@iblanks) { + my $ibl = pop @iblanks; if ( $ibl < $iend ) { push @iblanks, $ibl; last } $iend = $ibl; } @@ -22577,7 +22581,10 @@ EOM # now mark mark interior blank lines for deletion if requested return unless ($rOpts_kgb_delete); - while ( my $ibl = pop(@iblanks) ) { $rhash_of_desires->{$ibl} = 2 } + while (@iblanks) { + my $ibl = pop @iblanks; + $rhash_of_desires->{$ibl} = 2; + } return; } ## end sub kgb_delete_inner_blank_lines diff --git a/lib/Perl/Tidy/HtmlWriter.pm b/lib/Perl/Tidy/HtmlWriter.pm index cdd75e5a..b0b5a1fe 100644 --- a/lib/Perl/Tidy/HtmlWriter.pm +++ b/lib/Perl/Tidy/HtmlWriter.pm @@ -937,7 +937,8 @@ sub pod_to_html { if ( $self->{_pod_cut_count} <= 1 ) { $html_print->('
'); } - while ( my $rpre_string = shift @{$rpre_string_stack} ) { + while ( @{$rpre_string_stack} ) { + my $rpre_string = shift @{$rpre_string_stack}; $html_print->('
');
                     $html_print->( ${$rpre_string} );
                     $html_print->('
'); diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index c34f4707..c908f7fa 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -698,7 +698,8 @@ EOM else { # This will die if user's object does have a 'get_line' method - while ( my $line = $line_source_object->get_line() ) { + my $line; + while ( defined( $line = $line_source_object->get_line() ) ) { push( @{$rinput_lines}, $line ); } $source_string = join( EMPTY_STRING, @{$rinput_lines} ); @@ -1739,7 +1740,7 @@ sub find_starting_indentation_level { # ( or, for now, an =pod line) my $msg = EMPTY_STRING; my $in_code_skipping; - while ( $line = $self->peek_ahead( $i++ ) ) { + while ( defined( $line = $self->peek_ahead( $i++ ) ) ) { # if first line is #! then assume starting level is zero if ( $i == 1 && $line =~ /^\#\!/ ) { @@ -7516,7 +7517,7 @@ sub peek_ahead_for_n_nonblank_pre_tokens { my $i = 0; my ( $rpre_tokens, $rmap, $rpre_types ); - while ( $line = $self->peek_ahead( $i++ ) ) { + while ( defined( $line = $self->peek_ahead( $i++ ) ) ) { $line =~ s/^\s+//; # trim leading blanks next if ( length($line) <= 0 ); # skip blank next if ( $line =~ /^#/ ); # skip comment @@ -7535,7 +7536,7 @@ sub peek_ahead_for_nonblank_token { my $line; my $i = 0; - while ( $line = $self->peek_ahead( $i++ ) ) { + while ( defined( $line = $self->peek_ahead( $i++ ) ) ) { $line =~ s/^\s+//; # trim leading blanks next if ( length($line) <= 0 ); # skip blank next if ( $line =~ /^#/ ); # skip comment @@ -7825,7 +7826,7 @@ sub guess_if_here_doc { my $k = 0; my $msg = "checking <<"; - while ( $line = $self->peek_ahead( $k++ ) ) { + while ( defined( $line = $self->peek_ahead( $k++ ) ) ) { chomp $line; if ( $line =~ /^$next_token$/ ) {