Newbie external script return question.

Discussion in 'Perl' started by Jimmy Phillips, Sep 7, 2004.

  1. 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, Sep 7, 2004
    #1
    1. Advertising


  2. > 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
    Jimmy Phillips, Sep 7, 2004
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, Sep 7, 2004
    #3
  4. 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
    Jürgen Exner, Sep 8, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Allen
    Replies:
    8
    Views:
    10,931
    Scott Allen
    May 2, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    812
    Keith Thompson
    Mar 6, 2005
  3. Brian
    Replies:
    2
    Views:
    225
  4. Blue®
    Replies:
    10
    Views:
    311
    Thomas 'PointedEars' Lahn
    May 27, 2005
  5. Mellow Crow
    Replies:
    6
    Views:
    417
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page