<STDIN> working only the first time in Perl script when using Ctrl-Z

279 Views Asked by At

As a Perl beginner, I'm trying to do a simple exercise from "Learning Perl" from Randal L. Schwartz. The exercise consist in getting a list of element from the user input (<STDIN>), sort it, and display it on the screen when it is sorted. The list can be output in two different ways, on the same line using commas, or on different lines.

My problem is when I'm getting the list from the user input (Enter between each elements and Ctrl-Z when user is done), I cannot use <STDIN> again. Therefore, it is impossible for me to get the user input for the desired output method.

Please be indulgent and I'm open to every advice, including on how using Stack Overflow, since this is my first post here.

EDIT: I'm on Windows, using EPIC module in Eclipse. I already tried Crtl-D.

Here is my code:

#!/usr/bin/perl

use 5.014;
use warnings;
use utf8;

print "Enter the list you want to sort.";
print "Type each element followed by <Enter>.\n";
print "Type <Ctrl-Z> when you are done:\n";

my @list = <STDIN>;
@list = sort @list;

print "Do you want the elements to be printed on individual lines [i] or on the same line [s]?\n";

chomp (my $userChoice = <STDIN>);

if ($userChoice eq "i")
{
    foreach (@list)
    {
        print $_;
    }
}
else
{
    chomp(@list);        

    print shift @list;

    foreach (@list)
    {
        print ", " . $_;
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

The problem was from EPIC or Eclipse. If I'm running my code in the Windows console, everything is fine. I have to use Ctrl-Z + Enter though. Thanks to @Сухой27 for the comments.