Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl (http://www.velocityreviews.com/forums/f17-perl.html)
-   -   Newbie external script return question. (http://www.velocityreviews.com/forums/t25347-newbie-external-script-return-question.html)

Jimmy Phillips 09-07-2004 01:42 PM

Newbie external script return question.
 
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

Jimmy Phillips 09-07-2004 02:25 PM

Re: Newbie external script return question.
 

> 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


Gunnar Hjalmarsson 09-07-2004 08:47 PM

Re: Newbie external script return question.
 
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

Jürgen Exner 09-07-2004 11:40 PM

Re: Newbie external script return question.
 
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




All times are GMT. The time now is 03:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.