Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Returning from a program

by peschkaj (Pilgrim)
on Aug 12, 2002 at 13:45 UTC ( #189484=perlquestion: print w/replies, xml ) Need Help??

peschkaj has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to return a value from a program to another calling program? I have some code that will be run as a menu from a kshell. I would like to return a value from the perl to the kshell for the kshell to listen for and then execute accordingly. Obviously, I can't use return() since I'm not doing this from inside a sub, eval, or do.

Thanks in adavance.

Replies are listed 'Best First'.
Re: Returning from a program
by twerq (Deacon) on Aug 12, 2002 at 13:53 UTC
Re: Returning from a program
by Tomte (Priest) on Aug 12, 2002 at 14:03 UTC

    Well, as a shell programmer you could have guessed it:

    exit <statuscode>

    see perlfunc exit

    your shell code may look like:

    $my_perl_exec myprog.pl arg1 arg2 arg3 if [ "${?}" -eq "<exitcode1>" ] then blahblah else xxyyzz fi

    regards,
    tomte
Re: Returning from a program
by BrowserUk (Patriarch) on Aug 12, 2002 at 16:59 UTC

    One possibility.

    If the volume of information you wish to return is greater than can be contained in an int, but writing it to and reading it back from a file would present difficulties in the shell script, you could write the information in the name of an empty file.

    If you choose (say) a prefix for the application and put the relavent info in the suffix, it should be fairly easy to find and process within you shell script.

    eg. myapp.nm09 or myapp.op01....

    Just a thought.

    UpdateRemoved brain-fart repetition.

Re: Returning from a program
by fuzzyping (Chaplain) on Aug 12, 2002 at 13:55 UTC
    You haven't specified how kshell expects the returned data, so it's hard to help. Does it expect it via some IPC, or will it simply read from STDOUT? I expect that you could simply do command substitution via your perl script (using backticks) and have kshell read in the script's STDOUT results.

    -fp
Re: Returning from a program
by ColtsFoot (Chaplain) on Aug 12, 2002 at 13:55 UTC
    Why not try exit($retval) where $retval has been set to the
    value you wish to return as the error/success status

    Hope this helps

Re: Returning from a program
by peschkaj (Pilgrim) on Aug 12, 2002 at 14:32 UTC
    I suppose I should be more specific. The perl script will allow the user to select from a menu of 1 to x of possible instances of a program. We have a setup script that runs like this:setup_env [INSTANCE_NAME] where INSTANCE_NAME is something like nm09 or op01. In this instance, it looks like exit() will not work because exit() sends EXPR as an integer.

    I suppose I could substr() out the first two letters of the parameter and pass the 09 or 70 or whatever through to the shell script and have it do that. (But then wouldn't I be better off writing the whole thing as a shell script?)

    UPDATE: The problem is that we are writing the kshell wrapper to ensure that the environment is setup, rather than contained in a subshell which is gone once the perl program stops running.

    Is this possible with IPC, or would it be easier to just write to disk and read the file from the kshell?
Re: Returning from a program
by graff (Chancellor) on Aug 12, 2002 at 21:28 UTC
    If kshell supports the same kind of shell variable assignments that are typical in (ba)sh, then something like this ought to work:

    The perl script simply prints the name of the selected file to stdout, terminated by a line-feed.

    The perl script is invoked from kshell using backticks, in a construct like the following:

    ENV_FILE=`selection_script.perl` setup_env ENV_FILE

    Unlike the perl backtick operator, the use of backticks in (ba)sh returns the output of the command with all line-feeds converted to spaces.

    update: revised the shell script to reflect the example given by peschkaj.

Returning from a program
by atopolc (Pilgrim) on Aug 12, 2002 at 16:51 UTC
    Why not print to standard error anything that you would like to return to kshell.
    Your kshell script could then capture that information.

    This is how it would look.

    In perl:
     print STDERR 12.34

    In kshell:
    scriptoutput=$(script.pl ${arg1} ${arg2} 2>&1) echo "number returned from script -> ${scriptoutput}"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://189484]
Approved by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2022-01-27 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (70 votes). Check out past polls.

    Notices?