<STDIN> blues

T

Tan Rezaei

I have this bit of code on a win2k Server:

-----------------------------------------
#!E:\Perl\bin\perl.exe
print "\n\n Productline (QA, PROD, ...)?\n ";
chop($opt_p=<STDIN>);

print "\n\n Company (0001, 1000, 1001, \.\.\.) ? \n";
chop($opt_c=<STDIN>);

if ($opt_c =~ /\D/) { warn "Company number contains non digits"}

print "\n\n Ready to Process Company $opt_c in Productline $opt_p
(Y/N)? \n";
chop($run=<STDIN>);
-----------------------------------------

Now when I run this, it does not prompt me for the variables but if I
enter all three of them then I will get the questions.

If I comment out all the lines with <STDIN> then I get all the promts
immediately.

Now this actually works fine on one machine and I only get this
problem when I put the file on the server.

Is there something about terminal settigs or clearing the buffer that
might be the issue. If so, how do I clear the screen buffer? This is
one of those things that you just don't want to waste your time on.
And it is really killing me.

Any help at all would be greatly appreciated.

thanks in advance

T
 
K

Kent Paul Dolan

I have this bit of code on a win2k Server:

Ah, someone wise enough to post the source code in a nicely trimmed
down example without being asked!
-----------------------------------------
#!E:\Perl\bin\perl.exe
print "\n\n Productline (QA, PROD, ...)?\n ";
chop($opt_p=<STDIN>);

print "\n\n Company (0001, 1000, 1001, \.\.\.) ? \n";
chop($opt_c=<STDIN>);

if ($opt_c =~ /\D/) { warn "Company number contains non digits"}

print "\n\n Ready to Process Company $opt_c in Productline $opt_p
(Y/N)? \n";
chop($run=<STDIN>);
-----------------------------------------

Now when I run this, it does not prompt me for the variables but if I
enter all three of them then I will get the questions.

If I comment out all the lines with <STDIN> then I get all the promts
immediately.

Now this actually works fine on one machine and I only get this
problem when I put the file on the server.

Is there something about terminal settigs or clearing the buffer that
might be the issue. If so, how do I clear the screen buffer? This is
one of those things that you just don't want to waste your time on.
And it is really killing me.

Any help at all would be greatly appreciated.

thanks in advance

T

It's been too long since I committed Perl with malice aforethought to
give you a detailed fix, but indeed your problem is buffering; when
you write from (any Unix program at all, and probably elsewhere),
unless you force matters to occur otherwise, written output is
buffered in memory until a standard bufferful (probably 512 bytes, but
can vary with OS) is ready to be written to "disk" (which in this case
is your screen) in a nice tidy efficient single write.

Sadly, that isn't at all what you want to have happen here.

The name of the magic you need is a forced "flush" of the STDOUT
stream, but I don't remember how to spell that in Perl, and the triage
of living homeless has left all my computer language manuals sold at
auction years ago. Still, perhaps that will be clue enough to help
you find the answer yourself.

[It's also possible that some variant of writing in "raw" mode will
work, but that is really inefficient, and all you need in this
instance is to get the characters out one whole line at a time, not
each single character separately pushed with an immediate I/O action.]

And by the way, in general, "chomp" is friendlier than "chop", you
really don't want to use "chop" where you are trying explicitly to
trim a newline, but only where you _really, really_ want to trim the
last byte of a string, no matter _what_ value that byte contains.
Usually that is only true when you are treating string contents as
strings of binary octets rather than as strings of characters. To
make a bad pun reversed, the habitual use of "chop" can bite you,
while "chomp", strangely, will not.

xanthian.
 
T

ThePotPlants

Tan Rezaei said:
I have this bit of code on a win2k Server:

-----------------------------------------
#!E:\Perl\bin\perl.exe
print "\n\n Productline (QA, PROD, ...)?\n ";
chop($opt_p=<STDIN>);

print "\n\n Company (0001, 1000, 1001, \.\.\.) ? \n";
chop($opt_c=<STDIN>);

if ($opt_c =~ /\D/) { warn "Company number contains non digits"}

print "\n\n Ready to Process Company $opt_c in Productline $opt_p
(Y/N)? \n";
chop($run=<STDIN>);
-----------------------------------------

Now when I run this, it does not prompt me for the variables but if I
enter all three of them then I will get the questions.

If I comment out all the lines with <STDIN> then I get all the promts
immediately.

Now this actually works fine on one machine and I only get this
problem when I put the file on the server.

Is there something about terminal settigs or clearing the buffer that
might be the issue. If so, how do I clear the screen buffer? This is
one of those things that you just don't want to waste your time on.
And it is really killing me.

Any help at all would be greatly appreciated.

thanks in advance

T

Looking at it a little differently...

It looks to me like you are mixing the use of @ARGV and GETOPT::STD (given
away by your variables $opt_c & $opt_p)
I posted a question related to getopt, have a look at it, you may want to
consider using that instead.
Ask yourself if you want to execute the programme and pass it variables, or
if you want to prompt the user for input.

P
 
T

Tan Rezaei

ThePotPlants said:
Looking at it a little differently...

It looks to me like you are mixing the use of @ARGV and GETOPT::STD (given
away by your variables $opt_c & $opt_p)
I posted a question related to getopt, have a look at it, you may want to
consider using that instead.
Ask yourself if you want to execute the programme and pass it variables, or
if you want to prompt the user for input.

P

Yes, I was using the GETOPT::STD before until I found out who the
users are. They are asking to get prompted for every question as
opposed to run time parameters.

I had to fix this yesterday, so I ended up using warn statements
instead of print and those had no trouble showing up. I know its a
cheap way out but it worked for yesterday. I am going to try some of
people's suggestions today. Thank you all, those are all some great
suggestions. I've been using Perl for a couple of years but have never
really needed to do anything advanced so I am stuck using the most
elementary functions.

P.S. Where is a good place to get some good example code?

Thanks
 
T

Tan Rezaei

I have this bit of code on a win2k Server:

-----------------------------------------
#!E:\Perl\bin\perl.exe
print "\n\n Productline (QA, PROD, ...)?\n ";
chop($opt_p=<STDIN>);

print "\n\n Company (0001, 1000, 1001, \.\.\.) ? \n";
chop($opt_c=<STDIN>);

if ($opt_c =~ /\D/) { warn "Company number contains non digits"}

print "\n\n Ready to Process Company $opt_c in Productline $opt_p
(Y/N)? \n";
chop($run=<STDIN>);
-----------------------------------------

Now when I run this, it does not prompt me for the variables but if I
enter all three of them then I will get the questions.

If I comment out all the lines with <STDIN> then I get all the promts
immediately.

Now this actually works fine on one machine and I only get this
problem when I put the file on the server.

Is there something about terminal settigs or clearing the buffer that
might be the issue. If so, how do I clear the screen buffer? This is
one of those things that you just don't want to waste your time on.
And it is really killing me.

Any help at all would be greatly appreciated.

thanks in advance

T


Well I tried

$| = 1;
at the begining of the script and everything worked.
I thought I should post it in case someone else runs into this issue.

Thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top