Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Process multiple filenames from command line or STDIN

by graff (Chancellor)
on Mar 18, 2016 at 23:39 UTC ( [id://1158294]=note: print w/replies, xml ) Need Help??


in reply to Process multiple filenames from command line or STDIN

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.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158294]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-25 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found