【发布时间】:2018-03-01 00:36:54
【问题描述】:
我正在将 perl 脚本转换为 python。
在 perl 中的很多东西让我感到困惑之前,我还没有使用过 perl 语言。
例如,在下面, opt 首先被声明为标量,但再次声明为哈希。 %opt = ();
是否可以在 perl 中声明具有相同名称的标量和哈希?
据我所知,foreach $opt (@opts) 表示标量 opt 一个接一个地获取数组 opts 的值。
此时opt是一个数组???
另外,$opt{$opt}是什么意思?
opt outside $opt{$opt} 是一个散列,而 opt inside $opt{$opt} 是一个标量?
我很困惑,请帮助我......
sub ParseOptions
{
local (@optval) = @_;
local ($opt, @opts, %valFollows, @newargs);
while (@optval) {
$opt = shift(@optval);
push(@opts,$opt);
$valFollows{$opt} = shift(@optval);
}
@optArgs = ();
%opt = ();
arg: while (defined($arg = shift(@ARGV))) {
foreach $opt (@opts) {
if ($arg eq $opt) {
push(@optArgs, $arg);
if ($valFollows{$opt}) {
if (@ARGV == 0) {
&Usage();
}
$opt{$opt} = shift(@ARGV);
push(@optArgs, $opt{$opt});
} else {
$opt{$opt} = 1;
}
next arg;
}
}
push(@newargs,$arg);
}
@ARGV = @newargs;
}
【问题讨论】:
标签: perl