I think your basic question is solved, and
ysth has warned you about a possible risk with your open() statements. I have a slightly different admonishment about getting the file names (and min/max params):
Use @ARGV, and get the file names, and min/max values as well, from the command line, instead of forcing the user into a dialog with printed prompts and keyboard input via STDIN. You can look at the Getopt::Long and Getopt::Std modules to see how most people do this sort of thing, or you can easily "roll your own" -- something like this:
my $Usage = "Usage: $0 --min MIN --max MAX --in INFILE --out OUTFILE\n
+";
my %args;
while ( @ARGV >= 2 ) {
if ( $ARGV[0] =~ /^--(min|max|in|out)$/ ) {
my $opt = substr( shift, 2 );
$args{$opt} = shift;
} else {
last;
}
}
die $Usage if ( @ARGV or ( keys %args != 4 ));
# all 4 args are mandatory, and only these 4 args are allowed
# now use $args{in} instead of $input, etc...
(But really, the Getopt modules are worthwhile -- reading their man pages is time well spent.)
There are several reasons for using command-line args in @ARGV instead of a runtime dialog with the user:
- Most command line shells provide a "command history" feature that lets you recall, modify and re-execute prior commands; your program will be a lot easier and quicker to use if it gets everything it needs from the command line.
- Most shells provide pretty good line-editing functions (moving back and forth on the line before executing the command, deleting / cutting / pasting characters or words, etc) whereas the handling of STDIN within a perl script may provide very limited editing abilities (unless you learn about and use a suitable Term module).
- On the command line, you can alter/correct any arg at any time before starting execution; but if you're typing in single answers to questions at runtime and notice a mistake after entering any one of the answers, you just have to quit and start over (very frustrating).
- If the program gets everything it needs from the command line, you can "automate" it -- i.e. run it from some other script; but if you have to carry on a dialog after it starts, running it via some other script becomes a fairly complicated and tricky business.
- (update -- forgot to mention:) Many shells provide "file name completion" on the command line, whereby you simply type the first few characters of a name, hit the tab key or something, and the rest of the file name is filled in for you (or you get a list of files that match what you've typed so far); again, you would only get this feature within a perl script if you learned and used one of the Term modules (which I haven't done yet, because @ARGV handles pretty much everything I ever need).
Note that the ordering of arg flags is flexible: "--min 0" can come before or after "--max 10", and likewise for the file name args; all that matters is that each value comes immediately after its flag.
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.