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

The Apache::SubProcess documneatation page shows :
$out_fh = Apache::SubProcess::spawn_proc_prog($r, $command, \@argv);

How do I read back the values from argv passed by reference ? Across process boundaries the address of the referent is retained, but the size of the array drops to zero. What am I missing here? This seems to be the case in the "normal" perl interepreter as well...

Replies are listed 'Best First'.
Re: passing argv by reference
by Chady (Priest) on Jun 09, 2003 at 08:51 UTC

    References in perl are a bit different from C or other languages.

    What you have passed is like a scalar with the reference to an array; so the ways to retrieve it are:

    sub spawn_proc_prog { my ($r, $command, $argv) = @_; # to access the elements of $argv you do my $first = $argv->[0]; my $second = $argv->[1]; # or my @argv = @{$argv}; # and there's a lot more..

    Update: try perldoc perlref for more information about references in perl.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      I seem to have asked the question incorrectly. spawn_proc_prog will spawn (like the "system" call) the program specified in $command and send it arguments in the argv array. What I want to do is read the content of the argv array in the program that receives it (the one in $command), and $r is the apache request object...

      My usage looks something like :

      $command = "foo.pl";
      @args = qw(1 2 3 4 5);
      spawn_proc_prog($r, $command, \@argv);


      Now I want to be able to read the argv array in foo.pl. I get the referent, but it arrives as an empty array...

      Thanks
      - BGa
        Since it's being handled as a command, did you look in @ARGV of foo.pl?

        This is reinforced by the module doc that says:
        The third optional argument is a reference to an array which if passed becomes ARGV to the spawned program.


        --Bob Niederman, http://bob-n.com
        I don't think perl automagically will allow two (or more) processes to read and write to the same memory. The solution? Look into the IPC::Shareable module. It provides routines to tie variables to Shared memory, and lock and unlock access to avoid race conditions. If I get some time I'll post sample code.
Re: passing argv by reference (copy vs. output)
by tye (Sage) on Jun 09, 2003 at 16:49 UTC

    Just because @argv is passed by reference, it doesn't always mean that @argv will be modified for you to read the changes. @argv is probably passed by reference simply to prevent the copying of a potentially long list of values or perhaps to make the API for that function easier to change/extend or even to make it possible to detect certain usage mistakes (or perhaps due to all of the above).

    In the Unix 'spawn' interface (see exec and/or "man exec"; 'spawn' also involves fork but that part doesn't affect 'argv') argv is clearly copied to the program being invoked and not copied back.

                    - tye