NAME

Text::Modify - oo-style interface for simple, rule-based text modification

SYNOPSIS

  use Text::Modify;

  my $mod = new Text::Modify(-file=>'my.txt', -writeto=>'new.txt', dryrun=>0);
  $mod->replace("sad","funny");
  $mod->replace('.*?logalhos$',"127.0.0.1       localhost",ifmissing=>'append');
  my $count = $mod->process();

DESCRIPTION

Text::Modify is a simple oo-style interface to perform variuos text modifcation tasks.

Instead of having to parse and modify textfiles with customized routines over and over Text::Modify provides a common ruleset, which allows simple to advanced editing tasks to be performend.

After instantiating a new Text::Modify object, rules are defined on it and finally processed.

        my $mod = new Text::Modify();

Text::Modify uses Text::Buffer internally to perform the editing tasks on the text-file.

Methods

new
    $mod = new Text::Modify(%options);

This creates a new object, starting with an empty buffer unless the -file or -array options are provided. The available attributes are:

append
        $mod->append("new last line");

Add a rule to append a new line at enf of text.

insert
        $mod->insert("new first line");

Add a rule to insert a new line at start of text.

delete
        $mod->delete('.*DELETE ME$');

Add a rule to delete lines matching the supplied string. The string is interpreted as a regular expression, so be sure to escape characters accordingly.

replace
        $mod->replace("foo","bar", ifmissing=>append, ignorecase=>1);

Add a rule to replace all occurences of foo with bar in the text.

replaceString
        $mod->replaceString("foo","bar", ifmissing=>append, ignorecase=>1);

Add a rule to replace all occurences of string foo with bar in the text.

replaceWildcard
        $mod->replace("*foo?","bar", ifmissing=>append, ignorecase=>1);

Add a rule to replace all occurences matching the wildcard *foo? with bar in the text. '*' (asterisk) will match any characters (as much as possible) and '?' (question mark) will match one character

replaceRegex
        $mod->replace("\s*foo\d+","bar", ifmissing=>append);
        $mod->replace("\s*foo(\d+)",'bar$1', ignorecase=>1);

Add a rule to replace all occurences matching the regular expression *foo? with bar in the text. Also regex parameters can be used in the replacement string.

defineRule
        $mod->defineRule(replace=>'foo\s+bar',with=>'foobar', ifmissing=>append);

# TODO add pod for all options supported by defineRule() Advanced interface to define a rule, which gives most flexibilty to perform a given task the way you need it.

undefineRule

Delete a rule, that was created with the supplied name

listRules

Returns a list of rules in the order they will be executed.

createBackup

create a backup of the specified file

backupExtension

get/set the backup extension used for backup files

getLinesModified =item getLinesProcessed =item getDeleteCount =item getAddCount =item getMatchCount =item getReplaceCount

Return statistics and counters of the processing performed

process
        $mod->process();

Start processing of the text, rule by rule. If dryrun is enabled, modification will be performed in memory, but are not written to file.

dryrun
        $mod->dryrun();

Start processing of the text, rule by rule with dryrun enabled. The setting for dryrun will be restored after processing. Modification will be performed in memory, but are not written to file.

isDryRun

Returns 1 if dryrun has been enabled, no modifications will be written to the text to process. Otherwise returns 0.

isError
getError
        if ($text->isError()) { print "Error: " . $text->getError() . "\n"; }

Simple error handling routines. isError returns 1 if an internal error has been raised. getError returns the textual error.

BUGS

There definitly are some, if you find some, please report them.

LICENSE

This software is released under the same terms as perl itself. You may find a copy of the GPL and the Artistic license at

   http://www.fsf.org/copyleft/gpl.html
   http://www.perl.com/pub/a/language/misc/Artistic.html

AUTHOR

Roland Lammel (lammel@cpan.org)