On Tue, May 22, 2001 at 09:37:13AM -0700, Paul wrote:
> Anybody know if there would likely be any problem with building a
> "case" statement like the folowing (without installing Switch.pm)?
>
> sub rate ($) {
> $_[0] eq 'A' ? .03 :
> $_[0] eq 'B' ? .05 :
> $_[0] eq 'C' ? .06 :
> .08; # the default
> }
> Does anyone know of any arbitrary limit on this sort of structure?
Apparently not:
print join("\n", rate('A'), rate('B'), rate('C'), rate('D'), undef);
produces:
0.03
0.05
0.06
0.08
If you were to add more rates, it'll get unmaintainable and confusing
pretty quickly. You're doing simple string comparisons. Try using
a hash instead:
my %rate = (
A => .03,
B => .05,
C => .06,
default => .08,
);
my $default_rate = .08;
sub rate ($) {
return $rate{$_[0]} || $rate{default};
}
You could also elimiate the function by examining the hash directly in your
code, depending on how you use it.
Z.