File Coverage

lib/Zimbra/Expect.pm
Criterion Covered Total %
statement 15 59 25.4
branch 0 12 0.0
condition 0 9 0.0
subroutine 5 10 50.0
pod 3 3 100.0
total 23 93 24.7


line stmt bran cond sub pod time code
1             package Zimbra::Expect;
2 1     1   6 use strict;
  1         1  
  1         19  
3 1     1   3 use warnings;
  1         1  
  1         16  
4 1     1   346 use IPC::Open2;
  1         3360  
  1         37  
5 1     1   327 use IO::Select;
  1         1132  
  1         31  
6 1     1   374 use IO::Handle;
  1         3520  
  1         343  
7              
8             our $VERSION = '0.1.1';
9              
10             sub new {
11 0     0 1   my $proto = shift;
12 0   0       my $class = ref($proto)||$proto;
13              
14 0           my $self = { @_ };
15 0           bless $self, $class;
16 0           my ($outFh, $inFh) = (IO::Handle->new(), IO::Handle->new());
17 0           my $pid = open2($outFh,$inFh,@{$self->{cmd}});
  0            
18 0           $outFh->blocking(0);
19 0           my $select = IO::Select->new($outFh);
20              
21 0           $self->{outFh} = $outFh;
22 0           $self->{inFh} = $inFh;
23 0           $self->{select} = $select;
24              
25 0           $self->cmd; #initialize
26 0           return $self;
27             }
28              
29             sub _printRead {
30 0     0     my $self = shift;
31 0           my $cmd = shift;
32 0           my $inFh = $self->{inFh};
33 0 0         $inFh->print($cmd."\n") if $cmd;
34 0           $inFh->flush();
35 0           my $buffer = '';
36 0           my $prompt = $self->{prompt};
37 0           while (1){
38 0           $self->{select}->can_read();
39 0           my $chunk;
40 0           sysread($self->{outFh},$chunk,1024);
41 0           $buffer .= $chunk;
42 0 0         if ($buffer =~ s/${prompt}.*?> $//){
43 0           last;
44             };
45             }
46 0 0         warn "ANSWER: '$buffer'\n" if $self->{debug};
47 0           return $buffer;
48             }
49              
50             sub cmd {
51 0     0 1   my $self = shift;
52 0           my $cmd = shift;
53 0 0 0       warn " - $cmd\n" if $self->{verbose} and $cmd;
54 0           $self->_printRead($cmd);
55             }
56              
57             sub act {
58 0     0 1   my $self = shift;
59 0           my $cmd = shift;
60 0 0 0       warn " > $cmd\n" if $self->{verbose} and $cmd;
61 0 0         $self->_printRead($cmd) unless $self->{noaction};
62             }
63              
64              
65             sub DESTROY {
66 0     0     my $self = shift;
67 0           my $inFh = $self->{inFh};
68 0           print $inFh "quit\n";
69 0           close $self->{inFh};
70 0           close $self->{outFh};
71             # the zm cmmands to strange things to the terminal ... fix them
72 0           system "stty sane";
73             }
74              
75             1;
76              
77             __END__