A way to grab string from another program in perl?

J

Jeff

I'm looking for a way to run another program from a system call and get
the output produced into a perl variable without going through a file
intermediary.

system('program');

produces a string on STDOUT. I want to grab this in a variable but can't
figure out how to do it. Any ideas?

-Jeff
 
J

Jeff

On Fri, 12 Dec 2003 18:44:28 +0000, Jeff wrote:

Whoops. I should have parsed down this list a little farther. I just found
the answer. Sorry about that.

-Jeff
 
M

Matt Garrish

Jeff said:
I'm looking for a way to run another program from a system call and get
the output produced into a perl variable without going through a file
intermediary.

system('program');

produces a string on STDOUT. I want to grab this in a variable but can't
figure out how to do it. Any ideas?

What part of the explanation on how to do this in the system function
description did you not understand?

http://www.perldoc.com/perl5.8.0/pod/func/system.html

Matt
 
M

Malcolm Dew-Jones

Jeff ([email protected]) wrote:
: I'm looking for a way to run another program from a system call and get
: the output produced into a perl variable without going through a file
: intermediary.

: system('program');

: produces a string on STDOUT. I want to grab this in a variable but can't
: figure out how to do it. Any ideas?

perldoc -q system

for me, about the 7th entry.
 
J

Jürgen Exner

Jeff said:
I'm looking for a way to run another program from a system call and
get the output produced into a perl variable without going through a
file intermediary.

system('program');

produces a string on STDOUT. I want to grab this in a variable but
can't figure out how to do it. Any ideas?

Did you read the man pages for the functions you are using?
Please pay particular attention to the third sentence in the third paragraph
for "perldoc -f system".

Or just check the FAQ: " Why can't I get the output of a command with
system()?"

jue
 
T

Tad McClellan

Jeff said:
system('program');

I want to grab this in a variable but can't
figure out how to do it.


Give up on programming. You are not suited to it.
 
J

Jeff

I'm looking for a way to run another program from a system call and get
the output produced into a perl variable without going through a file
intermediary.

system('program');

produces a string on STDOUT. I want to grab this in a variable but can't
figure out how to do it. Any ideas?

-Jeff

What a pack of asses you are. When someone asks a serious question,
you are silent as snow for days, but someone asks a very simple one and
you bray like donkeys with glee at your great opportunity to be abusive.

In any event, the reference you gave didn't even answer the question, so
you are fools as well. The correct answer would have been:

$string = `someprogram @args`;

If YOU all had read your own reference closely, you would have noticed
that the system function returns the error code, not what goes to STDOUT.

Regards,
-Jeff
 
M

Malcolm Ray

What a pack of asses you are. When someone asks a serious question,
you are silent as snow for days, but someone asks a very simple one and
you bray like donkeys with glee at your great opportunity to be abusive.

In any event, the reference you gave didn't even answer the question, so
you are fools as well. The correct answer would have been:

$string = `someprogram @args`;

If YOU all had read your own reference closely, you would have noticed
that the system function returns the error code, not what goes to STDOUT.

Regards,
-Jeff

Because this is a common confusion, 'perldoc -f system' *does* have
the answer:

The return value is the exit status of the program
as returned by the "wait" call. To get the actual
exit value shift right by eight (see below). See
also "exec". This is _not_ what you want to use to
capture the output from a command, for that you
should use merely backticks or "qx//", as
described in "`STRING`" in perlop. Return value
of -1 indicates a failure to start the program
(inspect $! for the reason).

See the mention of backticks and perlop?
 
D

David K. Wall

What a pack of asses you are. When someone asks a serious
question, you are silent as snow for days, but someone asks a very
simple one and you bray like donkeys with glee at your great
opportunity to be abusive.

In any event, the reference you gave didn't even answer the
question, so you are fools as well. The correct answer would have
been:

$string = `someprogram @args`;

If YOU all had read your own reference closely, you would have
noticed that the system function returns the error code, not what
goes to STDOUT.

Actually they did give you the correct answer, and what is better, a
technique you can use to find information for yourself (i.e.; read
the docs). They told you to read the docs for system(), which you
were already using. The third paragraph in the documentation for
system() says:

[system()] is *not* what you want
to use to capture the output from a command, for that you should
use merely backticks or "qx//", as described in "`STRING`" in
perlop.

Jürgen Exner explicitly pointed to this paragraph.

Not understanding the documentation and asking for clarification is
understandable. So is asking for a pointer to the proper place in
the admittedly voluminous Perl docs. Telling people they are
"fools" and "asses" for pointing you to the answer to your question
is completely different.
 
T

Tad McClellan

Jeff said:
What a pack of asses you are.


Then don't come here!

In any event, the reference you gave didn't even answer the question, so
you are fools as well.


Yes it did.


perldoc -f system

This is not what you want to use to
capture the output from a command, for that you
should use merely backticks or "qx//"



I think most people reading this thread might have a different
opinion on who is the fool.
 
M

Matt Garrish

In any event, the reference you gave didn't even answer the question, so
you are fools as well. The correct answer would have been:

$string = `someprogram @args`;

If YOU all had read your own reference closely, you would have noticed
that the system function returns the error code, not what goes to STDOUT.

Despite your obvious dyslexia you were able to figure out the answer. Kudos
to you!

Matt
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top