Perl Conditional not substituting in scalar value - perl

For some reason, my if statements aren't working the way I want them to.
use strict;
use warnings;
my $syl;
my $name = "Chris";
print "Enter my name\n";
$syl = <>;
if ($syl eq $name)
{
print "You entered my name!\n";
}
else
{
print "That's not my name!\n";
}
It looks like it should work from all of the tutorials I've read, but when I type in "Chris" whether capitalized, lowercase, with or without quotation marks, it always evaluates to false. Use Strict and Use Warnings don't tell me I'm doing anything wrong so what, if anything, can I do?

You need to use chomp. That strips the newline from the end of the input string that got put there when the user typed 'enter.'
$syl = <>;
chomp $syl;
#.... etc...

Related

Perl: Verifying user input

I'm trying to make a program where I need to verify if the user input is the first 2 segments of an IP, I already have the part to verify if the pattern is 000.000 but I'm having problems with verifying if the digits are under 256. Even the when I try to print the #abc array it is blank.
#abc = split(".", $ARGV[0]);
foreach $a (#abc){
if $a != [0 .. 256] {
print "not an ip class"
}
else {
print "it's an ip class"
}
}
Always start your Perl programs with
use strict;
use warnings;
The first argument to split is a regex. You can give it a string, but that will just interpret the contents of the string as a regex at runtime.
. is special in a regex, matching any character. You have to escape it:
my #parts = split /\./, $ARGV[0];
Don't use variables called $a or $b; those are a bit special and used internally by sort.
!= compares two numbers. You can't use it to see if an element is part of a list.
You need to do something like
for my $n (#parts) {
if ($n <= 255) {
print "ok\n";
} else {
print "not ok\n";
}
}
If you have already validated that $n consists of digits only, it cannot be negative.

How can I force the user to enter an integer?

