Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Send "Expect.pm" debug and screen output to a file

by chime (Friar)
on Feb 08, 2005 at 13:12 UTC ( [id://429049]=perlquestion: print w/replies, xml ) Need Help??

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

This problem has been bugging me and the solution is probably very simple.
But I "Can't see the wood for the trees"

I need to catch all output of the expect.pm
Specifically :
. $exp->exp_internal(1);
. $exp->debug(1);
and send it to a file
without it printing to the screen.


Changing from expect is not an option, as it is perfect
to run and capture an unreliable program.

# Define variables used to run the unreliable program my ($par1, $par2, $par3) = @_; # Setup array to hold parameters to pass to runrep my @parameterss; # Add login parameters push (@parameters, $login); # Create the path name for the expect log file my $expect_log = "/tmp/output.tmp"; # Open the expect log file open (EXPECT, ">$expect_log") or die ("Cannot open the expect log file: $expect_log $!\n"); # Spawn program using perl expect module my $exp = Expect->spawn($program, @parameters) or die "Cannot spawn $program $login: $!\n"; # Prevent the program's output from being shown on the screen as S +TDOUT $exp->log_stdout(0); # But I want to catch this into the $expec +t_log file # Populate the expect log file with any output from the expect run +rep $exp->log_file("$expect_log"); # This isn't catching the debug/exp +_internal output $exp->exp_internal(1); $exp->debug(1); # Wait for program to initialise for 5 minutes only $exp->expect(300,[ qr/starting/]); # Read in the parameters $exp->send("read $parameters\n"); # Send appropriate command to program $exp->send("run $program with parameters"); # Wait for program to run or generate an error $exp->expect(18000, [ qr/Error/i, sub { my $self = shift; $self->send("quit\n");exp_cont +inue;}], [ qr/success/i, sub { my $self = shift; $self->send("quit\n"); exp_con +tinue;}]); my $output = $exp->exp_error(); # Clear any output and close the program gently $exp->clear_accum(); $exp->soft_close(); # Close the except file close(EXPECT); <\code> This is a simple example of expect if anyone needs to run it <readmore> <code> ###################################################### #!/usr/bin/perl # # Fri Dec 13 23:10:54 PST 2002 # # Copyright Tom Anderson 2002, All rights reserved. # This program may be copied under the same terms as Perl itself. # Please send modifications to t@tomacorp.com # # # unreliable.pl - Simulate a program that sometimes just hangs # my $VERSION=".01"; use strict; use warnings; use diagnostics; if (rand(10) > 8) { sleep 1 while 1 > 0; } else { print "--------------------------------\n", "It worked this time, no problems\n", "--------------------------------\n"; } ############################################## use warnings; use diagnostics; use Expect; my $timeout=5; foreach my $i (1..20) { my $spawn_ok="not OK"; my $exp = Expect->spawn("./unreliable.pl") or die "Cannot spawn unreliable process $!\n"; $exp->expect($timeout, [ 'It worked', sub { $spawn_ok = "OK"; exp_continue; } ], [ timeout => sub { print "Process timed out.\n"; } ] ); print "Status: $spawn_ok\n"; }

Replies are listed 'Best First'.
Re: Send "Expect.pm" debug and screen output to a file
by Fletch (Bishop) on Feb 08, 2005 at 13:21 UTC

    Just redirect stderr into a file. See also the log_file method.

    $object->log_file("filename" | $filehandle | \&coderef | undef) Log session to a file. All characters send to or received +from the spawned process are written to the file. Normally appends +to the logfile, but you can pass an additional mode of "w" to trun +cate the file upon open():
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found