String::FlexMatch - flexible ways to match a string
use String::FlexMatch; my $s = String::FlexMatch->new(string => 'foobar'); if ($s eq 'foobar') { ... } my $s = String::FlexMatch->new(regex => 'Error .* at line \d+'); if ($s eq 'Error "foo" at line 58') { ... } my $s = String::FlexMatch->new(code => 'sub { length $_[0] < 10 }'); # or: # my $s = String::FlexMatch->new(code => sub { length $_[0] < 10 }); if ($s ne 'somelongstring') { ... }
Normally when trying to see whether two strings are equal, you use the eq operator. If you want to find out whether one string matches another more flexibly, you'd use a regular expression. And sometimes you have to call a subroutine with a string argument that will tell you whether that argument is interesting, i.e. matches in a broader sense.
eq
When running data-driven tests, you sometimes don't know per se which form of matching (eq, regex or code) you need. Take the following example:
use Test::More; use String::FlexMatch; use YAML; sub frobnicate { $_[0] + $_[1] } my $tests = Load do { local $/; <DATA> }; plan tests => scalar @$tests; for my $test (@$tests) { my $baz = frobnicate($test->{testarg}{foo}, $test->{testarg}{bar}); is($baz, $test->{expect}{baz}); } __DATA__ - testarg: foo: 2 bar: 3 expect: baz: 5 - testarg: foo: 21 bar: 34 expect: baz: !perl/String::FlexMatch regex: '\d+'
A setup like this was the reason for writing this class. If you find any other uses for it, please let me know so this manpage can be expanded with a few cookbook-style examples.
If you talk about this module in blogs, on del.icio.us or anywhere else, please use the stringflexmatch tag.
stringflexmatch
No bugs have been reported.
Please report any bugs or feature requests to bug-string-flexmatch@rt.cpan.org, or through the web interface at http://rt.cpan.org.
bug-string-flexmatch@rt.cpan.org
See perlmodinstall for information and options on installing Perl modules.
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/>.
Marcel Grünauer, <marcel@cpan.org>
<marcel@cpan.org>
Copyright 2004-2007 by Marcel Grünauer
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install String::FlexMatch, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::FlexMatch
CPAN shell
perl -MCPAN -e shell install String::FlexMatch
For more information on module installation, please visit the detailed CPAN module installation guide.