我的一个同事写了一个perl脚本,要求用户的windows域名/用户名,当然我们输入以下格式domainnameusername
。然后,Getopt:Long模块将其转换为去掉''字符的字符串,并使字符串不正确。当然,我们可以要求所有用户以domainname\username
的形式输入他们的域名/用户组合,但我真的不想因为"修复用户,而不是程序"而感到内疚。我们还使用我们为此制作的模块,我将其称为OurCompany::ColdFusionAPI
,因为它访问ColdFusion。
我们的代码是这样的:
#!/usr/bin/perl
use common::sense;
use Getopt::Long;
use OurCompany::ColdFusionAPI;
my ($server_ip, $username, $password, $need_help);
GetOptions (
"ip|server-address=s" => $server_ip,
"user-name=s" => $username,
"password=s" => $password,
"h|help" => $need_help,
);
$username ||= shift;
$password ||= shift;
$server_ip ||= shift;
if (!$server_ip or $need_help){
print_help();
exit 0;
}
my $print_hash = sub { my $a = shift; say "$_t=> $a->{$_}" foreach keys %$a; };
...
如果我添加say $username
行,那么它只是给出没有''的字符串。我怎样才能让perl保持""?类似bash.
read -r
行您的shell会这样做,而不是Getopt::Long。您需要转义,以便shell将其解释为文字反斜杠,而不是试图转义某些内容。
您确定这是由于Getopt::Long?很可能您的shell已经在解析您输入的内容,并且弄乱了反斜杠。
为什么不分别询问Domain和Username ?这样可以比较优雅地解决问题。