Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Newbie external script return question.

Reply
Thread Tools

Newbie external script return question.

 
 
Jimmy Phillips
Guest
Posts: n/a
 
      09-07-2004
I am playing with how to call external scripts but can't get the return
value. Example

Caller script...
*************
#!/usr/bin/perl -w
use strict;
my $result = `./dice.pl 5`;
print $result . "\n";

The called script...
*************
#!/usr/bin/perl -w
use strict;
my $randresult;
$randresult = rand $ARGV[0];

If I run the called script with a commandline ./dice.pl 30 it works fine.
The caller script calls ok but gets back an undef. Obviously because
nothing got returned.

Question, how do I specify the $randresult to be a return value? The
keywork "return" is no good outside of a subroutine.

What am I missing?

Thanks
JP
 
Reply With Quote
 
 
 
 
Jimmy Phillips
Guest
Posts: n/a
 
      09-07-2004

> Have you tried to print it?


Thank you. That worked but it is not something that I would have
discovered until I finally backed off and got a new view of the problem.
I am too new to scripts and too used to all-in-one programming suites.

It also explains some of the weird results of my interspersing print
statements in both programs to try to figure out what was executing and
what wasn't. It was actually working before at times - I just didn't
realise just who was doing the printing.

So, as I think I understand it now, the calling program is "STDOUT" (so to
speak) to the called program. I should have realised that calling my own
program is no different than calling some system utility like PING and
receiving into a variable what normally would go to the screen. I've done
that before.

Thanks again.

JP

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-07-2004
Jimmy Phillips wrote:
> The called script...
> *************
> #!/usr/bin/perl -w
> use strict;
> my $randresult;
> $randresult = rand $ARGV[0];


<snip>

> Question, how do I specify the $randresult to be a return value? The
> keywork "return" is no good outside of a subroutine.
>
> What am I missing?


Have you tried to print it?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-07-2004
Jimmy Phillips wrote:
> I am playing with how to call external scripts but can't get the
> return value.


You got at least two problems:
- using the wrong function to capture the return value
- not returning anything to begin with

> Example
>
> Caller script...
> *************
> #!/usr/bin/perl -w
> use strict;
> my $result = `./dice.pl 5`;


Backticks will capture the output of the called process, not it's return
value.
If you want to capture the return value then you should use system(). But
beware, system() doesn't return the plain return value. You will have to
post-process the value as explained in the system() perldoc page.

> print $result . "\n";
>
> The called script...
> *************
> #!/usr/bin/perl -w
> use strict;
> my $randresult;
> $randresult = rand $ARGV[0];
>
> If I run the called script with a commandline ./dice.pl 30 it works
> fine. The caller script calls ok but gets back an undef. Obviously
> because nothing got returned.


Correct analysis. You never set the return value of the called program.

> Question, how do I specify the $randresult to be a return value?


perldoc -f exit

However, program return values are typically restricted to small integers in
most OSes.

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a script from another script and other script does notdo busy wait. Rajat Python 3 01-08-2010 02:05 PM
Create references to external scipt files from within an external script file Mellow Crow Javascript 6 11-04-2005 01:16 PM
Call external file with <script src="content.js"></script> insteadof SSI. Blue® Javascript 10 05-27-2005 08:45 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
insert a script element into the document body from an external script.... Brian Javascript 2 06-24-2004 05:25 PM



Advertisments