Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to use getopts?

by bv (Friar)
on Mar 09, 2010 at 19:25 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-28 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found