There's something I don't understand. You say:

...backup the given file(s)...

implying that you would do more than one file in a given run, if that's what the user wants to do, and the user makes this clear EITHER by putting two or more file names as command line args (or a wildcard pattern that matches two or more files, which amounts to the exact same thing), OR, with no command-line args, typing two or more file names and hitting <enter> after the script has started.

But then in either case, the code you posted ignores all but the first file name. That seems inconsistent with your stated purpose.

Anyway, here's how I would approach things:

my @files_to_do; if ( @ARGV and -f $ARGV[0] ) @files_to_do = @ARGV; } else { print "Enter file name(s), separated by space and/or newline:\n" i +f ( -t ); while (<STDIN>) { chomp; push @files_to_do, split //; } } die "Nothing to do\n" unless @files_to_do; # now do something that loops over @files_to_do...
Note the "if (-t)" -- that causes the prompt message to be printed only if STDIN is coming from a terminal (rather than from some other command whose output is being piped to your script).

Also note that if the user decided to type in file names after the script has started, the approach I'm suggesting here requires that he must also type an EOF control character (^D on linux/unix, ^Z (I think) on windows), so that perl knows to stop reading STDIN.

Accepting input from a pipe is great stuff - e.g. using a *n*x shell environment, you could do stuff like:

ls | my_backup.pl find some_path -name '*.foo' | my_backup.pl
and countless other useful variants (including helper commands like grep, etc., as filters on the input to your script). These make it less likely that someone would need to type file names at all -- and that's good, because typing file names is error-prone, and it's a drag (especially if you don't have access to auto-completion or wildcard globs or decent line-editing keystrokes, which is what happens when you use the default method of typing when perl is reading from the terminal keyboard).

(Update: I'll just mention that I never write a perl script such that it prompts the user to type something via STDIN after the script starts. If some input from the user is needed, and there are no command-line args, and nothing being piped to the script, my scripts will die with a "Usage: ..." synopsis, explaining what sort(s) of input are expected from the user at the point when the script is first launched.)


In reply to Re: Process multiple filenames from command line or STDIN by graff
in thread 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.