|
|
楼主 |
发表于 2017-4-18 20:20:21
|
显示全部楼层
本帖最后由 PerlMonk 于 2017-4-18 21:57 编辑
管理复杂数据结构
使用调试器查看数据结构
参考 PerlDebug
示例代码:
- my %total_bytes;
- while (<DATA>) {
- my ($source, $destination, $bytes) = split;
- $total_bytes{$source}{$destination} += $bytes;
- }
- for my $source (sort keys %total_bytes) {
- for my $destination (sort keys %{ $total_bytes{$source} }) {
- print "$source => $destination:",
- " $total_bytes{$source}{$destination} bytes\n";
- }
- print "\n";
- }
- __DATA__
- professor.hut gilligan.crew.hut 1250
- professor.hut lovey.howell.hut 910
- thurston.howell.hut lovey.howell.hut 1250
- professor.hut lovey.howell.hut 450
- ginger.girl.hut professor.hut 1218
- ginger.girl.hut maryann.girl.hut 199
复制代码
操作示例:
perl -d bytecounts.pl
Loading DB routines from perl5db.pl version 1.37
Editor support available.
Enter h or 'h h' for help, or 'perldoc perldebug' for more help.
main::(bytecounts.pl:1): my %total_bytes;
DB<1> s
main::(bytecounts.pl:2): while (<DATA>) {
DB<1> s
main::(bytecounts.pl:3): my ($source, $destination, $bytes) = split;
DB<1> s
main::(bytecounts.pl:4): $total_bytes{$source}{$destination} += $bytes;
DB<1> x $source, $destination, $bytes
0 'professor.hut'
1 'gilligan.crew.hut'
2 1250
|
在 perlDB 控制台中输入 s 执行下一句,x 后附加变量名显示对应变量的状态
也可以直接输入代码,查看数组:`x @array`, 查看哈希字典:`x \%hash`
DB<8> @a = (1 .. 3);
DB<9> x @a
0 1
1 2
2 3
|
DB<10> %h = qw/a 1 b 2 c 3/;
DB<12> x \%h
0 HASH(0x2ac9e2c)
'a' => 1
'b' => 2
'c' => 3
|
也可以在 x 后面使用列表、哈希操作符(sort, keys, values ... )
DB<31> %h = qw(a b c d e f);
DB<34> x sort keys %h
0 'a'
1 'c'
2 'e'
|
一些总结
s [函数名] 进入函数并逐步运行,提示符从 `DB<>` 变为 `DB<<>>`
n [函数名] 执行函数,并且一次执行完。
b [line|event|sub] 添加断点。
B [line|*] 删除断点
w [expr] 监视变量,受监视的变量在变化时将显示到终端,`w $var`
W [expr|*] 删除变量监视器
p 同 print
S [[!]pat] 枚举当前加载的所有子例程名单,可以设置过滤、排除,例:
DB<24> S main
main::BEGIN
main::dumpValue
main::dumpvar
main::test
|
y 查看当前脚本的变量列表和对应的值
c [n] 连续执行代码直到某一行,如果不带参数,会结束当前循环或者脚本。
a [ln]+[cmd] 在某行之前执行命令,例 `a 8 print "$var\n"` ,实测有时候没有效果,最好先为该行设置断点
A [ln|*] 删除命令
使用 Data::Dumper 打印/导出复杂数据结构
- use Data::Dumper;
- print Dumper(\%total_bytes);
复制代码
$VAR1 = {
'thurston.howell.hut' => {
'lovey.howell.hut' => 1250
},
'ginger.girl.hut' => {
'maryann.girl.hut' => 199,
'professor.hut' => 1218
},
'professor.hut' => {
'gilligan.crew.hut' => 1250,
'lovey.howell.hut' => 1360
}
};
|
来看另一段代码,@data1 , @data2 互相包含对方的引用,Data::Dumper 能够正确打印他们的结构 注意这里给 Dumper 传入两个数组引用:
- use Data::Dumper;
- $Data::Dumper::Purity = 1; # 声明打印的数据可能出现自引用的情况
- my @data1 = qw(one won);
- my @data2 = qw(two too to);
- push @data2, \@data1;
- push @data1, \@data2;
- print Dumper(\@data1, \@data2);
复制代码
$VAR1 = [
'one',
'won',
[
'two',
'too',
'to',
[]
]
];
$VAR1->[2][3] = $VAR1;
$VAR2 = $VAR1->[2];
|
如果使用 Perl Debugger
DB<2> x \@data1, \@data2
0 ARRAY(0x24a4e84)
0 'one'
1 'won'
2 ARRAY(0x47f324)
0 'two'
1 'too'
2 'to'
3 ARRAY(0x24a4e84)
-> REUSED_ADDRESS
1 ARRAY(0x47f324)
-> REUSED_ADDRESS
|
Data::Dump
如果不介意数据的建,可以考虑使用 Data::Dump,输出相对简洁:
- use Data::Dump qw(dump);
- dump( \%total_bytes );
复制代码
{
"ginger.girl.hut" => { "maryann.girl.hut" => 199, "professor.hut" => > 1218 },
"professor.hut" => { "gilligan.crew.hut" => 1250, "lovey.howell.hut" > => 1360 },
"thurston.howell.hut" => { "lovey.howell.hut" => 1250 },
}
|
Data::Printer
这个模块在 ActivePerl V5.16 ppm 安装失败,但是可以从CPAN下载安装,同时需要安装 Sort::Naturally, Clone::PP
- use Data::Printer;
- p( %total_bytes );
复制代码
{
ginger.girl.hut {
maryann.girl.hut 199,
professor.hut 1218
},
professor.hut {
gilligan.crew.hut 1250,
lovey.howell.hut 1360
},
thurston.howell.hut {
lovey.howell.hut 1250
}
}
|
使用 Storeable 模块存储复杂数据结构
YAML
JSON
对数据的间接操作,通过 grep 和 map
|
|