Text::Filter

Base class for objects that can read and write text lines
Download

Text::Filter Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Johan Vromans
  • Publisher web site:
  • http://search.cpan.org/~jv/

Text::Filter Tags


Text::Filter Description

Base class for objects that can read and write text lines Text::Filter's main purpose is to abstract out the details out how input and output must be done. Although in most cases input will come from a file, and output will be written to a file, advanced modules require more detailed control over the input and output. For example, the module could be called from another module, in this case the callee could be allowed to process only a part of the input. Or, a program could have prepared data in an array and wants to call the module to process this data as if it were read from a file. Also, the input stream provides a pushback functionality to make peeking at the input easy.Text::Filter can be used on its own as a convenient input/output handler. For example: use Text::Filter; my $filter = Text::Filter->(input => *STDIN, output => *STDOUT); my $line; while ( defined($line = $filter->readline) ) { $filter->writeline($line); }Or, even simpler: use Text::Filter; Text::Filter->run(input => *STDIN, output => *STDOUT);Its real power shows when such a program is turned into a module for optimal reuse.When creating a module that is to process lines of text, it can be derived from Text::Filter, for example: package MyFilter; use base 'Text::Filter';The constructor method must then call the new() method of the Text::Filter class to set up the base class. This is conveniently done by calling SUPER::new(). A hash containing attributes must be passed to this method, some of these attributes will be used by the base class setup. sub new { my $class = shift; # ... fetch non-attribute arguments from @_ ... # Create the instance, using the attribute arguments. my $self = $class->SUPER::new(@_);Finally, the newly created object must be re-blessed into the desired class, and returned: # Rebless into the desired class. bless($self, $class); }When creating new instances for this class, attributes input and output can be used to specify how input and output is to be handled. Several possible values can be supplied for these attributes.For input: * A scalar, containing a file name. The named file will be opened, input lines will be read using . * A file handle (glob). Lines will be read using . * An instance of class IO::File. Lines will be read using . * A reference to an array. Input lines will be shift()ed from the array. * A reference to a scalar. Input lines will be taken from the contents of the scalar (which will be modified). When exhausted, it will be set to undefined. * A reference to an anonymous subroutine. This routine will be called to get the next line of data.The default is to read input using de operator.For output: * A scalar, containing a file name. The named file will be created automatically, output lines will be written using print(). * A file handle (glob). Lines will be written using print(). * An instance of class IO::File. Lines will be written using print(). * A reference to an array. Output lines will be push()ed into the array. The array will be initialised to () if necessary. * A reference to a scalar. Output lines will be appended to the scalar. The scalar will be initialised to "" if necessary. * A reference to an anonymous subroutine. This routine will be called to append a line of text to the destination.The default is to write output to STDOUT.Additional attributes can be used to specify actions to be performed after the data is fetched, or prior to being written. For example, to strip line endings upon input, and add them upon output.SYNOPSISA plethora of tools exist that operate as filters: they get data from a source, operate on this data, and write possibly modified data to a destination. In the Unix world, these tools can be chained using a technique called pipelining, where the output of one filter is connected to the input of another filter. Some non-Unix worlds are reported to have similar provisions.To create Perl modules for filter functionality seems trivial at first. Just open the input file, read and process it, and write output to a destination file. But for really reusable modules this approach is too simple. A reusable module should not read and write files itself, but rely on the calling program to provide input as well as to handle the output.Text::Filter is a base class for modules that have in common that they process text lines by reading from some source (usually a file), manipulating the contents and writing something back to some destination (usually some other file).This module can be used on itself, but it is most powerfull when used to derive modules from it. See section EXAMPLES for an extensive example. Requirements: · Perl


Text::Filter Related Software