【发布时间】:2021-03-02 15:53:34
【问题描述】:
我正在尝试使用 Getopt::Long 删除一些目录。 我想要做的是在一个目录中运行脚本并只删除我指定的目录(和所有文件)(target_1、target_2、target_3)。 我想这样执行它-
cleanup_script.pl -target target_1,target_2,target_3
这是我到目前为止写的 -
#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;
#Logs
open(STDOUT, '>', 'results.log') or die "Can't open log";
open(STDERR, '>', 'errors.log') or die "Can't open log";
#Input
use Getopt::Long;
my $target = "target";
(my $target_1, my $target_2, my $target_3) = split ',', $target;
GetOptions ("target=s" => \$target);
#Check and delete
if ($target_1 =~ '/target_1'){
system("rm -rf $target_1")};
if ($target_2 =~ '/target_2'){
system("rm -rf $target_2")};
if ($target_3 =~ '/target_3'){
system("rm -rf $target_3")};
errors.log -
Use of uninitialized value $target_1 in pattern match (m//) at
cleanup_script.pl line 22 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl will try to tell you the
name of the variable (if any) that was undefined. In some cases it cannot
do this, so it also tells you what operation you used the undefined value
in. Note, however, that perl optimizes your program and the operation
displayed in the warning may not necessarily appear literally in your
program. For example, "that $foo" is usually optimized into "that "
. $foo, and the warning will refer to the concatenation (.) operator,
even though there is no . in your program.
Use of uninitialized value $target_3 in pattern match (m//) at
cleanup_script.pl line 26 (#1)
谢谢!
【问题讨论】:
-
请永远不要说你有错误,然后就这样。剪切并粘贴确切的错误,以便我们可以看到它所说的内容。如果我们看不到错误,我们就无法判断问题所在。这就像把你的车开到修理工那里,然后说“这辆车发出了噪音”,但没有说出噪音是什么。
-
另外,不清楚您的代码的意图是什么。你能用英语解释一下你希望你的 Perl 代码做什么吗?
-
你说得对,安迪,编辑了所有信息