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

I have a certain exe and wish to write a perl script to spawn multiple instances of the same. The input to each instance is the argument to the perl program. Lets say exe name is James.exe Perl script is Bond.pl Bond 1 "M" 2 "007" 3 "MI6" input to First instance is "M" input to Second instance is "007" input to Third instance is "MI6"

Replies are listed 'Best First'.
Re: Multiple Instances
by hbm (Hermit) on Mar 11, 2009 at 15:22 UTC

    Sequentially?

    use strict; use warnings; foreach (@ARGV) { &process($_); }

    In parallel?

    use strict; use warnings; foreach (@ARGV) { die "Fork failed: $!\n" unless defined (my $pid = fork); exit &process($_) unless $pid; } 1 until -1 == wait; ...

    Restrained parallel?

    use strict; use warnings; my $max = 4; my $c = 0; foreach (@ARGV) { wait unless ++$c <= $max; die "Fork failed: $!\n" unless defined (my $pid = fork); exit &process($_) unless $pid; } 1 until -1 == wait; ...

    I leave the process routine and appropriate error checking to you.

Re: Multiple Instances
by Anonymous Monk on Mar 11, 2009 at 15:10 UTC
Re: Multiple Instances
by jethro (Monsignor) on Mar 11, 2009 at 15:42 UTC

    Do the parameters 1 2 3... give any additional information? I.e. is 1 "M" 3 "007" a useful combination of parameters that is different from 1 "M" 2 "007"? If not, just drop them, the number of instances you want to call is trivially determinded by the number of arguments you give your perl program

Re: Multiple Instances
by VinsWorldcom (Prior) on Mar 11, 2009 at 18:06 UTC

    Rather than using the ~ 1 "M" 2 "007" 3 "MI6" ~ format, just provide the arugments you want to each instance of the EXE to the Perl script as arguments.

    #!/usr/bin/perl use strict; foreach (@ARGV) { system ("James.exe $_") }

    Then simply use:

    bond.pl M 007 "MI-6" "this is how youd send multiple to one EXE instan +ce"

    to run

    James.exe M James.exe 007 James.exe MI-6 James.exe this is how youd send multiple to one EXE instance

      How do you provide the double quotes for double quoted arguments - for example, when there are paths or file names with spaces in them?


      True laziness is hard work

        I tested this on Windows XP with ActiveState Perl v5.8.8

        #!/usr/bin/perl use strict; foreach (@ARGV) { print "Staring: NOTEPAD $_\n"; system ("start notepad $_") }
        {C} > test \AUTOEXEC.BAT "\Documents and Settings\601404184\My Documents\mcafee.txt" test.pl
        Staring: NOTEPAD \AUTOEXEC.BAT
        Staring: NOTEPAD \Documents and Settings\601404184\My Documents\mcafee.txt
        Staring: NOTEPAD test.pl

        {C} >

        NOTE: That's the output you see in CMD.EXE window. Also, three instances of NOTEPAD.EXE open, with each file in one of the Notepads.

        Does that answer the question re: double-quotes for file names? The second argument had spaces and the first two had backslashes, also could be an issue, but handled fine. I understand slashes would be the other way on *nix path names and spaces would need to be escaped so I don't know how well this would work. Also, you wouldn't use "start" in the system() call; rather, an ampersand (&) to launch the program in the background perhaps?