构建大型Perl程序的实用技巧
1. 引言
随着程序规模的不断扩大,我们会发现部分代码可应用于其他任务。此时,将这些代码迁移到一个库中,能在多个程序间共享,甚至与他人分享。同时,利用库还能按功能或用途对代码进行模块化,将其与执行无关任务的代码分离。
2. 通用代码问题及解决思路
- 问题场景 :有一位船长编写了许多Perl程序,用于为船只导航。在每个程序中,他都频繁复制粘贴一个通用的子程序
turn_toward_heading:
sub turn_toward_heading {
my $new_heading = shift;
my $current_heading = current_heading( );
print "Current heading is ", $current_heading, ".\n";
print "Come about to $new_heading ";
my $direction = 'right';
my $turn = ($new_heading - $current_heading) % 360;
if ($turn > 180) { # long way around
$turn = 360 - $turn;
$direction = 'left';
}
print "by turning $direction $turn degrees.\n";
}
这个子程序
超级会员免费看
订阅专栏 解锁全文
1012

被折叠的 条评论
为什么被折叠?



