Why doesn't eq work with my string input?

I just started to learn Perl, namely, to study the program flow - the main noticeable differences between evaluating strings and numbers and using the appropriate operators. The simple script I have here is driving me crazy because it is super simple if the other expression that needs to be entered in "mike" does not work. Instead, the else statement is displayed. Please, help

#!C:\strawberry\perl\bin\perl.exe

use strict;
#use warnings;
#use diagnostics;

print("What is your name please?");
$userName = <STDIN>;


if($userName eq "mike"){
    print("correct answer");
}
else{
    print("Wrong answer");
}
+3
source share
2 answers

When I read your question, I thought you would have a problem with equal strings and numeric values. Consider the following case:

#!/usr/bin/env perl

use strict;
use warnings;

print("What is the meaning of life, the universe and everything? ");
chomp(my $response = <STDIN>);

if ( $response == 42) {
#if ( 42 ~~ $response ) {
    print "correct answer\n";
} else {
    print "Wrong answer\n" ;
}

if. - , family, , . ~~ - smart match, Perl. ( " " ). chomp.

+1

chomp STDIN:

$userName = <STDIN>;
chomp($userName);

, STDIN, . chomp() .

+11

Source: https://habr.com/ru/post/1789842/


All Articles