Printable Version of Topic

Click here to view this topic in its original format

Dream.In.Code _ Perl and Python _ Getopt::Std

Posted by: Sun751 23 Jun, 2009 - 11:46 PM

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

Posted by: code_m 24 Jun, 2009 - 08:58 AM

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.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)