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

Hi I am running a perl script which creates three variables..I want to send these variables to another script..How do I send them from the first script and then how do I receive them in the second script Thank you

Replies are listed 'Best First'.
Re: Send variables
by BrowserUk (Patriarch) on Feb 03, 2010 at 17:07 UTC

    In addition to javafan and BioLion responses, you need to clarify if these "three variables" are simple variables like numbers or shortish strings--in which case they can potentially be transfered via the command line.

    Or are they complex variables (like arrays or hashes) which would require the use of pipes or IO::Sockets and perhaps Storable; or something more complex.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Send variables
by zentara (Cardinal) on Feb 03, 2010 at 17:13 UTC
    There are simple commandline pipes which you can set up, but here is a better example. If you need many scripts communicating, you probably want to use sockets.

    The master script to run:

    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; my $pid = open3(\*WRITE, \*READ,0, './myslave1'); #if \*ERROR is false, STDERR is sent to STDOUT while (1){ #send something to slave my $test = time; my $str = $test x 50; print WRITE "$str\n\n\n\n"; #get the answer from slave my $buf; sysread(READ,$buf,1024); print "$test = $buf\n"; sleep 1; } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    and the myslave1 script

    #!/usr/bin/perl use warnings; use strict; use IO::Select; my $timeout = 1; my $s = IO::Select->new(); $s->add( \*STDIN ); while (1) { if ( my @ready = $s->can_read($timeout) ) { # we got input for my $fh (@ready) { my $input; sysread( $fh, $input, 1024); #do some processing, for example syswrite(STDOUT, "processed: $input"); } } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: Send variables
by BioLion (Curate) on Feb 03, 2010 at 16:56 UTC
Re: Send variables
by JavaFan (Canon) on Feb 03, 2010 at 16:48 UTC
    Are you sure you want to send variables? Don't you want to send values?

    As for "sending", what to do depends on what you want. Does the first script start the other? Or are both scripts running by the time you want to exchange values? Or perhaps the second script doesn't start running long after the first one finished? Are they running on the same machine?

    There are several ways of Inter Process Communication discussed in the perlipc manual page. Doesn't any of them work for you?

Re: Send variables
by chromatic (Archbishop) on Feb 03, 2010 at 21:02 UTC

    Don't; you're making your life too complicated.

    Turn both programs into modules and then use them from the same program. Call functions in one module from another module.

    That seems like more work up front, but it's much less fragile and system dependent.

Re: Send variables
by molecules (Monk) on Feb 03, 2010 at 17:08 UTC
    You could also write values to a file using one script and then read them from the file with the second script.