Howto get return code from a extern program?

Y

yong

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.
 
S

Sherm Pendley

yong said:
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--
 
D

Dr.Ruud

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.
 
I

it_says_BALLS_on_your forehead

yong said:
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";
 
A

A. Sinan Unur

#!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
 
S

Sherm Pendley

Purl Gurl said:
#!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.

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--
 
P

Paul Lalli

Sherm said:
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
 
A

axel

Purl Gurl said:
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
 
I

it_says_BALLS_on_your forehead

Purl said:
TEST.PL

#!perl

system ("perl test2.pl") == 0 || die $!;

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

Mark Clements

Purl Gurl wrote:
Reads you boys are still facing challenges in clarity of writing.
"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
 
A

axel

Purl Gurl said:
axel wrote:
Your "it" is what?

Your statement which I quoted.
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.
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
 
M

Mark Clements

Purl said:
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
 
X

xhoster

it_says_BALLS_on_your forehead said:
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
 
E

Eric J. Roode

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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
J

John Bokma

Purl Gurl said:
"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.
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top