http://www.perlmonks.org?node_id=39472

Compilers-R-Us has asked for the wisdom of the Perl Monks concerning the following question:

I have a question related to "Calling a method within a double-quoted string" - I would like to call a method within regex match expression, like
print "Matched!" if $myvar =~ /\Q$myob->print\E/;
I can almost do it with 5.005+ as in:
print "Matched!" if $myvar =~ /(?{$myob->escaped})/;
However, I have to pre-escape the return result. Of course I can use a temporary variable, but that damages the readability. Unfortunately, I am stuck at 5.004 compatibility and (?{...}) is not supported. Also, I feel that it is my birthright to do this in one line. Is there a one-liner to do this? Also, is there a reason that the binding is not stronger for '->'?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I call a method within a regular expression (5.004)?
by tedv (Pilgrim) on Nov 02, 2000 at 00:28 UTC
    I'd assume that one reason 5.005 supports ?{} is that 5.004 needed it. :) Although the object would definitly have to return a match string, right?... So if you have an object that just outputs data, the proper syntax would be:  $myobj->print() =~ /$myvar/ So you might want to/need to swap what's doing the matching on what. Of course, without context I'm not sure if this is even possible.

    -Ted
Re: How do I call a method within a regular expression (5.004)?
by Compilers-R-Us (Initiate) on Nov 02, 2000 at 01:50 UTC

    Thanks. By the way, the (?{}) functionality is listed as 'experimental' in the docs. But yes, hopefully it will remain because people like me want it. Also, $myobj->print is expected to be a subset of $myvar, in my case, so I can't do it the other way around. And, I was wrong, (?{}) does have the effect of escaping non-alphanumeric characters in the return value. Sean