Do these two statements behave equally or could they yield different results?
if ( ... ) {...}
elsif( ... ) {... }
elsif( ... ) { ... }
else { ... }
.
given ( ... ) {
when ( ... ) { ... }
when ( ... ) { ... }
default { ... }
}
I’ve found the problem – with a modified ninth “when” it works now.
...
no warnings qw(numeric);
my $c = &getch();
given ( $c ) {
when ( $c == $KEY_LEFT and 1 > 0 ) { say 1; say $c }
when ( $c == $KEY_RIGHT ) { say 2; say $c }
when ( $c eq "\cH" or $c eq "\c?" ) { say 3; say $c }
when ( $c eq "\cC" ) { say 4; say $c }
when ( $c eq "\cX" or $c eq "\cD" ) { say 5; say $c }
when ( $c eq "\cA" ) { say 6; say $c }
when ( $c eq "\cE" ) { say 7; say $c }
when ( $c eq "\cL" ) { say 8; say $c }
when ( not( not $SpecialKey{$c} ) ) { say 9; say $c }
when ( ord( $c ) >= 32 ) { say 10; say $c }
default { say 11; say $c }
}
if ( $c == $KEY_LEFT and 1 > 0 ) { say 1; say $c }
elsif ( $c == $KEY_RIGHT ) { say 2; say $c }
elsif ( $c eq "\cH" or $c eq "\c?" ) { say 3; say $c }
elsif ( $c eq "\cC" ) { say 4; say $c }
elsif ( $c eq "\cX" or $c eq "\cD" ) { say 5; say $c }
elsif ( $c eq "\cA" ) { say 6; say $c }
elsif ( $c eq "\cE" ) { say 7; say $c }
elsif ( $c eq "\cL" ) { say 8; say $c }
elsif ( $SpecialKey{$c} ) { say 9; say $c }
elsif ( ord( $c ) >= 32 ) { say 10; say $c }
else { say 11; say $c }
close TTYIN;
Whatever you can do with the
given/when, you can do withif/elsif/else. The idea is thatwhen/givenis suppose to be easier to read, and can automatically use smartmatching by default while you have to specify smart matching with theif/elsestatement.I didn’t parse through your code to make sure they’re exact equivalents, but it looks like you’ve got more or less the right idea about
if/elsif/elseandgiven/when.I never really understood the desire for what was referred to as the switch statement. It was something Perl coders always complained about — the lack of a switch statement in Perl. Maybe it’s a C thing that most Perl developers fondly remember. But I never found the
if/elsif/elsestatements that bad.What really confuses me is that when they finally implemented the switch statement, they didn’t call it
switch. Why the heck not?And why
sayinstead ofprintnl?And, why
lastandnextinstead ofbreakandcontinue. Why not simply use the standard names that other languages already use?But enough of this Seinfeld style whining…
As davorg said, there’s a big difference between a hash key that doesn’t exist, a hash key that exists, but isn’t defined, and a hash key that’s defined, but gets evaluated to false:
For example:
You can see that
$hash{BAR}doesn’t even exist as a key in the%hashhash, but it is also undefined, and it evaluates asfalse. And, you can also see that it also evaluates asundef(notice I had to setno warnings qw(uninitialized);to prevent it from complaining about$hash{BAR}being uninitialized in my lastifstatement).However, if I do this:
The first
ifstatement no longer evaluates astruebecause the keyBARnow does exist in the hash, but the value is still undefined and still evaluates asfalse.And, if I did this:
$hash{BAR}now exists as a key in%hash, and it is no longerundefined, but it still evaluates asfalse.I like simply being able to say this:
because it is short and sweet, and probably does what I want. However, I do have to understand the difference between existence of a key in a hash, evaluation to
false, and not beingdefinedas three separate things. It can be important if you have a subroutine that could return aNULLstring or zero value:If there’s a blank line in my file, it’ll return a
NULLstring, but the subroutine will still return a defined value. The above probably does not do what I want.