tags:

views:

114

answers:

3

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?

+2  A: 

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
How do this help you step through the subroutine?
mobrule
A: 

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.

jsoverson
+3  A: 

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
mobrule