前面我们讨论了如何使用以下代码编写和执行perl程序的基础知识 Perl Hello World示例.
在本文中,让我们回顾一下 如何调试Perl程序/脚本 使用 Perl调试器,类似于 用于调试C代码的gdb工具.
要调试perl程序,请使用“ 佩尔 -d”调用perl调试器,如下所示。
# 佩尔 -d ./perl_debugger.pl
要详细了解perl调试器命令,让我们创建以下示例perl程序(perl_debugger.pl)。
$ cat 佩尔_debugger.pl #!/usr/bin/perl -w # Script to list out the filenames (in the pwd) that contains specific pattern. #Enabling lur饮模式 $/=undef; # Function : get_pattern # Description : to get the pattern to be matched in files. sub get_pattern { my $pattern; print "输入搜索字符串:"; chomp ($pattern = <> ); return $pattern; } # Function : find_files # Description : to get list of filenames that contains the input pattern. sub find_files { my $pattern = shift; my (@files,@list,$file); # 使用 glob, obtaining the filenames, @files = <./*>; # taking out the filenames that contains pattern. @list = grep { $file = $_; open $FH,"$file"; @lines = <$FH>; $count = grep { /$pattern/ } @lines; $file if($count); } @files; return @list; } # to obtain the pattern from STDIN $pattern = get_pattern(); # to find-out the list of filenames which has the input pattern. @list = find_files($pattern); print join "\n",@列表;
1.输入Perl调试器
#perl -d ./perl_debugger.pl
它提示
DB<1>
2.使用(l)查看特定的行或子例程语句
DB<1> l 10
10:我的$ pattern
DB<2> l get_pattern
11 {
12:我的$ pattern
13:打印“输入搜索字符串:“;
14:排骨($ pattern =);
15:返回$ pattern;
16 }
3.使用(b)在get_pattern函数上设置断点
DB<3> b find_files
4.使用(b)在特定行上设置断点
DB<4> b 44
5.使用(L)查看断点
DB<5> L
./perl_debugger.pl:
22:我的$ pattern = shift
如果(1)
44:打印加入“\n”,@list;
如果(1)
6.使用(s和n)逐步执行
DB<5> s
main ::(../ 佩尔_debugger.pl:39):$pattern = get_pattern();
DB<5> s
main :: get_pattern(./ 佩尔_debugger.pl:12):
12:我的$ pattern
选项s和n会逐步执行每个语句。 Option进入子例程。选项n一步执行子例程(逐步执行该子例程)。
s选项确实会进入子例程,而n选项会执行子例程(单步执行)。
7.使用(c)继续直到下一个断点(或行号或子例程)
DB<5> c
输入搜索字符串:perl
main :: find_files(./ 佩尔_debugger.pl:22):
22:我的$ pattern = shift
8.使用(c)继续到特定的行号
DB<5> c 36
main :: find_files(./ 佩尔_debugger.pl:36):
36:返回@list
9.使用(p)打印特定变量中的值
DB<6> p $模式
佩尔
DB<7> c
main ::(../ 佩尔_debugger.pl:44):print加入“\n”,@list;
DB<7> c
./perl_debugger.pl
调试程序终止。使用q退出或使用R重新启动,
使用o disable_exit避免在程序终止后停止,
h q,h R或h o以获取其他信息。
在最后一次继续操作之后,由于输出与模式“ 佩尔”匹配,因此输出将在标准输出上打印为“ ./perl_debugger.pl”。
10.从文件(源)获取调试命令
Perl调试器可以从文件中获取debug命令并执行它。例如,使用perl debug命令创建名为“ debug_cmds”的文件,如下所示:
c
p $模式
q
请注意,R用于重新启动操作(无需退出并再次启动调试器)。
DB<7> R
DB<7> source debug_cmds
>> c
输入搜索字符串:perl
./perl_debugger.pl
调试程序终止。使用q退出或使用R重新启动,
使用o disable_exit避免在程序终止后停止,
h q,h R或h o以获取其他信息。
>> p $模式
佩尔
>> q
注意:如果您不熟悉perl,请参阅我们的上一篇文章: 针对初学者的20个Perl编程技巧.
Perl调试器命令摘要
进入perl调试器后,可以使用以下选项。
- h或h h– for help page
- c –从当前执行继续到断点,否则直到子程序名称或行号,
- p –显示变量的值
- b –放置断点,
- L –要查看设置的断点,
- d –删除断点,
- s –进入下一行执行。
- n –跳过下一行的执行,因此,如果下一行是子例程调用,它将执行该子例程,但不会进入子例程进行检查。
- 源文件–从文件中获取调试命令。
- l子名称–查看子例程中可用的执行语句。
- q –退出调试器模式。
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
你是什么意思“surf mode”(第6/7行)。您在这里做什么:将OUTPUT_RECORD_SEPARATOR($ \)设置为undef,这是默认设置。
我没听说过“surf mode”,但如果您打算启用“slurp mode”您需要将INPUT_RECORD_SEPARATOR($ /)设置为undef。如有疑问,请参阅perlvar(此处 http://perldoc.perl.org/perlvar.html )。但是,即使对于此脚本,这也完全没有用。
此外,脚本已损坏,因为您在第15行中忘记了。