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

How do i pass an array from one perl program to the other perl program and get back the array to the original program after doing some manipulations?

( Let say i have Perl_program_1 and perl_program_2.)

I have to pass an array from Perl_program_1 to Perl_program_2 and receive the array back to Perl_program_1 after doing some calculations on the array values)

Please help me out!

Thanks for your help

edited: 14 June 2002, by footpad - Changed title, added HTML formatting, and fixed typos.

  • Comment on Passing Data between Perl Programs (was: arrays)

Replies are listed 'Best First'.
Re: Passing Data between Perl Programs (was: arrays)
by Abigail-II (Bishop) on Jun 14, 2002 at 18:24 UTC
    There are several ways. Using shared memory seems to obvious choice. Alternatively, you can translate the array to a string, pass the string from on program to another (pipe, file, fifo, socket), make an array of that again, do your calculations, stringify it again, pass it back over some channel, and turn it into an array again. You could use Data::Dumper for the string/array translations.

    I'd redesign the setup and make sure I wouldn't need to pass arrays from one program to the other. But I'm funny that way.

    Abigail

Re: Passing Data between Perl Programs (was: arrays)
by blackjudas (Pilgrim) on Jun 14, 2002 at 18:39 UTC
    Well my first thought on how you can manage this is using modules. If you are specifically tying one script to the other via passed variables, then the solution I suggest is using modules.

    Try the following:

    ----- mainscript.pl use SecondScript qw ( modify_array ); my @array = ('one', 'two', 3); my @modified_array = @{modify_array(\@array)}; ----- END ----- SecondScript.pm package SecondScript; use strict; use warnings; require Exporter; use vars qw(@ISA @EXPORT_OK); @ISA = qw(Exporter); @EXPORT_OK = qw(modify_array); sub modify_array { my @array = @{(shift)}; # make your modifications here return \@array; } ----- END


    The first script calls on the module called SecondScript as well as an Exported subroutine called modify_array. @array is declared with default values, then passed to the subroutine contained in SecondScript. Notice the backslash used, this is used to pass a reference of the array to the called subroutine.

    The modify_array sub in the SecondScript module is called into action. The first thing it does is pull in the passed array and dereferences it into a local variable. There it will make whatever modifications you want it to and return a reference of that array (return \@array;).

    And now.. back to the first script. It declares a new array called @modified array which will be populated wit the results returned by the modify_array subroutine in SecondScript.
    BlackJudas
      My understanding is that question involves communicating between different processess. The question says programs, but really meant processes.

      So I don't think that your anwer addresses the stated problem.

      -- stefp -- check out TeXmacs wiki

Re: Passing Data between Perl Programs (was: arrays)
by stefp (Vicar) on Jun 15, 2002 at 14:00 UTC
    If you really need to pass perl structures between two process you need to serialize/deserialize them. you may want to use FreezeThaw, Data::Serializer or Storable. The last has the advantage to be part of recent Perl distros.

    -- stefp -- check out TeXmacs wiki