admin 管理员组文章数量: 1184232
2024年3月9日发(作者:安卓应用一般用什么开发的)
perl拥有诡异的语法,丰富的类库以及强大的功能,是系统管理员必备的脚本技能。
学会debug perl,将使我们在调试perl脚本的过程中事半功倍,再也不用东print一下西print一下了。
下面我们来看看如何对perl脚本进行单步跟踪。
首先准备一个例程,,代码如下:
?
#!/usr/bin/perl
1
use strict;
2
use IO::File;
3
4
my $fh = IO::File->new('<$fh>){
5
print($_);
6
}
7
$fh->close();
8
sub testFunction(){
9
my ($a, $b) = @_;
10
my $i = 0;
11
while($i < 100){
12
$i += 1;
13
}
14
}
15
my $a1 = "hello world!";
16
my @a2 = (1,2,3,4,5,6);
17
foreach my $ii (@a2){
18
print($ii."n");
19
}
20
my %a3 = ('key1'=>'val1','key2' => 'val2', 'key3'
21
=> 'val3');
22
print($a3{key1});
然后我们执行:perl -d 进入perl的debug窗口。
?
1 [root@cnsz12345 perl]# perl -d
2
3 Loading DB routines from version 1.27
4 Editor support available.
5
6 Enter h or `h h' for help, or `man perldebug' for
7 more help.
8
9 main::(:5): my $fh = IO::File->new('
DB<1>
输入h能看到帮助,输入h h能看到更完整的帮助。
?
List/search source lines: Control script
execution:
l [ln|sub] List source
code T Stack trace
- or . List previous/current line s
1
[expr] Single step [in expr]
2
v [line] View around line n
3
[expr] Next, steps over subs
4
f filename View source in file Repeat last n
5
or s
6
/pattern/ ?patt? Search
7
forw/backw r Return from subroutine
8
M Show module versions c
9
[ln|sub] Continue until position
10
Debugger
11
controls: L List
12
break/watch/actions
13
o [...] Set debugger options t
14
[expr] Toggle trace [trace expr]
15
<[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub]
16
[cnd] Set breakpoint
17
! [N|pat] Redo a previous command B
18
ln|* Delete a/all breakpoints
19
H [-num] Display last num commands a [ln] cmd Do
20
cmd before line
21
= [a val] Define/list an alias A
22
ln|* Delete a/all actions
23
h [db_cmd] Get help on command w expr Add
24
a watch expression
25
h h Complete help page W
26
expr|* Delete a/all watch exprs
|[|]db_cmd Send output to pager ![!] syscmd Run
cmd in a subprocess
q or
^D Quit R Attempt
a restart
Data Examination: expr Execute perl code, also
see: s,n,t expr
x|m expr Evals expr in list context, dumps the
result or lists methods.
p expr Print expression (uses script's current
package).
S [[!]pat] List subroutine names [not] matching
pattern
V [Pk [Vars]] List Variables in Package. Vars can be
~pattern or !pattern.
X [Vars] Same as "V current_package [Vars]". i
class inheritance tree.
y [n [Vars]] List lexicals in higher scope . Vars same
as V.
For more help, type h cmd_letter, or run man perldebug for
all docs.
DB<1>
这里介绍几个常用的命令:
n:step over,执行下一行,执行到子函数时不进入函数内部。
s:step in,执行下一行,执行到子函数时进入函数内部继续跟踪。
w:增加一个watch变量,当变量发生变化时,执行中断。
b:增加一个断点,这个就不用说了吧。
.:显示当前行代码(现在执行到的那一行)。
还有很多有用的命令,这里不一一介绍。
举两个例子:
1、监控变量$ii的变化情况:
?
1 DB<2> w $ii
2 DB<3> r
3 aaaaaaaaaaaaaaaaaa
4 bbbbbbbbbbbbbbbbbb
5 cccccccccccccccccc
6 dddddddddddddddddd
7 eeeeeeeeeeeeeeeeee
8 Watchpoint 0: $ii changed:
9 old value: ''
10 new value: '1'
11 main::(:21): print($ii."n");
12 DB<3> r
13 1
14 Watchpoint 0: $ii changed:
15 old value: '1'
16 new value: '2'
17 main::(:21): print($ii."n");
18 DB<3>
大家发现,每执行一次r,当$ii发生变化,执行都会中断,告诉你$ii的变化情况。
2、在第18行设置断点
?
Enter h or `h h' for help, or `man perldebug' for
1
more help.
2
3
main::(:5): my $fh = IO::File->new('
4
DB<10> r
5
aaaaaaaaaaaaaaaaaa
6
bbbbbbbbbbbbbbbbbb
7
cccccccccccccccccc
8
dddddddddddddddddd
9
eeeeeeeeeeeeeeeeee
10
main::(:18): my $a1 = "hello world!";
11
DB<10> n
12
main::(:19): my @a2 = (1,2,3,4,5,6);
13
DB<10> p $a1
14
hello world!
15
DB<11>
当执行到18行时,执行自动中断,可以使用n或s命令执行下一行代码。
执行跳过18行后,可以使用p $a1命令查看$a1变量的赋值。
剩下的功能请大家自行发掘,没什么技术含量,只要大家会用~
版权声明:本文标题:单步调试perl脚本 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1709970569a551684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论