So I am writing a script that will backup the given file(s) to a Backups directory. I have the portion working for checking for the directory and creating a new one if it doesn't exist, as well as copying the original file to the directory with a new name.

What I want to be able to do now is do this for multiple files that are specified either with ARGV, or if the user forgets that, from either a comma or space separated list from STDIN. I'm not sure how to do this part however. Do I somehow do this with a foreach loop, or am I supposed to use a while? Do I need to push STDIN to an array using the comma or space as the delimiter?

Here's an example of my current code:

my $filename = shift @ARGV; unless ($filename) { print "Enter file name(s) to be backed up:\n"; $filename = <STDIN>; chomp $filename; } if ( -e $filename ) { if ( -d "Backups") { # copy file to directory } else { mkdir "Backups"; # copy file to directory } }

Thanks for any help you can provide.

Edit: I now have the script working to read multiple files from the command line, and I can take the space separated list from STDIN and input into an array. I still cannot figure out how to get it to step through each element of the STDIN array like I have it working for ARGV.

My if statements have been moved into a while loop: while ($filename). I added $filename = shift before I close out the while loop. The initial portion of my code has been updated to this (second try, this time trying an if/else):

if (!@ARGV) { print "Enter file name(s) to be backed up, separated by spaces:\n" +; $filename = <STDIN>; chomp $filename; my @filenames = split(/\ /, $filename); $filename = shift(@filenames); } else { $filename = shift(@ARGV); }

In reply to Process multiple filenames from command line or STDIN by Katanya

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.