2

I have 2 scripts, say main_script.pl and secondary_script.pl

First I am running the main_script.pl which calls seconday_script.pl.

Code looks below for main_script.pl:

#!/usr/bin/perl
use strict;
use warnings;

my $var1 = "val_1";
my $var2 = "val_2";

my $sec_script = "/home/shared/Vinod/Perl_Test/secondary_script.pl";
my $result = `perl $sec_script $var1 $var2`;

print "Result:$result\n";

secondary_script.pl

#!/usr/bin/perl
use strict;
use warnings;

my $arg1 = $ARGV[0];
my $arg2 = $ARGV[1];

....
....
# DO SOME OPEARTION BY USING THE ARGUMENTS PASSED FROM main_script.pl
# FINALLY CREATE AN ARRAY @data

print Dumper(\@data);

Here I can able to generate output in array @data. But how can I pass this @data values to main_script.pl so that it will get stored in result.

Since in main_script.pl I have declared result as an scalar variable. My data value from secondary_script.pl would be array, so should I make result as an array variable? and how can I capture the data in main_script.pl?

3
  • 2
    Curiously, just a couple of days ago I answered a question quite like that, in this post
    – zdim
    Jun 10, 2020 at 6:24
  • @zdim this is much useful answer like Dave's answer.
    – vkk05
    Jun 10, 2020 at 6:30
  • 1
    Great, glad to hear that and thank you :) That's why I dropped the link, hoping it may come useful
    – zdim
    Jun 11, 2020 at 5:36

3 Answers 3

6

In the secondary script, you can join the values with a known seperator, say |. In het main script you can split the string you receive on that seperator to get your array.

To give an idea how that could work:

use Data::Dumper;

my @arr = ("1", "3", "", "5");    
my $result = join ('|', @arr);   # in secondary script. print to stdout 
print STDERR Dumper(split( /\|/, $result));  # in main script: undo join

There is an edge case: if the first or last string of the array is an empty string, it will get lost and you have to do some extra tricks to account for that.

0
5

First, I suggest asking yourself why you're doing this. Do they really need to be two separate programs? Could one, perhaps, become a module which you can then load in the other?

Maybe there are good reasons for doing it like this. Personally, I doubt it.

But, assuming you decide that you need to do it how you're currently doing it, then we need to talk about "serialisation" and "deserialisation". Serialisation is the act of taking a complex internal data structure and turning it into a representation (often a string) which can be safely passed outside of your program. Deserialisation is the opposite - you take the external representation and turn it back into a data structure.

So in your secondary script you need a subroutine called serialise() that you call like this:

print serialise(@data);

And in your main script, you can have:

my @result_array = deserialise(`... your call ...`);

You have several options for (de)serialisation. You could make your own (de)serialiser using Perl built-ins like join() and split().

# This assumes you're sure that your array elements won't contain ':'.
# If that's a problem, then choose a different character.
sub serialise {
  return join ':', @_;
}

sub deserialise {
  return split /:/, $_[0];
}

You could use freeze() and thaw() subroutines from the standard Storable library:

use Storable qw[freeze thaw];

# Note that freeze() takes a reference and thaw() returns a reference.
sub serialise {
  return freeze($_[0]);
}

sub deserialise {
  return thaw($_[0]);
}

But I think the approach I would take would be to use a human-readable serialisation format like YAML or JSON.

1
  • I strongly agree to your logic to make secondary script as an package.
    – vkk05
    Jun 9, 2020 at 14:58
5

In the first script, output one element per line:

print join "\n", @data;

Then, read from backticks in list context to get one output line per element, then remove the line separator:

my @array = `...`;
chomp( @array );

If a newline is significant, use some other separator (I tend to like form feeds).

But, instead of that, perhaps consider something along the modulino design where the data-generating program can act as both a module and a program. When another program needs it, it can load it as a module and call the right subroutine. No second program needed.

Beyond that, there are various interchange formats that might be appropriate. The particular modules I link to in this list may not be suitable for every task:

Note that it's not always appropriate to reach for these formats right away. There's a cost associated with marshaling and unmarshalling the data. Profiling the hidden costs of JSON and HTTP(S) is an interesting read.

I'd tend to optimize for the "most friendly" output. The output format you choose decides how much pain the other side has to feel. My facile idea of simply printing to standard output means that everyone else has to treat your output as special. Maybe JSON fits in with what they're already doing elsewhere.

Or, you can output whatever they like. Have a way to do line-oriented output, JSON, XML, or whatever. Coming up with a way to do any format you choose means that you can easily add another format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.