Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SWITCH statement

Status
Not open for further replies.

new2unix

Programmer
Feb 5, 2001
143
US
Hi,

I am still a little unclear on how to use SWITCH (similar to case in other languages) after looking through the Perl documentation. Basically, base on the string value in a variable called, "$tmp_mode", the script should perform different file I/O task. For example, if $tmp_mode eq "input" then open(FILE, "$file_name"); or if $tmp_mode eq "output" then open(FILE, ">$file_name"). I can simply use if... but would like to know how can it be done in a case-like fashion.

Thanks

Mike
 
there is no switch statement in perl, as was the focus of a lecture at last year's perl con, and often found in newsgroup postings. either way, i took this from:

basically, you use a "for" loop named SWITCH, then each case is a regular expression compared against the value of the variable you pass into it, followed by an '&&' and the block of code you want to execute for each case. the default case is just the last line, with no regular expression, with the code to be run in the default case...
[tt]
SWITCH: for ($tmp_mode)
{
/1/ && do { push @flags, '-e'; last; };
/2/ && do { push @flags, '-h'; last; };
/3/ && do { last; };
die "unknown value for form variable where: `$where'";
}
[/tt]
"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top