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.


In reply to Re: remove files by Don Coyote
in thread remove files by catfish1116

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.