++ed by:

3 PAUSE users
2 non-PAUSE users.

and 1 contributors




version 0.0405


Fast full featured post-modern Object oriented programming. Available both in PP and C. See https://github.com/alexbyk/perl-evo/tree/master/bench


  package main;
  use Evo;


    package My::Human;
    use Evo -Class, -Loaded;

    has 'name' => 'unnamed';
    has 'gender', ro;
    has age => optional, check sub($v) { $v >= 18 };
    sub greet($self) { say "I'm " . $self->name }

  my $alex = My::Human->new(gender => 'male');

  # default value "unnamed"
  say $alex->name;

  # fluent design
  say $alex->name, ': ', $alex->age;

  # method

  ## ------------ protecting you from errors, uncomment to test
  ## will die, gender is required

  ## will die, age must be >= 18
  #My::Human->new(age => 17, gender => 'male');
  #My::Human->new(gender => 'male')->age(17);

  # --------- code reuse

    package My::Developer;
    use Evo -Class;
    with 'My::Human';    # extends 'My::Human'; implements 'My::Human';

    has lang => 'Perl';

    sub show($self) {
      say "I like ", $self->lang;


  my $dev = My::Developer->new(gender => 'male');



You will find thet syntax differs from other modules, such Moose, Moo. That's because I decided not to copy and made it to be as short/safe/obvious/common as possible. Give it a try

Also multiple inheritance is considered a good development pattern, because a composition (mixing) is used for code reuse.

A note about @ISA. Evo classes don't require it, all methods and attributes are copied from parents to children. But @ISA is still in use to work fine with external modules and to be able to call isa.

Also every class is a subclass of Evo::Class::Base


By default this module exports some keywords, like has, check, rw and so on. If your code conflicts with them, don't worry, perl will notify you and you can either rename conflicting methods, or exclude them from exporting/rename them this way:

  use Evo '-Class * -check -has has:attr';
  attr foo => 'FOO';

  sub check { }
  sub has   { }

  say __PACKAGE__->new->foo;

We skipped check and has, because they conflict with our methods, and export has under attr name, because we need it.


creating an object

  package My::Class;
  use Evo -Class;
  has 'simple';


  my $foo = My::Class->new(simple => 1);
  my $foo2 = My::Class->new();

We're protected from common mistakes, because constructor won't accept unknown attributes. Also, if attributes aren't optional and have additional flags, they will be checked too.


  has 'foo', ro;
  has 'bar' => 'BAR', check sub {1};
  has 'baz' => 'BAZ';

Without options attributes are required and read-only. You can pass extra flags/options + a default value in any order. If you make a mistake, smart syntax parser will notify you. In the example above default values are BAR and BAZ. Pay attention, ro and check are not strings, so 'ro' or check => is a mistake.

Flags and Options


Make attribute read-only. You can set it only once via new. You still will be able to change it as a normal hash key like

  $obj->{ro_attr} = 33;

default value

Default value can be a scalar or a code reference, which will be called with a class as the first argument, unless lazy flag is passed

  has 'def_code' => sub($class) { uc "$class" };
  say __PACKAGE__->new->def_code;

You can't use a reference, except a code reference, as a default value. To return, for example, a hashref, use this:

  has foo => sub($class) { { class => $class } };
  say __PACKAGE__->new->foo->{class};


This flag changes a behaviour of default value. It should be a code that will be called at the first invocation, not in constructor, and an instance will be passed as the argument. The result of this invocation will be stored in attribute

  has foo => lazy, sub($self) { [] };
  say __PACKAGE__->new->foo;

You should know that using this feature is an antipattern in the most of the cases.


  has 'foo', optional;

By default, attributes are required. You can pass this flag to mark attribute as optional (but in most cases this is antipattern)


You can provide function that will check passed value (via constuctor and changing), and if that function doesn't return true, an exception will be thrown.

  has big => check sub { shift > 10 };

You can also return (0, "CustomError") to provide more expressive explanation

  package main;
  use Evo;


    package My::Foo;
    use Evo '-Class *';

    has big => check sub($val) { $val > 10 ? 1 : (0, "not > 10"); };

  my $foo = My::Foo->new(big => 11);

  $foo->big(9);    # will die
  my $bar = My::Foo->new(big => 9);    # will die


Used to describe dependencies of a class. We can build Foo that depends on Bar and we don't care how Bar is implemented. Evo::Di will resolve all dependencies

  package Foo;
  use Evo -Class, -Loaded;

  has bar => inject 'Bar';

  package Bar;
  use Evo -Class, -Loaded;
  has host => inject 'HOST';

  package main;
  use Evo '-Di';
  my $di = Evo::Di->new();
  $di->provide(HOST => '');

  my $foo = $di->single('Foo');
  say $foo->bar->host;

See Evo::Di for more information.


Register an attribute but don't install method(accessor) for it.

  package Foo;
  use Evo -Class, -Loaded;
  has foo => no_method;

  package main;
  use Evo;
  my $obj = Foo::->new(foo => 'FOO');
  warn $obj->{foo};    # FOO
  warn $obj->can('foo') ? 'can' : "can't";    # can't


Only public functions will be inherited.

Subroutine will be considered is a private if: It's imported from other modules It's monkey patched from other module by *Foo::foo = sub {} It's exported by Evo::Export functions, (when a package is both a library and a class)

Subroutine will be considered as a public if: It is defined in a class. It is inherited by extends or with

All attributes are public.

Methods, generated somehow else, for example by *foo = sub {}, can be marked as public by "reg_method" in Evo::Class::Meta

Private methods

If you want to mark a method as private, use new lexical_subs feature

  my sub _private {'private'}

This if preferred way to garantee that no one will be able to use this subroutine.

In some cases you may want to access private method for testing in the parent, but make unaccessible from a child. In this case use the folowing example:

  package Foo;
  use Evo -Class, -Loaded;
  sub _hello($self) : Private { 'hello from ' . $self }
  sub greet($self)            { say _hello($self) }

  package Bar;
  use Evo -Class;
  with 'Foo';

  package main;
  say 'Foo can _hello?: ', !!Foo->can('_hello');
  say 'Bar can _hello?: ', !!Bar->can('_hello');

Pay attention. _hello method should be called in this way: _hello($self), NOT $self->_hello - this will break your code.

:Private attribute marks a method as private making an invocation of "mark_as_private" in Evo::Class::Meta to skip this method from inheritance process

And of course, many developers just name methods like _private (this method is actually private) to show that it can be changed, call them as they want and don't bother about clashing problems at all. Choose your own way.


Evo protects you from method clashing. But if you want to override method or fix clashing, use "has_over" function or :Override attribute

    package My::Peter;
    use Evo -Class;
    with 'My::Human';

    has_over name => 'peter';
    sub greet : Over { ... }

If you want to call parent's method, call it by full name instead of SUPER

  sub foo($self) : Over {
    say "Child";


This functions will be exported by default even without export list use Evo::Class; You can always export something else like use Evo::Class 'has'; or export nothing use Evo::Class ();


Return current Evo::Class::Meta object for the class.

  use Data::Dumper;
  say Dumper __PACKAGE__->META->info;

See what's going on with the help of "info" in Evo::Class::Meta


Extends classes or roles. See also "EXTENDING ALIEN CLASSES"


Check if all required methods are implemented. Like interfaces


This does "extend + check implementation". Consider this example:

  package main;
  use Evo;


    package My::Role::Happy;
    use Evo -Class, -Loaded;

    requires 'name';

    sub greet($self) {
      say "My name is ", $self->name, " and I'm happy!";

    package My::Class;
    use Evo -Class;

    has name => 'alex';

    #extends 'My::Role::Happy';
    #implements 'My::Role::Happy';
    with 'My::Role::Happy';



My::Role::Happy requires name in derivered class. We could install shared code with extends and then check implemantation with implements. Or just use with wich does both.

You may want to use extends and implements separately to resolve circular requirements, for example


  sub foo : Over { 'OVERRIDEN'; }

Mark name as overridden. Overridden means it will override the "parent's" method with the same name without diying


Every class gets $EVO_CLASS_META variable which holds an Evo::Class::Meta instance. See "register" in Evo::Class::Meta




This software is copyright (c) 2016 by alexbyk.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.