in reply to Re: Problem with Windows Console input
in thread Problem with Windows Console input

Thanks buddy it works. But yes there should be a way to re open the stdin handle.
  • Comment on Re^2: Problem with Windows Console input

Replies are listed 'Best First'.
Re^3: Problem with Windows Console input
by BrowserUk (Patriarch) on Feb 24, 2014 at 06:02 UTC
    But yes there should be a way to re open the stdin handle.

    There is:

    #! perl -slw use strict; close STDIN; readline(STDIN); open *STDIN, '<', 'CON:' or die $!; my $x = readline(STDIN); print "Got: $x from reopened STDIN"; __END__ C:\test>junk84.pl readline() on closed filehandle STDIN at C:\test\junk84.pl line 6. fred Got: fred from reopened STDIN

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks for this! It certainly works as shown. Is it documented anywhere? I haven’t been able to find CON: in either the Perl documentation or Programming Perl. In any case, it seems I was wrong: re-opening STDIN doesn’t solve anshulzunke’s problem:

      1. If I add open *STDIN, '<', 'CON:' or die $!; immediately before the final call to readInput(), the script dies with:

        Bad file descriptor at...
      2. If I add it immediately before return $Password; in sub readPasswordWindows, there is no error but the script generates:

         Use of uninitialized value $ip in concatenation (.) or string at...

        as in the OP.

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        re-opening STDIN doesn’t solve anshulzunke’s problem:

        His problem is that my $StdIn = Win32::Console->new(STD_INPUT_HANDLE); doesn't get a handle to the console input buffer; it get's the handle to it. Thus, as you've correctly surmised, letting it go out of scope means it get closed automatically.

        And note. The console input buffer is not the same thing as STDIN. Thought they are normally connected, they are not synonymous. Windows processes can have multiple input buffers; but only one of them can be associated with the console (and thus STDIN) at a time.

        One simple solution would be to obtain the handle at the top level scope of the program -- it is a global resource -- and pass that handle into the subroutines.

        A perhaps simpler -- and definitely more portable if that is in any way a concern -- solution would be to use Term::ReadKey with ReadMode 2;


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        msdn CON: CON:->DOS#Reserved device names

        See also  help mode

        $ mode Status for device LPT1: ----------------------- Printer output is not being rerouted. Status for device COM3: ----------------------- Baud: 1200 Parity: Even Data Bits: 7 Stop Bits: 1 Timeout: OFF XON/XOFF: OFF CTS handshaking: OFF DSR handshaking: OFF DSR sensitivity: OFF DTR circuit: ON RTS circuit: ON Status for device CON: ---------------------- Lines: 1000 Columns: 120 Keyboard rate: 31 Keyboard delay: 1 Code page: 437
      Thanks for the way for reopening the stdin :)

        Welcome! See also Filenames of "PRN" (or "PrN" etc) on Win32.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.