in reply to How to store the output of the process in my own array?

The log_file routine accepts a filehandle, but it can also take a coderef.

So you could do

#!/usr/bin/perl -w use Expect; my @mylog; sub reporter { # print "PASSED:<",join("><",@_).">\n"; push(@mylog,@_); } $object=Expect->spawn("some command I need"); $object->log_file(\&reporter);
(Yes, I tested this)

Replies are listed 'Best First'.
Re: Re: How to store the output of the process in my own array?
by seanborland (Initiate) on Mar 01, 2004 at 09:51 UTC
    thank you very much. All the output can be store in the array but I still meet a small problem. I input ls command, and then print @mylog, but it doesn't contain the result of ls. When I input quit command to quit ftp, and then print @mylog, I get the ls result and quit result together. How can I solve this problem? Thanks a log. By the way, I use to meet this problem when I use C++ and C# to write the same program. In C++ and C#, anonymous pipe is used to implement it and I use multi-thread to solve the problem.
      I still meet a small problem. I input ls command, and then print @mylog, but it doesn't contain the result of ls.
      The reason for the difference, is because ls detects whether it's connected to a pipe or to a TTY, and behaves differently for both cases. You have to fool it into believing it's connected to a terminal. The common way to do that, is to connect it to a pseudo-terminal.

      To this end, you can use the module IO::Pty. And judging by the documentation, Expect (which I've never ever used in my life, sorry) ought to have support for it built-in.

      Happy hunting.

      Well, from my experimenting with Expect it looks like the subroutine for logging the output is called when $object->expect is called, or even when $object->expect finds a match. Probably also when it's buffer becomes full, but you realy don't want to count on that.

      My suggestion is to put in some $object->expect calls that match on whatever prompt you are getting in your input stream.

        Thank you very much. Another veteran tells me another method. It looks like follow code: my @array; ... @array=$session->expect(...); ... I put print @array after each call on the expect, and it works. It is strange because as far as I know, if under list context, the return value of the expect is not the content that output of the command line.