Reputation: 161
I'm writing a simple perl script on Mac which takes bunch of words in separate lines and print them out:
#!/usr/bin/perl -w
use strict;
select STDERR; $| = 1;
select STDOUT; $| = 1; # auto flushing
my %count;
print "Please type in a series of words in seperate lines and ends with Ctrl+D.\n";
chomp(my @series = <STDIN>);
# print "\n";
foreach my $name (@series){`
print "$name\n";
}
This is the input :
a
a
b
(ctrl+D)
And I noticed after I finished input and press ctrl+D, I saw this in terminal:
aD
a
b
And the reason I think is because the terminal also echoed the ^D without a "\n" and when my program is printing out the input it steps on the ^ which leaves aD as I saw (you can check this by uncomment the print "\n" in line 8) So I guess what I'm asking is what should I do do prevent the terminal from echoing the ^D?
Upvotes: 0
Views: 5892
Reputation: 54505
A simple way would be to use stty
to temporarily turn off the feature. On the command-line that would be
stty -echoctl
to turn off, and
stty echoctl
to turn on. If you did a system
call on those, from your script, it would do what was asked. Here is an example:
#!/usr/bin/env perl
use strict;
select STDERR; $| = 1;
select STDOUT; $| = 1; # auto flushing
system("stty -echoctl");
my %count;
print "Please type in a series of words in separate lines and ends with Ctrl+D.\n";
chomp(my @series = <STDIN> );
print "\n";
foreach my $name (@series){
print "$name\n";
}
system("stty echoctl");
1;
Upvotes: 1