赞
踩
在本文中,让我们了解一下如何使用Perl 调试器调试 perl 程序/脚本,它类似于调试 C 代码的 gdb 工具。
要调试 perl 程序,请使用“perl -d”调用 perl 调试器,如下所示。
# perl -d ./perl_debugger.pl
要详细了解 perl 调试器命令,让我们创建以下示例 perl 程序 (perl_debugger.pl)。
$ cat perl_debugger.pl #!/usr/bin/perl -w # Script to list out the filenames (in the pwd) that contains specific pattern. #Enabling slurp mode $/=undef; # Function : get_pattern # Description : to get the pattern to be matched in files. sub get_pattern { my $pattern; print "Enter search string: "; 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); # using 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",@list;
# perl -d ./perl_debugger.pl
it prompts,
DB<1>
DB<1> l 10
10: my $pattern;
DB<2> l get_pattern
11 {
12: my
p
a
t
t
e
r
n
;
13
:
p
r
i
n
t
“
E
n
t
e
r
s
e
a
r
c
h
s
t
r
i
n
g
:
“
;
14
:
c
h
o
m
p
(
pattern; 13: print “Enter search string: “; 14: chomp (
pattern;13:print“Entersearchstring:“;14:chomp(pattern = );
15: return $pattern;
16 }
DB<3> b find_files
DB<4> b 44
DB<5> L
./perl_debugger.pl:
22: my $pattern = shift;
break if (1)
44: print join “\n”,@list;
break if (1)
DB<5> s
main:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。