Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Win32::Console problem

by hoffy (Acolyte)
on Nov 23, 2009 at 01:40 UTC ( [id://808708]=perlquestion: print w/replies, xml ) Need Help??

hoffy has asked for the wisdom of the Perl Monks concerning the following question:

Good morning all. This is my first question/post/etc here, so please forgive me if I completely get it all wrong!

OK, I am having a little bit of an issue with some code developed for a Windows environment, that is not behaving as it should. The code was originally developed by another person using active states distribution of V5.6 (where it worked just fine!) and relates to (as far as I can see) the use of the Win32::Console module

What is supposed to happen is a question is supposed to be displayed in the cmd window, which the user adds input to, another question asked, new input, etc. The input is then used as variables within the script. The cruncher is the code works fine when using V5.6, but fails miserably when using V5.8

The issue I am getting is that the questions do not appear until after you enter the input. As you can guess, this is rather frustrating. The code responsible is as follows:

#!c:\perl\bin\perl.exe -w # use strict; use Win32::Console; use Time::Local; my $CONSOLE = new Win32::Console(STD_INPUT_HANDLE); my %vals = ('cuname'=>'','defolder'=>'','cutoffdt'=>''); my %labs = ('cuname'=>'Customer Import Folder','defolder'=>'DE SubFold +er','cutoffdt'=>'Cutoff Date ddmmyy'); system "cls"; my $console_mode = $CONSOLE->Mode(); my $echo_on = (ENABLE_PROCESSED_INPUT) | (ENABLE_ECHO_INPUT); my $echo_off = (ENABLE_PROCESSED_INPUT); $CONSOLE->Mode($echo_on); $CONSOLE->Title("Change Backdated DE files"); my $promptstr = " "; my $seq = 0; foreach my $val ('cuname','defolder','cutoffdt') { $promptstr = sprintf('Enter %-25.25s = ',"\u$labs{$val}"); $CONSOLE->Mode($echo_off); print "$promptstr"; $_=''; my $chr = ''; READLINE: for(;;) { $chr = $CONSOLE->InputChar(1); last READLINE if ($chr eq "\r"); next READLINE if ($chr eq "\n"); if ($chr lt ' ') { $seq = ord($chr); # # allow backspace to edit # if ($seq == 8 && $_ ne '') { chop; # trim the last char print "$chr $chr"; # and overprint with a space on screen } next READLINE; } $_ .= $chr; if ($labs{$val} =~ /password/i) { print '*' ; } else { print "$chr" ; } } &err_exit(0) unless($_ && $_ !~ /^exit$/i); $vals{$val} = $_; print "\n"; } $CONSOLE->Mode($console_mode); # restore default mode

From here, the input is used as variables in the program.

I have tried a few different bits and pieces (my perl knowledge is at the +1 from newbie stage at the moment!), such as a dummy print around print "$promptstr";, which work fine. I have also added a newline on the print command above, which then makes the prompt print, but puts the cursor on a new page.

Can anyone give me some advice on what I should do and where I should look to get this working as it should. Any help would be appreciated!!

Cheers!

Replies are listed 'Best First'.
Re: Win32::Console problem
by BrowserUk (Patriarch) on Nov 23, 2009 at 02:09 UTC

    Add $|++; at the top of the program.


    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! Seems so simple and it works!

      Now, I do have a non essential quest for knowledge....why did it work on V5.6 but no V5.8?

        why did it work on V5.6 but no V5.8?

        The rules regarding automatic flushing of the STDOUT buffer changed from perl-5.6 to perl-5.8. Setting $| (or incrementing it from 0 to 1, as per BrowserUk's approach) is the way to make sure that the perl-5.8 STDOUT buffer gets flushed in much the same way as happened automatically with perl-5.6.

        For a simple demo of the difference, the following outputs hellogoodbye on 5.6, but goodbyehello on 5.8:
        perl -e "print STDOUT \"hello\";print STDERR \"goodbye\""
        Setting $| will ensure that hellogoodbye is output on both 5.6 and 5.8:
        perl -e "$|=1;print STDOUT \"hello\";print STDERR \"goodbye\""
        Cheers,
        Rob
        why did it work on V5.6 but no V5.8?

        I have no idea.


        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://808708]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found