Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Howto get return code from a extern program?

Reply
Thread Tools

Howto get return code from a extern program?

 
 
yong
Guest
Posts: n/a
 
      12-05-2005
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.
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      12-05-2005
yong <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      12-05-2005
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."
 
Reply With Quote
 
yong
Guest
Posts: n/a
 
      12-05-2005


Thanks

Sherm Pendley wrote:
> yong <(E-Mail Removed)> 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--

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      12-05-2005

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";

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-05-2005
"Purl Gurl" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) :

> 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      12-05-2005
"Purl Gurl" <(E-Mail Removed)> 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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      12-05-2005
Sherm Pendley wrote:
> "Purl Gurl" <(E-Mail Removed)> 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

 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      12-05-2005
Purl Gurl <(E-Mail Removed)> 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

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      12-05-2005

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
Howto obfuscate OBJECT code (and not source code)? Matt C++ 11 05-18-2005 04:51 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM



Advertisments