Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440898
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:29:14+00:00 2026-05-18T08:29:14+00:00

Do these two statements behave equally or could they yield different results? if (

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:29:15+00:00Added an answer on May 18, 2026 at 8:29 am

    Whatever you can do with the given/when, you can do with if/elsif/else. The idea is that when/given is suppose to be easier to read, and can automatically use smartmatching by default while you have to specify smart matching with the if/else statement.

    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/else and given/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/else statements 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 say instead of printnl?

    And, why last and next instead of break and continue. 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:

    use strict;
    use warnings;
    no warnings qw(uninitialized);
    
    my %hash;
    $hash{FOO} = "bar";
    
    if (not exists($hash{BAR})) {
    print "\$hash{FOO} doesn't exist\n";
    }
    if (not defined($hash{BAR})) {
    print "\$hash{BAR} is undefined\n";
    }
    if (not $hash{BAR}) {
    print "\$hash{BAR} evaluates to false\n";
    }
    if ($hash{BAR} eq undef) {
    print "\$hash{BAR} is equal to 'undef'\n";
    }
    

    You can see that $hash{BAR} doesn’t even exist as a key in the %hash hash, but it is also undefined, and it evaluates as false. And, you can also see that it also evaluates as undef (notice I had to set no warnings qw(uninitialized); to prevent it from complaining about $hash{BAR} being uninitialized in my last if statement).

    However, if I do this:

    $hash{FOO} = bar;
    $hash{BAR} = undef;
    

    The first if statement no longer evaluates as true because the key BAR now does exist in the hash, but the value is still undefined and still evaluates as false.

    And, if I did this:

    $hash{FOO} = bar;
    $hash{BAR} = 0;
    

    $hash{BAR} now exists as a key in %hash, and it is no longer undefined, but it still evaluates as false.

    I like simply being able to say this:

    if (not $hash{BAR}) {
    

    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 being defined as three separate things. It can be important if you have a subroutine that could return a NULL string or zero value:

    if (not foo($bar)) {
        die qq(Error of some sort\n);
    }
    
    sub foo {
        $bar = <FOO> or return;
        return chomp($bar);
    }
    

    If there’s a blank line in my file, it’ll return a NULL string, but the subroutine will still return a defined value. The above probably does not do what I want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Access, what is the difference between these two statements? DBEngine.BeginTrans and DBEngine.Workspaces(0).BeginTrans The
These two may look like they have no correlation but bear with me! In
What is the better practice of the following two switch/case statements? Is there an
These two methods exhibit repetition: public static Expression<Func<Foo, FooEditDto>> EditDtoSelector() { return f =>
Consider these two function definitions: void foo() { } void foo(void) { } Is
Do these two keywords have exactly the same effect, or is there something I
Notice these two RedHat Linux system configuration settings: $ getconf GNU_LIBC_VERSION glibc 2.3.4 $
Are these two terms interchangeable?
Can these two SVN clients collaborate? I have my projects checked out with Tortoise,
When writing a switch statement, there appears to be two limitations on what you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.