我应该在 Perl 中使用嵌套子例程吗?

发布于 2024-09-25 20:21:34 字数 1044 浏览 10 评论 0原文

  • 我有 5 个 Perl 文件,它们是针对我的环境的 5 种不同状态的验证脚本。

    它们每个都至少有几个子例程。

  • 到目前为止,状态数量仅限于 5 个,而且这些状态运行良好。但现在,根据当前的设计,我有大约 20 个环境状态,因此还有 20 个 Perl 脚本。

  • 我想将所有五个脚本移至一个脚本,该脚本将状态作为参数,并针对 5 个不同的状态有 5 个不同的子例程。

    这样,当我需要为另一个状态添加验证时,我只需定义一个新的子例程,而不是一个全新的 Perl 脚本。

  • 问题在于,这意味着使用嵌套子例程(即 已知会遇到问题),或展开子例程本身。

例如,

原始脚本

$ cat verify1.pl
sub a1 {
    ...
}
sub b1 {
    ...
}
a1(); b1(); a1();
$ cat verify2.pl
sub a2 {
    ...
}
sub b2 {
    ...
}
sub c2 {
    ...
}
a2(); b2(); c2(); a2();
$

合并脚本

$ cat verify.pl
sub one {
    ...
}
sub two {
    ...
}
my ($arg) = @ARGV;
if ($arg == 1) {
    one();  # should do what verify1.pl did
}
elsif ($arg == 2) {
    two();  # should do what verify2.pl did
}
$

我应该如何解决这个问题?

  • I have 5 Perl files that are verification scripts for 5 different states of my environment.

    Each of them has at least a couple of subroutines.

  • Till now, the number of states was limited to 5 and these worked fine. But now, I have like 20 more states of the environment and hence 20 more Perl scripts according to the current design.

  • I want to move all the five scripts to just one script which takes the state as an argument and have 5 different subroutines for 5 different states.

    This way, when I need to add a verify for yet another state, I will just have to define a new subroutine instead of a whole new Perl script.

  • The problem is that it will mean using nested subroutines (which are known to run into issues), or unrolling the subroutines themselves.

For example,

original scripts

$ cat verify1.pl
sub a1 {
    ...
}
sub b1 {
    ...
}
a1(); b1(); a1();
$ cat verify2.pl
sub a2 {
    ...
}
sub b2 {
    ...
}
sub c2 {
    ...
}
a2(); b2(); c2(); a2();
$

consolidated script

$ cat verify.pl
sub one {
    ...
}
sub two {
    ...
}
my ($arg) = @ARGV;
if ($arg == 1) {
    one();  # should do what verify1.pl did
}
elsif ($arg == 2) {
    two();  # should do what verify2.pl did
}
$

What should I do to resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人如玉 2024-10-02 20:21:34
sub one {
    do 'verify1.pl';
}
sub two {
    do 'verify2.pl';
}

然而,从长远来看,最好将脚本转换为模块,以便以现代且理智的方式管理复杂性。

sub one {
    do 'verify1.pl';
}
sub two {
    do 'verify2.pl';
}

In the long run, however, it is better to convert your scripts into modules in order to manage the complexity in a modern and sane fashion.

晚风撩人 2024-10-02 20:21:34

您可以按正常方式放置子例程。

sub a1 {
    ...
}
sub b1 {
    ...
}
sub a2 {
    ...
}
sub b2 {
    ...
}
sub c2 {
    ...
}
sub one {
    a1(); b1(); a1();
}
sub two {
    a2(); b2(); c2(); a2();
}
my ($arg) = @ARGV;
if ($arg == 1) {
    one();  # should do what verify1.pl did
}
elsif ($arg == 2) {
    two();  # should do what verify2.pl did
}

You can place subroutines normally as the way they should be.

sub a1 {
    ...
}
sub b1 {
    ...
}
sub a2 {
    ...
}
sub b2 {
    ...
}
sub c2 {
    ...
}
sub one {
    a1(); b1(); a1();
}
sub two {
    a2(); b2(); c2(); a2();
}
my ($arg) = @ARGV;
if ($arg == 1) {
    one();  # should do what verify1.pl did
}
elsif ($arg == 2) {
    two();  # should do what verify2.pl did
}
寻梦旅人 2024-10-02 20:21:34

我只需将所有子例程放在一个文件中并重命名任何冲突即可解决此问题。

但是,听起来您的问题是您正在针对可能遇到的每种可能的验证情况进行硬编码。更好的方法是提出一个可以动态构建验证管道的流程。因为我不知道你需要什么,所以我不知道是否有类似 Data::Constraint< /a> 或其他验证模块适合您。由于问题中的信息如此之少,很难给出任何有用的建议。

I'd resolve this by just putting all the subroutines in one file and renaming any that conflict.

However, it sounds like your problem is that you're hardcoding for every possible validation situation that you might run into. The better way is to come up with a process where you can build validation pipelines dynamically. Since I don't know what you need, I don't know if something like Data::Constraint or the other validation modules would be right for you. It's extremely tough to give any sort of useful advice with so little information in the question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文