NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
Aack! That's ugly (Score:2)
No, I wouldn't use a switch-like construct there. What's the point of a label when there's a much cleaner solution? I would probably use a dispatch table like the following:
my %dispatch = (
foo => \&foo,
bar => \&bar,
baz => \&baz
);
#later
if ( exists $dispatch{$function} ) {
$dispatch{function}->(@args);
}
else {
die "No such function ($function)";
}
I don't know, maybe it's just me, but that seems much cleaner and
Re:Aack! That's ugly (Score:2)
Re:Aack! That's ugly (Score:2)
I humbly submit an example of when SWITCH can go horribly wrong. The temptation to use labels for control flow when they are not required makes bugs like this more likely.
while ( my $data = $t_sth->fetchrow_arrayref ) { /= PRECISION;
my ( $amt, $id ) = @$data;
$amt
SWITCH: {
$id == $CASH && ($tcash += $amt) && last SWITCH;
$id == $ACCOUNT
Re:Aack! That's ugly (Score:2)
Maybe I can turn this into a "THAT IS FIRE, DO NOT TOUCH THE FIRE" ;-)
Reply to This
Parent