Andy's answer is a good answer, except he uc
s every key, and then uc
s it again if it doesn't match.
This uc
s 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
.