lundi 27 novembre 2017

If else doesn't work in Perl

So, I made a program in Perl where the user should type in a username. If he types in "admin", the message "Welcome, admin!". In other cases, the console output will be: "The username is incorrect." Here is my code:

use utf8;

print "Username: ";
$username = <STDIN>;
if ($username eq "admin")
{
    print "Welcome, admin!";
}
else
{
    print "The username is incorrect.";
}

The problem is that whatever the user inputs, the program continues, going on the else branch. So, even if he types in: "admin", the console's output will be: "The username is incorrect."

Why does this happen?

And, another question: why do I need to put braces to surround the if-else instructions? It is only one instruction in each branch.

Aucun commentaire:

Enregistrer un commentaire