Howto get return code from a extern program?

Discussion in 'Perl Misc' started by yong, Dec 5, 2005.

  1. yong

    yong Guest

    Hello all

    I lauch a program with system(),but the function returns only the
    outpout.how can I get the value of the program return to shell?

    Thanks.
     
    yong, Dec 5, 2005
    #1
    1. Advertising

  2. yong <> writes:

    > I lauch a program with system(),but the function returns only the
    > outpout.how can I get the value of the program return to shell?


    Please read the documentation for the function you're referring to:

    perldoc -f system

    The system() function does *not* return the output of the launched
    program, and it *does* get the return value.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Dec 5, 2005
    #2
    1. Advertising

  3. yong

    Dr.Ruud Guest

    yong schreef:

    > I lauch a program with system(),but the function returns only the
    > outpout.how can I get the value of the program return to shell?


    Always read `perldoc -f system` first.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Dec 5, 2005
    #3
  4. yong

    yong Guest

    Thanks :)

    Sherm Pendley wrote:
    > yong <> writes:
    >
    > Please read the documentation for the function you're referring to:
    >
    > perldoc -f system
    >
    > The system() function does *not* return the output of the launched
    > program, and it *does* get the return value.
    >
    > sherm--
     
    yong, Dec 5, 2005
    #4
  5. yong wrote:
    > Hello all
    >
    > I lauch a program with system(),but the function returns only the
    > outpout.how can I get the value of the program return to shell?
    >
    > Thanks.


    backticks ( ` ) get the output. system captures the return code in the
    special variable $?. to get the actual exit code, shift >> 8, or divide
    by 256 (same thing).

    system("print 'hello world'") == 0 or die "command failed: $?\n";
     
    it_says_BALLS_on_your forehead, Dec 5, 2005
    #5
  6. "Purl Gurl" <> wrote in
    news::

    > Sherm Pendley wrote:
    >
    >> yong writes:

    >
    >> > I lauch a program with system(),but the function returns only the
    >> > outpout.how can I get the value of the program return to shell?

    >
    >> Please read the documentation for the function you're referring to:

    >
    >> perldoc -f system

    >
    >> The system() function does *not* return the output of the launched
    >> program, and it *does* get the return value.

    >
    >
    > #!perl
    >
    > print system("dir *.txt");


    You should realize that the output is not being "returned". You would
    get the same directory listing below without the print call above.

    > C:\APACHE\USERS\TEST>perl test.pl
    >
    > Volume in drive C is DISK_1
    > Volume Serial Number is 3C25-1DF9
    > Directory of C:\APACHE\USERS\TEST
    >
    > FRAMES TXT 937 06-15-04 11:01a frames.txt


    ....

    > 0


    This is the value returned by system, and thus the only thing printed by
    the call to print in your program.

    *Sigh*

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Dec 5, 2005
    #6
  7. "Purl Gurl" <> writes:

    > Sherm Pendley wrote:
    >
    >> yong writes:

    >
    >> > I lauch a program with system(),but the function returns only the
    >> > outpout.how can I get the value of the program return to shell?

    >
    >> Please read the documentation for the function you're referring to:

    >
    >> perldoc -f system

    >
    >> The system() function does *not* return the output of the launched
    >> program, and it *does* get the return value.

    >
    >
    > #!perl
    >
    > print system("dir *.txt");
    >
    >
    > C:\APACHE\USERS\TEST>perl test.pl


    First, dir *.txt runs, printing its output to the stdout stream it inherits
    from its parent.

    > Volume in drive C is DISK_1
    > Volume Serial Number is 3C25-1DF9
    > Directory of C:\APACHE\USERS\TEST
    >
    > FRAMES TXT 937 06-15-04 11:01a frames.txt
    > TEST TXT 911 12-03-05 11:38p test.txt
    > CHOCTAW TXT 199 06-16-03 6:53a CHOCTAW.TXT
    > DEFINE2 TXT 147,628 12-04-05 8:22p define2.txt
    > TEST2 TXT 57 12-03-05 10:11a test2.txt
    > STDIN TXT 255 12-04-02 5:30p STDIN.TXT
    > WINLOCK TXT 594 12-03-02 11:14a WINLOCK.TXT
    > CRASH TXT 10,157 06-18-04 5:26p crash.txt
    > DEFINE TXT 274,940 12-04-05 12:57p define.txt
    > DEFINE3 TXT 151,459 12-04-05 11:01p define3.txt
    > WHERE TXT 2,353 12-04-05 10:48p where.txt
    > WHO_IS TXT 1,899 12-04-05 10:48p who_is.txt
    > TEMP TXT 232 12-05-05 8:00a temp.txt
    > 13 file(s) 591,621 bytes
    > 0 dir(s) 28,771.94 MB free


    Now, the print executes, printing the value returned by the dir command.

    > 0


    You can easily verify this - simply redirect stdout to a file, and print
    to stderr. The dir output will be in the file, but the 0 won't be.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Dec 5, 2005
    #7
  8. yong

    Paul Lalli Guest

    Sherm Pendley wrote:
    > "Purl Gurl" <> writes:
    >
    > > Sherm Pendley wrote:
    > >
    > >> The system() function does *not* return the output of the launched
    > >> program, and it *does* get the return value.

    > >
    > > #!perl
    > > print system("dir *.txt");

    > First, dir *.txt runs, printing its output to the stdout stream it inherits
    > from its parent.
    ><snip>
    > Now, the print executes, printing the value returned by the dir command.
    >
    > You can easily verify this - simply redirect stdout to a file, and print
    > to stderr. The dir output will be in the file, but the 0 won't be.


    You can verify it more easily than that. Simply remove the word
    "print" from the above code.

    Paul Lalli
     
    Paul Lalli, Dec 5, 2005
    #8
  9. yong

    Guest

    Purl Gurl <> wrote:
    > Sherm Pendley wrote:


    > " The system() function does *not* return the output of the launched
    > program, and it *does* get the return value."


    > I have exemplified system() does return the output of a launched program.


    Prove it by by modifying your example program to manipulate that
    so-called returned output - maybe just printing the short name and
    long name.

    Axel
     
    , Dec 5, 2005
    #9
  10. Purl Gurl wrote:
    > Purl Gurl wrote:
    >
    > > yong wrote:

    >
    > (snipped)
    >
    > > > I lauch a program with system(),but the function returns only the
    > > > outpout.how can I get the value of the program return to shell?

    >
    > > You will discover testing the return value of a system call to
    > > be somewhat easier than trying to capture the exit value. However,
    > > "testing" the return exit code, is not reliable.

    >
    > > For that example, a zero success is returned, but the
    > > results are not expected results.

    >
    > > Another example of a zero success return code,
    > > although an error condition is created; error handling
    > > is successful, not the program call.

    >
    > > It is a good practice to include error checking in a program
    > > to be called, by another program. Best practice, is to check
    > > secondary program results for accuracy.

    >
    >
    > TEST.PL
    >
    > #!perl
    >
    > system ("perl test2.pl") == 0 || die $!;


    I don't think system() calls write errors to $!, i believe it goes to $?
     
    it_says_BALLS_on_your forehead, Dec 5, 2005
    #10
  11. Purl Gurl wrote:
    <snip>
    > Reads you boys are still facing challenges in clarity of writing.

    <snip>

    > "Use of system() can produce both return output and a return exit code.
    > Capturing either the returned output or capturing the return exit code,
    > requires additional coding beyond system() use alone."
    >
    > My quoted statement is clarity in writing, yes?


    It isn't grammatically correct, for a start. What price clarity?

    That aside, there is a difference between standard output and the return
    value of a function call. Are you dabbling with "Socratic irony" again?

    print testreturn();

    sub testreturn {
    print "this is printed (normally to stdout)\n";
    return "this is the return value of the sub\n";
    }

    To argue otherwise is not only needlessly argumentative, it is incorrect.

    Mark
     
    Mark Clements, Dec 5, 2005
    #11
  12. yong

    Guest

    Purl Gurl <> wrote:
    > axel wrote:


    >> > I have exemplified system() does return the output of a launched program.


    >> Prove it


    > Your "it" is what?


    Your statement which I quoted.

    >> by by modifying your example program to manipulate that
    >> so-called returned output


    > You are confused.


    > Producing results and "manipulating" results, are not the same.


    You used the word 'return' as did I, not 'produce'. The word 'return'
    in the context of 'returning a result' has a specific meaning when
    applied to Perl functions - it means passing back a result to the
    context in which the function was invoked. It has nothing to do
    with sending data to standard output or anywhere else.

    >> maybe just printing the short name and long name.


    > Your "short name" is what?
    > Your "long name" is what?


    Sigh... your program produced a DOS type directory listing. I would
    have thought it obvious what I meant.

    > "Use of system() can produce both return output and a return exit code.
    > Capturing either the returned output or capturing the return exit code,
    > requires additional coding beyond system() use alone."


    > My quoted statement is clarity in writing, yes?


    Clarity maybe. Correctness, no. In fact, it is totally incorrect.

    Axel
     
    , Dec 5, 2005
    #12
  13. Purl Gurl wrote:
    > axel wrote:
    >
    >>> "Use of system() can produce both return output and a return exit code.
    >>> Capturing either the returned output or capturing the return exit code,
    >>> requires additional coding beyond system() use alone."

    >
    >>> My quoted statement is clarity in writing, yes?

    >
    >> Clarity maybe. Correctness, no. In fact, it is totally incorrect.

    >
    > You have a reading comprehension problem.
    >
    > Purl Gurl


    As with any discipline, a technical vocabulary is essential for the
    purpose of clear communication. It is also essential that everyone
    assigns the same meaning to words within that vocabulary, otherwise
    communication becomes difficult if not impossible.

    You seem to go to great lengths to interpret terms, eg "return value"
    (this is just one example - there are many others from your posting
    history) in an unconventional, not to say perverse, way.

    Your arguments in support of such misinterpretation are reminiscent of
    those that somebody with no understanding of relativity, or indeed
    physics, would use to argue against its validity. It all makes rather
    tiresome reading.


    Mark
     
    Mark Clements, Dec 5, 2005
    #13
  14. yong

    Guest

    "it_says_BALLS_on_your forehead" <> wrote:
    > Purl Gurl wrote:
    > > Purl Gurl wrote:
    > >
    > > > yong wrote:

    > >
    > > (snipped)
    > >
    > > > > I lauch a program with system(),but the function returns only the
    > > > > outpout.how can I get the value of the program return to shell?

    > >
    > > > You will discover testing the return value of a system call to
    > > > be somewhat easier than trying to capture the exit value. However,
    > > > "testing" the return exit code, is not reliable.

    > >
    > > > For that example, a zero success is returned, but the
    > > > results are not expected results.

    > >
    > > > Another example of a zero success return code,
    > > > although an error condition is created; error handling
    > > > is successful, not the program call.

    > >
    > > > It is a good practice to include error checking in a program
    > > > to be called, by another program. Best practice, is to check
    > > > secondary program results for accuracy.

    > >
    > >
    > > TEST.PL
    > >
    > > #!perl
    > >
    > > system ("perl test2.pl") == 0 || die $!;

    >
    > I don't think system() calls write errors to $!, i believe it goes to $?


    $? gets the exit status of the thing the system is trying to run. If the
    error is with the system call itself, the error goes to $!. If system was
    unable to start the thing it is trying to start, then the thing can't have
    exited, so its exit status can't be put into $?. (-1 is put in it instead,
    but that tells you nothing about *why* it couldn't start.

    $ perl -le 'system "./foo" and die "! $! ? $?"'
    ! No such file or directory ? -1 at -e line 1.
    $ perl -le 'system "foo" and die "! $! ? $?"'
    ! Permission denied ? -1 at -e line 1.

    If the error is in the thing the system call is invoking, the error goes
    whereever that thing puts it.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Dec 5, 2005
    #14
  15. Sherm Pendley <> wrote in
    news::

    > "Purl Gurl" <> writes:
    >
    > You can easily verify this - simply redirect stdout to a file, and
    > print to stderr. The dir output will be in the file, but the 0 won't
    > be.


    Sherm:

    Please don't feed the troll.

    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
     
    Eric J. Roode, Dec 6, 2005
    #15
  16. yong

    John Bokma Guest

    "Purl Gurl" <> wrote:

    > "My thinking is the only right thinking."


    Yup, that sums it up quite well. And the root of many problems you have on
    Usenet, and probably in life as well.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Dec 6, 2005
    #16
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,558
    tom_usenet
    Aug 2, 2004
  2. Michael Bo
    Replies:
    3
    Views:
    502
    Michael Bo
    Feb 7, 2007
  3. Greenhorn
    Replies:
    15
    Views:
    885
    Keith Thompson
    Mar 6, 2005
  4. Sheikko
    Replies:
    1
    Views:
    391
    Salt_Peter
    Dec 22, 2006
  5. Andre
    Replies:
    5
    Views:
    567
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page