line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: /mirror/perl/File-Extract/trunk/lib/File/Extract/Filter/Exec.pm 4210 2007-10-27T13:43:07.499967Z daisuke $ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright (c) 2005 Daisuke Maki |
4
|
|
|
|
|
|
|
# All rights reserved. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package File::Extract::Filter::Exec; |
7
|
1
|
|
|
1
|
|
909
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
8
|
1
|
|
|
1
|
|
7
|
use base qw(File::Extract::Filter::Base); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
862
|
|
9
|
1
|
|
|
1
|
|
1392
|
use IO::Scalar; |
|
1
|
|
|
|
|
6232
|
|
|
1
|
|
|
|
|
51
|
|
10
|
1
|
|
|
1
|
|
1331
|
use IPC::Open2; |
|
1
|
|
|
|
|
5815
|
|
|
1
|
|
|
|
|
65
|
|
11
|
1
|
|
|
1
|
|
1287
|
use UNIVERSAL::isa; |
|
1
|
|
|
|
|
2913
|
|
|
1
|
|
|
|
|
7
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new |
14
|
|
|
|
|
|
|
{ |
15
|
1
|
|
|
1
|
0
|
18
|
my $class = shift; |
16
|
1
|
|
|
|
|
4
|
my %args = @_; |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
|
|
7
|
my $self = bless { cmd => $args{cmd} }, $class; |
19
|
1
|
|
|
|
|
12
|
return $self; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
0
|
10
|
sub cmd { shift->{cmd} } |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub filter |
25
|
|
|
|
|
|
|
{ |
26
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
27
|
1
|
|
|
|
|
6
|
my %args = @_; |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
3
|
my $file = $args{file}; |
30
|
1
|
|
|
|
|
2
|
my $o = $args{output}; |
31
|
1
|
0
|
33
|
|
|
11
|
my $output = |
|
|
50
|
0
|
|
|
|
|
32
|
|
|
|
|
|
|
(ref($o) && UNIVERSAL::isa($o, 'GLOB')) ? $o : |
33
|
|
|
|
|
|
|
(ref($o) && UNIVERSAL::isa($o, 'SCALAR')) ? IO::Scalar->new($o) : |
34
|
|
|
|
|
|
|
die "output must be a GLOB or ref to SCALAR"; |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
47
|
my $cmd = $self->cmd; # XXX - if we wanted to be paranoid, we need to |
37
|
|
|
|
|
|
|
# cleanse this guy, but oh well.. |
38
|
|
|
|
|
|
|
=head1 |
39
|
|
|
|
|
|
|
if ($cmd !~ /\|\s*$/) { |
40
|
|
|
|
|
|
|
$cmd .= " |"; # make sure it's piped |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
if ($cmd !~ /^\s*\|$/) { |
43
|
|
|
|
|
|
|
$cmd = "| " . $cmd; # make sure it's piped |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
1
|
50
|
|
|
|
38
|
open(my $input, $file) or die "Failed to open file $file: $!"; |
48
|
1
|
|
|
|
|
3
|
my($p_read, $p_write); |
49
|
1
|
50
|
|
|
|
7
|
open2($p_read, $p_write, $cmd) or die "Failed to execute $cmd"; |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
7459
|
while (<$input>) { |
52
|
19
|
|
|
|
|
119
|
print $p_write $_; |
53
|
|
|
|
|
|
|
} |
54
|
1
|
|
|
|
|
17
|
close($input); |
55
|
1
|
|
|
|
|
7
|
close($p_write); |
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
6936
|
while (<$p_read>) { |
58
|
19
|
|
|
|
|
1879
|
print $output $_; |
59
|
|
|
|
|
|
|
} |
60
|
1
|
|
|
|
|
88
|
close($p_read); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
1; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
__END__ |