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

What is the syntax in a win32 perl script to run something like
`%compsec% /c type c:\somefile`
also how do you fork another perl script without including that script in a subroutine ? Thanks

Replies are listed 'Best First'.
Re: How to fork %comspec% in win32 perl
by Corion (Patriarch) on Dec 20, 2005 at 21:18 UTC

    The environment variables are stored in the %ENV hash in Perl.

    So what you want to do is:

    my $filename = 'c:\\somefile'; my @input = `$ENV{COMSPEC} /c type "$filename"`;

    Of course, that is silly, because there is little sense in calling an external program just to read a file when Perl has lots of ways to read a file directly:

    my $filename = 'c:\\somefile'; open my $fh, "<", $filename or die "Couldn't read '$filename': $!"; my @input = <$fh>;

    If you want to start another Perl script, you best invoke perl with the other script. The name of the current Perl interpreter is stored in the $^X variable:

    system($^X,"-w","path/to/other/script.pl");

    You talk about "fork", but I'm not sure whether you want to use fork, exec, system or qx, which all are different ways of invoking programs. Note though that fork likely doesn't do what you want and it doesn't work well enough on Win32 anyway.

Re: How to fork %comspec% in win32 perl
by xdg (Monsignor) on Dec 20, 2005 at 23:31 UTC

    You really want to look at Win32::Job. It seems to be hands-down the best way to run a subprocess in Win32 whether you want a separate, detached process or whether you just want to wait for it to finish.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.