Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

How do I call a method within a regular expression (5.004)?

by Compilers-R-Us (Initiate)
on Nov 02, 2000 at 00:06 UTC ( [id://39472]=perlquestion: print w/replies, xml ) Need Help??

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.

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://39472]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-06-23 01:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.