问题描述
我正在尝试使用 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)
谢谢!
解决方法
Getopt::Long 具有解析程序参数并将它们读入程序和变量的明确目的。 (它也可以执行任意代码,但将其用于除参数处理之外的任何其他用途将是一种非常糟糕的做法。)
因此,一旦使用库读取 dir1,dir2,...
之类的字符串,即您选择的名称为 target
† 的参数的值,然后您拆分该字符串并获取所需的列表。然后程序可以继续处理(删除提交的目录,在这种情况下)
use Getopt::Long;
use File::Path qw(remove_tree); # better use this than system's 'rm'
...
my $target; # only need to declare
# - I strongly advise an argument with a more informative name,like
# GetOptions ("dirs-to-remove=s" => \$target);
# - Don't want to proceed with a program that deletes stuff if there is an error!
# So exit with a brief usage message (or call a function with a nicer message)
GetOptions ("target=s" => \$target)
or die "Usage: $0 [arguments...]\n"; # or call a sub for usage message
# Check these directories if you have any way to do so
my @dirs_to_remove = split /,/,$target;
# Now iterate over all those `@dirs_to_remove`
foreach my $dir_to_del (@dirs_to_remove) {
#
# Check as best as you can. A whole hierarchy is getting blown away
#
if ( -d $dir_to_del and ... ) { # check somehow
my $num_removed = remove_tree(
$dir_to_del,{ error => \my $err,safe => 1 }
);
if ($err and @$err) {
# See module docs for how to work with $err
die "Errors while removing $dir_to_del ...";
}
# print,check ...; see docs for options
}
}
请参阅文档。特别是,remove_tree
可以采用一些非常有用的选项。我发现 error
选项特别好,因为我宁愿在删除条目的程序中出现任何错误时立即退出。
该库可以在多个目标上调用,所以你可以说
my $num_removed = remove_tree( @dirs_to_remove,... );
在这种情况下,您不需要循环,只需要一个语句。但是,如果每个目录即将被删除,或者在它被删除之后,就无法检查每个目录——它们都刚刚消失。
我建议始终使用完善的 Perl 库,而不是使用系统和使用外部工具,当有库时。在这种情况下肯定有,比如 File::Path。
† 一旦这个程序被调用,一堆目录就会消失,而且可能无法恢复它们的内容。一想到这个我就很紧张。
那么为什么不使用明显明显的东西命名该命令行参数,例如
program_name --dirs-to-remove dir1,dir2...
(即使程序的当前名称本身是信息丰富且具体的!)
然后,您可以更简单地调用它,甚至可以像 program_name -d dir1,dir2...
一样调用它,因为只要参数保持明确,Getopt::Long
就允许缩短参数。 (但我不会——我宁愿让自己输入该字符串,然后在此过程中重新思考即将发生的事情。)
您的问题使其不清楚。 (例如,当人们假定它们应该匹配时,正则表达式模式在示例中将不匹配。)您提供了错误和损坏的代码,但不是代码试图实现的目标。
因此,我将从程序的基本形式开始。
use Getopt::Long;
GetOptions("target=s" => \$target)
or die("usage");
my @targets = split /,$target // "target";
for my $target (@targets) {
system("rm","-rf","--",$target);
}
您可以为此添加验证。
my %valid_targets = map { $_ => 1 } qw( target1 target2 target3 );
die("...") if !$valid_targets{$target};
system("rm",$target);
您甚至可以将关键字映射到目录。
my %targets = (
target_1 => ".../foo",target_2 => ".../bar",target_3 => ".../baz",);
my $dir = $targets{$target}
or die("...");
system("rm",$dir);
,
看起来很奇怪的一件事是:
(my $target_1,my $target_2,my $target_3) = split ',',$target;
GetOptions ("target=s" => \$target);
您希望 $target
成为用户从命令行输入的选项,以便您可以从中获取目标,但是您在调用 {{1} 之前执行 split
} 读取 GetOptions
参数的值。这对我来说听起来不对。
另外,您不应该给 -target
一个初始值。