1

I have typed a little program to refresh my Perl knowledge, especially the IF and Else-Case I was interested in. How do I make Perl recognize that the input was empty? I checked the input with "==" and also with "eq" but my program always says "You have typed nothing!".

#!/usr/bin/perl

# input line
$input_line = "";

# terminal input
print "Input:";
$input_line = <STDIN>;

# input verification
print "Your input was: ", "$input_line";
print "\n";

# decider terminal input
if ($input_line ==  "" or $input_line eq "")
   { print "You have typed nothing!";
     print "\n"; }
     
   else
        { print "Typing nothing is bad!";
          print "\n"; }

And the output if I type "Hello" is:

Input:Hello
Your input was: Hello

You have typed nothing!

If I type Hello, then there is input.

If I type nothing, then the output should write "You have typed nothing!", and the Else-Case should also appear.

1
  • == converts both operands to a number (i.e. 0 for input Hello), and 0 == 0. Use only eq (but all that is explained in the duplicate) Commented Jun 30, 2024 at 13:00

2 Answers 2

2

Always add use warnings; to your code to get more information as to why it does not work:

Argument "" isn't numeric in numeric eq (==) at ... line ..., line 1.

One approach is to check the length of the string input. It is also a good practice to chomp the input to remove any newline character.

use warnings;
use strict;

# input line
my $input_line = "";

# terminal input
print "Input:";
$input_line = <STDIN>;
chomp $input_line;

# input verification
print "Your input was: ", "$input_line";
print "\n";

# decider terminal input
if (length($input_line) == 0) {
    print "You have typed nothing!";
    print "\n";
}

It is also good practice to add use strict;. In this case you need to declare variables with my.

Since you are new to Perl, refer to the Basic debugging checklist

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. The trick, if Variable is empty by checking their lenght works!
1

toolic has already given the correct answer, but I wanted to throw in some beginner advice.

Your code can be condensed down to this:

use strict;
use warnings;   # always use these two pragmas

print "Input: ";
chomp (my $input_line = <>);
print "Your input was: '$input_line'\n";
if (length $input_line == 0) {
    print "You have typed nothing!\n";
}
  • Remove initialization and pre-assigning "". No $input = ""
  • Variables are already empty when created, and even so, when you assign to them, any previous content is deleted.
print "Input: ";                   # extra space to make it look nicer
chomp (my $input_line = <>);       # chomp can be used directly on init and assignment
  • Instead of <STDIN> we can use <>, which chooses either STDIN, or file input as necessary
print "Your input was: '$input_line'\n";
  • You don't need a new print for \n, nor a new quote for a variable
  • Variables don't need to be quoted
  • Adding '' around the variable shows more context, especially with space and empty string
  • You can also use say, which adds a newline to print. Enable with:
use feature 'say';
say "Your input was: $input";   # no \n needed

A very good module for debugging is Data::Dumper. It is a core module, so no installation required. It will show you the content of a variable

use Data::Dumper;
print Dumper $input_line;
# Will print: $VAR1 = '';

While the below is a good solution for normal answers:

if (length($input_line) == 0) {
    print "You have typed nothing!\n";
}

What if someone answers with a space (not empty), or you forget to add chomp? You can for example be more specific by using a regex:

if ($input !~ /\S/)         # no non-whitespace
if ($input !~ /^[abcd]+$/)  # contains something other than what you need, e.g. abcd
if ($input =~ /[^abcd]/)    # alternative phrasing

A few examples for more studies. Good luck!

As ikegami says, it is possible to enter eof only to the program (by for example pressing Ctrl-D (unix) or Ctrl-Z (win) at the prompt), causing readline to return undef to the input variable. In which case the program issues a few undefined warnings for the different operations. To resolve this, you could change your input readline chomp (my $input.... to:

my $input_line = <>;
if (! defined $input_line) {
    die "Input required.";
}
chomp $input_line;

Which is a bit more complicated, but will prevent warnings for this edge case.

3 Comments

You addressed the situation where a LF is provided. This should also be handled. But you don't handle empty input. Your program warns three times when provided an empty input.
@ikegami I take it you mean if someone entered Ctrl-D (Ctrl-Z windows), making the input variable undef? I suppose that is an exception to handle, but then we may be leaving the scope of the original question. It is possible to handle with for example $input //= ''.
Or redirecting from an empty file, etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.