NAME
Text::Pipe - Common text filter API
SYNOPSIS
my $pipe = Text::Pipe->new('List::First', code => { $_ < 7 });
my $result = $pipe->filter('foo');
# or
use Web::Scraper;
my $scraper = scraper {
process '//p/a',
'texts[]' => [ 'TEXT', PIPE('Trim'), PIPE('Uppercase') ];
};
DESCRIPTION
This class is a factory for text pipes. A pipe has a filter()
method through which input can pass. The input can be a string or a reference to an array of strings. Pipes can be stacked together using Text::Pipe::Stackable.
The problem that this distribution tries to solve is that there are several distributions on CPAN which use text filtering in some way or other, for example the Template Toolkit or Web::Scraper. But each distribution is somewhat different, and they have to reimplement the same text filters over and over again.
This distribution aims at offering a common text filter API. So if you want to use text pipes with Template Toolkit, you just need to write an adapter. With Web::Scraper, you can even use text pipes directly Using the PIPE()
function, as shown in the synopsis.
Text pipe segments live in the Text::Pipe::
namespace. So if you implement a Text::Pipe::Foo::Bar
pipe segment, you can instantiate it with
my $pipe = Text::Pipe->new('Foo::Bar');
Some pipe segments take arguments. These are described in their respective class documentations.
EXPORTS
PIPE
-
my $pipe = PIPE('Reverse', times => 2, join => ' = '); my $pipe = PIPE('UppercaseFirst');
Text::Pipe exports, on request, the function
PIPE()
that makes it easier to construct pipes. It takes the same arguments as new() and returns the corresponding pipe.
METHODS
new
-
my $pipe = Text::Pipe->new('List::First', code => { $_ < 7 });
Constructs a new pipe. The first argument is the pipe segment type - in the example above a
Text::Pipe::List::First
would be constructed. The remaining arguments are passed to that segment's constructor. def_pipe
-
Text::Pipe->def_pipe('Foobar', sub { lc $_[1] }); my $pipe_lowercase = Text::Pipe->new('Foobar'); is($pipe_lowercase->filter('A TEST'), 'a test', 'lowercase pipe');
This method provides a lightweight way to define a new pipe segment class. The first argument is the segment type - in the example above, a new segment class
Text::Pipe::Foobar
would be defined. The second argument is a coderef that acts as the segment's filter. The segment class will subclassText::Pipe::Base
.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests through the web interface at http://rt.cpan.org.
INSTALLATION
See perlmodinstall for information and options on installing Perl modules.
AVAILABILITY
The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you. Or see http://www.perl.com/CPAN/authors/id/M/MA/MARCEL/.
The development version lives at http://github.com/hanekomu/text-pipe/. Instead of sending patches, please fork this project using the standard git and github infrastructure.
AUTHORS
Marcel Grünauer, <marcel@cpan.org>
COPYRIGHT AND LICENSE
Copyright 2007-2009 by the authors.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.