debugging perl program with Tk mainloop

N

ngoc

Hi
I want to debugg a program with GUI. I dislike using "print $variable"
in the program. So I use perl -d instead. But "<DB4>c 2700" start GUI
section and I have to click exit on GUI to turn back to command line
again and the program is already terminated. As consequence, I can not
use "p $variable" after that. How can I solve this problem?
ngoc
 
Z

zentara

Hi
I want to debugg a program with GUI. I dislike using "print $variable"
in the program. So I use perl -d instead. But "<DB4>c 2700" start GUI
section and I have to click exit on GUI to turn back to command line
again and the program is already terminated. As consequence, I can not
use "p $variable" after that. How can I solve this problem?
ngoc

Try using Devel::ptkdb on Tk programs and see how you like it.
 
P

Peter Scott

I want to debugg a program with GUI. I dislike using "print $variable" in
the program. So I use perl -d instead. But "<DB4>c 2700" start GUI section
and I have to click exit on GUI to turn back to command line again and the
program is already terminated. As consequence, I can not use "p $variable"
after that. How can I solve this problem? ngoc

Set breakpoints inside the callbacks before continuing.
 
N

ngoc

Set breakpoints inside the callbacks before continuing.
I use
<DB 1>b 1974 ->"which is before mainloop"
<DB 2>p $variable_name ->"no value"
So I type
<DB 3>c 2877 ->"GUI show up. And I can now click on button to give
values to variables"
But I can not type "p $variable name" to know the value of variable. How
can I make it jump over to <DB 4> to type in "p $variable_name"?
 
P

Peter Scott

I use
<DB 1>b 1974 ->"which is before mainloop" <DB 2>p $variable_name ->"no
value"
So I type
<DB 3>c 2877 ->"GUI show up. And I can now click on button to give values
to variables"
But I can not type "p $variable name" to know the value of variable. How
can I make it jump over to <DB 4> to type in "p $variable_name"?

1. Learn what a callback is.
2. Learn what scope is.
3. Set a breakpoint inside a callback that has $variable_name in its scope.
 
N

ngoc

1. Learn what a callback is.
2. Learn what scope is.
3. Set a breakpoint inside a callback that has $variable_name in its scope.

A callback as in Master Perl/Tk is a subroutine to be called
Scope is inside or in the reach

But the problem here is

use Tk;
..................
$mw->Button (
-text => 'Add',
-command => \&add,
)->pack (-side => 'bottom', -anchor => 'sw', -pady => 3);
..........................
MainLoop;

sub add {
my $variable = &other_function;

}

So add() subroutine is a callback of -command. $variable is in the scope
of add().
To set breakpoint <DB1>b "line before end }" as you suggest (inside a
callback). And run <DB2>c

But It does not go over to <DB3> to type in "p $variable". It just goes
over when I click the exit button of the GUI.
 
P

Peter Scott

But the problem here is

use Tk;
.................
$mw->Button (
-text => 'Add',
-command => \&add,
)->pack (-side => 'bottom', -anchor => 'sw', -pady => 3);
.........................
MainLoop;

sub add {
my $variable = &other_function;

}

So add() subroutine is a callback of -command. $variable is in the scope
of add().
To set breakpoint <DB1>b "line before end }" as you suggest (inside a
callback). And run <DB2>c

But It does not go over to <DB3> to type in "p $variable". It just goes
over when I click the exit button of the GUI.

If you're not invoking the callback you set the breakpoint in, how can it
reach it? You have to click on your Add button. Also, your code is
misleading in that you show a blank line where you claim to be setting a
breakpoint. A blank line is not breakable.

Here is a real example that shows how this works:

$ perl -d foo

Loading DB routines from perl5db.pl version 1.27
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(foo:7): my $mw = MainWindow->new;
DB<1> l 1-18
1 #!/usr/local/bin/perl
2: use strict;
3: use warnings;
4
5: use Tk;
6
7==> my $mw = MainWindow->new;
8: $mw->Label(-text => 'Hello, world!')->pack;
9: $mw->Button(
10 -text => 'Quit',
11 -command => \&quit,
12 )->pack;
13: MainLoop;
14
15 sub quit {
16: my $variable = 42;
17: exit;
18 }
DB<2> b 17
DB<3> c
# << Press Quit button on GUI >>
main::quit(foo:17): exit;
DB<3> p $variable
42
DB<4> q
 
N

ngoc

Thank you very much. You solve my problem. Now I can debugg without
using print command in the code -> better quality of software -> my boss
will be happy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top