Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4599
  • Last Modified:

how to flush the input buffer ?

This is the code to get a y/n but when I make a subsequent call it does not ask for the input !

#! /in/local/bin/perl
sub demande {
my $in=0;
system("echo \"Have u modified \"");
$in = lc(getc);
$in  =~ s/ //ge;
$in  =~ s/\n//ge;
if ($in ne "y")
{
system("echo \"U are out NO BOSS .... $in\"");
exit;
}
else
{
system("echo \"U are in YES BOSS .... $in\"");
}
}
#end od demande
########MAIN#########
demande();
demande();


thanks
0
anil27
Asked:
anil27
  • 4
  • 3
  • 3
  • +1
1 Solution
 
maneshrCommented:
anil27,

".. code to get a y/n but when I make a subsequent call it does not ask for the input !..."

What exactly are you trying to do? Can you explain in plain english what you are trying to acheive?

Let me know.

This will help you get a more accurate answer, faster.

Meanwhile, here is a sample script that will be run from the command prompt.
This script will ask the user for input.

If input is Y or y, then it will print a "YES" message.
If input is N or n, then it will print a "NO" message.
if input is anything else it will print a "INVALID" message.

#!/in/local/bin/perl

$|++; ## Disable output buffering

print "Enter your reply ";
$ans=<STDIN>; ##  Read the users input

##  Remove the newline character
chomp($ans);

if ($ans=~ /^y$/i){ ##  Y or y entered!!
  print "YES\n";
}elsif ($ans=~ /^n$/i){ ##  N or n entered!!
  print "NO\n";
}else{
  print "INVALID\n";
}
0
 
ahoffmannCommented:
> $|++; ## Disable output buffering

hmm, and if someone decided to set perl's default of $|=(-1); ??
I would recommend to use $|=1;

anil27, getc() gets the first typed character only, so your second call to demande() gets the newline (or carriage return) from the previous one, probably not what you want.
0
 
anil27Author Commented:
This is a duplicate question. It has already been answered by maneshr.
thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
$|++ is the same as $|=1
0
 
ahoffmannCommented:
ozo, see my comment. I know I'm pedantic, sometimes, but I disagree that  $|++  "is the same" as  $|=1

Also, with $|!=0 only STDOUT will be flushed (according to man-pages), I don't know of a method to flush STDIN (probably with select(), never tried such a hack).
0
 
anil27Author Commented:
I agree ahoffman with both of ur points. ! $| = 1 is reqd to be 100% sure, but by default it is 0 so $|++ will work in most cases.
I have put the comment for input buffer flush although I accepted the answer,as STDIN solved my problem.
input buffer is still NOT CLEARED.
regards
0
 
ozoCommented:
$|++ is 100% sure
$| = -1 is the same as $| = 1
0
 
ahoffmannCommented:
it's a pedantic discussion, anyway:

> $|++ will work in most cases
agreed

> $| = -1 is the same as $| = 1
agreed

> $|++ is 100% sure
disagreed (if the goal is to get $|!=0 )

2 examples:
  1) the default is set to -1 somehow, somewhere, ..
  2) if someone uses $|++, I assume that there is also $|--, in this case you do not know the value of $|, usually, and it may be -1 when you call $|++, accidently

IMHO, $|=1 is 100% sure, 'cause it it defensive programming
0
 
ozoCommented:
#ahoffman, try this:
$| = -1;
print "$|\n";
$|++;
print "$|\n";
$|--;
print "$|\n";
$|--;
print "$|\n";
0
 
ahoffmannCommented:
opps, underestimated perl's sophisticated handling of booleans ;-)
But is the result of following obvious? IMO no:

$|++;$|--;$|--;$|--;print $|;
0
 
anil27Author Commented:
Duplicate answer but it is still lyng there so accepted.
thanks
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now