$my_object_of_class_A =~ qclassB/$string/ #### 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" } }