I always find that that it's a real drag having to supply any keyboard input (beyond a basic "y/n" confirmation) to a perl script while it's running. (Same goes for any command-line tool.) The more such input is needed, the sooner I tend to regard the tool as not worth using. (Update: Of course, I'm not referring to tools that actually involve shell-like interaction with the user, such as the perl debugger or the cpan shell; I'm just talking about tools where all the needed information can be specified at start-up.)
A decent command-line shell offers things like path-name completion (type a few characters and hit the tab key), and command history with easy recall(/edit)/rerun of previous commands, to minimize the amount of typing needed.
IMHO, the start of your script should just be:
use strict;
use warnings;
my $Usage = "Usage: $0 [-f] directory_path\n";
my $force;
if ( @ARGV > 1 and $ARGV[0] eq '-f' ) {
$force = shift;
}
die $Usage unless ( @ARGV == 1 );
my $dirname = shift;
if ( -e $dirname and ! $force ) {
die "$dirname exists. Use '-f' option to delete the existing $dir
+name\n$Usage";
}
...
That way, if the person typed an existing path name by mistake, they just recall, edit and rerun the command line. If they already know that the existing path name should be deleted/replaced, they include "-f" on the command line. Now, the operation can be run as part of in any shell script (or another perl script), and can run without manual intervention (e.g. as a scheduled task).
If there are other run-time user decisions to be handled, which should be provided by means of additional command-line options, then you'll want to get acquainted with Getopt::Long or Getopt::Std (which come as core modules with Perl).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.