This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove a redundant line.
[perl5.git] / lib / perl5db.pl
index c7dbaf8..1147caa 100644 (file)
@@ -5441,13 +5441,23 @@ later.
 sub _min {
     my $min = shift;
     foreach my $v (@_) {
-        if ($v < $min) {
-            $v = $min;
+        if ($min > $v) {
+            $min = $v;
         }
     }
     return $min;
 }
 
+sub _max {
+    my $max = shift;
+    foreach my $v (@_) {
+        if ($max < $v) {
+            $max = $v;
+        }
+    }
+    return $max;
+}
+
 sub _minify_to_max {
     my $ref = shift;
 
@@ -5475,11 +5485,10 @@ sub _cmd_l_handle_var_name {
     $line = "$1 $s";
 
     # Call self recursively to really do the command.
-    return cmd_l( 'l', $s );
+    return _cmd_l_main( $s );
 }
 
 sub _cmd_l_handle_subname {
-    my $cmd  = shift;
     my $line = shift;
 
     my $s = $subname;
@@ -5531,8 +5540,7 @@ sub _cmd_l_handle_subname {
         }
 
         # Call self recursively to list the range.
-        $line = $subrange;
-        return cmd_l( 'l', $subrange );
+        return _cmd_l_main( $subrange );
     } ## end if ($subrange)
 
     # Couldn't find it.
@@ -5547,7 +5555,7 @@ sub _cmd_l_empty {
     $incr = $window - 1;
 
     # Recurse to do it.
-    return cmd_l( 'l', $start . '-' . ( $start + $incr ) );
+    return _cmd_l_main( $start . '-' . ( $start + $incr ) );
 }
 
 sub _cmd_l_plus {
@@ -5562,114 +5570,134 @@ sub _cmd_l_plus {
 
     # Create a line range we'll understand, and recurse to do it.
     my $line = $start . '-' . ( $start + $incr );
-    return cmd_l( 'l', $line );
+    return _cmd_l_main( $line );
 }
 
-sub cmd_l {
-    my $current_line = $line;
-    my $cmd  = shift;
-    my $line = shift;
+sub _cmd_l_calc_initial_end_and_i {
+    my ($line, $start_match, $end_match) = @_;
 
-    # If this is '-something', delete any spaces after the dash.
-    $line =~ s/^-\s*$/-/;
+    # Determine end point; use end of file if not specified.
+    my $end = ( !defined $start_match ) ? $max :
+    ( $end_match ? $end_match : $start_match );
 
-    # If the line is '$something', assume this is a scalar containing a
-    # line number.
-    # Set up for DB::eval() - evaluate in *user* context.
-    if ( my ($var_name) = $line =~ /\A(\$.*)/s ) {
-        return _cmd_l_handle_var_name($var_name);
-    } ## end if ($line =~ /^(\$.*)/s)
+    # Go on to the end, and then stop.
+    _minify_to_max(\$end);
 
-    # l name. Try to find a sub by that name.
-    elsif ( ($subname) = $line =~ /\A([\':A-Za-z_][\':\w]*(?:\[.*\])?)/s ) {
-        return _cmd_l_handle_subname($cmd, $line);
-    } ## end elsif ($line =~ /^([\':A-Za-z_][\':\w]*(\[.*\])?)/s)
+    # Determine start line.
+    my $i = $start_match;
 
-    # Bare 'l' command.
-    elsif ( $line !~ /\S/ ) {
-        return _cmd_l_empty();
+    if ($i eq '.') {
+        $i = $line;
     }
 
-    # l [start]+number_of_lines
-    elsif ( my ($new_start, $new_incr) = $line =~ /\A(\d*)\+(\d*)\z/ ) {
+    $i = _max($i, 1);
 
-        return _cmd_l_plus($new_start, $new_incr);
-    } ## end elsif ($line =~ /^(\d*)\+(\d*)$/)
+    $incr = $end - $i;
 
-    # l start-stop or l start,stop
-    elsif ( $line =~ /^((-?[\d\$\.]+)([-,]([\d\$\.]+))?)?/ ) {
+    return ($end, $i);
+}
 
-        # Determine end point; use end of file if not specified.
-        my $end = ( !defined $2 ) ? $max : ( $4 ? $4 : $2 );
+sub _cmd_l_range {
+    my ($line, $current_line, $start_match, $end_match) = @_;
 
-        # Go on to the end, and then stop.
-        _minify_to_max(\$end);
+    my ($end, $i) =
+        _cmd_l_calc_initial_end_and_i($line, $start_match, $end_match);
 
-        # Determine start line.
-        my $i    = $2;
-        $i    = $line if $i eq '.';
-        $i    = 1 if $i < 1;
-        $incr = $end - $i;
+    # If we're running under a slave editor, force it to show the lines.
+    if ($slave_editor) {
+        print {$OUT} "\032\032$filename:$i:0\n";
+        $i = $end;
+    }
+    # We're doing it ourselves. We want to show the line and special
+    # markers for:
+    # - the current line in execution
+    # - whether a line is breakable or not
+    # - whether a line has a break or not
+    # - whether a line has an action or not
+    else {
+        I_TO_END:
+        for ( ; $i <= $end ; $i++ ) {
 
-        # If we're running under a slave editor, force it to show the lines.
-        if ($slave_editor) {
-            print $OUT "\032\032$filename:$i:0\n";
-            $i = $end;
-        }
+            # Check for breakpoints and actions.
+            my ( $stop, $action );
+            if ($dbline{$i}) {
+                ( $stop, $action ) = split( /\0/, $dbline{$i} );
+            }
 
-        # We're doing it ourselves. We want to show the line and special
-        # markers for:
-        # - the current line in execution
-        # - whether a line is breakable or not
-        # - whether a line has a break or not
-        # - whether a line has an action or not
-        else {
-            I_TO_END:
-            for ( ; $i <= $end ; $i++ ) {
+            # ==> if this is the current line in execution,
+            # : if it's breakable.
+            my $arrow =
+            ( $i == $current_line and $filename eq $filename_ini )
+            ? '==>'
+            : ( $dbline[$i] + 0 ? ':' : ' ' );
 
-                # Check for breakpoints and actions.
-                my ( $stop, $action );
-                if ($dbline{$i}) {
-                    ( $stop, $action ) = split( /\0/, $dbline{$i} );
-                }
+            # Add break and action indicators.
+            $arrow .= 'b' if $stop;
+            $arrow .= 'a' if $action;
 
-                # ==> if this is the current line in execution,
-                # : if it's breakable.
-                my $arrow =
-                  ( $i == $current_line and $filename eq $filename_ini )
-                  ? '==>'
-                  : ( $dbline[$i] + 0 ? ':' : ' ' );
+            # Print the line.
+            print {$OUT} "$i$arrow\t", $dbline[$i];
 
-                # Add break and action indicators.
-                $arrow .= 'b' if $stop;
-                $arrow .= 'a' if $action;
+            # Move on to the next line. Drop out on an interrupt.
+            if ($signal) {
+                $i++;
+                last I_TO_END;
+            }
+        } ## end for (; $i <= $end ; $i++)
 
-                # Print the line.
-                print {$OUT} "$i$arrow\t", $dbline[$i];
+        # Line the prompt up; print a newline if the last line listed
+        # didn't have a newline.
+        if ($dbline[ $i - 1 ] !~ /\n\z/) {
+            print {$OUT} "\n";
+        }
+    } ## end else [ if ($slave_editor)
 
-                # Move on to the next line. Drop out on an interrupt.
-                if ($signal) {
-                    $i++;
-                    last I_TO_END;
-                }
-            } ## end for (; $i <= $end ; $i++)
+    # Save the point we last listed to in case another relative 'l'
+    # command is desired. Don't let it run off the end.
+    $start = $i;
+    _minify_to_max(\$start);
 
-            # Line the prompt up; print a newline if the last line listed
-            # didn't have a newline.
-            if ($dbline[ $i - 1 ] !~ /\n\z/) {
-                print {$OUT} "\n";
-            }
-        } ## end else [ if ($slave_editor)
+    return;
+}
 
-        # Save the point we last listed to in case another relative 'l'
-        # command is desired. Don't let it run off the end.
-        $start = $i;
-        _minify_to_max(\$start);
+sub _cmd_l_main {
+    my $spec = shift;
 
-        return;
-    } ## end elsif ($line =~ /^((-?[\d\$\.]+)([-,]([\d\$\.]+))?)?/)
+    # If this is '-something', delete any spaces after the dash.
+    $spec =~ s/\A-\s*\z/-/;
+
+    # If the line is '$something', assume this is a scalar containing a
+    # line number.
+    # Set up for DB::eval() - evaluate in *user* context.
+    if ( my ($var_name) = $spec =~ /\A(\$.*)/s ) {
+        return _cmd_l_handle_var_name($var_name);
+    }
+    # l name. Try to find a sub by that name.
+    elsif ( ($subname) = $spec =~ /\A([\':A-Za-z_][\':\w]*(?:\[.*\])?)/s ) {
+        return _cmd_l_handle_subname($spec);
+    }
+    # Bare 'l' command.
+    elsif ( $spec !~ /\S/ ) {
+        return _cmd_l_empty();
+    }
+    # l [start]+number_of_lines
+    elsif ( my ($new_start, $new_incr) = $spec =~ /\A(\d*)\+(\d*)\z/ ) {
+        return _cmd_l_plus($new_start, $new_incr);
+    }
+    # l start-stop or l start,stop
+    elsif (my ($s, $e) = $spec =~ /^(?:(-?[\d\$\.]+)(?:[-,]([\d\$\.]+))?)?/ ) {
+        return _cmd_l_range($spec, $line, $s, $e);
+    }
+
+    return;
 } ## end sub cmd_l
 
+sub cmd_l {
+    my (undef, $line) = @_;
+
+    return _cmd_l_main($line);
+}
+
 =head3 C<cmd_L> - list breakpoints, actions, and watch expressions (command)
 
 To list breakpoints, the command has to look determine where all of them are