我有一个perl脚本,它有主脚本和几个子程序交错在一起。它看起来像:
sub utils1 {
...
}
# some code
# some more code that calls utils1
sub utils2 {
...
}
# some code that calls utils2
sub utils3 {
...
}
# some code that calls utils3
# the rest of code
有没有更好的方法来组织代码?寻找一个好的编码约定。根据我的python编码经验,我在想下面的内容。这看起来怎么样?
sub utils1 {
...
}
sub utils2 {
...
}
sub utils3 {
...
}
sub main {
# some code
# some more code that calls utils1
# some code that calls utils2
# some code that calls utils3
# the rest of code
}
&main();
答案 0 :(得分:2)
据我所知,没有为子程序的组织定义best-practice。根据我的经验,文档通常会指示订单。
E.g。
use Getopt::Lucid;
=head1 SYNOPSIS
This program does nothing so long ...
=cut
# ... main code here, not necessary to wrap into a sub
=head1 PUBLIC METHODS
=cut
sub method1 {
=head2 method1
This method does something ...
=cut
$self = shift;
# ...
}
sub method2 {
=head2 method2
This method does something different...
=cut
$self = shift;
# ...
}
=head1 PRIVATE METHODS
These methods are private their interface may change.
=cut
sub _priv1 {
=head2 _priv1
The _priv1 method is for ... and used by ....
=cut
my $self = shift;
# ...
}