Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How to pass array as a reference from one perl file to other

by PyrexKidd (Monk)
on May 14, 2011 at 21:41 UTC ( [id://904885]=note: print w/replies, xml ) Need Help??


in reply to How to pass array as a reference from one perl file to other

As the previous replier stated you can't. You pass a list of -values- (by value) to your second script. You CAN pass an array by values (unroll it), pass the values, then roll it back up as an array. i.e.:
#!/usr/bin/perl # test1.pl use strict; use warnings; my $array = ( 1, 2, 3, 4); open my $FH, '| ./test2.pl'; print $FH @{$array}; close $FH; #!/usr/bin/perl # test2.pl use strict; use warnings; while (my $var = <>){ print $var; }

However, it is safer to use exec or system then using the two argument open method for opening a file handle. i.e.:

#!/usr/bin/perl # test1.pl use strict; use warnings; my $array = ( 1, 2, 3, 4); my $program = 'test2.pl'; exec { $program } @{$array}; #!/usr/bin/perl # test2.pl use strict; use warnings; print "$_ " foreach @ARGV;
an aside:
`exec` can be written: exec { $program } @{$array}; exec $program, @{$array}; exec { './test2.pl' } @{$array}; exec './test2.pl', @{$array}; exec './test2.pl', 1, 2, 3, 4;

That being said, you may want to investigate modules as this is probably more along the lines of what you are looking for. (I could be way off about the modules, but in general, every time I've wanted to call an external -Perl- script, I've been able to solve my problem more effectively by writing a module that I then import; as you haven't provided any details about the context in which you are attempting to call this external script, I could be wrong.)

Replies are listed 'Best First'.
Re^2: How to pass array as a reference from one perl file to other
by Gangabass (Vicar) on May 15, 2011 at 09:07 UTC
    You have a typo: my $array = ( 1, 2, 3, 4); should be my $array = [ 1, 2, 3, 4 ];
Re^2: How to pass array as a reference from one perl file to other
by romy_mathew (Beadle) on May 15, 2011 at 16:01 UTC
    I am explaning my function below. I am using perl tk application to extract user input from this main program and pass the arguments from the user to child program where it connect to database and perform query. The child program after performing the query should pass the result to the parent module. I need some suggestion on how to perform an two way communication between 2 perl programs where each program can chnage it control from each other

    saw some tips from text books on 2 way communication via use IO::Handle but its very hard to grasp its concept

Re^2: How to pass array as a reference from one perl file to other
by romy_mathew (Beadle) on May 15, 2011 at 19:52 UTC
    okke The use of moudle worked....but still a curious to know how to transfer data to and from two files in perl via pipes or other mehtod. I am confused on below text book e.g.

    use IO::Handle;

    pipe(READFROMCHILD.WRITEFROMCHILD);

    pipe(READFROMPARENT,WRITETOCHILD);

    WRITETOPARENT->autoflush(1);

    if ($pid ==fork) {

    close(READFROMPARENT);

    close(WRITETOPARENT);

    print WRITETOCHILD "parent says hi\n";

    $data = <READFROMCHILD>;

    close READFROMCHILD;

    close WRITETOCHILD;

    waitpid(-1,0); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://904885]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found