gpt4 book ai didi

Perl如何在子程序和主脚本之间组织代码

转载 作者:行者123 更新时间:2023-12-02 01:45:18 26 4
gpt4 key购买 nike

我得到了一个 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();

最佳答案

据我所知没有best-practice为子程序的组织而定义。根据我的经验,文档通常决定了顺序。

例如。

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;
# ...
}

关于Perl如何在子程序和主脚本之间组织代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26013874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com