Class-Trait

 view release on metacpan or  search on metacpan

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

            # note the conflict and ...
            $trait_config->conflicts->{$overload}++;

            # get rid of it (conflicts results in exclusions)
            delete ${ $trait_config->overloads }{$overload};

            # since the overload is now excluded, then it then
            # becomes a requirement for the implementing package
            $trait_config->requirements->{"${overload}"}++;
        }
        else {
            debug "+ operator ($overload) added successfully" if DEBUG;

            # otherwise add it to the list of methods
            $trait_config->overloads->{$overload}
              = $trait->overloads->{$overload};
        }
    }
    $debug_indent-- if DEBUG;
}

sub _check_requirements {
    my ( $trait, $trait_config ) = @_;

    # now check the requirements
    debug "? checking for trait ($trait->{name})" if DEBUG;
    foreach my $requirement ( keys %{ $trait->requirements } ) {

        # if the method does not exist in
        # our new combined method group
        unless ( exists ${ $trait_config->methods }{$requirement} ) {
            if (DEBUG) {
                $debug_indent++;
                debug "* requirement ($requirement) not fufilled";
                $debug_indent--;
            }

            # make it a reuiqement for the package
            $trait_config->requirements->{$requirement}++;
        }
    }
}

## ----------------------------------------------------------------------------
## utility methods
## ----------------------------------------------------------------------------

# short quick predicate functions
sub is_method_label { $_[0] =~ /[[:alpha:]][[:word:]]+/ }

1;

__END__

=head1 NAME

Class::Trait - Deprecated.  Please use Moose::Role.

=head1 DEPRECATED

This module has long been deprecated. It was a precursor to Moose::Role and
other attempts to implement roles/traits in Perl. If you need standalone
roles, please use L<Role::Basic> or L<Role::Tiny>. If you need full OO, please
use L<Moose> or L<Moo>.

=head1 SYNOPSIS

    # to turn on debugging (do this before
    # any other traits are loaded)
    use Class::Trait 'debug';
    
    # nothing happens, but the module is loaded
    use Class::Trait;
    
    # loads these two traits and flatten them
    # into the current package
    use Class::Trait qw(TPrintable TComparable);	
    
    # loading a trait and performing some
    # trait operations (alias, exclude) first
    use Class::Trait (
        'TPrintable' => {
            alias   => { "stringValue" => "strVal" },
            exclude => "stringValue",
         },
     );
            
    # loading two traits and performing
    # a trait operation (exclude) on one 
    # module to avoid method conflicts
    use Class::Trait 
        'TComparable' => {
            # exclude the basic equality method
            # from TComparable and use the ones
            # in TEquality instead.
            exclude => [ "notEqualTo", "equalTo" ]
        },
        'TEquality' # <- use equalTo and notEqualTo from here
    );			
            
    # when building a trait, you need it
    # to inherit from the trait meta/base-class
    # so do this ...
    use Class::Trait 'base';		

=head1 DESCRIPTION

This document attempts to explain Traits in terms of Perl.

=head2 Trait composition

A Trait can be defined as a package containing:

=over 4

=item *
A set of methods

=item *
A hash of overloaded operators mapped to the method labels



( run in 0.698 second using v1.01-cache-2.11-cpan-39bf76dae61 )