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

The following code telnets to google and saves the "expect" log to a file. That is, as long as I don't spawn a thread before I spawn my Expect object! If I define $make_it_break to 1, this code will only save to the logfile the strings I send.
use Expect; use FileHandle; use threads; my $telnet = new Expect; my $telnet_logfile_name = "telnet.log"; my $telnet_logfile = new FileHandle (">$telnet_logfile_name") or die " +Can't write $telnet_logfile_name! $!"; $telnet_logfile->autoflush(1); $telnet->log_file (sub { my $str=shift; $telnet_logfile->print ("$str +"); }); my @command = qw(telnet www.google.com 80); my $make_it_break = 0; if ($make_it_break) { threads->new (\&daves_handler); $telnet->spawn (@command) or die "Cannot spawn @command: $!\n"; } else { $telnet->spawn (@command) or die "Cannot spawn @command: $!\n"; threads->new (\&daves_handler); } $telnet->send ("GET / HTTP/1.1\r\nUser-Agent: dave\r\nHost: www.google +.com\r\n\r\n"); $telnet->expect (10, 'HTTP/1.1 200 OK'); $telnet->print_log_file("Dave was here\n"); sub daves_handler { while (1) { sleep 10; } }
Unfortunately, in my application, I need to spawn a thread before spawning my telnet session. I also need automatic expect logging. Is there any workaround for me?

Thanks,
Dave

Replies are listed 'Best First'.
Re: Is there a workaround for threads breaking Expect.pm logging?
by merlyn (Sage) on Jul 30, 2005 at 01:37 UTC
    The following code telnets to google
    Why?

    First, you have LWP, which handles HTTP far better than Expect ever will.

    Second, Google specifically has rules about automatic access, so you are probably about to violate some license agreement.

    Learn about the Google API. Use it with Net::Google and friends. Get a license key—they're free!

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Why did I telnet to Google? Because it is demonstration code!

      I know and love LWP and Net::Google. My real application has no need for them. My real application is a console app for manufacturing test systems. It uses Expect to drive hardware functionality and performance tests of multiple Units-Under-Test, over both telnet and serial sessions. I could never post the original code that reproduces this bug because 1) it would be huge, 2) nobody would be able to use it, and 3) I don't even know if I have the rights to post it.

      So I ginned up a reproduction that happened to telnet to the site most likely to be accessible to all perl monks!

      Dave
Re: Is there a workaround for threads breaking Expect.pm logging?
by mrborisguy (Hermit) on Jul 30, 2005 at 00:31 UTC

    This is a shot in the dark, but have you tried putting 'use threads' above 'use Expect'? I've noticed that some modules need to be after the use threads line in order to work correctly.

        -Bryan

      Nope, after much experimentation, no order of module usage declarations affects the behavior.

      Dave