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

no strict 'refs'; my $method = "$package::rewrite_$2"; $_ = &$method($self, $1) . "\n";
Just curious...am I going to burn for this?

Replies are listed 'Best First'.
Re: Am I evil for doing this?
by jdporter (Paladin) on Mar 10, 2005 at 21:40 UTC
    Yes. But there's a better way to sin — assuming that $self has $package in its hereditary tree:
    use strict; my $method = "rewrite_$2"; $_ = $self->$method($1) . "\n";
      jdporter suggested: use strict; my $method = "rewrite_$2"; $_ = $self->$method($1) . "\n";
      Actually, that seems slightly LESS evil to me...you are calling a method as a method, allowing Perl to Do The Right Thing in terms of argument passing and inheritance. I'm calling a method as a sub, and tricking it into working by explicitly passing the $self ref. Which means, of course, that your version is better code...just not better evil. :>
        Actually, that seems slightly LESS evil to me

        It's so much less evil, in fact, that it's actually allowed by strict. That should say a lot. Frankly, I think jdporter's method isn't evil at all. If you wanted to be extra careful, you could use UNIVERSAL::can to make sure the method exists before trying to call it:

        use strict; my $method = "rewrite_$2"; if ($self->can($method)) { $self->$method($1); } else { warn "$self does not have $method"; }

        Update: ihb++, I agree fully with Re^5: Am I evil for doing this?