当前位置:   article > 正文

Perl 调试器教程:调试 Perl 程序的 10 个简单步骤

perl 调试

在本文中,让我们了解一下如何使用Perl 调试器调试 perl 程序/脚本,它类似于调试 C 代码的 gdb 工具。

要调试 perl 程序,请使用“perl -d”调用 perl 调试器,如下所示。

# perl -d ./perl_debugger.pl
  • 1

要详细了解 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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

1.进入Perl调试器

# perl -d ./perl_debugger.pl

it prompts,
DB<1>

2.使用(l)查看特定的行或子程序语句

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:printEntersearchstring:;14:chomp(pattern = );
15: return $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: my $pattern = shift;
break if (1)
44: print join “\n”,@list;
break if (1)

6. 使用 (s 和 n) 逐步执行

DB<5> s
main:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/738321
推荐阅读