I'm trying to delete a few directories using Getopt::Long. What I want to do is run the script in a directory and delete only the directories (and all the files) I specified (target_1, target_2, target_3). I want to execute it like that -
cleanup_script.pl -target target_1,target_2,target_3
Here is what I wrote so far -
#!/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)
Thanks!