Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b8b08cab82
Fetching contributors…

Cannot retrieve contributors at this time

649 lines (450 sloc) 11.294 kb
0:00:03.700,0:00:06.190
Hi and welcome back to the channel
0:00:06.190,0:00:09.070
This time I'm going
to talk about built-in debugger
0:00:09.070,0:00:11.309
of perl
0:00:11.309,0:00:14.089
Normally if you have a script
0:00:14.089,0:00:16.350
Let's say in this case 'outline.pl'
0:00:16.350,0:00:21.569
You can run in this way in the command line
perl name of the script and potential parameters
0:00:21.569,0:00:23.960
So let's see if I run this script
0:00:23.960,0:00:25.599
We can see that it provides some output
0:00:25.599,0:00:29.849
The way to debug it is to run
perl -d
0:00:29.849,0:00:33.120
and the name of the script
and then the parameters
0:00:33.120,0:00:33.770
Once
0:00:33.770,0:00:36.240
I run this
0:00:36.240,0:00:38.920
it will give some kind of an output
0:00:38.920,0:00:43.340
Prints out the first line of code,
the next line that is
0:00:43.340,0:00:45.010
supposed to be executed
0:00:45.010,0:00:47.220
and then provides me with a prompt
0:00:47.220,0:00:50.290
where I can type in various commands
to the debugger.
0:00:50.290,0:00:51.839
First and most important
0:00:51.839,0:00:52.610
is the 'q'
0:00:52.610,0:00:54.960
that will let us quit the debugger
0:00:54.960,0:00:58.000
So when we want to get rid of it
0:00:58.000,0:00:58.689
We press
0:00:58.689,0:01:00.950
q enter and it quits
0:01:00.950,0:01:02.820
Now let's run it again
0:01:02.820,0:01:04.840
The second most important
0:01:04.840,0:01:06.420
which is 'h'
0:01:06.420,0:01:08.390
Providing us this help
0:01:08.390,0:01:12.100
So you can see here the various options.
This is actually the short list
0:01:12.100,0:01:13.840
of the debugger
0:01:13.840,0:01:18.110
So you can know that there are tons of
potential...
0:01:18.110,0:01:20.800
possible ways to use the debugger
0:01:20.800,0:01:24.520
In order to see now the
code again
0:01:24.520,0:01:25.530
I press 'l'
0:01:25.530,0:01:29.120
which provides a listing of the
code
0:01:29.120,0:01:30.400
And in this 'l'
0:01:30.400,0:01:33.660
In this listing you will see the line
numbers
0:01:33.660,0:01:35.830
and this arrow points to the line that is the
0:01:35.830,0:01:39.260
next line to be executed. So this line
0:01:39.260,0:01:40.240
hasn't been executed. yet
0:01:40.240,0:01:43.690
If I want to see what's in the $file
variable
0:01:43.690,0:01:45.430
which
0:01:45.430,0:01:47.100
is supposed to be empty actually
0:01:47.100,0:01:49.750
I can use the
0:01:49.750,0:01:52.720
'p' command which stands for print
0:01:52.720,0:01:54.799
Provide the name of the variable
0:01:54.799,0:01:56.799
and it will just print nothing
0:01:56.799,0:01:59.269
Actually I could even check whether
0:01:59.269,0:02:02.720
it is defined or not.
0:02:02.720,0:02:05.750
So, I can write p defined and it return
0:02:05.750,0:02:06.869
false
0:02:06.869,0:02:08.499
0:02:08.499,0:02:10.419
So it doesn't print anything
0:02:10.419,0:02:14.299
If I was trying to do something more
complex I could try
0:02:14.299,0:02:18.680
if not defined and it will
print out some kind of true value
0:02:18.680,0:02:20.180
But anyway.
0:02:20.180,0:02:24.670
Let's see how can I execute these lines
0:02:24.670,0:02:26.959
I would like to get back to this point
0:02:26.959,0:02:29.219
and execute this line of code
0:02:29.219,0:02:33.729
To do that I press 's' which means step
or step in
0:02:33.729,0:02:35.669
and press enter
0:02:35.669,0:02:39.560
That will execute that line of code and
show me the next
0:02:39.560,0:02:41.269
line to be executed
0:02:41.269,0:02:46.040
If I print out the
content of the file
0:02:46.040,0:02:49.140
Then you will see that is already have
that value that was
0:02:49.140,0:02:51.519
provided in the command line
0:02:51.519,0:02:53.119
Once I have this
0:02:53.119,0:02:54.519
piece of information
0:02:54.519,0:02:58.139
and I know that I would like to call the
next line
0:02:58.139,0:03:01.379
Now the next line is calling the
slurp function
0:03:01.379,0:03:04.180
which is coming from another module
0:03:04.180,0:03:05.949
Pressing 's' again
0:03:05.949,0:03:06.939
will execute
0:03:06.939,0:03:08.619
the next
0:03:08.619,0:03:09.499
piece of code
0:03:09.499,0:03:11.999
Which is actually calling
that
0:03:11.999,0:03:13.959
function
0:03:13.959,0:03:17.539
So what you see here is that this time
0:03:17.539,0:03:19.200
it printed out the first
0:03:19.200,0:03:19.839
line
0:03:19.839,0:03:22.430
to be executed in that other file
0:03:22.430,0:03:23.480
So you can see that
0:03:23.480,0:03:25.150
this is the name of the
0:03:25.150,0:03:26.619
package
0:03:26.619,0:03:29.309
where the code is
0:03:29.309,0:03:32.819
read_file is the name of the
function that is being called
which is a bit
0:03:32.819,0:03:36.919
strange because we called a function
that is called 'slurp'
0:03:36.919,0:03:38.270
and instead of that
0:03:38.270,0:03:38.990
we are running read_file
0:03:38.990,0:03:40.950
And you will have to see what's
0:03:40.950,0:03:44.559
going on there and this is the name of the
file
0:03:44.559,0:03:48.479
the actual implementation of the read_file
function
0:03:48.479,0:03:51.010
And we are in line 75
0:03:51.010,0:03:52.749
So this is the next
0:03:52.749,0:03:55.489
line to be executed
0:03:55.489,0:04:00.029
Let's look around so let's type 'l'
0:04:00.029,0:04:02.919
and the a range of line numbers
0:04:02.919,0:04:04.309
70 to 80
0:04:04.309,0:04:06.310
and to show us the lines
0:04:06.310,0:04:08.119
0:04:08.119,0:04:11.419
File/Slurp.pm
0:04:11.419,0:04:15.449
In between those lines. And here you can
see that yes, we are in the
0:04:15.449,0:04:17.419
read_file
0:04:17.419,0:04:19.680
function as it was told
0:04:19.680,0:04:20.580
above it
0:04:20.580,0:04:25.340
You see that actually the slurp
function is and alias to the read_file function
0:04:25.340,0:04:28.650
So that explains why when we are calling slurp
0:04:28.650,0:04:29.560
it actually
0:04:29.560,0:04:32.530
gets us into the read_file function
0:04:32.530,0:04:34.280
within the read_file function
0:04:34.280,0:04:35.469
0:04:35.469,0:04:37.850
This is the next step to be executed
0:04:37.850,0:04:39.750
If I want to do and execute
0:04:39.750,0:04:41.470
I can press s again
0:04:41.470,0:04:44.280
but it's not necessary actually because
if I just
0:04:44.280,0:04:47.949
press enter that you can't really see
executes the same 's'
0:04:47.949,0:04:51.690
that was given earlier
0:04:51.690,0:04:55.389
This way I can just press enter
all the time and see it goes
0:04:55.389,0:04:56.830
step-by-step
0:04:56.830,0:04:58.160
and go over
0:04:58.160,0:04:59.659
all the values
0:04:59.659,0:05:03.110
All the steps that are in the code.
0:05:03.110,0:05:07.499
Now, after all you might think that
this is not he function you
0:05:07.499,0:05:09.409
want to debug
0:05:09.409,0:05:10.539
You wanted to get
0:05:10.539,0:05:12.659
over that function
0:05:12.659,0:05:15.899
so you stepped into that slurp
function but you don't really care
0:05:15.899,0:05:19.209
Because you already know that the
problem whatever you're looking for is
0:05:19.209,0:05:20.069
not there
0:05:20.069,0:05:23.019
So you would like to return from that
function
0:05:23.019,0:05:24.470
Without stepping
0:05:24.470,0:05:25.979
through all the
0:05:25.979,0:05:31.490
lines one-by-one for this
there is the 'r' command'
0:05:31.490,0:05:32.949
Before doing that actually
0:05:32.949,0:05:34.239
Let's do something else
0:05:34.239,0:05:37.729
Let's see the 't' command
which provides us the stack trace
0:05:37.729,0:05:40.199
So when I press 't'
I can see
0:05:40.199,0:05:41.789
where am I
0:05:41.789,0:05:46.019
in the code and you can see that this
is the only call
0:05:46.019,0:05:48.280
that has been made so there's only one
0:05:48.280,0:05:50.879
function in this
0:05:50.879,0:05:52.949
Pressing 'r'
0:05:52.949,0:05:55.979
will return from the current function
0:05:55.979,0:06:00.890
and print out their return values
because this time the return value
0:06:00.890,0:06:04.249
is the content of the Padre pm file
0:06:04.249,0:06:06.540
You see the content of the
file
0:06:06.540,0:06:10.199
which is just a end of the pm file
with the documentation
0:06:10.199,0:06:13.620
And with the list of the names
of the people
0:06:13.620,0:06:19.729
who contributed to the Padre project.
And by the way thank you for all them.
0:06:19.729,0:06:21.320
So we go back to the code.
0:06:21.320,0:06:24.939
Here now you can see the next
line
0:06:24.939,0:06:26.930
that should be executed
0:06:26.930,0:06:28.470
Which is the call to the
0:06:28.470,0:06:32.300
editor tools outline new and then define
0:06:32.300,0:06:33.399
function
0:06:33.399,0:06:35.039
0:06:35.039,0:06:40.099
So let's press 's' again
and step in that method
0:06:40.099,0:06:42.180
Let's look around
0:06:42.180,0:06:46.680
from 45 to let's say 60
0:06:46.680,0:06:49.059
You will see here,
0:06:49.059,0:06:52.050
Probably these are just empty
lines that are not displayed here
0:06:52.050,0:06:56.879
And then we are in the find
method on the first line of it.
0:06:56.879,0:06:57.709
So now you can
0:06:57.709,0:07:00.210
step again
0:07:00.210,0:07:02.010
at this time
0:07:02.010,0:07:08.089
We decide that we don't care about
the process_doc so we don't want to step in
0:07:08.089,0:07:09.409
Previously
0:07:09.409,0:07:13.589
we stepped into the slurp function and then
later on we found out that we
0:07:13.589,0:07:16.990
don't care about it and we stepped out of it
using the 'r'
0:07:16.990,0:07:20.089
In this time I can use that 'n' letter
0:07:20.089,0:07:21.719
which means next step
0:07:21.719,0:07:25.130
And if I press enter on this
0:07:25.130,0:07:27.999
Then the code of process_doc will be executed
0:07:27.999,0:07:31.130
but the debugger will stop
0:07:31.130,0:07:32.120
at that point
0:07:32.120,0:07:33.360
just after
0:07:33.360,0:07:34.909
the function call
0:07:34.909,0:07:38.069
So the next line 53
0:07:38.069,0:07:40.620
Here at the top you can see that this is
next
0:07:40.620,0:07:42.130
line to be executed
0:07:42.130,0:07:43.839
And here is where we are
0:07:43.839,0:07:45.750
Now we can press enter again
0:07:45.750,0:07:47.619
and enter will do
0:07:47.619,0:07:51.460
either 's' or 'n' so either will
step in or step over
0:07:51.460,0:07:55.030
Whichever was the last thing we did earlier
0:07:55.030,0:07:56.179
So if I press enter now
0:07:56.179,0:07:58.270
it steps over the code
0:07:58.270,0:08:01.419
through the ppi
0:08:01.419,0:08:02.800
and
0:08:02.800,0:08:06.750
I think that's enough for now
probably I'll give another
0:08:06.750,0:08:08.770
tutorial about it
0:08:08.770,0:08:14.629
Just don't forget using q will allow you
to exit the debugger
0:08:14.629,0:08:18.830
If you're interested to read more
about the debugger
0:08:18.830,0:08:23.529
there's an excellent book let
me show you this one
0:08:23.529,0:08:27.889
I recommend to buy this book and read it
0:08:27.889,0:08:30.830
I didn't write it
0:08:30.830,0:08:33.680
But you can learn quite a
lot from it
0:08:33.680,0:08:36.090
So thank you for watching and
0:08:36.090,0:08:36.790
please come back later on
Jump to Line
Something went wrong with that request. Please try again.