We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,453 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Using variables in external system calls

I have 2 perl files. 1st file is my main program and the 2nd is a small sub which I call by using system("file.pl"); I would like to reuse a stored value/s in variables from the main program and use them in the external file. How is this possible?

2
Contributors
2
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
4
Views
MCLASS80
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I have 2 perl files. 1st file is my main program and the 2nd is a small sub which I call by using system("file.pl"); I would like to reuse a stored value/s in variables from the main program and use them in the external file. How is this possible?

Main script calls perl and passes the name of another script plus a couple of values for the second script to print.

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

my $command = '/usr/bin/perl';
my $script = '/home/david/Programming/Perl/print_values.pl';
my $value1 = 'Boiling point of water';
my $value2 = '100 degrees celsius';

system($command, $script, $value1, $value2) == 0
    or die "system $command, $script, $value1, $value2 failed: $?"

print_values.pl

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

my ($v1, $v2) = @ARGV;

print "First variable contains '$v1'\n";
print "Second variable contains '$v2'\n";
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

Have a look at this similar question on StackOverflow. I like the following idea:

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

my $command = '/usr/bin/perl';
my $script = '/home/david/Programming/Perl/print_values.pl';
my $value1 = 'Boiling point of water';
my $value2 = '100 degrees celsius';

{
    local @ARGV = ($value1, $value2);
    unless (my $return = do $script) {
        warn "couldn't parse $script: $@" if $@;
        warn "couldn't do $script: $!"    unless defined $return;
        warn "couldn't run $script"       unless $return;
    }
}
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5190 seconds using 2.66MB