tags:

views:

1362

answers:

2

I am writing a script which is likely to be modified by users. Currently I am storing the configuration settings inside the script. It exists in the form of a hash-of-hashes.

I would like to guard against people accidentally using lowercase characters in the hash keys, because that will break my script.

It would be simple to inspect the hash keys and merely issue warnings for any keys with lowercase characters, but I would rather fix the case sensitivity automatically.

In other words, I want to convert all the hash keys in the top-level hash to uppercase.

+11  A: 

Walk through the hash and replace any lowercase keys with their uppercase equivalents, and delete the old ones. Roughly:

for my $key ( grep { uc($_) ne $_ } keys %hash ) {
    my $newkey = uc $key;
    $hash{$newkey} = delete $hash{$key};
}
Andy Lester
+7  A: 

Andy's answer is a good answer, except he ucs every key, and then ucs it again if it doesn't match.

This ucs it once:

%hash = map { uc $_ => $hash{$_} } keys %hash;

But since you spoke of users storing keys, a tie is a much more sure way, even if slower.

package UCaseHash;
require Tie::Hash;

our @ISA = qw<Tie::StdHash>;

sub FETCH { 
    my ( $self, $key ) = @_;
    return $self->{ uc $key };
}

sub STORE { 
    my ( $self, $key, $value ) = @_;
    $self->{ uc $key } = $value;
}

1;

And then in main:

tie my %hash, 'UCaseHash';

That's a show. The tie "magic" encapsulates it, so the users can't unknowingly mess with it.

Of course, as long as you're using a "class", you can pass in the config file name and initialize it from there:

package UCaseHash;
use Tie::Hash;
use Carp qw<croak>;

...

sub TIEHASH { 
    my ( $class_name, $config_file_path ) = @_;
    my $self = $class_name->SUPER::TIEHASH;
    open my $fh, '<', $config_file_path 
        or croak "Could not open config file $config_file_path!"
        ;
    my %phash = _process_config_lines( <$fh> );
    close $fh;
    $self->STORE( $_, $phash{$_} ) foreach keys %phash;
    return $self;
}

Where you would have to call it like:

tie my %hash, 'UCaseHash', CONFIG_FILE_PATH;

...assuming some constant CONFIG_FILE_PATH.

Axeman
You know, there's a module on CPAN which does this. No need to code it yourself: Hash::Case, see http://search.cpan.org/dist/Hash-Case/
bart
Your method will overwrite the entire hash, while Andy's method will only overwrite lowercased ones. Unless you expect the hash to contain a terrible lot of lowercase keys (not likely in this scenario), it is not going to be faster.
Leon Timmermans
Thanks for the heads up, bart. I forgot the first rule of Perl Club: Don't talk before you've checked CPAN. My post started out simple illustration of a tied class, and then I got fancy. Doh!
Axeman
Appreciate the point, Leon. But I don't like to throw away data already computed, it's not necessarily about speed. Mine is a more functional expression, IMO.
Axeman