http://www.perlmonks.org?node_id=790065


in reply to if/else options

Something like
my @where; my $sql = 'select * from table'; push @where, 'column1 = "' . $var1 . '" ' if defined $var1; push @where, 'column2 = "' . $var2 . '" ' if defined $var2; push @where, 'column3 = "' . $var3 . '" ' if defined $var3; $sql = $sql . ' where ' . join(' and ', @where);
would do the trick.

When you put the values into an array, it would be even better:

my @values = (undef, $var1, $var2, $var3); my @where; my $sql = 'select * from table'; foreach my idx (1..3) { push @where, qq(column$dx = "$values[$idx]") if defined $values[$idx +]; } $sql = $sql . ' where ' . join(' and ', @where);