我正在尝试设置默认值,如果用户没有提供
以下是我的代码。请指出如何使用//运算符
完成use Data::Dumper;
use Getopt::Long;
my @viewFiles = ("cdl", "lef", "verilog", "lib");
my %viewPaths;
my $pctDir = ".";
GetOptions(
"pctDir=s" => \$pctDir,
"cdl=s" => \$viewPaths{"cdl"},
"lef=s" => \$viewPaths{"lef"},
"verilog=s" => \$viewPaths{"verilog"},
"lib=s" => \$viewPaths{"lib"},
);
@viewPaths{@viewFiles} //= ("$pctDir/cdl/topCellName.cdl", "$pctDir/lef/topCellName.lef", "$pctDir/model/verilog/topCellName.v", "$pctDir/model/timing/lib/topCellName.lib");
print Dumper \%viewPaths;
输出是:
$VAR1 = {
'cdl' => undef,
'verilog' => undef,
'lib' => './model/timing/lib/topCellName.lib',
'lef' => undef
};
预期产出:
$VAR1 = {
'cdl' => './cdl/topCellName.cdl',
'verilog' => './model/verilog/topCellName.v',
'lib' => './model/timing/lib/topCellName.lib',
'lef' => './lef/topCellName.lef'
};
答案 0 :(得分:3)
布尔运算符和//
对标量进行操作,因此它们的LHS在标量上下文中进行评估。这意味着
@viewPaths{@viewFiles} //= ...;
相当于
$viewPaths{ $viewFiles[-1] } //= ...;
相反,您需要在每个条目中执行此操作:
my @viewFiles = ("cdl", "lef", "verilog", "lib");
my %defaults;
@defaults{@viewFiles} = ("pctDir/cdl/topCellName.cdl", "pctDir/lef/topCellName.lef", "pctDir/model/verilog/topCellName.v", "pctDir/model/timing/lib/topCellName.lib");
$viewPaths{$_} //= $defaults{$_} for keys %defaults;
然而作为替代方案 - 预先填充哈希应该工作我认为:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use Getopt::Long;
my @viewFiles = ("cdl", "lef", "verilog", "lib");
my %viewPaths;
@viewPaths{@viewFiles} = ("pctDir/cdl/topCellName.cdl", "pctDir/lef/topCellName.lef", "pctDir/model/verilog/topCellName.v", "pctDir/model/timing/lib/topCellName.lib");
GetOptions(
"cdl=s" => \$viewPaths{"cdl"},
"lef=s" => \$viewPaths{"lef"},
"verilog=s" => \$viewPaths{"verilog"},
"lib=s" => \$viewPaths{"lib"},
);
print Dumper \%viewPaths;