Why can't I match my string to standard input in Perl?

Why is my script not working correctly?

I follow a YouTube video and worked for a guy.

I run Perl on Windows using ActiveState ActivePerl 5.12.2.1202

Here is my tiny tiny block of code.

print "What is your name?\n";
$name = <STDIN>;
if ($name eq "Jon") {
print "We have met before!\n";
} else {
print "We have not met before.\n";
}

The code automatically goes into the else statement and does not even check the if statement.

+3
source share
3 answers

The operator $name = <STDIN>;reads from standard input and includes the trailing newline character " \n". Remove this character with the function chomp:

print "What is your name?\n";
$name = <STDIN>;
chomp($name);
if ($name eq "Jon") {
  print "We have met before!\n";
} else {
  print "We have not met before.\n";
}
+18
source

- , . - , , , , . :

 print "The name is [$name]\n";

, , . :

The name is [Jon
]

, . eq , .

Perl, Perl. , YouTube.:)

+5

$name = <STDIN>;

$namewill have a trailing newline. Therefore, if I enter foo, $namewill have foo\n.

To get rid of this new line, you can use chomplike:

chomp($name = <STDIN>);
+4
source

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


All Articles