TVDB: better handling of first run
[nonametv.git] / lib / NonameTV / StringMatcher.pm
blobe7dc9df3fc9806287c97d152fa47c0af30cc11b1
1 package NonameTV::StringMatcher;
3 use strict;
4 use warnings;
6 =pod
8 =head1 NAME
10 NonameTV::StringMatcher
12 =head1 DESCRIPTION
15 my $sm = NonameTV::StringMatcher->new();
16 $sm->AddRegexp( qr/\bt.st\b/, [ 1,2 ] );
18 my $res = $sm->Match( "this is a test" );
19 if( defined( $res ) )
21 print $res->[0] . " " . $res->[1] . "\n";
24 =head1 METHODS
26 =over 4
28 =cut
30 =item new
32 The constructor for the object.
34 =cut
36 sub new
38 my $class = ref( $_[0] ) || $_[0];
40 my $self = { };
41 bless $self, $class;
43 return $self;
46 =item AddRegexp
48 Add a new regexp that all strings should be matched against.
49 Takes two parameters, the regexp to match against and the data
50 that should be returned if the match is successful.
52 =cut
54 sub AddRegexp
56 my $self = shift;
57 my( $re, $res ) = @_;
59 push @{$self->{regexps}}, [$re,$res];
62 =item Match
64 Match a string against all regexps in the object. Returns
65 the result for the first regexp that matches.
66 Takes a single parameter, the string to match against the regexps.
68 Returns undef if no regexp matches.
70 =cut
72 sub Match
74 my $self = shift;
75 my( $s ) = @_;
77 foreach my $r (@{$self->{regexps}})
79 my $re = $r->[0];
80 if( $s =~ /$re/ )
82 return $r->[1];
86 return undef;
90 =head1 COPYRIGHT
92 Copyright (C) 2004 Mattias Holmlund.
94 =cut