in reply to How to use getopts?
Your code has some other issues, which I'll get to in a bit, but regarding Getopt, here are some ideas:
1. Call GetOptions early, so that you can validate inputs. You are prompting for input even when the user provides groupDir as an argument. Instead, call GetOptions and then prompt if $groupDir is not set.
2. Check for failure of GetOptions in case the user provided an incorrect option. This is easiest like this: GetOptions('slopefile=s' => \$s) or die "Invalid option";
3. Take advantage of the Long part of Getopt::Long and use long option names. It won't prevent the user from using something like -s if they want to, but it makes your code (and your documentation) more readable to allow -slopefile.
Lastly, a few words about your code in general. You should always use strict; use warnings;. Use the 3-argument form of open, with lexical (my) filehandles, and use or, not || for or die error checking. This would change your code to look like this:
open my $userinput, '<', $groupDir or die "Error opening $groupDir: $!";Your use of the ternary operator (?:)could be changed to the more straightforward defined-or assignment operator, //=. If you have an older version of perl than 5.10.0, logical-or ||= may work in most cases, but may cause bugs in others.
Best of luck with your program!
|
|---|