LinuxQuestions.org

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

nanda22 07-13-2009 07:07 AM

call perl script through another perl script with arguments
 
Hi All,
I'm trying to call a perl script with arguments, through another perl script, but its not taking the argumements. if i'm excutating the same script outside with argumetns its working fine,

the Main script looks like this

</
use warnings;
require "addition.pl";

my $n=system("perl addition.pl -12.3 3.2");
print "Sum is $n"
/>

When i call addition.pl separately out side, with same arguments it gives result.

</
perl addition.pl -12.3 3.2
Sum is -9.1
/>
thanks in advance

PMP 07-13-2009 07:46 AM

Provide the content of your addition.pl

David1357 07-13-2009 10:07 AM

Quote:

Originally Posted by nanda22 (Post 3605963)
I'm trying to call a perl script with arguments, through another perl script, but its not taking the argumements.

You really want
Code:

</
use warnings;
require "addition.pl";

# Use qx to capture output of shell command.
# "system" requires an array and only returns the exit code.
my $n = qx("/path/to/perl addition.pl -12.3 3.2");
print "Sum is $n"
/>


nanda22 07-14-2009 04:45 AM

Quote:

Originally Posted by David1357 (Post 3606139)
You really want
Code:

</
use warnings;
require "addition.pl";

# Use qx to capture output of shell command.
# "system" requires an array and only returns the exit code.
my $n = qx("/path/to/perl addition.pl -12.3 3.2");
print "Sum is $n"
/>


Hi I tried using "qx" still it doesn't show up any result

The full code is as given below

add.pl

Code:


my $sum= qx("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;

$n = 0;
$n = $a + $b;

}


PMP 07-14-2009 05:04 AM

Code:

($a, $b) = @ARGV;

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

$n = 0;
$n = $a + $b;

}

This if clause here will be true only if $a is not defined. hence the sum will be calculated then only.

This seems to me a logical error.

nanda22 07-14-2009 05:14 AM

Quote:

Originally Posted by PMP (Post 3607036)

This if clause here will be true only if $a is not defined. hence the sum will be calculated then only.

This seems to me a logical error.

Sorry for typo error, the closing brace is before
$n = 0, i misplaced while typing my question

and i forgot to give, what error am facing

Code:

C:\Perl\perl add.pl
syntax error at add.pl line 4, near "my "
Execution of add.pl aborted due to compilation errors.


PMP 07-14-2009 05:17 AM

This code works for me try this out

add.pl
Code:

my $sum = qx(perl addition.pl 10 -20); ## Remove double quotes from your code
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;


nanda22 07-14-2009 05:38 AM

I'm still facing the same error
I'm working on Windows XP, does anything to do with that?

PMP 07-14-2009 05:53 AM

Well I tried it on Red Hat. I cannot see the line 4 in your add.pl May be some syntex error.

Will be able to comment only after seeing the code.

nanda22 07-14-2009 07:05 AM

add.pl
Code:

use warnings;
require "addition.pl"
 
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;

the above is the code, same as what you've asked me to do

PMP 07-14-2009 07:32 AM

Your code misses semicolon in this
Code:

print "Result is $sum"

David1357 07-14-2009 04:44 PM

As PMP pointed out, the double quotes in my example were incorrect.

Quote:

Originally Posted by David1357 (Post 3606139)
You really want
Code:

</
use warnings;
require "addition.pl";

# Use qx to capture output of shell command.
# "system" requires an array and only returns the exit code.
my $n = qx(/path/to/perl addition.pl -12.3 3.2);  # No double quotes needed
print "Sum is $n";  # Semi-colon is needed
/>



nanda22 07-14-2009 11:12 PM

Code:

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


i've removed the quotes, Still the same error
syntax error at add.pl line 4, near "my "....
Please help its been 2 days, i'm struggling with this error

nanda22 07-14-2009 11:22 PM

i've added "use strict " at the top of the code, now it has given bit more explanative error lines

syntax error at add.pl line 5, near "my "
Global symbol "$sum" requires explicit package name at add.pl line 5.
Global symbol "$sum" requires explicit package name at add.pl line 6.
Execution of add.pl aborted due to compilation errors.

I hope this would help you more

nanda22 07-14-2009 11:36 PM

I've resolved that syntax error, it is the missing semicolon which irritated till now. we need to add semicolon after require additon.pl

thanks David and PMP

But, the new problem is it doesn't give the Result, it just gave "Result is "
One more thing is even though i passed argumetns here
Code:

my $sum= qx(perl addition.pl 10 -20);
it doesn't take. when i pass externally like "perl add.pl 10 -20" it is working fine but giving "Result is ".

Please help


All times are GMT -5. The time now is 03:58 PM.