Class-Implant

 view release on metacpan or  search on metacpan

lib/Class/Implant.pm  view on Meta::CPAN

package Class::Implant;
# ABSTRACT: Manipulating mixin and inheritance out of packages

use 5.008008;
use strict;
no  strict "refs";
use warnings;
use Class::Inspector;

our $VERSION = '0.01';

sub import {
  *{(caller)[0] . "::implant"} = \&implant;
}

sub implant (@) {
  my $option = ( ref($_[-1]) eq "HASH" ? pop(@_) : undef );
  my @class = @_;

  my $target = caller;

  if (defined($option)) {
      $target = $option->{into} if defined($option->{into});
      eval qq{ package $target; use base qw(@class); } if $option->{inherit};
  }

  for my $class (reverse @class) {
    for my $function (@{ _get_methods($class) }) {
      *{ $target . "::" . $function } = \&{ $class . "::" . $function };
    }
  }

}

sub _get_methods { Class::Inspector->functions(shift) }

1;


__END__
=head1 NAME

Class::Implant - Manipulating mixin and inheritance out of packages

=head1 VERSION

version 0.01

=head1 SYNOPSIS

There are two ways to use Class::Implant.

procedural way as follow.

  package main;
  use Class::Implant;

  implant qw(Foo::Bar Less::More) { into => "Cat" }   # import all methods in Foo::Bar and Less::More into Cat

or in classical way. just using caller as default target for implanting.

  package Cat;
  use Class::Implant;

  implant qw(Less::More);                 # mixing all methods from Less::More, 
                                          # like ruby 'include'

  implant qw(Foo::Bar), { inherit => 1 }; # import all methods from Foo::Bar and inherit it
                                          # it just do one more step: unshift Foo::Bar into @ISA
                                          # this step is trivial in Perl
                                          # but present a verisimilitude simulation of inheritance in Ruby

=head1 DESCRIPTION



( run in 1.937 second using v1.01-cache-2.11-cpan-6de40a662fe )