extends()をコンパイル時に

extends()がコンパイル時に実行されるようになればいくつかの問題が解決されるはず,と思って書いてみた。このextendsプラグマは,プラグマの呼び出し元がextends()を持っていればそれを呼び出し,持っていなければparentプラグマと同じ処理を行う。

# extends.pm
package extends;
use strict;
use warnings;
sub import{
    my $extends = do{ no strict 'refs'; \&{caller() . '::extends'} };
    if(defined &{$extends}){
        shift; # self class
        goto &{$extends};
    }
    else{
        require parent;
        goto &parent::import;
    }
}
1;
__END__

=head1 NAME

extends - Establishes an ISA relationship with extends() at compile time

=head1 SYNOPSIS

    package My::Class::Derived;
    use Moose; # or Mouse
    use extends qw(My::Class::Base); # instead of extend('My::Class::Base)';

=cut

流石にこれはモジュール名前空間の浪費のように思えるのでCPANには上げない。実際には,Mo[ou]seのimport()で直接-extends => qw(My::Class::Base)をサポートするのが最善策ではないかと思う。