As far as I know, you can't introduce a new quotelike operator akin to your example

$my_object_of_class_A =~ qclassB/$string/
only redefine the meanings of existing ones or create your objects with an explicit function/method call.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.