【发布时间】:2013-02-05 03:42:28
【问题描述】:
我有以下 Perl 代码来提示用户回答是/否。如果用户输入的不是是/否,请继续提示。没有其他词是可以接受的。我不知道为什么这段代码不起作用。我测试了答案“noooooo”,我期待它再次提示,但它没有进入 while 循环。
谁能帮忙找出我的错误?
@files = A, B, C;
foreach $c (@files) {
if (-e $c ) {
print " $c already exists. Do you want to overwrite? (yes or no): ";
chomp ($file_yes_no = <STDIN>);
while ($file_yes_no !~ m/yes{1}|no{1}/i ) {
print "$c already exists. Do you want to overwrite? (yes or no): ";
chomp ($file_yes_no = <STDIN>);
}
if ($file_yes_no =~ m/yes/i ) {
if (system ("cp -f /home/old_path/ /home/new_path/ == 0) {
print "$c successfully copied;
} else {
die "Error: Copy failed, Check the message above";
}
}
else { print "No files copied\n; }
【问题讨论】:
-
使用
File::Copy复制文件。use File::Copy qw(copy); copy($src, $dest) or die $!; -
do { print ...; chomp ... } while condition;怎么样。更好的是,p3rl.org/IO::Prompt -
你在用
use warnings; use strict;吗?
标签: perl