in reply to overload::constant passes fragments, not whole strings under interpolation. Help?

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__; }
  • Comment on Re: overload::constant passes fragments, not whole strings under interpolation. Help?
  • Download Code

Replies are listed 'Best First'.
Re^2: overload::constant passes fragments, not whole strings under interpolation. Help?
by diotalevi (Canon) on Sep 17, 2005 at 01:59 UTC

    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;