Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I put the result of <STDOUT> in a variable

Status
Not open for further replies.

wysiwyg

Programmer
May 8, 2000
6
CA
Hi I'm new to perl and I like to know how to put the result of the command STDOUT in a variable.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br>My needs are <br>1)to capture the output at the screen<br>2)To put the output in a variable<br><br>here's a sample of my code:<br><br>`xcom62 -cf
 
First things first, STDOUT isn't a command, it's a pointer to an output stream.&nbsp;&nbsp;In perl these pointers are referred to as &quot;filehandles&quot;.&nbsp;&nbsp;STDOUT is a special filehandle, in that it refers to the &quot;standard output&quot; stream.&nbsp;&nbsp;This is usually the screen.<br><br>Although it's possible to redirect STDOUT, I tend to leave STDOUT alone and create my own filehandle if I need ot redirect output.&nbsp;&nbsp;For example, the following code prints a message to the file &quot;message.out&quot;, overwriting it if it already exists.<br><FONT FACE=monospace><br>open (MYSTDOUT, &quot;&gt;message.out&quot;);<br>print MYSTDOUT &quot;My test message.\n&quot;;<br>close (MYSTDOUT);<br></font><br>The disadvantage with this approach is that you do not see the message on the screen.&nbsp;&nbsp;If you want to write a message to the screen and see it in the log file you could try the following:<br><FONT FACE=monospace><br>open (MYSTDOUT, &quot;¦tee message.out&quot;);<br>print MYSTDOUT &quot;My test message.\n&quot;;<br>close (MYSTDOUT);<br></font><br>This opens a pipe to the Unix command &quot;tee&quot;.&nbsp;&nbsp;&quot;tee&quot; will print the output to screen as well as sending it to the named file.<br><br>If you want to put the output from your script into a variable, why not turn this on it's head a little?&nbsp;&nbsp;Put the message into a variable, and print the variable?&nbsp;&nbsp;For example:<br><FONT FACE=monospace><br>$My_Message = &quot;My test message\n&quot;;<br>open (MYSTDOUT, &quot;&gt;message.out&quot;);<br>print MYSTDOUT $My_Message;<br>close (MYSTDOUT);<br></font><br><br>Hope this helps.
 
<br>If I understand correctly you are running an external command &quot;xcom62 -cf&quot; in your perl program. You need to capture the output from this command, and put it in a variable.<br><br>The trick is to use backtics, like in shell scripting. If you want everything in one string then use<br><br>$str_var = `xcom62 -cf`;<br><br>If you want an array with one line per entry then use<br><br>@arr_var = `xcom62 -cf`;<br><br>- Kai.<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top