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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
07-15-2009, 10:55 AM
|
#16
|
|
Senior Member
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,300
Rep: 
|
Quote:
Originally Posted by nanda22
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>
|
|
|
|
07-15-2009, 06:45 PM
|
#17
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
'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
|
|
|
|
07-16-2009, 11:54 PM
|
#18
|
|
Member
Registered: Mar 2008
Posts: 58
Original Poster
Rep:
|
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.
|
|
|
|
07-19-2009, 08:34 PM
|
#19
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
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?
|
|
|
|
07-20-2009, 11:41 AM
|
#20
|
|
Senior Member
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,300
Rep: 
|
Quote:
Originally Posted by nanda22
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
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.
|
|
|
|
07-21-2009, 12:14 AM
|
#21
|
|
Member
Registered: Mar 2008
Posts: 58
Original Poster
Rep:
|
I got it. Thanks a lot David.
|
|
|
|
07-21-2009, 12:18 AM
|
#22
|
|
Member
Registered: Mar 2008
Posts: 58
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:11 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|