<STDIN> blues

Discussion in 'Perl Misc' started by Tan Rezaei, Jun 28, 2003.

  1. Tan Rezaei

    Tan Rezaei Guest

    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
    Tan Rezaei, Jun 28, 2003
    #1
    1. Advertising

  2. (Tan Rezaei) wrote:

    > 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.
    Kent Paul Dolan, Jun 29, 2003
    #2
    1. Advertising

  3. Tan Rezaei

    ThePotPlants Guest

    "Tan Rezaei" <> wrote:
    > 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
    ThePotPlants, Jun 29, 2003
    #3
  4. Tan Rezaei

    Tan Rezaei Guest

    "ThePotPlants" <> wrote in message news:<m3tLa.57155$>...
    > "Tan Rezaei" <> wrote:
    > > 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


    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
    Tan Rezaei, Jun 29, 2003
    #4
  5. Tan Rezaei

    Tan Rezaei Guest

    Solution to <STDIN> problem in Perl and print statements not printing on w2k server

    (Tan Rezaei) wrote in message news:<>...
    > 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
    Tan Rezaei, Jul 8, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,958
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    785
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,343
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    424
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    291
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page