School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,412 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,583 people online right now. Registration is fast and FREE... Join Now!




Getopt::Std

 

Getopt::Std, using Getopt in script

Sun751

23 Jun, 2009 - 11:46 PM
Post #1

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
In below code I have tried using Getopt::Std, where i am trying to check if all options are provided or not and I am trying to make "-x" ($dest_file) mandatory,

Please check my code if its up to the general standard or not? Any improvement please let me know.

use strict;
use warnings;
use Getopt::Std;

my %options;
if (scalar @ARGV < 3) {usage()};

sub usage
{
print STDERR << "EOF";
\nThis program does...
usage: $0 [-hcfo]

-h : this (help) message
-c : config file
-o : xcf release file
-x : output file

EOF
exit;
}

getopts ("hc:f:o:", \%options);
usage() if $options{h};

my $source_file = $options{o} || 'as.xcf';
my $config_file = $options{c} || 'config.ini';
my $dest_file = $options{x} || usage();

[\code]
Cheers

This post has been edited by Sun751: 23 Jun, 2009 - 11:51 PM

User is offlineProfile CardPM
+Quote Post


code_m

RE: Getopt::Std

24 Jun, 2009 - 08:58 AM
Post #2

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
I haven't mess with perl at all, but I see two things I would do different:

I may be wrong but shouldn't this be STDOUT since it's not an error? :
CODE
print STDERR << "EOF";


I would use <= here:
CODE
if (scalar @ARGV < 3) {usage()};
only because you want to be clear as to how many args you want.

This post has been edited by code_m: 24 Jun, 2009 - 09:03 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:10PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month