Paul Nathan
Paul Nathan

Reputation: 40309

How do I rerun a subroutine without restarting the script in Perl's debugger?

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

 perl -d foo.pl

Foo.pl chugs it's merry way around (it's a big script), and I decide I want to rerun a particular subroutine and single step through it, but without restarting the process. How would I do that?

Upvotes: 3

Views: 672

Answers (3)

mob
mob

Reputation: 118605

The debugger command b method sets a breakpoint at the beginning of your subroutine.

  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 have to qualify the subroutine names with a package name.

  DB<1> use MyModule
  DB<2> b MyModule::MySubroutine

Upvotes: 5

jsoverson
jsoverson

Reputation: 1717

Responding to the edit regarding wanting to re-step through a subroutine.

This is not entirely the most elegant way of doing this, but I don't have another method off the top of my head and am interested in other people's answers to this question :

my $stop_foo = 0;

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

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

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

Again, I don't really feel like that's the best way but it does get the job done with only minor additions to the debugged code.

Upvotes: 0

naumcho
naumcho

Reputation: 19891

just do: func_name(args)

e.g.

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

In perl -d:

  DB<1> foo('tom')
hello tom

Upvotes: 2

Related Questions