Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Restarting a Perl script on Windows 10

by pryrt (Abbot)
on Sep 20, 2019 at 13:44 UTC ( [id://11106434]=note: print w/replies, xml ) Need Help??


in reply to Restarting a Perl script on Windows 10

Since no one has given an answer yet, I'll chime in with confirmation. I see the same thing on Windows 10, even when not using Tk. I ended up spawning $^X, $pathToMyself because my perl association isn't working (new computer, apparently hadn't used my assoc yet), but I get the same results -- I have to hit ENTER to actual enter the new instance.

#!/usr/bin/env perl use warnings; use strict; my $pathToMyself = $0; local $, = "\n\t* "; local $\ = $/; print "\t".$^X, $0, @ARGV; restartMe() unless @ARGV; 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: $!"; }

As you can see from my commented lines, I tried various combinations of exec and system, both with and without an explicit cmd.exe interpreter. I don't know what's going on, but I can definitely confirm it.

edit: fixed my association to be perl "%1" %*, so it would run without the cmd or $^X, but no change in behavior

system(1, $pathToMyself, "don't Restart", "final" ); exit; #exec( $pathToMyself, "final" ) or die "couldn't exec $pathToMyse +lf: $!"; # can't exec ___: Exec format error -- because this runs wit +hout the cmd.exe overhead, so it doesn't try the association for .pl +file

Replies are listed 'Best First'.
Re^2: Restarting a Perl script on Windows 10
by BillKSmith (Monsignor) on Sep 21, 2019 at 15:06 UTC
    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

      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.)

Re^2: Restarting a Perl script on Windows 10
by Anonymous Monk on Sep 22, 2019 at 18:59 UTC
      Hi, thanks for your hints.

      Your suggestion with Win32::Unicode::Process restarts the program without having to press Enter, but doesn't exit the original instance.

      The keys in the registry are the same as on my Windows 7 machine:
      CompletionChar 0x00000009 DefaultColor 0x00000000 EnableExtensions 0x00000001 PathCompletionChar 0x00000009
      I don't understand what you mean with system_detached
        Use system_detached...exit...instead of exec and report what happens
        execW doesnt exit? What if you call exit afterwards? Does the program memory increase?

Log In?
Username:
Password:

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

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

    No recent polls found