Build Status

NAME

Package::Prototype - Super easily to create prototype object

SYNOPSIS

use strict;
use warnings;
use Data::Dumper;
use feature 'say';
use Package::Prototype;

my $obj = Package::Prototype->bless({
        foo => 10,
        bar => "Hello",
        baz => sub {
            my ($self, $arg) = @_;
            say "$arg, World";
        },
        # It do not create a method if key is started at '_'
        _data => "internal data"
    });

say "\$obj classname: " . ref $obj;
say Dumper $obj;
say $obj->foo;        # 10
say $obj->bar;        # "Hello"
$obj->baz($obj->bar); # "Hello, World"

my $obj2 = Package::Prototype->bless({
        hoge => [1..10],
        fuga => {abc => "def"}
    }, 'CLASS');

say "\$obj2 classname: " . ref $obj2;

# reference
my $a = $obj2->hoge;
my $h = $obj2->fuga;

# wantarray
my @a = $obj2->hoge;
my %h = $obj2->fuga;

$obj2->prototype(bow => sub { say "Bow!!" }, nyao => sub { say "Nyao!!" });
$obj2->prototype(baw => 10, nyan => "nyan");
$obj2->nyao(); # "Nyao!!"
$obj2->bow();  # "Bow!!"
say "bow: " . $obj2->baw . " nyan: " . $obj2->nyan;

DESCRIPTION

Package::Prototype can create prototype object like javascript.

This module can provide anonymous packages which are independent of the main namespace if not specified by classname. Also, available as an object instance.

METHODS

SEE ALSO

Package::Anon

Plack::Util::Prototype

LICENSE

Copyright (C) K.

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

AUTHOR

K x00.x7f@gmail.com