You're on target. It's interesting that.....
...interesting that the regexp returned by qr// is automatically an object in package Regexp, and that there's nothing preventing you from adding other methods to the Regexp class. They can then be invoked as object instance methods like this:
package Regexp;
use strict;
use warnings;
sub wierd {
print "Invoked wierd\n";
return $_[1] =~ $_[0];
}
package main;
use strict;
use warnings;
my $string = "This is a test.\n";
my $re = qr/\bis\b/;
print "Match!\n" if $re->wierd( $string );
This allows functionality to be added to the base Regexp class. But that's probably not the best way to mess around with regular expressions. It's probably more sane to bless a regular expression into a new class like this:
package WierdRE;
sub new {
my( $class, $regexp ) = @_;
bless qr/$regexp/, $class;
}
sub wierd {
print "Invoked wierd\n";
return $_[1] =~ $_[0];
}
package main;
use strict;
use warnings;
my $string = "This is a test.\n";
my $re = WierdRE->new( '\bis\b' );
print "Match!\n" if $re->wierd( $string );
The latter method at least leaves the built-in Regexp class as is, and creates a new class that is based on a blessed Regular Expression object.
....just food for thought...