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

is there an easy way for perl to capture the output of system commands? for example: if i wanted to put the output of "ls -l" into a Tk textbox, how would i do it? right now i have to do it like this.
system("$got > hold"); open(FLE, "hold"); $text->insert('1.0', <FLE>);

janitored by ybiC: Retitle from "Reading from the screen"

Replies are listed 'Best First'.
Re: Reading from the screen
by ysth (Canon) on Nov 18, 2003 at 02:22 UTC
    Try $text->insert('1.0', qx[$got]).
      thanks, it worked, but could you explain how qx does it?
        The 'qx' keyword is just another way to write the back quote (`). The code -
        $n = qx/ $got /;
        is equivalent to
        $n = `$got`;
        What it does is to execute the $got command, and capture its output to a scalar.

        You should have a look on CPAN perlop under the section "Quote and Quote-like Operators".

        Update: Thanks to ysth to point out my typo.

Re: Reading from the screen
by Abigail-II (Bishop) on Nov 18, 2003 at 10:14 UTC
    is there an easy way for perl to capture the output of system commands?
    Nope, not possible. If it was, it would have been documented with perldoc -f system, and you did read that, didn't you?

    Abigail