Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Scalar/ Array values from a called program 2

Status
Not open for further replies.

ljsmith91

Programmer
May 28, 2003
305
US
This question is in regards to receiving info from a called program(script).

Program A calls Program B via a SYSTEM call in PROGRAM A. Program B writes out to many arrays and scalars information that Program A needs. However, when attempting to extract the values from PROGRAM B defined and populated scalars and arrays on return from PROGRAM B, the values of the arrays and acalars are empty.

Example draft of code:

Program A
system(PROGRAM B)
# on return from Program B print a PROGRAM B scalar
print "Program B has a scalar and its value is
$programB_scalar\n";

I cannot get the scalar value to print on return. I am looking to do this for several scalars and arrays created by the PROGRAM B program.

How is this done without moving the entire PROGRAM B code into PROGRAM A ? Do the scalars have to be defined a certain way ? Does the program have to be called a certain way? Or is there no way of doing this ?

Thanks.

LJS




 
Have a look a Storable (1) as a more reliable way to move data between scripts. Note that Storable is in Core Perl, so you don't need to download it and install, as you already have it.

(1) [URL unfurl="true"]http://search.cpan.org/dist/Storable/Storable.pm[/link

In Program B you can do something like:

Code:
#!/usr/bin/perl -w
use strict;

use Storable;

my @list = qw(1 3 5);
my %hash = (a => 1, b => 3);

my %data = (
  scalar => 8,
  list   => \@list,
  hash   => \%hash
);

store \%data, 'xfer_file.dat';

The in Program A do the following:

Code:
#!/usr/bin/perl -w
use strict;

use Storable;

my $data = retrieve('xfer_file.dat');

my $scalar = $data->{scalar};
my @list   = @{ $data->{list} };
my %hash   = %{ $data->{hash} };

Barbie
Leader of Birmingham Perl Mongers
 
Unfotunately, Storable is not on my system. It will take some time to get the mod approved for install. Any other way of getting access to a scalar/ array created by a called program ? -LJS
 
While you're waiting for Storable, are you sure it's not, as Barbie points out it's part of the core distribution

What version, and breed of Perl are you running?

You could dump the data to a file, and read it back in, in Program A

Program A
Code:
#!/usr/bin/perl
$filename=time ."$$.txt";  #$$ is the process id, just an old habit
$result=`perl scriptb.pl $filename`;
open FH, "<$filename";
$/=undef;
$results=<FH>;
close FH;
@data=split /\|::\|/, $results;
foreach (@data) {
#do whatever
}
Program B
Code:
#!/usr/bin/perl
$filename=$ARGV[0];
open FH, ">$filename";
#
#
#   do what you gotta
#
$data=join ("|::|", @results);
print FH $data;
close FH;

Hope that makes sense
--Paul

cigless ...
 
Or write to a package and require it:

Program B:
Code:
#!/usr/bin/perl -w
use strict;

open FH, ">MyData.pm" or die "Cannot open file: $!\n";
print "package MyData;\n\n";
print q!my $scalar=!.$scalar.";\n";
print q!my @list=('!.join(q!','!,@list)."');\n";
close FH;

Program A:
Code:
#!/usr/bin/perl -w
use strict;

use lib (".");

system("programb.pl");

require "MyData";

my $scalar = $MyData::scalar;
my @list = @MyData::list;

But that can get a bit messy with quoting. I am surprised you can't use Storable though. Ah Having check with Module::CoreList, Storable didn't go into core until 5.7.3, but I believe its in ActivePerl 5.6.1. Hope you can get it installed as it is really useful :)

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top