note
TGI
Hide the complexity behind an interface.
<c>
package My::FieldMap;
our %INTERNAL_VALUES = (
'foo|bar' => 'fb_val'
);
our %INTERNAL_NAMES = (
'foo|bar' => 'fb_val'
);
sub _build_key {
return lc( join '|', @_);
}
sub get_internal_name_value {
shift if $_[0] eq __PACKAGE__; # Can call as pkg method
my $ext_name = shift;
my $ext_value = shift;
my $composite_key = _build_key( $ext_name, $ext_value );
# probably want to put some error checking code here.
# What do you do if called in scalar context?
# What if one or both keys don't exist?
return (
$INTERNAL_NAMES{$composite_key},
$INTERNAL_VALUES{$composite_key}
);
}
</c>
<p>Use it like this:
<c>
my ($in,$iv) = My::FieldMap->get_internal_name_value( $ext_name, $ext_value );
# or
my ($in,$iv) = My::FieldMap::get_internal_name_value( $ext_name, $ext_value );
</c>
<p>
Now, when you have to deal with millions of field/name pairs, and you move to Redis or memcached to store them, you just have to update the lookup function.
</p>
<p>A good interface with horrible code behind it is 10000% better than the most elegant snippets sprinkled everywhere.
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-25825">
<P><BR>TGI says <B>moo</B></P>
</div></div>
985798
985798