LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-13-2009, 07:07 AM   #1
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Rep: Reputation: 15
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

Last edited by nanda22; 07-13-2009 at 07:08 AM.
 
Old 07-13-2009, 07:46 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Provide the content of your addition.pl
 
Old 07-13-2009, 10:07 AM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by nanda22 View Post
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"
/>
 
Old 07-14-2009, 04:45 AM   #4
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by David1357 View Post
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;

}
 
Old 07-14-2009, 05:04 AM   #5
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
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.
 
Old 07-14-2009, 05:14 AM   #6
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by PMP View Post

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.
 
Old 07-14-2009, 05:17 AM   #7
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
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;
 
Old 07-14-2009, 05:38 AM   #8
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
I'm still facing the same error
I'm working on Windows XP, does anything to do with that?
 
Old 07-14-2009, 05:53 AM   #9
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
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.
 
Old 07-14-2009, 07:05 AM   #10
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
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
 
Old 07-14-2009, 07:32 AM   #11
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Your code misses semicolon in this
Code:
print "Result is $sum"
 
Old 07-14-2009, 04:44 PM   #12
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
As PMP pointed out, the double quotes in my example were incorrect.

Quote:
Originally Posted by David1357 View Post
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
/>
 
Old 07-14-2009, 11:12 PM   #13
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
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
 
Old 07-14-2009, 11:22 PM   #14
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
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
 
Old 07-14-2009, 11:36 PM   #15
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
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

Last edited by nanda22; 07-14-2009 at 11:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl script call python -linux cdd Programming 3 05-04-2007 03:08 PM
perl script call python -linux cdd Linux - Software 1 05-03-2007 03:45 PM
call perl cgi script from php j-ray Programming 2 01-14-2005 08:23 AM
How to Call a C program from Perl CGI Script anoop_cn Programming 1 05-11-2004 04:37 PM
Perl: Getting a script to accept arguments JStew Programming 10 03-10-2003 06:56 AM


All times are GMT -5. The time now is 10:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration