I wonder if anyone can shed some light on this issue. I have an object that behaves as a regular expression. As I toy with overload strategies, I have stumbled up against a wall. Consider the following package definition:

package Foo; use strict; use overload '""' => \&interp; sub new { my $class = shift; bless { str => $_[0], re => qr/^$_[0]$/, }, $class; } sub as_string { $_[0]->{str}; } sub re { $_[0]->{re}; } sub interp { my $self = shift; my $condition = 1; # 1 if called in qr{} context # 0 otherwise $condition ? $self->re : $self->as_string; } 1;

You might use the above package in the following way:

use Foo; my $r = Foo->new( '[aeiou]+\\d+' ); my $super_re = qr/[A-Z]+$r/; print <<OUT; r = /$r/ super = /$super_re/ OUT for( @ARGV ) { my $match = ($_ =~ /$r/) ? 1 : 0; print "$_ match=$match\n"; }

That may sound a little insane, that's the trouble with a contrived example, but the gist is this: I have an object that contains a compiled regular expression as an attribute, and sometimes I'd like to be able to return the compiled expression, and other times the original text. And, most of all, make perl make the right choice as to when.

So, if the object is interpolated in a qr{} context, the idea is to return the underlying string that was used for the RE, so that it can be included into a larger RE built in the client code (e.g. building the $super_re pattern. Otherwise, if it's used in another context (e.g. /$r/ in the loop), then it should return the compiled regular expression).

As a result, I would like to see

r = /(?-xism:^[aeiou]+\d+$)/ super = /(?-xism:[A-Z]+[aeiou]+\d+)/

but of course it gives

r = /(?-xism:^[aeiou]+\d+$)/ super = /(?-xism:[A-Z]+(?-xism:^[aeiou]+\d+$))/

I'm not sure if there's a use for it (I'm thinking more along the lines of +imsx modifiers rather than outright additions to the RE in Foo::new), but it might come in handy. I thought about caller, but these sorts of shenanigans are invisible to it.

- another intruder with the mooring of the heart of the Perl


In reply to Distinguishing between // and qr{} interpolation by grinder

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.