Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: How to use getopts?

by bv (Friar)
on Mar 09, 2010 at 19:25 UTC ( #827614=note: print w/replies, xml ) Need Help??

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!

print map{substr'hark, suPerJacent other l',$_,1}(11,7,6,16,5,1,15,18..23,8..10,24,17,0,12,13,3,14,2,4);

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2020-10-01 18:59 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (18 votes). Check out past polls.