Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

not working in Win10

by Boschman (Initiate)
on May 31, 2019 at 20:52 UTC ( [id://11100814]=perlquestion: print w/replies, xml ) Need Help??

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

My working perl code on my old Windows7 laptop no longer works on my new laptop running Windows10

Old laptop is running Strawberry Perl v5.26.1, and I run the scripts in Git Bash. I'm switching over to my new laptop running Windows10, using Strawberry Perl v25.6.2 in Git Bash. On the new laptop, the codes' first user interaction *should* ask for, and process, a number from the user but it appears to pause. IF I hit Enter if proceeds in the code without processing the user's input

use strict ; use warnings ; use diagnostics ; my $index ; my $Instance ; my $numClients ; my @ClientList ; use Scalar::Util qw(looks_like_number) ; print "How many Clients are you entering? \n" ; # 5 $numClients = <STDIN> ; chomp($numClients) ; if ( !looks_like_number($numClients) ) { print " Your input is NOT a numeric\n" ; exit(1) ; }

any help is appreciated, thanks O Wise Ones

Replies are listed 'Best First'.
Re: not working in Win10
by huck (Prior) on May 31, 2019 at 23:43 UTC

    Try adding

    $| = 1;
    before the first print. If that works it means that rather than going into autoflush mode on STDOUT it is going into buffering mode. perl normally sets autoflush mode on STDOUT if it thinks it is writing to a terminal, so if this works then for some reason when you start perl it doesnt think STDOUT is connected to a terminal.

      huck! your suggestion worked! A million thanks!! and thanks to all responders as well!!

Re: not working in Win10
by swl (Parson) on May 31, 2019 at 23:06 UTC

    Is there a difference in the paths on the two machines?

    Git comes with its own perl interpreter, so perhaps the Strawberry perl is now coming after that one? (This general issue recently came up in 11100237).

    This might help diagnose the issue:

    which -a perl
Re: not working in Win10
by holli (Abbot) on May 31, 2019 at 22:26 UTC
    Works fine for me, in Git Bash and in regular CMD.exe too. Running Win10 and Strawberry 5.26.1 (5.26.1 in git).


    holli

    You can lead your users to water, but alas, you cannot drown them.
Re: not working in Win10
by Marshall (Canon) on Jun 01, 2019 at 03:15 UTC
    You basic code works just fine on Win10 using Active State Perl v5.24.3.
    Get to a native Win10 command line and try it.

    Update: If you are running some kind of Unix variant under Windows. I would favor using a regex instead of chomp for line ending removal. A regex will always work, not matter which direction you move input files around. $line =~ s/^\s*|\s*$//g removes leading and trailing whitespaces (including any CR or LF characters) and is faster than 2 separate regexes for that purpose.

    use strict ; use warnings ; my $numClients ; use Scalar::Util qw(looks_like_number) ; print "How many Clients are you entering?" ; $numClients = <STDIN> ; chomp($numClients) ; if ( !looks_like_number($numClients) ) { print " Your input is NOT a numeric\n" ; exit(1) ; } print "$numClients\n"; __END__ C:\PerlProjects\Monks>perl getposinteger3.pl How many Clients are you entering?0 0 C:\PerlProjects\Monks>perl getposinteger3.pl How many Clients are you entering? -12 -12 C:\PerlProjects\Monks>perl getposinteger3.pl How many Clients are you entering? 0005 0005 C:\PerlProjects\Monks>perl getposinteger3.pl How many Clients are you entering?14.3 14.3
    I would code a loop like this as below.....
    It loops until a valid number is entered instead of aborting.
    Instead of "looks like a number" which allows fractions and exponentials, the code below looks for a non-zero positive integer although leading zeroes are allowed.
    In this scenario, parens around the print statement are required to get the print to happen "in the middle" of the while statement. Try with and without the parens to see what I mean.
    use strict; use warnings; my $numClients; while ( (print "Enter Number of Clients: "), $numClients=<STDIN>, $numClients !~ /^\s*(?:[0]*)?[1-9]\d*\s*$/) { print "Your input is NOT a positive integer!\n"; } $numClients += 0; #optional conversion to numeric print "Number of Clients: $numClients\n"; __END__ C:\PerlProjects\Monks>perl getposinteger2.pl Enter Number of Clients: 5 Number of Clients: 5 C:\PerlProjects\Monks>perl getposinteger2.pl Enter Number of Clients: -23 Your input is NOT a positive integer! Enter Number of Clients: 2 3 Your input is NOT a positive integer! Enter Number of Clients: 14.3 Your input is NOT a positive integer! Enter Number of Clients: -12 23 Your input is NOT a positive integer! Enter Number of Clients: 5 Number of Clients: 5 C:\PerlProjects\Monks>perl getposinteger2.pl Enter Number of Clients: 00003 Number of Clients: 3
    You can add extra error checking like this:
    use strict; use warnings; my $numClients; while ( (print "Enter Number of Clients: "), $numClients=<STDIN>, $numClients !~ /^\s*(?:[0]*)?[1-9]\d*\s*$/ or $numClients >=1 +00) { print "Your input is NOT a positive integer less than 100!\n"; } $numClients += 0; #optional conversion to numerio print "Number of Clients: $numClients\n"; __END__ C:\PerlProjects\Monks>perl getposinteger2.pl Enter Number of Clients: 123 Your input is NOT a positive integer less than 100! Enter Number of Clients: 5 Number of Clients: 5
Re: not working in Win10
by 1nickt (Canon) on May 31, 2019 at 21:35 UTC

    Just a guess: something with the line endings in Windows? You could try

    use feature 'say'; say "How many Clients are you entering?"; ... say " Your input is NOT a numeric";
    and I think Perl will handle the line endings correctly.

    Hope this helps!


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-26 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found