Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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 ( #39472=categorized question: print w/replies, xml ) Need Help??
Contributed by Compilers-R-Us on Nov 02, 2000 at 00:06 UTC
Q&A  > object-oriented programming


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 '->'?

Answer: How do I call a method within a regular expression (5.004)?
contributed by tedv

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.


Answer: How do I call a method within a regular expression (5.004)?
contributed by Compilers-R-Us

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

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (6)
    As of 2020-09-29 08:47 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (146 votes). Check out past polls.