Perl 使用复制、创建、删除和重命名创建菜单

Perl 使用复制、创建、删除和重命名创建菜单,perl,Perl,重命名部分还没有任何代码,但其余部分都有相应的代码 我一直认为copy和remove的变量没有声明,但我声明了。我做错了什么 #!/usr/bin/perl use warnings; use strict; sub menu { my $args = shift; my $title = $args->{title}; my $choices = $args->{choices}; while (1) { print "----------------

重命名部分还没有任何代码,但其余部分都有相应的代码

我一直认为copy和remove的变量没有声明,但我声明了。我做错了什么

#!/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});
#/usr/bin/perl
使用警告;
严格使用;
子菜单{
我的$args=班次;
my$title=$args->{title};
my$choices=$args->{choices};
而(1){
打印“--------------\n”;
打印“$title\n”;
打印“--------------\n”;
(my$i=1;$i[$i-1][0];
打印“$i.$t$itemHeading\n”;
}
打印“\n?:”;
我的$i=;咀嚼$i;
如果($i&&$i=~m/[0-9]+/&&$i[$i-1][1]}();
}否则{
打印“\n无效输入。\n\n”;
}
}
}
我的$menus={};
$menus={
"1" => {
“标题”=>“菜单1标题”,
“选择”=>[
[“创建文件”,子文件{
打印“要创建的文件名:”;
我的$createFile=;
打开我的$fh、“>>”、“$createFile”或“无法打开”\n;
打印$fh“这是您订购的文件\n”;
收盘价$fh;
打印“完成”\n;
}],
[“重命名文件”,sub{
打印“要重命名的文件(添加外部文件):”;
我的$oldName=;
打印“您想为其重命名什么:”;
我的$newName=;
}],
[“复制文件”,分{
打印“要复制的文件:”
我的$copyFile=;
打印“您想将此文件命名为什么::”;
我的$newFile=;
复制“$copyFile”、“$newFile”| | die“无法复制”;
}],
[“删除文件”,sub{
打印“要删除哪个文件”;
我的删除=;
打开我的$fh;
取消链接“$removeF”;
}],
},
};
菜单($menus->{1});

我清理了丢失的分号、美元符号、大括号、重复的行…和格式。我还添加了丢失的
use File::Copy
。您的代码一点也不遥远,但我可以看到您在许多小错误中迷失了方向。因此,这里是最重要的注意事项:

一次输入一点代码,并确保它干净地运行并按预期工作。 添加一小部分代码和测试、测试、测试。首先构建功能的小型、完整和粗糙(基本)组件,以便您能够明智地测试它们和您的设计。然后进一步开发

那么,每个错误要么容易发现,要么具有教育意义

这是您的代码(只有两个函数),经过清理,可以正常工作,还有一些其他功能

use warnings;
use strict;
use feature 'say';

use File::Copy qw(copy move);

sub menu {
    my $args = shift;
    my $title = $args->{title};
    my $choices = $args->{choices};

    while (1) {
        say $title, "\n", '-' x length $title;
        for my $i (1..@$choices) {
            my $itemHeading = $choices->[$i-1][0];
            say "$i.\t$itemHeading";
        }
        print "\n?: ";
        my $i = <STDIN>;
        chomp $i;
        if ($i && $i =~ m/[0-9]+/ && $i <= @$choices) {
            $choices->[$i-1][1]();
        } else {
            say "\nInvalid input.\n";
        }
    }
}

my $menus = {
    1 => {
        "title" => "Menu 1 header",
        "choices" => [
            [ "Create file " ,
                sub {
                    print "Name of file you want to create:  ";
                    my $createFile = <STDIN>;
                    chomp $createFile;
                    open my $fh, ">>", $createFile 
                        or die "Can't open $createFile: $!\n";
                    say $fh "Here's that file you ordered";
                    close $fh;

                    say "done";
                }
            ],
            [ "Rename a file" ,
                sub {
                    print "What file do you want to rename (add ext): ";
                    my $oldName = <STDIN>;
                    chomp $oldName;
                    if (not -e $oldName) {
                        say "\n\tNo file \"$oldName\"\n";
                        return;
                    }
                    print "What do you want to rename it: ";
                    my $newName = <STDIN>;
                    chomp $newName;          # what if it exists already?
                    move $oldName, $newName
                        or die "Can't move $oldName to $newName: $!";
                }
            ],
            [ "Quit", sub { exit } ]
        ]
    }
};

menu($menus->{1});
使用警告;
严格使用;
使用特征“说”;
使用文件::复制qw(复制移动);
子菜单{
我的$args=班次;
my$title=$args->{title};
my$choices=$args->{choices};
而(1){
说出$title、“\n”、“-”x长度$title;
对于我的$i(1..@$选项){
my$itemHeading=$choices->[$i-1][0];
说“$i.$t$itemHeading”;
}
打印“\n?:”;
我的$i=;
咀嚼$i;
如果($i&&$i=~m/[0-9]+/&&$i[$i-1][1]();
}否则{
说“\n输入无效。\n”;
}
}
}
我的$menus={
1 => {
“标题”=>“菜单1标题”,
“选择”=>[
[“创建文件”,
潜艇{
打印“要创建的文件名:”;
我的$createFile=;
chomp$createFile;
打开我的$fh,“>>”,$createFile
或“无法打开$createFile:$!\n”;
说$fh“这是您订购的文件”;
收盘价$fh;
说“完成”;
}
],
[“重命名文件”,
潜艇{
打印“要重命名的文件(添加外部文件):”;
我的$oldName=;
chomp$oldName;
如果(非-e$oldName){
说“\n\t无文件\”$oldName \“\n”;
返回;
}
打印“您想为其重命名什么:”;
我的$newName=;
chomp$newName;#如果它已经存在怎么办?
移动$oldName,$newName
或死亡“无法将$oldName移动到$newName:$!”;
}
],
[“退出”,sub{exit}]
]
}
};
菜单($menus->{1});
我还添加了退出选项和一些错误检查

一些注释

  • 很少需要C风格的
    for
    循环

  • 子名称前面的
    &
    有一个非常特殊的用途。通常不需要它

  • 打印错误时添加
    $!
    ,以实际查看错误,和/或使用其他


  • 当数组在标量上下文中时,它的长度是使用的元素数;因此在您的条件下不需要
    scalar
    ,只要
    1..@$choices
    $i我就清除了缺少的分号、美元符号、大括号、重复的行…和格式。我还添加了缺少的
    use File::Copy
    。您的代码一点也不遥远,但我可以看到您在许多小错误中迷失了方向。因此,这里是最重要的注意事项:

    一次输入一点代码,并确保它干净地运行并按预期工作。 添加一小部分代码和测试、测试、测试。首先构建功能的小型、完整和粗糙(基本)组件,以便您能够明智地测试它们和您的设计。然后进一步开发

    那么,每个错误要么容易发现,要么具有教育意义

    这是您的代码(只有两个函数),经过清理,可以正常工作,还有一些其他功能

    use warnings;
    use strict;
    use feature 'say';
    
    use File::Copy qw(copy move);
    
    sub menu {
        my $args = shift;
        my $title = $args->{title};
        my $choices = $args->{choices};
    
        while (1) {
            say $title, "\n", '-' x length $title;
            for my $i (1..@$choices) {
                my $itemHeading = $choices->[$i-1][0];
                say "$i.\t$itemHeading";
            }
            print "\n?: ";
            my $i = <STDIN>;
            chomp $i;
            if ($i && $i =~ m/[0-9]+/ && $i <= @$choices) {
                $choices->[$i-1][1]();
            } else {
                say "\nInvalid input.\n";
            }
        }
    }
    
    my $menus = {
        1 => {
            "title" => "Menu 1 header",
            "choices" => [
                [ "Create file " ,
                    sub {
                        print "Name of file you want to create:  ";
                        my $createFile = <STDIN>;
                        chomp $createFile;
                        open my $fh, ">>", $createFile 
                            or die "Can't open $createFile: $!\n";
                        say $fh "Here's that file you ordered";
                        close $fh;
    
                        say "done";
                    }
                ],
                [ "Rename a file" ,
                    sub {
                        print "What file do you want to rename (add ext): ";
                        my $oldName = <STDIN>;
                        chomp $oldName;
                        if (not -e $oldName) {
                            say "\n\tNo file \"$oldName\"\n";
                            return;
                        }
                        print "What do you want to rename it: ";
                        my $newName = <STDIN>;
                        chomp $newName;          # what if it exists already?
                        move $oldName, $newName
                            or die "Can't move $oldName to $newName: $!";
                    }
                ],
                [ "Quit", sub { exit } ]
            ]
        }
    };
    
    menu($menus->{1});
    
    使用警告;
    严格使用;
    使用特征“说”;
    使用文件::复制qw(复制移动);
    子菜单{
    我的$args=班次;
    my$title=$args->{title};
    my$choices=$args->{choices};
    而(1){
    说出$title、“\n”、“-”x长度$title;
    对于我的$i(1..@$选择