重命名部分还没有任何代码,但其余的代码都有它的代码。制作一个带有复制,创建,删除和重命名的菜单
我总是得到复制和删除的变量没有声明,但我做到了。我究竟做错了什么?
#!/usr/bin/perl
use warnings;
use strict;
sub menu {
my $args = shift;
my $title = $args->{title};
my $choices = $args->{choices};
while (1) {
print "--------------------\n";
print "$title\n";
print "--------------------\n";
for (my $i = 1; $i <= scalar(@$choices); $i++) {
my $itemHeading = $choices->[$i-1][0];
print "$i.\t $itemHeading\n";
}
print "\n?: ";
my $i = <STDIN>; chomp $i;
if ($i && $i =~ m/[0-9]+/ && $i <= scalar(@$choices)) {
&{$choices->[$i-1][1]}();
} else {
print "\nInvalid input.\n\n";
}
}
}
my $menus = {};
$menus = {
"1" => {
"title" => "Menu 1 header",
"choices" => [
[ "Create file " , sub {
print "Name of file you want to create: ";
my $createFile = <STDIN>;
open my $fh, ">>", "$createFile" or die "can't open\n";
print $fh "Here's that file you ordered\n";
close $fh;
print "done\n";
}],
[ "Rename a file" , sub {
print "What file do you want to rename (add ext): ";
my $oldName = <STDIN>;
print "What do you want to rename it: ";
my $newName = <STDIN>;
}],
[ "Copy file" , sub {
print "What file do you want to copy: "
my $copyFile = <STDIN>;
print "What do you want to call this file: :";
my $newFile = <STDIN>;
copy "$copyFile" , "$newFile" || die "can't copy";
}],
[ "Remove file" , sub {
print "Which file do you want to remove";
my removeF = <STDIN>;
open my $fh;
unlink "$removeF";
}],
},
};
menu($menus->{1});
与陌生/不一致/缺乏压痕有什么关系?当你无法提出一个很好的格式化版本时,期待其他人阅读你的代码是很粗鲁的! –
我已经清理了你的代码。请在将来自己做。一致而有意义的缩进是一个重要的自我文档工具。 –