File::Extract::Filter::Exec - Execute A Command To Filter File Contents


File-Extract documentation Contained in the File-Extract distribution.

Index


Code Index:

if ($cmd !~ /\|\s*$/) { $cmd .= " |"; # make sure it's piped } if ($cmd !~ /^\s*\|$/) { $cmd = "| " . $cmd; # make sure it's piped } =cut

Top

NAME

Top

File::Extract::Filter::Exec - Execute A Command To Filter File Contents

SYNOPSIS

Top

  use File::Extract::Filter::Exec;

  my $filter = File::Extract::Filter::Exec->new(
    cmd => "/usr/bin/pdf2html",
    output => $output
  )

  $filter->filter($file);

DESCRIPTION

Top

This filter executes a command, and writes the filtered output into a temporary file such that the new temporary file can be passed to


File-Extract documentation Contained in the File-Extract distribution.
# $Id: /mirror/perl/File-Extract/trunk/lib/File/Extract/Filter/Exec.pm 4210 2007-10-27T13:43:07.499967Z daisuke  $
#
# Copyright (c) 2005 Daisuke Maki <dmaki@cpan.org>
# All rights reserved.

package File::Extract::Filter::Exec;
use strict;
use base qw(File::Extract::Filter::Base);
use IO::Scalar;
use IPC::Open2;
use UNIVERSAL::isa;

sub new
{
    my $class = shift;
    my %args  = @_;

    my $self = bless { cmd => $args{cmd} }, $class;
    return $self;
}

sub cmd { shift->{cmd} }

sub filter
{
    my $self = shift;
    my %args = @_;

    my $file = $args{file};
    my $o = $args{output};
    my $output = 
        (ref($o) && UNIVERSAL::isa($o, 'GLOB'))   ? $o :
        (ref($o) && UNIVERSAL::isa($o, 'SCALAR')) ? IO::Scalar->new($o) :
        die "output must be a GLOB or ref to SCALAR";

    my $cmd = $self->cmd; # XXX - if we wanted to be paranoid, we need to
                          # cleanse this guy, but oh well..
    open(my $input, $file) or die "Failed to open file $file: $!";
    my($p_read, $p_write);
    open2($p_read, $p_write, $cmd) or die "Failed to execute $cmd";

    while (<$input>) {
        print $p_write $_;
    }
    close($input);
    close($p_write);

    while (<$p_read>) {
        print $output $_;
    }
    close($p_read);
}

1;

__END__