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

I'm new to this whole IPC thing, so please bear with me...

I need to run the same external executable 100 times with slightly different parameters, and I can only have 5 instances running at a time (not counting the initiating Perl script).

Here's my code:

$counter = 0; $pid_count = 0; do { if ($pid_count < 5) { ++$counter; ++$pid_count; if (!defined($$counter = fork())) { ## <---This is line 175 warn "Unable to fork $counter: $!"; } elsif ($$counter == 0) { print "Running this command line: $cmd[$counter]\n"; exec("$cmd[$counter]"); warn "Can't exec $cmd[$counter]: $!"; } else { waitpid($$counter, 0); --$pid_count; } } } until ($counter == $max_frag_id);

When I run this, I get the following error message: Modification of a read-only value attempted at script.pl line 175.

What does this mean and how do I fix it?

Many thanks!

Kevin J.

Replies are listed 'Best First'.
Re: Forking 100 processes, 5 at a time
by Abigail-II (Bishop) on Jun 14, 2002 at 18:52 UTC
    $counter is a number. So, $$counter is something like $3, one the special variables of Perl. And those are read-only (and set by regular expressions).

    Your design seems to be flawed. fork returns a process-id. Why you want to dereference that is beyond me.

    Abigail

Re: Forking 100 processes, 5 at a time
by particle (Vicar) on Jun 14, 2002 at 18:50 UTC
    Update: eat lunch, particle. Abigail-II is right. it's trying to interpret the value of $counter as a variable name-- and it's a perl-special constant var. things like this are often not what you mean, and are caught by use strict 'refs';
    ---

    it's a syntax error, i assume you mean $counter and not $$counter. you're dereferencing the scalar, which is giving you it's memory location. you can't assign to that, because it's a constant. you'll have to fix lines 178 and 184 as well.

    i must say, this code doesn't look right. i'd use Parallel::ForkManager for this application -- it's a well-oiled wheel.

    ~Particle *accelerates*

      I didn't think of it as "dereferencing" (although Lord knows why I didn't...), I was thinking of variable substitution. Bu now I see what's going on - many thanks!
Re: Forking 100 processes, 5 at a time
by Anonymous Monk on Jun 14, 2002 at 23:15 UTC