Newbie external script return question.

J

Jimmy Phillips

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
 
J

Jimmy Phillips

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
 
G

Gunnar Hjalmarsson

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

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?
 
J

Jürgen Exner

Jimmy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top