As far as I know, you can't introduce a new quotelike operator akin to your example
only redefine the meanings of existing ones or create your objects with an explicit function/method call.$my_object_of_class_A =~ qclassB/$string/
I think, however, within some limits, you can create an object you can apply to a string with the =~ operator but behaives in a custom way. For this, you have to use the /(?{})/ regexp feature. The code inside this construct can access the matched string from the $^N provided you put a /\A(?=.*\z)/s capture before the construct that captures the entire string. Then, the code can do whatever computations with the string to decide if it wants to return a match or a non-match.
Here's a simple proof-of-concept example.
use warnings; use strict; sub len_3mult { my($s) = @_; 0 == length($s) % 3; } my $len_3mult = qr/\A(.*)\z(?(?{ len_3mult($^N) })|(?!))/s; for my $s (qw"my his your their socket connect protocol terminate") { if ($s =~ /$len_3mult/) { print "<<$s>> matches\n"; + } else { print "<<$s>> does not match\n" } }
As you can see, the regexp $len_3mult matches only if the length of the string is a multiply of three or more characters. You could replace this condition with arbitary perl code.
I don't know if you can return arbitary captures ($1, $2, ..., @-, @+) from such a regexp.
In reply to Re: overload =~ operator
by ambrus
in thread overload =~ operator
by dk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |