Newbie Question: Could someone show me how to implement options

A

Andreas Kahari

thanks,
Borniac

Please don't put the question in the subject only...

Untested:

#!/usr/bin/perl -w

use strict;
use warnings;

use Getopt::Std;

my %opts;

if (!getopts("hn:", \%opts) || (defined(%opts{h}) && $opts{h})) {
die "Usage: $0 -h | -n name\n";
}

if (defined($opts{n})) {
print "Hi $opts{n}!\n";
}

print "(Done)\n";


Given -h: Will print usage info and die.
Given -n name: Will print "Hi <name>!".

See "perldoc Getopt::Std".
 
B

Bill

Andreas Kahari said:
if (!getopts("hn:", \%opts) || (defined(%opts{h}) && $opts{h})) {
die "Usage: $0 -h | -n name\n";
}


You know, this brings up a question that's bugged me a while.

Why should the idiom

(defined($hash{key}) && $hash{key})

be needed, really? Is it just to prevent a syntax error or undefined warning?

If so, I'd like a

no use blah_warnings;

for it. Is there one?
 
J

Jay Tilton

(e-mail address removed) (Bill) wrote:

: You know, this brings up a question that's bugged me a while.
:
: Why should the idiom
:
: (defined($hash{key}) && $hash{key})
:
: be needed, really?

The defined() part isn't needed. The only value of $hash{key} that
would return false in that part will also return false in the second
part.

I wouldn't call that an idiom at all. The idiom would be to let
$hash{key} in boolean context return false on undef.

: Is it just to prevent a syntax error or undefined warning?

No warning is produced if $hash{key} is undefind and the defined() test
is eliminated.

: If so, I'd like a
:
: no use blah_warnings;
:
: for it. Is there one?

Yes, there is.

no warnings 'undefined';
 
J

Jay Tilton

(e-mail address removed) (Jay Tilton) wrote:

: (e-mail address removed) (Bill) wrote:
:
: : If so, I'd like a
: :
: : no use blah_warnings;
: :
: : for it. Is there one?
:
: Yes, there is.
:
: no warnings 'undefined';

Check that.

no warnings 'uninitialized';
 
B

Barry Kimelman

[This followup was posted to comp.lang.perl.misc]

Borniac_1 said:
thanks,
Borniac

#!/usr/bin/perl -w

use Getopt::Std;

%options = ( "d" => 0 , "c" => 0 , "s" => 0 , "S" => 0 ,
"t" => 0 , "m" => 0 , "D" => 0 , "p" => 0 ,
"f" => 0 );
$status = getopts("DdftTspScmy:e:",\%options);
if ( !$status ) {
die("Usage : $0 [-dftTsScmp] [-e exclude_pattern]" .
"[-y year] [pattern ... pattern]\n");
} # IF
 
B

Bart Lateur

Andreas said:
use Getopt::Std;

My preferred way it to use Getopt::Long, like this:

use Getopt::Long;
GetOptions(\my %opt, 'id=i', 'verbose', 'foo=s');

# see what we got:
use Data::Dumper;
print Dumper \%opt;

use like:

perl test.pl -verbose -id=123 -foo=abc FILES

You may use single or double hyphens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top