gpt4 book ai didi

perl - 如何让子程序具有所需的参数?

转载 作者:行者123 更新时间:2023-12-01 23:39:12 24 4
gpt4 key购买 nike

是否可以在 Perl 中声明一个子例程,以便在调用脚本时如果未向其传递参数,脚本将无法编译?

最佳答案

我认为这种事情是你目前能做的最好的事情:

#!/usr/local/bin/perl -w
use strict;
require 5.020;
use warnings;
use feature qw( say signatures );
no warnings "experimental::signatures";

print "Enter your first name: ";
chomp(my $name1 = <STDIN>);

print "Enter your last name: ";
chomp(my $name2 = <STDIN>);

say "Calling takesOneOrTwoScalars with 2 args";
takesOneOrTwoScalars($name1, $name2);

say "Calling takesOneOrTwoScalars with 1 arg";
takesOneOrTwoScalars($name1);

say "Calling takesOneOrTwoScalars with 0 args";
takesOneOrTwoScalars();

sub takesOneOrTwoScalars($firstName, $lastName="")
{
print "Hello, $firstName";
if ($lastName)
{
say " $lastName";
}
else
{
say ", I see you didn't give your last name.";
}
}

关于perl - 如何让子程序具有所需的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46678010/

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