diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

The following codes shows that when I attempt to trap regular expression syntax with overload.pm, as documented, use of interpolation causes only static fragments to be passed through to my package. Is there a standard way I can get access to the final generated string or to the original including the $bar interpolation? I'm about to write a piece of syntax like (?&91;NAME&93;...) but this requires that I be able to see the closing parenthese.

use My::Package; /<<<$bar>>>/; # RE=<<< # RE=>>>

My/Package.pm

package My::Package; use overload; sub import { overload::constant qr => \ &convert } sub convert { my $re = shift; print "RE=$re\n"; } 1;

Replies are listed 'Best First'.
Re: overload::constant passes fragments, not whole strings under interpolation. Help?
by jql (Acolyte) on Sep 17, 2005 at 00:22 UTC
    You need to overload some more. If you return an object which can spy on any strings concatenated with it, you can grab the entire entire regex.
    sub convert { my $re = shift; return bless \$re, __PACKAGE__; } use overload '""' => sub { ${ shift() } }; use overload '.' => \&concat; sub concat { my($self, $str, $rev) = @_; my $string = $rev ? "$str" . "$self" : "$self" . "$str"; return bless \$string, __PACKAGE__; }

      That's great. Here's the outline for code that does exactly that. It passes a postponing object to overload::constant, postpones any concatenation and then runs the actual conversion function on stringification at the end.

      package My::Package; use strict; use warnings; sub convert { YOUR CODE GOES HERE } use overload( '.' => \ &_concat, '""' => \ &_finalize ); sub import { overload::constant qr => \ &_postpone; } sub _postpone { my $re = shift; bless \ $re, __PACKAGE__; } sub _concat { my ( $a, $b, $inverted ) = @_; ($a,$b)=($b,$a) if $inverted; $a = $$a if ref( $a ) eq __PACKAGE__; $b = $$b if ref( $b ) eq __PACKAGE__; my $re = "$a$b"; bless \ $re, __PACKAGE__; } sub _finalize { @_ = ${shift()}; &convert; } 1;
Re: overload::constant passes fragments, not whole strings under interpolation. Help?
by sauoq (Abbot) on Sep 16, 2005 at 23:59 UTC

    I think you're out of luck. I don't think there's an easy way to do this.

    Perl.com: Maintaining Regular Expressions contains a minor amount of discussion about this. One relevant statement:

    Therefore, Regexp::DeferredExecution should only be used with "constant" regular expressions; there is currently no way to overload dynamic, "interpolated" regular expressions.

    Of course, whenever I see "no way" in a context like that, I know it means "no easy way"... and, knowing you, you'll manage to figure it out somehow. So, let us know when you do! ;-)

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: overload::constant passes fragments, not whole strings under interpolation. Help?
by Tanktalus (Canon) on Sep 17, 2005 at 00:24 UTC

    "as documented"? According to overload:

    qr   to overload constant pieces of regular expressions.
    That said, I'm not entirely sure how overload::constant is actually useful. I'm having a hard time imagining it - although it may just be because it's Friday evening, and my mind is completely wiped at this point... ;-)

    I'm sure you could find a way with a source filter... <duck!> ;-)