0

Randomly giving the numbers as input. Loop is not exiting even after pressing ctrl+d

my $num;
my @numbers =();

print("\n enter the numbers :\n");
print("\n press ctrl+d to exit once done\n");

while (my $input = <>) {
    print(">");
    #chomp $input;
    $num =int($input);
    push(@numbers, $num);
}
print( " entered number are :@numbers\t");
  • Are you hitting enter first? – Shawn Mar 11 '22 at 07:47
  • Does this answer your question? [Why do I need to type Ctrl-D twice to mark end-of-file?](https://stackoverflow.com/questions/21260674/why-do-i-need-to-type-ctrl-d-twice-to-mark-end-of-file) – Shawn Mar 11 '22 at 07:58
  • Hi Shawn. Even I'm hitting enter, there's no response – saikanthan777 Mar 11 '22 at 08:21
  • In my case CTRL+D isn't working – saikanthan777 Mar 11 '22 at 08:27
  • works for me (Linux, Bash). But my guess is that the output isn't flushed correctly so that the output doesn't appear when you expect it to appear. The program also has some minor cosmetic bugs (e.g. no `>` before the first time input is expected) – amon Mar 11 '22 at 10:06
  • Works for me on Windows, although in cmd prompt it is Ctrl-Z instead of Ctrl-D. Add more info about your problem, or the question will and should be closed as "not reproducible". – TLP Mar 11 '22 at 11:03
  • 1
    Instead of forcing the user to enter an EOF command, you could just end the loop at a given input, or lack thereof. I.e. `last if $input !~ /\d/`, or `last if $input =~ /q/i` (Q for quit). – TLP Mar 11 '22 at 11:09

1 Answers1

1

To signal EOF on Windows, use Ctrl-Z followed by Enter. (You'll need to use it on a blank line when using <>.)

To signal EOF on unix, use Ctrl-D[1] immediately after having pressed Enter.

If these don't work, it's an issue with your terminal.


  1. Ctrl-D is only the default. stty -a will show the current setting.

    $ stty -a
    ...; eof = ^D; ...
    
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • I have tried giving input numbers and have CTRL+Z being pressed followed by enter key. I could see the following error. " ^Z Argument "^Z\n" isn't numeric in int at new.pl line 406, <> line 5. " – saikanthan777 Mar 12 '22 at 03:12
  • 1 2 3 4 5^Z Argument "^Z\n" isn't numeric in int at new.pl line 406, <> line 5. – saikanthan777 Mar 12 '22 at 03:13
  • 1
    I don't know what you're terminal you're using. In any case, this has nothing to do with Perl. This is about knowing how to use your own terminal. – ikegami Mar 12 '22 at 04:23
  • I am using notepad++ with perl being used as a plugin. And I'm checking the result then and there in NPP. – saikanthan777 Mar 13 '22 at 16:48