Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Perl Switch Errors on certain version

by Corion (Patriarch)
on Nov 29, 2005 at 09:31 UTC ( [id://512550]=note: print w/replies, xml ) Need Help??


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
    Hi Corion, I followed your suggestion and changed my code by removing switch statements to hash-key value loop; and i was thus able to convert my perl script to windows executable. Thanks for the tip :) I learned something new in Perl along the way. Regards, Mohit Vohra.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://512550]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-19 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found