The goal of this module is to assist in the drudgery of string cleaning by allowing data objects to define what and how to clean.
use String::Clean; my $clean = String::Clean->new(); $clean->replace( { this => 'that', is => 'was' } , 'this is a test' ); # returns 'that was a test' # see the tests for more examples
Each function can take an optonal hash that will change it's behaviour. This hash can be passed to new and will change the defaults, or you can pass to each call as needed.
opt: Any regex options that you want to pass, ie {opt => 'i'} will allow for case insensitive manipulation. replace : If the value is set to 'word' then the replace function will look for words instead of just a collection of charicters. example: replace( { is => 'was' }, 'this is a test', ); returns 'thwas was a test', where replace( { is => 'was' }, 'this is a test', { replace => 'word' }, ); will return 'this was a test' strip : Just like replace, if the value is set to 'word' then strip will look for words instead of just a collection of charicters. word_ boundary : Hook to change what String::Clean will use as the word boundry, by default it will use '\b'. Mainly this would allow String::Clean to deal with strings like 'this,is,a,test'. escape : If this is set to 'no' then String::Clean will not try to escape any of the things that you've asked it to look for.
You can also override options at the function level again, but this happens as merged hash, for example:
my $clean = String::Clean->new({replace => 'word', opt => 'i'}); $clean->strip( [qw{a}], 'an Array', {replace =>'non-word'} ); #returns 'n rray' because opt => 'i' was pulled in from the options at new.
The only thing exciting here is that you can pass the same options hash at construction, and this will cascade down to each function call.
Takes a hash where the key is what to look for and the value is what to replace the key with.
replace( $hash, $string, $opts );
A shortcut that does the same thing as passing {replace => 'word'} to replace.
replace_word( $hash, $string, $opts );
Takes an arrayref of items to completely remove from the string.
strip( $list, $sring, $opt);
A shortcut that does the same thing as passing {strip => 'word'} to strip.
strip_word( $list, $string, $opt);
Because we have to basic functions that take two seperate data types... why not wrap those up, enter YAML.
clean_by_yaml( $yaml, $string, $opt );
But how do we do that? Heres an example:
$string = 'this is still just a example for the YAML stuff'; $string =~ s/this/that/; $string =~ s/is/was/; $string =~ s/\ba\b/an/; $string =~ s/still//; $string =~ s/for/to explain/; $string =~ s/\s\s/ /g; # 'that was just an example to explain the YAML stuff'
$string = 'this is still just a example for the YAML stuff'; $yaml = q{ --- this : that is : was a : an --- - still --- for : to explain ' ': ' ' }; $string = $clean->clean_by_yaml( $yaml, $string, { replace => 'word' } ); # 'that was just an example to explain the YAML stuff'
Order matters:
As you can see in the example we have 3 seperate YAML docs, this allows for replaces to be doene in a specific sequence, if that is needed. Here in this example is would not have mattered that much, here's a better example:
#swap all instances of 'ctrl' and 'alt' $yaml = q{ --- ctrl : __was_ctrl__ --- alt : ctrl --- __was_ctrl__ : alt };
Options are global to the YAML doc :
If you need to have seperate options applied to seperate sets then they will have to happen as seprate calls.
ben hengst, <notbenh at CPAN.org>
<notbenh at CPAN.org>
Please report any bugs or feature requests to bug-string-clean at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Clean. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-string-clean at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc String::Clean
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=String-Clean
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/String-Clean
CPAN Ratings
http://cpanratings.perl.org/d/String-Clean
Search CPAN
http://search.cpan.org/dist/String-Clean
Copyright 2007 ben hengst, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install String::Clean, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::Clean
CPAN shell
perl -MCPAN -e shell install String::Clean
For more information on module installation, please visit the detailed CPAN module installation guide.