Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

Hi I'm new to perl and I like to know how to put the result of the command STDOUT in a variable.     

My needs are
1)to capture the output at the screen
2)To put the output in a variable

here's a sample of my code:

`xcom62 -cf

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

First things first, STDOUT isn't a command, it's a pointer to an output stream.  In perl these pointers are referred to as "filehandles".  STDOUT is a special filehandle, in that it refers to the "standard output" stream.  This is usually the screen.

Although it's possible to redirect STDOUT, I tend to leave STDOUT alone and create my own filehandle if I need ot redirect output.  For example, the following code prints a message to the file "message.out", overwriting it if it already exists.

open (MYSTDOUT, ">message.out");
print MYSTDOUT "My test message.\n";
close (MYSTDOUT);

The disadvantage with this approach is that you do not see the message on the screen.  If you want to write a message to the screen and see it in the log file you could try the following:

open (MYSTDOUT, "¦tee message.out");
print MYSTDOUT "My test message.\n";
close (MYSTDOUT);

This opens a pipe to the Unix command "tee".  "tee" will print the output to screen as well as sending it to the named file.

If you want to put the output from your script into a variable, why not turn this on it's head a little?  Put the message into a variable, and print the variable?  For example:

$My_Message = "My test message\n";
open (MYSTDOUT, ">message.out");
print MYSTDOUT $My_Message;
close (MYSTDOUT);

Hope this helps.

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

If I understand correctly you are running an external command "xcom62 -cf" in your perl program. You need to capture the output from this command, and put it in a variable.

The trick is to use backtics, like in shell scripting. If you want everything in one string then use

$str_var = `xcom62 -cf`;

If you want an array with one line per entry then use

@arr_var = `xcom62 -cf`;

- Kai.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close