Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Reading from terminal

by uvnew (Acolyte)
on Nov 15, 2008 at 17:44 UTC ( [id://723810]=perlquestion: print w/replies, xml ) Need Help??

uvnew has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I wrote a Perl script (on my Mac) that executes a compiled (binary) C++ file, which output I later need to analyse with my Perl script. The binary file is printing the output I need on the terminal screen, but unfortunately it doesn't write it on a file. Is there any way for Perl to gather all that output data by accessing the terminal screen after the binary run is finished, and then either write it on a file or on a Perl data structure so I could use it later in my Perl program? Many thanks.

Replies are listed 'Best First'.
Re: Reading from terminal
by Corion (Patriarch) on Nov 15, 2008 at 17:51 UTC

    Most likely, qx or backticks will do, or IPC::Run, if you want to bring out the heavy guns. Also, you could try simply redirecting the output of the C++ program like this:

    my $outfile = 'outfile.txt'; system("cplusplusprogram >$outfile") == 0 or die "Couldn't launch cplusplusprogram: $!/$?"; open my $fh, "<", $outfile or die "Couldn't read '$outfile': $!";
      Thanks a lot for you and eighty-one. I originally used system() but now qx() does the job by assigning the data into a variable, cheers!
Re: Reading from terminal
by eighty-one (Curate) on Nov 15, 2008 at 17:52 UTC

    Without a code example it's hard to know why it's working the way it is instead of the way you want.

    The first thing that comes to mind, is how are you executing the external program? System or backticks?

    Try using backticks and assigning the results to a variable; i.e.:

    my $passwd = `cat /etc/passwd`;

    Note that those are backticks (`) which share the same key as the tilde (~)(at least on my keyboard, but I'm not a Mac user, so YMMV), not single quotes (').

Re: Reading from terminal
by eye (Chaplain) on Nov 16, 2008 at 18:15 UTC
    In most cases, using the qx// mechanism is the preferred way of doing this. That said, if you had preferred that the output appear on the terminal for users to see, you could do that by piping the output of your C++ program through "tee":
    system("cplusplusprogram | tee $capturefile");
    The "tee" filter copies its STDIN to its STDOUT while making a copy to $capturefile. "tee" is also a useful tool when trying to diagnose complex pipes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://723810]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-25 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found