【发布时间】:2013-02-19 09:42:46
【问题描述】:
我目前正在将一些脚本从 csh 翻译成 perl。我遇到了一个具有以下开关控制的脚本
#And now some control
set get_command = h
set finish = 0
while (1)
switch ($get_command)
case "h":
case "H":
set cine_command = ""
cat << EOF
Control synchronised cine by (case insensitive):
A - A view data
B - B view data
a - accelerate
d - decelerate
r - real time heart rate
<num> - rate (frames per second)
i - toggle interpolation
s - step through (may lose a little synchronisation)
c - continue (restart) after stepping
y - reverse direction
h - help (repeat this)
f - finish (quit)
q - quit (finish)
<return> - quit
EOF
breaksw
case "":
case "f":
case "F":
case "q":
case "Q":
set cine_command = '-f'
set finish = 1
breaksw
case "a":
set cine_command = '-a'
breaksw
case "d":
case "D":
set cine_command = '-d'
breaksw
case "r":
case "R":
set cine_command = "-t $time_per_frame"
breaksw
case "i":
case "I":
set cine_command = '-i'
breaksw
case "s":
case "S":
set cine_command = "-s"
breaksw
case "c":
case "C":
set cine_command = "-c"
breaksw
case "y":
case "Y":
set cine_command = "-y"
breaksw
case '[0-9]*':
set cine_command = "-r $get_command"
breaksw
default:
echo "$get_command ignored"
set cine_command = ""
endsw
if ('$cine_command' != '') then
select_tv $FIRST_TV
cine $cine_command
select_tv $SECOND_TV
cine $cine_command
endif
#
# If we're stopping then get out of this loop.
#
if ($finish) break
echo -n "cine > "
set get_command = $<
end
我的系统上安装了 Perl 5.8.8 并使用 use Strict;,我知道它可能会在下一个 perl 版本中被弃用,我尝试了以下方法
#Add some fine control to script
my $get_command = 'h';
my $finish = 0;
my $cine_command;
while(<>)
{
switch ($get_command)
{
case [hH] {$cine_command = "";}
print STDOUT << 'END';
Control synchronised cine by (case insensitive):
A - A view data
B - B view data
a - accelerate
d - decelerate
r - real time heart rate
<num> - rate (frames per second)
i - toggle interpolation
s - step through (may lose a little synchronisation)
c - continue (restart) after stepping
y - reverse direction
h - help (repeat this)
f - finish (quit)
q - quit (finish)
<return> - quit
END
case [fFqQ]
{
$cine_command = '-f';
$finish = 1;
}
case "a"
{
$cine_command = '-a';
}
case [dD]
{
$cine_command = '-d';
}
case [rR]
{
$cine_command = "-t $time_per_frame";
}
case [iI]
{
$cine_command = '-i';
}
case [sS]
{
$cine_command = '-s';
}
case [cC]
{
$cine_command = '-c';
}
case [yY]
{
$cine_command = '-y'
}
case /\d/
{
$cine_command = "-r $get_command";
}
else
{
print "$get_command ignored\n";
$cine_command = "";
}
if ($cine_command ne "")
{
`select_tv $FIRST_TV`;
`cine $cine_command`;
`select_tv $SECOND_TV`;
`cine $cine_command`;
}
exit if( $finish == 1);
print STDOUT "cine > \n";
chomp(my $get_command = <STDIN>);
}
}
当我按下回车键时,我会在终端上打印所需的选项。但是,当我在 STDIN 中输入任何选项时——例如 a、h 或 d——我没有得到任何响应。当我输入 retirn - 我得到消息“h被忽略”按预期打印到终端。
有什么想法吗?
【问题讨论】:
-
"Perl 5.8.8 … 在下一个 perl 版本中已弃用" — 你错过了下一个 Perl 版本。还有其他几个。我们现在使用 Perl 5.16.2。
Switch.pm(我假设你的意思是Strict)在 5.14 中从核心中删除。升级并使用given/when(从 5.10 开始可用,尽管您需要更新的、错误更少的版本)。 -
@Quentin 是的,我想是的。我使用的是 red hat,perl 5.8.8 是最新版本
-
安装一个较新的。使用Perlbrew 将其从系统 Perl 中分离出来。 (因为 (1) 与 Perl 系统混用会破坏脆弱的发行版管理脚本,并且 (2) Red Hat 在打包 Perl 方面的记录非常差)。
-
您的错误消息声称来自“sh”,即 Bourne Shell,而不是来自 perl 或 csh。正如我所见,没有“#!”行,你确定要调用 perl 吗?
-
我建议您完全绕过开关处理并使用 Getopt::Long 正确处理命令选项。