Solved

how to flush the input buffer ?

Posted on 2002-04-25
11
4,584 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now