Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: switch stattement

by leighsharpe (Monk)
on Aug 04, 2009 at 03:21 UTC ( [id://785630]=note: print w/replies, xml ) Need Help??


in reply to switch stattement

You're going to be told repeatedly that "In perl, there's more than one way to do it.". And, there is more than one way to do what you want.
Are you using strict and warnings in your example? The first thing that jumps out at me is that you may have a problem with scoping. If you have a
my $Folder; my $Readme;
Before your switch statement, the results may be somewhat different to what you are seeing. (by the way, just what is it doing?)

You may also want to consider using a hash, if you don't have too many options:
my %folder_options=('170'=>'winvista', '173'=>'winvista64', '119'=>'winvxp', '210'=>'winxp64', '541'=>'windows*7 graphics', '525'=>'winvista64'); my $Folder=$folder_options{$operatingsystem};

Another option is to use something like this:
my $Folder; my $Readme; SWITCH: { ($operatingsystem eq '170') and do { $Folder='winvista'; $Readme='readme_winvista.txt'; last SWITCH; } ($operatingsystem eq '173') and do { $Folder='winvista64'; $Readme='readme_winvista64.txt'; last SWITCH; } }
etc. etc.
Or, as already mentioned, use Switch.
use Switch; my $Folder; my $Readme; switch ($operatingsystem) { case '170' { $Folder='winvista'; $Readme='readme_winvista.txt'; } case '173' { $Folder='winvista64'; $Readme='readme_winvista64.txt'; } }
etc. etc.
As a final note, if you're using 5.10, there's always the builtin given/when.

Replies are listed 'Best First'.
Re^2: switch stattement
by dsheroh (Monsignor) on Aug 04, 2009 at 09:18 UTC
    You may also want to consider using a hash, if you don't have too many options:
    I would argue that you may want to consider using a hash, especially if you have many options. The hash can be built up incrementally across many locations, allowing you to create a plugin-style structure, with the handling for each filetype placed in its own module, each of which adds itself to the hash. Even if the hash is defined entirely in one place, it's more compact than other representations.

    Oh, and don't use Switch.

Re^2: switch stattement
by wazoox (Prior) on Aug 05, 2009 at 10:01 UTC
    Or, as already mentioned, use Switch.

    Oh no, don't. Use given, when if you're running perl 5.10, or use some other nethod, but don't use Switch.pm if you can. It's a source filter so it's slow, prone to weird untractable bugs, won't work in "eval", etc.

Re^2: switch stattement
by mtrasp (Acolyte) on Aug 05, 2009 at 16:17 UTC
    Really Thank you very much .your reply helped me alot Thanks again

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-25 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found