in reply to remove files
I created a script that would remove 1 file
The script you created will remove as many files you supply as arguments to your script.
my @removed_files = <STDIN> will create an array over the supplied input, split on $/ IFS (Input Field Separator).
changing the array sigil @ to a scalar sigil $ will take only the next line of input based on the $/ value. my $removed_files = <STDIN>.
This may not gaurantee that only one file will be passed to unlink, depending on how your code evolves. You should validate the data to ensure you are getting only the number of files you expect.
update 26 Nov I did not clearly explain why it was your script is not doing what you think it is doing.
The reason that your script will remove as many files you supply as arguments to your script is that your foreach loop uses the Perl special variable @ARGV, while your filelist is input from the STDIN filehandle. ++hippo
The distinction is that the arguments in @ARGV are recieved from the command line when you run the script. On some systems the terminal is called a command line, so this may be a source of confusion. The command line here, is the line that you pass to the terminal to invoke the command.
#command line passed to the terminal > cl_args.pl myfile1.txt myfile2.txt myfile3.txt
# cl_args.pl use strict; use warnings; @ARGV == 3 and print @ARGV; # @ARGV is an array that can be be assigned to within your script @ARGV = ( 'myfile4.txt' ); @ARGV == 1 and print @ARGV;
The STDIN filehandle is the input that the process has access to once it has been invoked. Again command and process can be synonyms so take care with these terms. When you access STDIN from within your program using the input operator < >, you are setting the process input to come from the terminal.
While it is pertinent to validate data before doing anythng with it, command line arguments (from @ARGV), are processed by the shell before entering the process, whereas the program is responsible for validating data recieved from STDIN
There is some crossover between the two, but this hopefully helps the reader to understand what is going on here a little better.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: remove files
by catfish1116 (Beadle) on Nov 25, 2019 at 17:40 UTC | |
by haukex (Archbishop) on Nov 25, 2019 at 21:42 UTC | |
by Don Coyote (Hermit) on Nov 25, 2019 at 21:32 UTC | |
by haukex (Archbishop) on Nov 25, 2019 at 21:43 UTC | |
by Don Coyote (Hermit) on Nov 25, 2019 at 22:22 UTC |