[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Omaha.pm] uppercasing some things in complex nested structures
This isn't recursive or general use or anything, but it's what I came up with to uppercase specific things in 16 different use cases in some software I'm working on today... I thought it was pretty clean given the task at hand. Feedback welcome. :)
j
=head2 uppercase_everything
RT7835 - Uppercase everything sent to OCIS
$self->uppercase_everything($qualifiers);
=cut
sub uppercase_everything {
my ($self, $qualifiers) = @_;
foreach my $key (keys %$qualifiers) {
# these two have to remain lower case or OWS pukes
next if ($key =~ /^(primary|preferred)$/);
if (ref($qualifiers->{$key}) eq "ARRAY") {
# Ack. Sometimes an arrayref is handed in.
for (@{$qualifiers->{$key}}) {
if (ref $_ eq "HASH") {
# Ack. Hashref inside the arrayref (e.g. UserDefinedValues)
my $href = ""> foreach my $key (keys %$href) {
$href->{$key} = uc $href->{$key};
}
} else {
# Simple scalars in our arrayref (e.g. AddressLine)
next unless (defined $_);
tr/a-z/A-Z/;
}
}
} elsif (ref($qualifiers->{$key}) eq "HASH") {
# Are we supposed to uppercase any hashrefs?
} else {
# Simple scalar
$qualifiers->{$key} = uc $qualifiers->{$key};
}
}
#warn Dumper($qualifiers);
return 1;
}
$ cat Common.t
use Test::More tests => 8;
# Have to instantiate any of the children to test Common...
use Omni2::Model::MF::OWS::Name;
my $c = Omni2::Model::MF::OWS::Name->new();
# Test uppercase_everything().
# 'lower' values should stay lowercase. 'upper's should turn to 'UPPER'
my $in = {
primary => 'lower',
test1 => 'upper',
preferred => 'lower',
test2 => [ 'upper', 'upper'],
test3 => [ { test4 => 'upper', test5 => 'upper' } ]
};
ok($c->uppercase_everything($in), 'uppercase_everything()');
is($in->{primary}, 'lower', 'primary');
is($in->{test1}, 'UPPER', 'test1');
is($in->{preferred}, 'lower', 'preferred');
is($in->{test2}->[0], 'UPPER', 'test2');
is($in->{test2}->[1], 'UPPER', 'test2');
is($in->{test3}->[0]->{test4}, 'UPPER', 'test4');
is($in->{test3}->[0]->{test5}, 'UPPER', 'test5');
$ perl Common.t
1..8
ok 1 - uppercase_everything()
ok 2 - primary
ok 3 - test1
ok 4 - preferred
ok 5 - test2
ok 6 - test2
ok 7 - test4
ok 8 - test5