sub a_query_builder{ my ($table_name, $display_cols_ref, $where_cols_ref) = @_; if ($where_cols_ref->{'year_min'}) { push @clauses, "Year >= $where_cols_ref->{'year_min'}" } if ($where_cols_ref->{'year_max'}) { push @clauses, "Year <= $where_cols_ref->{'year_max'}" } # ... $clause = join(" AND ", @clauses); $display_cols = join(",", @$display_cols_ref); $sth = $dbh->prepare("SELECT $display_cols FROM $table_name WHERE $clause"); }