misbehaving column names

[prev] [thread] [next] [Date index for 2004/05/14]

From: David R. Baird
Subject: misbehaving column names
Date: 12:45 on 14 May 2004






Hi, 

I've just started using Class::DBI, and it's making my life a lot 
easier already! Great tool. 

But there's always a but. 

I have a MySQL table with lots of column names that have spaces 
in them, or other characters like '-' or '.', e.g. 

    Membership No.
    E-mail
    Website address

Changing the column names is a possible, but difficult option. 
I'd prefer something Lazier. 

I can set the CDBI subclass up by escaping the column names like 
so: 

    My::Class->columns( Primary   => '`Membership No`' );

    My::Class->columns( Essential => ( '`E-mail`', 
                                       '`Website address`',
                                       'some_other',
                                       'well_behaved_name',
                                       );


But then when I try to extract the data like this: 

    foreach my $column ( sort My::Class->columns ) {
        sprintf '%s: %s', $column, $my_class_object->$column;
    }

I get no output for the ugly column names (I mean, I get column 
names like `e-mail` and `website address`, but no values). 

I've tried re-naming these columns with 

    sub accessor_name {
        my ($class, $column) = @_;
    
        $column =~ s/\s/_/g;
        $column =~ s/-/_/g;
        $column =~ s/`//g;
    
        return $column;
    }

and then calling 

    $my_class_object->website_address

which produces no error, but still no output. 

With or without the accessor_name sub, Data::Dumper shows that 
the object has double entries for the ugly column names, like so: 

    ...
    '`website address`' => undef, 
    ...
    'website address' => 'www.example.com', 
    ...


So I CAN retrieve the data by directly accessing the hashref like 
this: 

    print $my_class_object->{ 'website address' };

but that makes me feel dirty. 

Can I be Lazy and clean at the same time, or must I choose my 
poison? 

Cheers! 

David.



        -- 
        Dr. David R. Baird
ZeroFive Web Design
dave@xxxxxxxx.xx.xx
http://www.zerofive.co.uk

misbehaving column names
David R. Baird 12:45 on 14 May 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52