Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error on bad combination of parameters

Status
Not open for further replies.

mohankrishna919

Programmer
Jan 18, 2012
14
US
#!/usr/bin/perl
#use warnings;
use strict;
use Getopt::Long;

my $level = 'full';
my $loc = 'disk';
my $disp ='bkp';

GetOptions( 'd=s' => \ my $database,
'i=s' => \ my $servername,
'level=s' => \$level,
'location=s' => \$loc,
'disposition=s' => \$disp,
);

how to throw error when I execute this perl file with parameters combination like perl name.pl -level -disposition
during execution if enter values for both these parameters how to throw error.
Please suggest, I am new to perl
 
Please stop creating multiple threads about what is basically the same problem. You already have 6 threads going from the last few days, but only covering 2 subjects!

If it is invalid to specify a "level" and a "disposition", why do you have default values defined for both of them?

Does this do what you want?

Perl:
[COLOR=#006600]#!/usr/bin/perl -w[/color]

[COLOR=#0000FF]use[/color] strict;

[COLOR=#0000FF]use[/color] Getopt::Long;

[COLOR=#0000FF]my[/color] $loc = [COLOR=#808080]'disk'[/color];
[COLOR=#0000FF]my[/color] $level;
[COLOR=#0000FF]my[/color] $disp;
[COLOR=#0000FF]my[/color] $database;
[COLOR=#0000FF]my[/color] $servername;

GetOptions( [COLOR=#808080]'d=s'[/color]           => \$database,
            [COLOR=#808080]'i=s'[/color]           => \$servername,
            [COLOR=#808080]'level=s'[/color]       => \$level,
            [COLOR=#808080]'location=s'[/color]    => \$loc,
            [COLOR=#808080]'disposition=s'[/color] => \$disp,
);

[COLOR=#FF0000]die[/color] [COLOR=#808080]"Please specify either level or disposition, not both\n"[/color] [COLOR=#0000FF]if[/color] ([COLOR=#FF0000]defined[/color]($level) && [COLOR=#FF0000]defined[/color]($disp));

[COLOR=#006600]# assign defaults[/color]
$disp=[COLOR=#808080]'bkp'[/color] [COLOR=#0000FF]if[/color] ![COLOR=#FF0000]defined[/color]($disp);
$loc=[COLOR=#808080]'disk'[/color] [COLOR=#0000FF]if[/color] ![COLOR=#FF0000]defined[/color]($loc);

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top