在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.";
}
}