in reply to Re: Restarting a Perl script on Windows 10
in thread Restarting a Perl script on Windows 10

I do not have a solution, but I may have narrowed the problem for you. I ran your example (without updates) on Windows 7. The second instance does run, but does not return to the command window until I type a return. I suspect (without any real reason) that the problem is related to terminal I/O. I modified you program to avoid it (Removed both print statements). In order to verify that the second instance actually ran, I added an open statement to create a file. The resulting program runs to normal completion. The presence of the zero-length file in my current directory verifies that the second instance ran. I have no idea if this applies to windows 10 or if it will be of any help with the original Tk problem.
#!/usr/bin/env perl use warnings; use strict; my $pathToMyself = $0; local $, = "\n\t* "; local $\ = $/; #print "\t".$^X, $0, @ARGV; restartMe() unless @ARGV; open my $foo, '>', 'UniqFileName.___'; # Look for file in CWD sub restartMe { # print "Restarting $pathToMyself...\n"; #exec( $^X, $pathToMyself ) or die "couldn't exec $pathToMyself: $ +!"; #system(1, $^X, $pathToMyself, "don't Restart", "final" ); exit; #system(1, "cmd.exe", "/c", $^X, $pathToMyself, "final"); exit; exec("cmd.exe", "/c", $^X, $pathToMyself, "final") or die "couldn +'t exec $pathToMyself: $!"; }
Bill

Replies are listed 'Best First'.
Re^3: Restarting a Perl script on Windows 10
by pryrt (Abbot) on Sep 23, 2019 at 14:53 UTC

    BillKSmith, interestingly, if I try to write anything to the UniqFileName.___, it goes back to needing the ENTER. For example:

    #!/usr/bin/env perl # started from https://perlmonks.org/?node_id=11106496 => BillKSmith's + version... # as use warnings; use strict; my $pathToMyself = $0; local $, = "\n\t* "; local $\ = $/; open our $foo, '>>', 'UniqFileName.___'; # Look for file in CWD print {$foo} $^X, "0 = $0", "ARGV", @ARGV; restartMe() unless @ARGV and $ARGV[0]>1; sub restartMe { $ARGV[0] ||= 0; print {$foo} "Restarting $pathToMyself 1+$ARGV[0]...\n"; exec("cmd.exe", "/c", $^X, $pathToMyself, 1+$ARGV[0]) or die "cou +ldn't exec $pathToMyself: $!"; }

    If I run that code using perl 11106428-11106496.pl or using my association at the command line using 11106428-11106496.pl, I get:

    c:\usr\local\apps\berrybrew\perls\system\perl\bin\perl.exe
    	* 0 = 11106428-11106496.pl
    	* ARGV
    Restarting 11106428-11106496.pl 1+0...
    

    Then if I hit ENTER, I get the next block, and ENTER again I get the final block:

    c:\usr\local\apps\berrybrew\perls\system\perl\bin\perl.exe
    	* 0 = 11106428-11106496.pl
    	* ARGV
    	* 1
    Restarting 11106428-11106496.pl 1+1...
    
    c:\usr\local\apps\berrybrew\perls\system\perl\bin\perl.exe
    	* 0 = 11106428-11106496.pl
    	* ARGV
    	* 2
    

    However, if I run by double-clicking on the script in Explorer, then it generates all three blocks in the UniqFileName.___ right away. With that result, and jcb's suggestion of wperl, wperl 11106428-11106496.pl (from the command line) will also generate all three blocks without typing ENTER. (Seeing jcb's wperl mention reminded me that wperl is supposed to launch without needing the cmd.exe window)

    Taking that lesson learned: if I use the original code, if I double-click on the file, it opens up the single cmd.exe for STDOUT, but properly respawns other than that. And from the command-line, if I run with wperl 11106428.pl instead of perl 11106428.pl, then the Tk properly respawns every time (and the STDOUT is lost to wperl's absorption of such.

    So, for petro4213, you might want to have a special extension (like .wpl for windows-perl, or .tkpl or .pltk for perl-with-tk), and use an association with wperl.exe rather than perl.exe for that/those extension(s). (If you do, you'll want to make sure you never write to STDOUT or STDERR... or redirect those to some logfile.)