]> git.donarmstrong.com Git - perltidy.git/commitdiff
simplify several while loops
authorSteve Hancock <perltidy@users.sourceforge.net>
Sat, 5 Oct 2024 19:59:57 +0000 (12:59 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sat, 5 Oct 2024 19:59:57 +0000 (12:59 -0700)
lib/Perl/Tidy.pm
lib/Perl/Tidy/Formatter.pm
lib/Perl/Tidy/HtmlWriter.pm
lib/Perl/Tidy/Tokenizer.pm

index 375f581c2fad1a8a11604dce7bcc411b8bdeea5d..f996b96f699227356cbe4d6cd786f378bd794915 100644 (file)
@@ -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 ) {
index c109938f0ddbcdd8cd829346240b88a605de37b8..3968f9f1a207c6938a4e156bcf39169e602d8240 100644 (file)
@@ -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
index cdd75e5a613dddefd5394c86ed42e82c49f96e35..b0b5a1fe7df60a6a5875afbe53d3ee617a3599fe 100644 (file)
@@ -937,7 +937,8 @@ sub pod_to_html {
                 if ( $self->{_pod_cut_count} <= 1 ) {
                     $html_print->('<hr />');
                 }
-                while ( my $rpre_string = shift @{$rpre_string_stack} ) {
+                while ( @{$rpre_string_stack} ) {
+                    my $rpre_string = shift @{$rpre_string_stack};
                     $html_print->('<pre>');
                     $html_print->( ${$rpre_string} );
                     $html_print->('</pre>');
index c34f47076fe08c2e69d494c4921aef7c485c3d9e..c908f7fa16c01b2a8f1278641f468e2b47ff9c94 100644 (file)
@@ -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$/ ) {