LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 10: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, 06:45 PM   #17
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
'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-16-2009, 11:54 PM   #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, 08:34 PM   #19
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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, 11:41 AM   #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, 12: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, 12: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 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:48 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
Open Source Consulting | Domain Registration