> >       caller->eval EXPRESSION;
> 
> That's mad, bad, scary and dangerous. Let's do it.

Yes, this is cool. In fact, I'm writing Regexp::Func right now as a
prototype for RFC 164 and discovering I could really use this - in fact,
need it.

A couple things:

   1. Implement this eval as UNIVERSAL::eval. Then...

   2. Change the above syntax to self->eval(EXPR)

No reason to have to change caller(). If we had a self() that can be
called anywhere  and returned a reference to the corresponding
package/parent namespace (like RFC 152's), then this works dandy. Then
UNIVERSAL::eval is simply invoked via simple inheritance. Presto:

 package Regexp::Func;

 sub replace (;$@) {
    # By default, strip leading/trailing whitespace
    my $pat = shift || '/(?:^\s+|\s+$)//g';
    my @in = @_ ? @_ : $_;

    # Just a simple eval is all we need
    _pattern_check($pat);
    for (my $i=0; $i < @in; $i++) {
        self->eval("\$in[$i] =~ s$pat;");  # Yes!!
        do { chomp $@; croak $@, " in replace" } if $@;
    }

    # Always return the first string in scalar
    wantarray ? return @in : return $in[0];
 }


Yeah, I could really use this. ;-)

-Nate

Reply via email to