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

I want to do something akin to the following shell loop in Perl on a computer running Microsoft Windows XP:

while read arg do somecmd $arg -o ${arg}.txt & done <<EOL arg1 arg2 arg3 arg4 arg5 EOL

In other words, I want to iterate a list of arguments and use each one in the invocation of an external program. I want to run the multiple processes simultaneously in the background. I don't really care about stdout or stderr, nor do I care about error checking. (I have another way to check the success or failure of the multiple runs of the command. It's a separate process.)

For me, the trick is emulating in Perl the shell's asynchronous execution à la '&'. I would prefer not to use a module, but I understand I may have to. I'm really looking for the simplest, most straighforward and shell-like solution that gets the job done.

Where is system(1, ...) documented? What does it do? I've seen references to it but I cannot find its documentation.

By the way, I have several Unixish shells installed on the computer and could use one of them for this task. However, the reason I want to use Perl instead of, say, the Korn shell is because the list of arguments isn't actually a trivial one as in the example. Generating and iterating the right arguments in the right order will be much easier to do in Perl than in a shell script.

Jim

UPDATE: Thank you for the helpful responses. I was forced to use the Korn shell after all because it's prohibitively difficult to do this kind of thing in Perl compared to the shell. Instead of using asynchronous processes, I just wrote several separate shell loops (i.e., wrapper scripts) and ran them simultaneous. It worked well. I solved the problem by not programming (or at least "programming" much less).

  • Comment on Simple Perl "Shell" Script on Windows XP to Run Multiple, Simultaneous Processes in Parallel
  • Select or Download Code

Replies are listed 'Best First'.
Re: Simple Perl "Shell" Script on Windows XP to Run Multiple, Simultaneous Processes in Parallel
by ig (Vicar) on Jul 29, 2009 at 06:18 UTC
Re: Simple Perl "Shell" Script on Windows XP to Run Multiple, Simultaneous Processes in Parallel
by trwww (Priest) on Jul 29, 2009 at 09:37 UTC

    prefix somecmd with start /B. This will fire off the process in its own environment, and the parent process will continue without waiting for the child to return.

    C:\Documents and Settings\trw>start /? Starts a separate window to run a specified program or command. START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED] [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMA +L] [/WAIT] [/B] [command/program] [parameters] "title" Title to display in window title bar. path Starting directory B Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application I The new environment will be the original environment passed to the cmd.exe and not the current environment. MIN Start window minimized MAX Start window maximized SEPARATE Start 16-bit Windows program in separate memory space SHARED Start 16-bit Windows program in shared memory space LOW Start application in the IDLE priority class NORMAL Start application in the NORMAL priority class HIGH Start application in the HIGH priority class REALTIME Start application in the REALTIME priority class
Re: Simple Perl "Shell" Script on Windows XP to Run Multiple, Simultaneous Processes in Parallel
by jrsimmon (Hermit) on Jul 29, 2009 at 03:52 UTC

    Something like this?

    use strict; use warnings; my $cmd = 'some_command'; my @args = qw(arg1 arg2 arg3 ... argN); system("$cmd $_) foreach @args;

    The doc on the system call:
    system

      Thanks, but where's the background execution part?

      Jim

        You can modify it to:
        system("$cmd $_ &") foreach @args;
        The "&" starts the process in the background.
Re: Simple Perl "Shell" Script on Windows XP to Run Multiple, Simultaneous Processes in Parallel
by cdarke (Prior) on Jul 29, 2009 at 09:10 UTC
    If you don't wish to install Proc::Background then Win32::Process would be the way to go, assuming you are running ActiveState Perl then it should be bundled in already. There is an example in the doc. You can decide yourself when to call the Wait method.
Re: Simple Perl "Shell" Script on Windows XP to Run Multiple, Simultaneous Processes in Parallel
by apl (Monsignor) on Jul 29, 2009 at 11:32 UTC
    When in doubt use Google. For example, the results for Perl system will give you system as the first hit.

    What had you tried, that resulted in no useful documentation on the function?

      I think you may have misread my question.

      The first thing I tried was perldoc -f system at the command prompt. The system(1, ...) idiom is not documented there as one would expect it to be.

      I then searched the World Wide Web using both Google and Google Code Search. Neither of these search techniques was fruitful.

      Another respondent pointed me to the following explanation in perldoc perlport, which is laconic and difficult to understand if, like me, you don't already fully understand the language feature being "documented":

      system
      As an optimization, may not call the command shell specified in $ENV{PERL5SHELL}. system(1, @args) spawns an external process and immediately returns its process designator, without waiting for it to terminate. Return value may be used subsequently in wait or waitpid. Failure to spawn() a subprocess is indicated by setting $? to "255 << 8". $? is set in a way compatible with Unix (i.e. the exitstatus of the subprocess is obtained by "$?>> 8", as described in the documentation). (Win32)

      In fact, I'm prepared now to argue that system(1, ...) isn't properly documented at all.

      When I click on the Google search link you included in your post, I don't get the results you described at all. The URL http://perldoc.perl.org/functions/system.html doesn't show up on the first page of results at all. But it doesn't matter because my question isn't answered in perldoc -f system (i.e., perldoc perlfunc) anyway.

      Jim