Sorry, there currently is no switch statement in Perl. It's coming in Perl 6, but for now you'll have to use a cascading if .. elsif .. elsif .. else configuration. (And yes, it really is spelled elsif.)
However, your code looks like you could use a loop and some kind of data structure.
my @Data =
(
{person => "CC", who => "My name ", func => \&p_CC },
{person => "AB", who => "AB ", func => \&p_AB },
{person => "CD", who => "friend", func => \&p_CD },
);
PERSON:
foreach $person ( keys %house )
{
foreach ( @Data )
{
if ( $_->{ person } eq $person )
{
print $_->{ who };
&{ $_ }->{ func }();
next PERSON;
}
}
# Handle else here.
}
If the person in %House is found, the print and function call will be executed, and we'll continue with the next key in the loop. Otherwise, we exhaust the possibilities of the table and fall through to the bottom of the inner loop.
And I would put the most likely array elements near the top (if possible) in order to reduce the execution time.
That's it!
--t. alex
"Of course, you realize that this means war." -- Bugs Bunny.
|