in reply to Perl Switch Errors on certain version
Do not use Switch.pm. It is a bad idea that has gone too far. Switch.pm will introduce hard-to-track bugs into your code by its mere presence. Use a simple table lookup or elsif-based dispatch instead. For further reference, see Categorized Damian Modules.
In your case, you have two ways of progress - you need to decide which one suits your programming style or the problem better:
if ($subkey == 1) { $acc = $messagebin{$file}{$subkey}; if($debug){&writelog(3,"DEBUG: Account=$acc");} } elsif ($subkey == 11) { $ordid = $messagebin{$file}{$subkey}; if($debug){&writelog(3,"DEBUG: OrderId=$ordid"); } elsif ($subkey == 14) { ... } else { # you should _always_ have an else branch warn "Unhandled subkey value $subkey."; };
or, if your subkeys are more of a tabular structure:
my $acc; my %handler = ( 1 => sub { $acc = $messagebin{$file}{1}; }, 11 => sub { ... }, ); if (my $code = $handler{+$subkey}) { $code->($subkey); # I found it convenient to have the dispatch value # available in the dispatch handler } else { warn "Unhandled subkey value $subkey"; };
In your case, you seem to be filling some data structure, so you might need to create the handler hash and the subroutines in a scope which sees the tables. This might or might not be convenient in your case.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl Switch Errors on certain version
by holli (Abbot) on Nov 29, 2005 at 10:42 UTC | |
Re^2: Perl Switch Errors on certain version
by Anonymous Monk on Dec 16, 2010 at 11:11 UTC |