Solved

how to flush the input buffer ?

Posted on 2002-04-25
11
4,591 Views
Last Modified: 2012-05-04
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
Comment
Question by:anil27
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6969077
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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 6970060
> $|++; ## 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
 

Author Comment

by:anil27
ID: 6970492
This is a duplicate question. It has already been answered by maneshr.
thanks
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 84

Expert Comment

by:ozo
ID: 6970549
$|++ is the same as $|=1
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6970571
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
 

Author Comment

by:anil27
ID: 6970581
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
 
LVL 84

Expert Comment

by:ozo
ID: 6970634
$|++ is 100% sure
$| = -1 is the same as $| = 1
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6970661
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
 
LVL 84

Expert Comment

by:ozo
ID: 6970670
#ahoffman, try this:
$| = -1;
print "$|\n";
$|++;
print "$|\n";
$|--;
print "$|\n";
$|--;
print "$|\n";
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6970682
opps, underestimated perl's sophisticated handling of booleans ;-)
But is the result of following obvious? IMO no:

$|++;$|--;$|--;$|--;print $|;
0
 

Author Comment

by:anil27
ID: 7553065
Duplicate answer but it is still lyng there so accepted.
thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question