JackTheRussel
Programmer
Hi.
I have program where user can give parameters on the command line.
Program includes lots of if, elsif, else clauses and it is quite messy. What I could do ?
example
#Here I have GetOpt module. User give parameters, and values goes to variables.
#example if user write -a 17 then variable $age=17
I have program where user can give parameters on the command line.
Program includes lots of if, elsif, else clauses and it is quite messy. What I could do ?
example
#Here I have GetOpt module. User give parameters, and values goes to variables.
#example if user write -a 17 then variable $age=17
Code:
my $opt = new Getopt::Compact
(name => 'program',
struct =>
[[[qw(d days)], qq(start and end days), '=s', \my @days],
[[qw(n name)], qq(persons name), '=s', \my $name],
[[qw(a age)], qq(persons age), '=i', \$age],
[[qw(l location)], qq(location), '=s', \my $location],
etc...
]
)->opts;
#Here I chech what parameters user has give
#Example if user gave -a parameter -> $parameters{age} = 1
#else $parameters{age} = 0 etc..
if (@days ne ""){$parameters{date} = 1; } else{$parameters{date} = 0; }
if ($name ne ""){$parameters{name} = 1; } else{$parameters{name} = 0; }
if ($age ne ""){$parameters{age} = 1; } else{$parameters{age} = 0; }
if ($location ne ""){$parameters{location} = 1; } else{$parameters{location} = 0; }
etc...
#Here I check how many parameters user has gave
#If user didin't gave any parameters ->exit(0)
for my $key (keys(%parameters)){$total += $parameters{$key}}
if($total == 0)
{
exit(0)
};
# AND HERE WE START TO CREATE SQL-CLAUSES
#this is basic clause and we bond other clauses to this.
my $sql = SELECT something, something FROM TABBLE WHERE 1=1
#if name parameter has given bond this clause
if ($name ne ""){$sql .= " AND name = '$name'";}
#if age parameter has given bond this clause.
if ($age != ""){$sql .= " AND age = $age";}
# if location parameter has given bond this clause.
if ($location ne ""){$sql .= " AND location = $location";}
AND SO ON.. Actually I have 15 parameters so
in the end code is very,very messy.
Do you have cure for this problem? I would be very pleased if somebody could help me!.