How to restart a routine without restarting the script in the Perl debugger?

Suppose I have a situation where I am trying to experiment with some Perl code.

 perl -d foo.pl

Foo.pl makes it fun (this is a great script), and I decide that I want to reprogram a specific routine and take one step through it, but without restarting the process. How can I do it?

+3
source share
3 answers

The debugger command b methodsets a breakpoint at the beginning of your routine.

  DB<1> b foo
  DB<2> &foo(12)
main::foo(foo.pl:2):      my ($x) = @_;
  DB<<3>> s
main::foo(foo.pl:3):      $x += 3;
  DB<<3>> s
main::foo(foo.pl:4):      print "x = $x\n";
  DB<<3>> _

Sometimes you may need to qualify the routine names with the package name.

  DB<1> use MyModule
  DB<2> b MyModule::MySubroutine
+5
source

just do: func_name (args)

eg.

sub foo {
  my $arg = shift;
  print "hello $arg\n";
}

In perl -d:

  DB<1> foo('tom')
hello tom
+2
source

.

, , :

my $stop_foo = 0;

while(not $stop_foo) {
   foo();
}

sub foo {
   my $a = 1 + 1;
}

The debugger will constantly execute foo, but you can stop the next loop by executing $ stop_foo ++ in the debugger.

Again, I really don’t feel that this is the best way, but it does the job with only minor additions to the debugged code.

0
source

Source: https://habr.com/ru/post/1718421/


All Articles