Re: Passing Array to seperate file
by davido (Cardinal) on Mar 01, 2004 at 16:14 UTC
|
See perldoc -f open and perlopentut with respect to opening a pipe to another command or process. You could open a pipe to the other Perl script, and then print the contents of your array into that pipe for the other script to retrieve via STDIN.
| [reply] |
|
|
I have read over the perldoc -f open documentation. can you provide a simple code example of the process for an array in file1 called "@combined" and to be used in file2 called "switch.pl"
Thanks
| [reply] |
|
|
That's why I suggested reading perlopentut. Here's an example.....
# controlcript
open WORKER, "| workerscript.pl" or die;
print WORKER "$_\n" for @combined;
close WORKER;
# This is the worker script, entitled workerscript.pl
my @combined;
while ( <STDIN> ) {
push @combined, $_;
}
chomp @combined;
Or just slurp in the worker script...
my @combined = <STDIN>;
chomp @combined;
| [reply] [d/l] [select] |
|
|
Re: Passing Array to seperate file
by Abigail-II (Bishop) on Mar 01, 2004 at 16:27 UTC
|
It depends. It depends on when you want to run that other program. Do you want to run both programs simultaneously?
Will the other program be started from the first? Will the
other program be run sometime after the first program?
Possible ways of passing information from one program to another include, but are not limited to: on disk storage (files, databases), environment variables, command line arguments, pipes, sockets, shared memory.
What's the most appropriate method depends on many factors.
Abigail | [reply] |
Re: Passing Array to seperate file
by esskar (Deacon) on Mar 01, 2004 at 16:21 UTC
|
or use the system command
system("otherscript.pl", @stringarray);
and otherscript.pl has to evaluate @ARGV | [reply] [d/l] |
|
|
ok I have a modified version of that code system(targetfile.pl, @array);
how do I modify the "other files" open(HANDLE, final.txt) to now handle the array the same way as the final input since I was reading each $line of the file and then processing accordingly. Will $line pull each item in the array the same as the file way?
Thanks
| [reply] |
|
|
the "targetfile.pl" now can do the following:
foreach my $line (@ARGV)
{
print "$line\n";
# or do anything else with it
}
| [reply] [d/l] |
Re: Passing Array to seperate file
by Plankton (Vicar) on Mar 01, 2004 at 16:49 UTC
|
Why not just pipe the output from your first perl script to your second perl script?
$ perlfile1 | perlfile2
| Plankton: 1% Evil, 99% Hot Gas. |
| [reply] [d/l] |
Re: Passing Array to separate file
by Roy Johnson (Monsignor) on Mar 01, 2004 at 18:40 UTC
|
do secondfile;
work for you? It will execute secondfile (your second Perl script) in the current Perl environment, so your array is there (and so are any other variables, subroutines, etc. that you've defined in the calling program).
Update: your array would need to be not lexically scoped (with my), though. Declaring it as our would be a good choice.
The PerlMonk tr/// Advocate
| [reply] [d/l] [select] |
Re: Passing Array to seperate file
by iburrell (Chaplain) on Mar 01, 2004 at 21:33 UTC
|
One thing to worry about is how you encode the arrays for transmission. If the array only contains simple strings without line endings, then writing each string on a separate line would work well. If the arrays could contain anything, then you need to use a more robust serialization format.
I would suggest YAML.
Do you want the first script to run the second script? Or would having them run by an external process work?
The Windows NT/2000/XP command shell will do pipes.
command1 | command2
The advantage of the first script writing to stdout, and the second script reading from stdin, is that you can do different combinations:
command1 > output.yml
command2 < output.yml
| [reply] [d/l] [select] |