是否可以在 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/