perl - 我可以在 perl 子原型(prototype)中使变量可选吗?

标签 perl

我想了解是否可以在其中包含子原型(prototype) 可选参数。有了原型(prototype),我可以做到这一点:

sub some_sub (\@\@\@) {
    ...
}

my @foo = qw/a b c/;
my @bar = qw/1 2 3/;
my @baz = qw/X Y Z/;

some_sub(@foo, @bar, @baz);

这很好,可读性强,但在我尝试做的那一刻

some_sub(@foo, @bar);

甚至

some_sub(@foo, @bar, ());

我收到错误:

Not enough arguments for main::some_sub at tablify.pl line 72, near "@bar)"

Type of arg 3 to main::some_sub must be array (not stub) at tablify.pl line 72, near "))"

是否可以有一个原型(prototype)可变数量的参数?还是可以通过签名实现类似的东西?

我知道这可以通过始终传递 arrayrefs 来完成我想知道是否还有其他方法。毕竟,TMTOWTDI。

最佳答案

分号后的所有参数都是可选的:

sub some_sub(\@\@;\@) {
}

关于perl - 我可以在 perl 子原型(prototype)中使变量可选吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66889565/

相关文章:

mysql - perl脚本同时连接两个数据库

perl - 使用没有 "Subroutine redefined"的 PDL::常量

regex - s///返回错误的换行符

r - 如何从 R 运行带有特定模块的 perl 脚本?

linux - 为什么 strftime 大写标志 '^' 不起作用?

regex - grep 与 Perl。我应该修改它来工作,还是应该用 Perl 代码替换它?

perl - 在perl中执行命令时使用管道

php - Perl 'bless' 到 PHP?

multithreading - 跟踪后台进程?

perl - 使用标志使 Perl 脚本 * 自行运行 *