My Problem:
I wanted an easy way to ensure that the order of a list of columns read from database query could be modified, without any mistakes. Also wanted to write this data out in the same order that it was read in.
I was worried that if someone changed the select statement, they might forget to change the statement that actually reads the data.
My hopeful solution... create a single list of columns that would be used to construct the query statement, and be used to parse the query result.
Condition to this hopeful solution... I still want to use 'use strict' because it will also prevent some muck-ups.
I think I have got it!. See below
#!/usr/bin/perl -w # ------------------------------------------ # PURPOSE: # - Test if I can make a program that reads # a db table. # Maintainability issue: Want to be able to easily # manipulate the order in which the columns # are read. # # To change order of which columns are selected/read # from db only requires changing @parm_cols. # # Robustness? If a column is added to @parm_cols # without predefining the variable, use strict will # complain if one tries to use that variable.? # # ------------------------------------------ use strict; # ------------------ # define my variables # ------------------ my ( $abc, $def, $ghi, $jkl ); my @parm_cols = (); my $map_to_array; # ----------------- # define columns and order to read them # Note order of columns not same as the # my definition above. # ----------------- @parm_cols = qw( abc ghi jkl def ); # --------------------- # define select statement # --------------------- my $select = 'SELECT '.join(",",@parm_cols); # --------------------- # define how to map array which # is result of query statement to db # to individual variables # --------------------- map {$_='$'.$_} (my @tmp = @parm_cols); $map_to_array = join (",",@tmp); # --------------------- # read db (for testing purpose, just set array) # --------------------- my @array = (4,3,2,1); # --------------------- # populate my already declared # variables # --------------------- eval "($map_to_array) = ".'@array'; # --------------------- # test to see if it works # --------------------- print "$abc $def $ghi $jkl\n"; exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: map variables to arrays using as defined by a single list
by Aristotle (Chancellor) on Dec 07, 2003 at 10:11 UTC | |
|
Re: map variables to arrays using as defined by a single list
by Anonymous Monk on Dec 06, 2003 at 02:35 UTC |