The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Venus::Prototype - Prototype Class

ABSTRACT

Prototype Class for Perl 5

SYNOPSIS

  package main;

  use Venus::Prototype;

  my $prototype = Venus::Prototype->new(
    '$counter' => 0,
    '&decrement' => sub { $_[0]->counter($_[0]->counter - 1) },
    '&increment' => sub { $_[0]->counter($_[0]->counter + 1) },
  );

  # bless({value => {...}}, 'Venus::Prototype')

  # $prototype->counter # 0
  # $prototype->increment # 1
  # $prototype->counter # 1
  # $prototype->decrement # 0
  # $prototype->counter # 0

DESCRIPTION

This package provides a simple construct for enabling prototype-base programming. Properties can be called as methods when prefixed with a dollar or ampersand symbol. See "call" for more details.

INHERITS

This package inherits behaviors from:

Venus::Kind::Utility

INTEGRATES

This package integrates behaviors from:

Venus::Role::Buildable

Venus::Role::Proxyable

Venus::Role::Valuable

METHODS

This package provides the following methods:

apply

  apply(HashRef $data) (Prototype)

The apply method extends the underlying data structure by merging the data provided, and then returns the invocant.

Since 1.50

apply example 1
  package main;

  my $person = Venus::Prototype->new({
    '$name' => '',
  });

  $person->apply;

  # bless({value => {'$name' => ''}}, 'Venus::Prototype')
apply example 2
  package main;

  my $person = Venus::Prototype->new({
    '$name' => '',
  });

  $person->apply({
    '$name' => 'Elliot Alderson',
  });

  # bless({value => {'$name' => 'Elliot Alderson'}}, 'Venus::Prototype')
apply example 3
  package main;

  my $person = Venus::Prototype->new({
    '$name' => '',
    '&greet' => sub {'hello'},
  });

  $person->apply({
    '$name' => 'Elliot Alderson',
  });

  # bless({value => {...}}, 'Venus::Prototype')

call

  call(Str $method, Any @args) (Any)

The call method dispatches method calls based on the method name provided and the state of the object, and returns the results. If the method name provided matches an object property of the same name with an ampersand prefix, denoting a method, then the dispatched method call acts as a method call providing the invocant as the first argument. If the method name provided matches an object property of the same name with a dollar sign prefix, denoting an attribute, then the dispatched method call acts as an attribute accessor call. This method is also useful for calling virtual methods when those virtual methods conflict with the Venus::Prototype methods.

Since 1.50

call example 1
  package main;

  my $person = Venus::Prototype->new({
    '$name' => 'anonymous',
  });

  my $name = $person->call('name');

  # "anonymous"
call example 2
  package main;

  my $person = Venus::Prototype->new({
    '$name' => 'anonymous',
  });

  my $name = $person->call('name', 'unidentified');

  # "unidentified"

extend

  extend(HashRef $data) (Prototype)

The extend method copies the underlying data structure, merging the data provided if any, and then returns a new prototype object.

Since 1.50

extend example 1
  package main;

  my $mrrobot = Venus::Prototype->new({
    '$name' => 'Edward Alderson',
    '$group' => 'fsociety',
  });

  my $elliot = $mrrobot->extend({
    '$name' => 'Elliot Alderson',
  });

  # bless({value => {...}}, 'Venus::Prototype')
extend example 2
  package main;

  my $mrrobot = Venus::Prototype->new({
    '$name' => 'Edward Alderson',
    '$group' => 'fsociety',
    '$login' => { username => 'admin', password => 'secret', },
  });

  my $elliot = $mrrobot->extend({
    '$name' => 'Elliot Alderson',
    '$login' => { password => '$ecr3+', },
  });

  # bless({value => {...}}, 'Venus::Prototype')
extend example 3
  package main;

  my $ability = {
    '&access' => sub {time},
  };

  my $person = Venus::Prototype->new;

  my $mrrobot = $person->extend($ability);

  my $elliot = $mrrobot->extend($ability);

  # bless({value => {...}}, 'Venus::Prototype')