I want to check that the input from the user is an integer number. I tried the following, but it doesn't work:
try {
print "Enter int";
$num=int(<>);
print "okej";
}
catch (ValueError $e{
print"not int";
}
You really do not want to punish users for leading/trailing spaces (as too many web sites these days seem to be doing), but also avoid leading zeros. Also, make sure the output is flushed before asking for input:
#!/usr/bin/env perl
use strict;
use warnings;
my $input;
until (defined $input) {
local $| = 1;
print 'Enter an integer: ';
($input) = (<STDIN> =~ /\A \s* (-? [1-9] [0-9]* ) \s* \z/x);
}
print "$input\n";
The int function does not check if something is an integer. Instead, it converts its argument to an integer. So, int(1) is 1, int(1.11) is also 1, and int('a') is 0 — with a warning if you enabled warnings.
See also:
"How do I determine whether a scalar is a number/whole/integer/float?" in perlfaq4. You should read the excellent documentation that comes with Perl.
perldoc -v '$|'
Check to see if input has only digits and nothing else...
use warnings;
use strict;
print "Enter integer: ";
my $input = <>;
chomp $input;
print "is int\n" if $input =~ /^[0-9]+$/;

basic perl conditional script not working

I am a beginner to perl and have just been messing around trying to create little scripts. I'm not sure what is wrong here but it just falls through to the else every time as if nothing I input satisfies the if or elsif conditions. Is it because eq is the wrong operator? Or is there something else wrong in my code? Thanks!
#!/usr/bin/perl
use strict;
use warnings;
print "what is your name?\n";
my $name = readline STDIN;
print "Hello $name How are you today?\n";
my $feeling = readline STDIN;
if ($feeling eq "happy") {
print "that's good!\n";
}
elsif ($feeling eq "good") {
print "okay!\n";
}
else {
print "Interesting\n";
}
Use chomp($feeling);
#!/usr/bin/perl
use strict;
use warnings;
print "what is your name?\n";
my $name = readline STDIN;
chomp($name);
print "Hello $name How are you today?\n";
my $feeling = readline STDIN;
chomp($feeling);
if ($feeling eq "happy") {
print "that's good!\n";
}
elsif ($feeling eq "good") {
print "okay!\n";
}
else {
print "Interesting\n";
}
readline STDIN captures every character typed along with last enter hit as \n, say if you type "happy" and hit enter for $feeling then its accepted as "happy\n" notice \n is because enter hit to remove last \n newline character use chomp removes any trailing string
chomp is used to "chomp off" the input record separator, which by default is a newline character.
#!/usr/bin/perl
use strict;
use warnings;
use 5.012; # to use things like 'say' and 'given'
say "what is your name?"; # 'say' is like 'print', but means you don't have to use '\n'
my $name = <STDIN>; # good to include angled brackets <>
chomp($name); # remove the newline when entering the number
say qq{Hello $name, how are you today?}; # qq{} acts like double-quotes ("")
my $feeling = <STDIN>;
chomp $feeling; # notice parenthese aren't always needed
# you could also do chomp(my $feeling=<STDIN>);
given (lc $feeling){ # 'given' is Perl's version of a Switch and lc makes input lowercase
when('happy') { say q{That's good.} } # q{} acts like single-quotes ('')
when('good') { say q{Okay!} }
default { say q{Interesting} } # your else-case
}
As the warnings suggest, given is experimental until smartmatch is figured out. It is perfectly acceptable to use the if-elsif-else structure, if you choose.

Having Trouble with Perl if then Statements

I'm having trouble making yes/no questions with Perl, and I just couldn't figure it out. I'm kinda a noob at this.
#! usr/bin/perl
print "Hello there!\n";
print "What is your favorite game?\n";
$name = <STDIN>;
chomp $name;
print "That's awesome! $name is my favorite game too!\n";
print "Do you think this is a fun script (Y/n) \n";
$status = <STDIN>;
if [ $status = "y" ]: then
print "YAY! I knew you would like it!\n";
if [ $status = "n" ]: then
print "You suck, not me!\n";
What am I doing wrong?
if [ is a shell syntax. In Perl, you should use
if (...) {
Also, = is the assignment operator. For string equality, use eq:
if ($status eq 'y') {
print "YAY\n";
Before comparing, you should chomp $status the same way you're already chomping $name.
Also, note that Y and y are not equal.
Also, your first ("shebang") line misses the starting slash:
#! /usr/bin/perl
if [ $status = "y" ]: then
That's Bourne (or bash) shell syntax. The equivalent Perl code is:
if ($status eq "y") {
# ...
}
eq is equality comparison for strings; == compares numbers.
(The other thing you're doing wrong is not including the error message in your question.)
For example:
$status = <STDIN>;
chomp $status;
if ($status eq "y") {
print "YAY! I knew you would like it!\n";
}
There are some other things you can do to improve your Perl code. For example, you should always have:
use strict;
use warnings;
near the top of the source file (which will require declaring your variables, probably with my). I suggest getting this program working first before worrying about that, but it's definitely something you'll want to do in the long term.
First, always, always put use strict; and use warnings; at the top of your programs. This will catch all sorts of errors such as using = in if statements. = sets a variable's value. == tests numeric equality and eq tests string equality.
Here's your program rewritten. The first line with #! searches your PATH for an executable Perl. This way, you don't have to worry whether Perl is in /usr/bin/perl or /bin/perl or /usr/local/bin/perl.
#! /usr/bin/env perl
use strict;
use warnings;
use feature qw(say); # Allows the use of the "say" command
say "Hello there!";
print "What is your favorite game? ";
my $name = <STDIN>;
chomp $name;
say "That's awesome! $name is my favorite game too!";
print "Do you think this is a fun script (Y/n) \n";
my $status = <STDIN>;
chomp $status;
if ( $status eq "y" ) {
say "Yay! I knew you would like it!";
}
elsif ( $status eq "n" ) {
say "You suck, not me!";
}
A better way may be to check whether the input started with a y or not:
if ( $status =~ /^y/i ) { # Did it start with a 'y' or 'Y'?
say "Yay! I knew you would like it!";
else {
say "You suck, not me!";
}
Note the use of my to declare variables. This is something use strict; requires and will catch a lot of programming mistakes. Note that say is like print, but I don't have to keep putting \n on the end.

Why isn't eq working with my string input?

Just started to learn Perl and namely, learn program flow - main notable differences between evaluating strings and number and using the appropriate operators. Simple script, I have here is driving me crazy as it's a super simple, if else statement that should on "mike" being entered run and doesn't work. It outputs the else statement instead. 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");
}
Try adding a call to chomp after you get your value from STDIN:
$userName = <STDIN>;
chomp($userName);
Since the value read in from STDIN will have a newline character on the end. The chomp() built-in will remove a newline from the end of a string.
As I read your question I thought you were about to have a problem with strings versus numeric values in an equals. 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" ;
}
Try the two different if statements. Answer something nice like family and see what happens. The ~~ is the smart match operator which has helped some of this problem in Perl. Read more about it here (under "smart matching in detail"). Note also the inline use of the chomp operator.