LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   call perl script through another perl script with arguments (https://www.linuxquestions.org/questions/linux-newbie-8/call-perl-script-through-another-perl-script-with-arguments-739721/)

David1357 07-15-2009 10:55 AM

Quote:

Originally Posted by nanda22 (Post 3607976)
Please help

I changed the scripts to

add.pl
Code:

use warnings;

my $sum = qx(perl addition.pl 10 -20);
print "Result is $sum";

addition.pl
Code:

($a, $b) = @ARGV;

if ( ! defined($a) ) {
    print STDERR"usage add <a> <b>\n";
    exit;

}
else
{
    $n = 0;
    $n = $a + $b;
}
print $n;

When I ran "add.pl" from a Windows command line, I got this result:
Code:

C:\dir>add.pl
Result is -10

If I put the "require" statement in "add.pl", I get this:
Code:

C:\dir>add.pl
usage add <a> <b>


chrism01 07-15-2009 06:45 PM

'require' is like an include. You can't call the 'require'd code as an external program. Its one approach or the other.
You may find these links useful.

http://perldoc.perl.org/ - Perl lang with lots of examples & tutorials
http://www.perlmonks.org/?node=Tutorials

nanda22 07-16-2009 11:54 PM

Dear Chris,
Thank you for wonderrful links you've provided. I'm going through them. I hope i could find answer somewhere.


Dear David and Chris,
The program has some strange behaviour, that is what i wanted to understand.
See for example
if you put comment near print in add.pl
Code:

use warnings;

my $sum = qx(perl addition.pl 10 -20);
#print "Result is $sum";

it wont result in anything. Even though you've given print in additon.pl
Code:

($a, $b) = @ARGV;

if ( ! defined($a) ) {
    print STDERR"usage add <a> <b>\n";
    exit;

}
else
{
    $n = 0;
    $n = $a + $b;
}
print "\nSum of a, b is $n";

but if you remove comments in add.pl, if will result in
Code:

Result is
Sum of a, b is -10

I'm not understaning the program behaviour. Can you please explain me.
All i want is capture the result of addition.pl in $sum, becuase that i would be passing to some other perl script as input.
Thank you very much for your patience in dealing with this issue.

chrism01 07-19-2009 08:34 PM

All(!) the output from the 2nd perl script is captured in $sum in the first script.
You're making this overly complex. Why not just put the addition in a subroutine in the first perl script?

David1357 07-20-2009 11:41 AM

Quote:

Originally Posted by nanda22 (Post 3610303)
I'm not understaning the program behaviour. Can you please explain me.

The string "\nSum of a, b, is $n" get stored in $sum. Because you do not print $sum, nothing is ever output.

To reiterate, if you capture the output of a script using the "qx" function, you will not see the output.

Quote:

Originally Posted by nanda22 (Post 3610303)
All i want is capture the result of addition.pl in $sum, becuase that i would be passing to some other perl script as input.

The output is captured. But nothing gets printed, as explained above. If I create a third script, "show.pl":
Code:

($a) = @ARGV;

if ( ! defined($a) ) {
    print STDERR"usage show <a>\n";
    exit;
}

print "\$a = $a";

and modify "add.pl":
Code:

use warnings;

my $sum = qx(perl addition.pl 10 -20);
system("perl show.pl $sum");

and use the following "addition.pl":
Code:

($a, $b) = @ARGV;

if ( ! defined($a) ) {
    print STDERR"usage add <a> <b>\n";
    exit;

}
else
{
    $n = 0;
    $n = $a + $b;
}
print "$n";

I get this output
Code:

C:\blah>add
$a = -10

I hope this clears up your questions.

nanda22 07-21-2009 12:14 AM

I got it. Thanks a lot David.

nanda22 07-21-2009 12:18 AM

Hi chris, the above question is just a part of big coding work, done by my colleague and i'm continuing the work. where i was missing output of some other script captured into a variable. so i wrote this simple program just for my undertanding, as this is my first perl program.
Yet to swim the big sea, i'm just at first tide.... Anyway thanks for your links which are helping me to learn the basic skills.


All times are GMT -5. The time now is 06:17 AM.