I am running a program that I would periodically like to restart. I prefer to pass exec a list so that the shell is not involved as follows:
// Do stuff
.
.
exec $^X, $0, @ARGV;
When I do this (on Windows) and $^X contains spaces using the above it treats $^X as multiple items and fails.
The following works just fine, but starts a subshell in addition to the script:
# Do stuff
.
.
exec "\"$^X\" $0 @ARGV" ;
This is not a huge deal, but I would like to learn how to execute in a list context if possible.
Things I have tried:
exec "\"$^X\"", $0, @ARGV;
exec "\\\"$^X\\\"", $0, @ARGV;
exec "'$^X'", $0, @ARGV; # Even though Windows doesn't like '
Also:
push @restart, $^X, $0, @ARGV;
print $restart[0];
exec @restart;
The print statement correctly shows the executable with the space in it as the first item in the array.
I have copied the perl executable to a location that does not contain spaces in its path and from there exec given a list works.
Windows files have a regular name and a “short” name for backwards compatibility with the golden age of DOS. For the specific case of
$^X = 'C:\Program Files\perl\perl.exe, something likeC:/Progra~1/perl/perl.exewould probably work.For a more general solution, see the
Win32and theGetShortPathNamefunction. Then you could call(in some cases you may want to wrap
$0and the arguments inGetShortPathName, too).