Re: switch stattement
by ikegami (Patriarch) on Aug 04, 2009 at 04:09 UTC
|
Perl doesn't have a switch statement. Well, it does, but it's called given. Since it's only available since 5.10.0, you might have some backwards compatibility issues.
Ignore suggestions to use the Switch module. It's author recommends against using it since it's a buggy proof-of-concept module. Here at PerlMonks, we've seen a number of questions about problems caused by Switch. They are basiaclly impossible to debug.
Besides, a lookup table (hash) would be much more appropriate.
| [reply] [Watch: Dir/Any] [d/l] |
Re: switch stattement
by cdarke (Prior) on Aug 04, 2009 at 03:20 UTC
|
use warnings;
use strict;
# Folder Readme
my %oses = ("170" => ["winvista", "readme_winvista.txt"],
"173" => ["winvista64", "readme_winvista64.txt"],
"119" => ["winvxp", "readme_winxp.txt"],
"210" => ["winxp64", "readme_winxp64.txt"],
"541" => ["windows*7 graphics", "readme_windows *7 graphic
+s .txt"],
"525" => ["winvista64", "windows *7 /windows *7/wi
+ndows *.txt"]);
my $os = '210';
my $Folder = "win2k_xp";
my $Readme = "readme_2k_xp.txt";
if (exists $oses{$os}) {
$Folder = $oses{$os}[0];
$Readme = $oses{$os}[1]
}
print "Folder: $Folder, Readme: $Readme\n";
Update: Added default handling. | [reply] [Watch: Dir/Any] [d/l] |
|
Thank you..it helped me alot
| [reply] [Watch: Dir/Any] |
Re: switch stattement
by merlyn (Sage) on Aug 04, 2009 at 03:01 UTC
|
That looks like it'd be far better served with a hash, with a key of $operatingsystem.
-- Randal L. Schwartz, Perl hacker
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
| [reply] [Watch: Dir/Any] [d/l] |
Re: switch stattement
by leighsharpe (Monk) on Aug 04, 2009 at 03:21 UTC
|
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] |
|
Really Thank you very much .your reply helped me alot
Thanks again
| [reply] [Watch: Dir/Any] |
Re: switch stattement
by Bloodnok (Vicar) on Aug 04, 2009 at 11:58 UTC
|
As ikegami rightly says, you should use the Switch module at your peril - all sorts of dangers lurk therein. You _could_ roll your own poor mans' switch:
SWITCH:
{
$operatingsystem eq "170" && do {
$Folder = "winvista";
$Readme = "readme_winvista.txt";
last SWITCH;
};
$operatingsystem eq "173" && do {
$Folder = "winvista64";
$Readme = "readme_winvista64.txt";
last SWITCH;
};
$operatingsystem eq "119" && do {
$Folder = "winvxp";
$Readme = "readme_winxp.txt";
last SWITCH;
};
$operatingsystem eq "210" && do {
$Folder = "winxp64";
$Readme = "readme_winxp64.txt";
last SWITCH;
};
.
.
.
};
But, as others have pointed out, the case (pun intended:-) for a hash in these circumstances is overwhelming.
A user level that continues to overstate my experience :-))
| [reply] [Watch: Dir/Any] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |