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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |