LinuxQuestions.org
Visit Jeremy's Blog.
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-15-2009, 11:55 AM   #16
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
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>
 
Old 07-15-2009, 07:45 PM   #17
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
'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
 
Old 07-17-2009, 12:54 AM   #18
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
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.
 
Old 07-19-2009, 09:34 PM   #19
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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?
 
Old 07-20-2009, 12:41 PM   #20
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 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 View Post
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.
 
Old 07-21-2009, 01:14 AM   #21
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
I got it. Thanks a lot David.
 
Old 07-21-2009, 01:18 AM   #22
nanda22
Member
 
Registered: Mar 2008
Posts: 58

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


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 04:08 PM
perl script call python -linux cdd Linux - Software 1 05-03-2007 04:45 PM
call perl cgi script from php j-ray Programming 2 01-14-2005 09:23 AM
How to Call a C program from Perl CGI Script anoop_cn Programming 1 05-11-2004 05:37 PM
Perl: Getting a script to accept arguments JStew Programming 10 03-10-2003 07:56 AM


All times are GMT -5. The time now is 03:55 AM.

Main Menu
Advertisement
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